95204ceabf82e686491be4acbc21c776dbe2374f
[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 com.google.common.collect.ImmutableClassToInstanceMap;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
15 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
16 import org.opendaylight.controller.md.sal.binding.util.AbstractBindingSalProviderInstance;
17 import org.opendaylight.controller.md.sal.binding.util.BindingContextUtils;
18 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareService;
23 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
24 import org.opendaylight.controller.sal.binding.api.NotificationService;
25 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
26 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
27 import org.opendaylight.controller.sal.binding.api.rpc.RpcContextIdentifier;
28 import org.opendaylight.yangtools.concepts.Identifiable;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.concepts.Mutable;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.binding.RpcService;
33 import org.osgi.framework.BundleContext;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 @Deprecated
38 public class RootBindingAwareBroker implements Mutable, Identifiable<String>, BindingAwareBroker, AutoCloseable,
39         RpcProviderRegistry {
40
41     private static final Logger LOG = LoggerFactory.getLogger(RootBindingAwareBroker.class);
42
43     RootSalInstance controllerRoot;
44
45     private final String identifier;
46
47     private RpcProviderRegistry rpcBroker;
48
49     private NotificationProviderService notificationBroker;
50
51     private NotificationPublishService notificationPublishService;
52
53     private DataBroker dataBroker;
54
55     private ImmutableClassToInstanceMap<BindingAwareService> supportedConsumerServices;
56
57     private ImmutableClassToInstanceMap<BindingAwareService> supportedProviderServices;
58
59     private MountPointService mountService;
60
61     public RootBindingAwareBroker(final String instanceName) {
62         this.identifier = instanceName;
63     }
64
65     @Override
66     public String getIdentifier() {
67         return identifier;
68     }
69
70     public RootSalInstance getRoot() {
71         return controllerRoot;
72     }
73
74     public NotificationProviderService getNotificationBroker() {
75         return this.notificationBroker;
76     }
77
78     public NotificationPublishService getNotificationPublishService() {
79         return this.notificationPublishService;
80     }
81
82     public RpcProviderRegistry getRpcProviderRegistry() {
83         return this.rpcBroker;
84     }
85
86     public RpcProviderRegistry getRpcBroker() {
87         return rpcBroker;
88     }
89
90     public MountPointService getMountService() {
91         return mountService;
92     }
93
94     public void setDataBroker(final DataBroker asyncDataBroker) {
95         dataBroker = asyncDataBroker;
96     }
97
98     public void setMountService(final MountPointService mount) {
99         this.mountService = mount;
100     }
101
102     public void setRpcBroker(final RpcProviderRegistry rpcBroker) {
103         this.rpcBroker = rpcBroker;
104     }
105
106     public void setNotificationBroker(final NotificationProviderService notificationBroker) {
107         this.notificationBroker = notificationBroker;
108     }
109
110     public void setNotificationPublishService(final NotificationPublishService notificationPublishService) {
111         this.notificationPublishService = notificationPublishService;
112     }
113
114     public void start() {
115         checkState(controllerRoot == null, "Binding Aware Broker was already started.");
116         LOG.info("Starting Binding Aware Broker: {}", identifier);
117
118         controllerRoot = new RootSalInstance(getRpcProviderRegistry(), getNotificationBroker());
119
120         final ImmutableClassToInstanceMap.Builder<BindingAwareService> consBuilder = ImmutableClassToInstanceMap
121                 .builder();
122
123         consBuilder.put(NotificationService.class, getRoot());
124         consBuilder.put(RpcConsumerRegistry.class, getRoot());
125         if (dataBroker != null) {
126             consBuilder.put(DataBroker.class, dataBroker);
127         }
128         consBuilder.put(MountPointService.class, mountService);
129
130         supportedConsumerServices = consBuilder.build();
131         final ImmutableClassToInstanceMap.Builder<BindingAwareService> provBuilder = ImmutableClassToInstanceMap
132                 .builder();
133         provBuilder.putAll(supportedConsumerServices).put(NotificationProviderService.class, getRoot())
134                 .put(RpcProviderRegistry.class, getRoot());
135         if (notificationPublishService != null) {
136             provBuilder.put(NotificationPublishService.class, notificationPublishService);
137         }
138
139         supportedProviderServices = provBuilder.build();
140     }
141
142     @Override
143     public ConsumerContext registerConsumer(final BindingAwareConsumer consumer, final BundleContext ctx) {
144         return registerConsumer(consumer);
145     }
146
147     @Override
148     public ConsumerContext registerConsumer(final BindingAwareConsumer consumer) {
149         checkState(supportedConsumerServices != null, "Broker is not initialized.");
150         return BindingContextUtils.createConsumerContextAndInitialize(consumer, supportedConsumerServices);
151     }
152
153     @Override
154     public ProviderContext registerProvider(final BindingAwareProvider provider, final BundleContext ctx) {
155         return registerProvider(provider);
156     }
157
158     @Override
159     public ProviderContext registerProvider(final BindingAwareProvider provider) {
160         checkState(supportedProviderServices != null, "Broker is not initialized.");
161         return BindingContextUtils.createProviderContextAndInitialize(provider, supportedProviderServices);
162     }
163
164     @Override
165     public void close() throws Exception {
166         // FIXME: Close all sessions
167     }
168
169     @Override
170     public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(final Class<T> type,
171             final T implementation) throws IllegalStateException {
172         return getRoot().addRoutedRpcImplementation(type, implementation);
173     }
174
175     @Override
176     public <T extends RpcService> RpcRegistration<T> addRpcImplementation(final Class<T> type, final T implementation)
177             throws IllegalStateException {
178         return getRoot().addRpcImplementation(type, implementation);
179     }
180
181     @Override
182     public <T extends RpcService> T getRpcService(final Class<T> module) {
183         return getRoot().getRpcService(module);
184     }
185
186     @Override
187     public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L>
188             registerRouteChangeListener(final L listener) {
189         return getRoot().registerRouteChangeListener(listener);
190     }
191
192     public static class RootSalInstance extends
193             AbstractBindingSalProviderInstance<NotificationProviderService, RpcProviderRegistry> {
194
195         public RootSalInstance(final RpcProviderRegistry rpcRegistry,
196                 final NotificationProviderService notificationBroker) {
197             super(rpcRegistry, notificationBroker);
198         }
199     }
200 }