2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.binding.impl;
10 import static com.google.common.base.Preconditions.checkState;
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;
41 public class RootBindingAwareBroker implements Mutable, Identifiable<String>, BindingAwareBroker, AutoCloseable,
44 private final static Logger LOG = LoggerFactory.getLogger(RootBindingAwareBroker.class);
46 RootSalInstance controllerRoot;
48 private final String identifier;
50 private RpcProviderRegistry rpcBroker;
52 private NotificationProviderService notificationBroker;
54 private NotificationPublishService notificationPublishService;
56 @SuppressWarnings("deprecation")
57 private DataProviderService legacyDataBroker;
59 private DataBroker dataBroker;
61 private MountProviderService legacyMount;
63 private ImmutableClassToInstanceMap<BindingAwareService> supportedConsumerServices;
65 private ImmutableClassToInstanceMap<BindingAwareService> supportedProviderServices;
67 private MountPointService mountService;
69 public void setLegacyMountManager(final MountProviderService legacyMount) {
70 this.legacyMount = legacyMount;
73 public RootBindingAwareBroker(final String instanceName) {
74 this.identifier = instanceName;
78 public String getIdentifier() {
82 public RootSalInstance getRoot() {
83 return controllerRoot;
86 public DataProviderService getDataBroker() {
87 return this.legacyDataBroker;
90 public NotificationProviderService getNotificationBroker() {
91 return this.notificationBroker;
94 public NotificationPublishService getNotificationPublishService() {
95 return this.notificationPublishService;
97 public RpcProviderRegistry getRpcProviderRegistry() {
98 return this.rpcBroker;
101 public RpcProviderRegistry getRpcBroker() {
105 public MountPointService getMountService() {
109 public MountProviderService getLegacyMount() {
113 public void setDataBroker(final DataBroker asyncDataBroker) {
114 dataBroker = asyncDataBroker;
117 public void setMountService(final MountPointService mount) {
118 this.mountService = mount;
121 public void setRpcBroker(final RpcProviderRegistry rpcBroker) {
122 this.rpcBroker = rpcBroker;
125 public void setNotificationBroker(final NotificationProviderService notificationBroker) {
126 this.notificationBroker = notificationBroker;
129 public void setNotificationPublishService(final NotificationPublishService notificationPublishService) {
130 this.notificationPublishService = notificationPublishService;
133 public void setLegacyDataBroker(final DataProviderService dataBroker) {
134 this.legacyDataBroker = dataBroker;
137 public void start() {
138 checkState(controllerRoot == null, "Binding Aware Broker was already started.");
139 LOG.info("Starting Binding Aware Broker: {}", identifier);
141 controllerRoot = new RootSalInstance(getRpcProviderRegistry(), getNotificationBroker(), getDataBroker());
143 final ImmutableClassToInstanceMap.Builder<BindingAwareService> consBuilder = ImmutableClassToInstanceMap
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);
152 consBuilder.put(MountPointService.class, mountService);
153 consBuilder.put(MountService.class, legacyMount).build();
155 supportedConsumerServices = consBuilder.build();
156 final ImmutableClassToInstanceMap.Builder<BindingAwareService> provBuilder = ImmutableClassToInstanceMap
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);
164 supportedConsumerServices = consBuilder.build();
165 supportedProviderServices = provBuilder.build();
169 public ConsumerContext registerConsumer(final BindingAwareConsumer consumer, final BundleContext ctx) {
170 return registerConsumer(consumer);
174 public ProviderContext registerProvider(final BindingAwareProvider provider, final BundleContext ctx) {
175 return registerProvider(provider);
179 public ConsumerContext registerConsumer(final BindingAwareConsumer consumer) {
180 checkState(supportedConsumerServices != null, "Broker is not initialized.");
181 return BindingContextUtils.createConsumerContextAndInitialize(consumer, supportedConsumerServices);
185 public ProviderContext registerProvider(final BindingAwareProvider provider) {
186 checkState(supportedProviderServices != null, "Broker is not initialized.");
187 return BindingContextUtils.createProviderContextAndInitialize(provider, supportedProviderServices);
191 public void close() throws Exception {
192 // FIXME: Close all sessions
196 public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(final Class<T> type,
197 final T implementation) throws IllegalStateException {
198 return getRoot().addRoutedRpcImplementation(type, implementation);
202 public <T extends RpcService> RpcRegistration<T> addRpcImplementation(final Class<T> type, final T implementation)
203 throws IllegalStateException {
204 return getRoot().addRpcImplementation(type, implementation);
208 public <T extends RpcService> T getRpcService(final Class<T> module) {
209 return getRoot().getRpcService(module);
213 public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
215 return getRoot().registerRouteChangeListener(arg0);
218 public class RootSalInstance extends
219 AbstractBindingSalProviderInstance<DataProviderService, NotificationProviderService, RpcProviderRegistry> {
221 public RootSalInstance(final RpcProviderRegistry rpcRegistry,
222 final NotificationProviderService notificationBroker, final DataProviderService dataBroker) {
223 super(rpcRegistry, notificationBroker, dataBroker);