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