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.util.AbstractBindingSalProviderInstance;
16 import org.opendaylight.controller.md.sal.binding.util.BindingContextUtils;
17 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareService;
22 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
23 import org.opendaylight.controller.sal.binding.api.NotificationService;
24 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
25 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
26 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
27 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
28 import org.opendaylight.controller.sal.binding.api.mount.MountProviderService;
29 import org.opendaylight.controller.sal.binding.api.mount.MountService;
30 import org.opendaylight.controller.sal.binding.api.rpc.RpcContextIdentifier;
31 import org.opendaylight.yangtools.concepts.Identifiable;
32 import org.opendaylight.yangtools.concepts.ListenerRegistration;
33 import org.opendaylight.yangtools.concepts.Mutable;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.binding.RpcService;
36 import org.osgi.framework.BundleContext;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class RootBindingAwareBroker implements //
42 Identifiable<String>, //
43 BindingAwareBroker, AutoCloseable, RpcProviderRegistry {
45 private final static Logger LOG = LoggerFactory.getLogger(RootBindingAwareBroker.class);
47 RootSalInstance controllerRoot;
49 private final String identifier;
51 private RpcProviderRegistry rpcBroker;
53 private NotificationProviderService notificationBroker;
55 @SuppressWarnings("deprecation")
56 private DataProviderService legacyDataBroker;
58 private DataBroker dataBroker;
60 private MountProviderService legacyMount;
62 private ImmutableClassToInstanceMap<BindingAwareService> supportedConsumerServices;
64 private ImmutableClassToInstanceMap<BindingAwareService> supportedProviderServices;
66 private MountPointService mountService;
68 public void setLegacyMountManager(final MountProviderService legacyMount) {
69 this.legacyMount = legacyMount;
72 public RootBindingAwareBroker(final String instanceName) {
73 this.identifier = instanceName;
77 public String getIdentifier() {
81 public RootSalInstance getRoot() {
82 return controllerRoot;
85 public DataProviderService getDataBroker() {
86 return this.legacyDataBroker;
89 public NotificationProviderService getNotificationBroker() {
90 return this.notificationBroker;
93 public RpcProviderRegistry getRpcProviderRegistry() {
94 return this.rpcBroker;
97 public RpcProviderRegistry getRpcBroker() {
101 public MountPointService getMountService() {
105 public MountProviderService getLegacyMount() {
109 public void setDataBroker(final DataBroker asyncDataBroker) {
110 dataBroker = asyncDataBroker;
113 public void setMountService(final MountPointService mount) {
114 this.mountService = mount;
117 public void setRpcBroker(final RpcProviderRegistry rpcBroker) {
118 this.rpcBroker = rpcBroker;
121 public void setNotificationBroker(final NotificationProviderService notificationBroker) {
122 this.notificationBroker = notificationBroker;
125 public void setLegacyDataBroker(final DataProviderService dataBroker) {
126 this.legacyDataBroker = dataBroker;
129 public void start() {
130 checkState(controllerRoot == null, "Binding Aware Broker was already started.");
131 LOG.info("Starting Binding Aware Broker: {}", identifier);
133 controllerRoot = new RootSalInstance(getRpcProviderRegistry(), getNotificationBroker(), getDataBroker());
135 final ImmutableClassToInstanceMap.Builder<BindingAwareService> consBuilder = ImmutableClassToInstanceMap
138 consBuilder.put(NotificationService.class, getRoot());
139 consBuilder.put(DataBrokerService.class, getRoot());
140 consBuilder.put(RpcConsumerRegistry.class, getRoot());
141 if (dataBroker != null) {
142 consBuilder.put(DataBroker.class, dataBroker);
144 consBuilder.put(MountPointService.class, mountService);
145 consBuilder.put(MountService.class, legacyMount).build();
146 supportedConsumerServices = consBuilder.build();
147 supportedProviderServices = ImmutableClassToInstanceMap.<BindingAwareService> builder()
148 .putAll(supportedConsumerServices).put(NotificationProviderService.class, getRoot())
149 .put(DataProviderService.class, getRoot()).put(RpcProviderRegistry.class, getRoot())
150 .put(MountProviderService.class, legacyMount).build();
154 public ConsumerContext registerConsumer(final BindingAwareConsumer consumer, final BundleContext ctx) {
155 return registerConsumer(consumer);
159 public ProviderContext registerProvider(final BindingAwareProvider provider, final BundleContext ctx) {
160 return registerProvider(provider);
164 public ConsumerContext registerConsumer(final BindingAwareConsumer consumer) {
165 checkState(supportedConsumerServices != null, "Broker is not initialized.");
166 return BindingContextUtils.createConsumerContextAndInitialize(consumer, supportedConsumerServices);
170 public ProviderContext registerProvider(final BindingAwareProvider provider) {
171 checkState(supportedProviderServices != null, "Broker is not initialized.");
172 return BindingContextUtils.createProviderContextAndInitialize(provider, supportedProviderServices);
176 public void close() throws Exception {
177 // FIXME: Close all sessions
181 public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(final Class<T> type,
182 final T implementation) throws IllegalStateException {
183 return getRoot().addRoutedRpcImplementation(type, implementation);
187 public <T extends RpcService> RpcRegistration<T> addRpcImplementation(final Class<T> type, final T implementation)
188 throws IllegalStateException {
189 return getRoot().addRpcImplementation(type, implementation);
193 public <T extends RpcService> T getRpcService(final Class<T> module) {
194 return getRoot().getRpcService(module);
198 public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
200 return getRoot().registerRouteChangeListener(arg0);
203 public class RootSalInstance extends
204 AbstractBindingSalProviderInstance<DataProviderService, NotificationProviderService, RpcProviderRegistry> {
206 public RootSalInstance(final RpcProviderRegistry rpcRegistry,
207 final NotificationProviderService notificationBroker, final DataProviderService dataBroker) {
208 super(rpcRegistry, notificationBroker, dataBroker);