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