ddfb1cd10cae77774e7348b6d7da8498c477b7f4
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / impl / RootBindingAwareBroker.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.sal.binding.impl;
9
10 import static com.google.common.base.Preconditions.checkState;
11
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.util.AbstractBindingSalProviderInstance;
14 import org.opendaylight.controller.md.sal.binding.util.BindingContextUtils;
15 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareService;
20 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
21 import org.opendaylight.controller.sal.binding.api.NotificationService;
22 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
23 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
24 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
25 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
26 import org.opendaylight.controller.sal.binding.api.mount.MountProviderService;
27 import org.opendaylight.controller.sal.binding.api.mount.MountService;
28 import org.opendaylight.controller.sal.binding.api.rpc.RpcContextIdentifier;
29 import org.opendaylight.yangtools.concepts.Identifiable;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.concepts.Mutable;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.binding.RpcService;
34 import org.osgi.framework.BundleContext;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import com.google.common.collect.ImmutableClassToInstanceMap;
39
40 public class RootBindingAwareBroker implements //
41         Mutable, //
42         Identifiable<String>, //
43         BindingAwareBroker, AutoCloseable, RpcProviderRegistry {
44
45     private final static Logger LOG = LoggerFactory.getLogger(RootBindingAwareBroker.class);
46
47     RootSalInstance controllerRoot;
48
49     private final String identifier;
50
51     private RpcProviderRegistry rpcBroker;
52
53     private NotificationProviderService notificationBroker;
54
55     private DataProviderService legacyDataBroker;
56
57     private DataBroker dataBroker;
58
59     private MountPointManagerImpl mountManager;
60
61     public MountPointManagerImpl getMountManager() {
62         return mountManager;
63     }
64
65     public void setMountManager(final MountPointManagerImpl mountManager) {
66         this.mountManager = mountManager;
67     }
68
69     private ImmutableClassToInstanceMap<BindingAwareService> supportedConsumerServices;
70
71     private ImmutableClassToInstanceMap<BindingAwareService> supportedProviderServices;
72
73     public RootBindingAwareBroker(final String instanceName) {
74         this.identifier = instanceName;
75         mountManager = new MountPointManagerImpl();
76     }
77
78     @Override
79     public String getIdentifier() {
80         return identifier;
81     }
82
83     public RootSalInstance getRoot() {
84         return controllerRoot;
85     }
86
87     public DataProviderService getDataBroker() {
88         return this.legacyDataBroker;
89     }
90
91     public NotificationProviderService getNotificationBroker() {
92         return this.notificationBroker;
93     }
94
95     public RpcProviderRegistry getRpcProviderRegistry() {
96         return this.rpcBroker;
97     }
98
99     public RpcProviderRegistry getRpcBroker() {
100         return rpcBroker;
101     }
102
103     public void setRpcBroker(final RpcProviderRegistry rpcBroker) {
104         this.rpcBroker = rpcBroker;
105     }
106
107     public void setNotificationBroker(final NotificationProviderService notificationBroker) {
108         this.notificationBroker = notificationBroker;
109     }
110
111     public void setLegacyDataBroker(final DataProviderService dataBroker) {
112         this.legacyDataBroker = dataBroker;
113     }
114
115     public void start() {
116         checkState(controllerRoot == null, "Binding Aware Broker was already started.");
117         LOG.info("Starting Binding Aware Broker: {}", identifier);
118
119         controllerRoot = new RootSalInstance(getRpcProviderRegistry(), getNotificationBroker(), getDataBroker());
120
121         ImmutableClassToInstanceMap.Builder<BindingAwareService> consBuilder = ImmutableClassToInstanceMap.builder();
122
123         consBuilder.put(NotificationService.class, getRoot());
124         consBuilder.put(DataBrokerService.class, getRoot());
125         consBuilder.put(RpcConsumerRegistry.class, getRoot());
126         if(dataBroker != null) {
127             consBuilder.put(DataBroker.class, dataBroker);
128         }
129         consBuilder.put(MountService.class, mountManager).build();
130         supportedConsumerServices = consBuilder.build();
131         supportedProviderServices = ImmutableClassToInstanceMap.<BindingAwareService> builder()
132                 .putAll(supportedConsumerServices).put(NotificationProviderService.class, getRoot())
133                 .put(DataProviderService.class, getRoot()).put(RpcProviderRegistry.class, getRoot())
134                 .put(MountProviderService.class, mountManager).build();
135     }
136
137     @Override
138     public ConsumerContext registerConsumer(BindingAwareConsumer consumer,
139             BundleContext ctx) {
140         return registerConsumer(consumer);
141     }
142
143     @Override
144     public ProviderContext registerProvider(BindingAwareProvider provider,
145             BundleContext ctx) {
146         return registerProvider(provider);
147     }
148
149     @Override
150     public ConsumerContext registerConsumer(final BindingAwareConsumer consumer) {
151         checkState(supportedConsumerServices != null, "Broker is not initialized.");
152         return BindingContextUtils.createConsumerContextAndInitialize(consumer, supportedConsumerServices);
153     }
154
155     @Override
156     public ProviderContext registerProvider(final BindingAwareProvider provider) {
157         checkState(supportedProviderServices != null, "Broker is not initialized.");
158         return BindingContextUtils.createProviderContextAndInitialize(provider, supportedProviderServices);
159     }
160
161     @Override
162     public void close() throws Exception {
163         // FIXME: Close all sessions
164     }
165
166     @Override
167     public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(final Class<T> type,
168             final T implementation) throws IllegalStateException {
169         return getRoot().addRoutedRpcImplementation(type, implementation);
170     }
171
172     @Override
173     public <T extends RpcService> RpcRegistration<T> addRpcImplementation(final Class<T> type, final T implementation)
174             throws IllegalStateException {
175         return getRoot().addRpcImplementation(type, implementation);
176     }
177
178     @Override
179     public <T extends RpcService> T getRpcService(final Class<T> module) {
180         return getRoot().getRpcService(module);
181     }
182
183     @Override
184     public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
185             final L arg0) {
186         return getRoot().registerRouteChangeListener(arg0);
187     }
188
189     public class RootSalInstance extends
190             AbstractBindingSalProviderInstance<DataProviderService, NotificationProviderService, RpcProviderRegistry> {
191
192         public RootSalInstance(final RpcProviderRegistry rpcRegistry,
193                 final NotificationProviderService notificationBroker, final DataProviderService dataBroker) {
194             super(rpcRegistry, notificationBroker, dataBroker);
195         }
196     }
197
198     public void setDataBroker(final DataBroker asyncDataBroker) {
199         dataBroker = asyncDataBroker;
200     }
201 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.