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 org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.util.AbstractBindingSalProviderInstance;
14 import org.opendaylight.controller.md.sal.binding.util.BindingContextUtils;
15 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareService;
20 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
21 import org.opendaylight.controller.sal.binding.api.NotificationService;
22 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
23 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
24 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
25 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
26 import org.opendaylight.controller.sal.binding.api.mount.MountProviderService;
27 import org.opendaylight.controller.sal.binding.api.mount.MountService;
28 import org.opendaylight.controller.sal.binding.api.rpc.RpcContextIdentifier;
29 import org.opendaylight.yangtools.concepts.Identifiable;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.concepts.Mutable;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.binding.RpcService;
34 import org.osgi.framework.BundleContext;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.google.common.collect.ImmutableClassToInstanceMap;
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 private DataProviderService legacyDataBroker;
57 private DataBroker dataBroker;
59 private MountPointManagerImpl mountManager;
61 public MountPointManagerImpl getMountManager() {
65 public void setMountManager(final MountPointManagerImpl mountManager) {
66 this.mountManager = mountManager;
69 private ImmutableClassToInstanceMap<BindingAwareService> supportedConsumerServices;
71 private ImmutableClassToInstanceMap<BindingAwareService> supportedProviderServices;
73 public RootBindingAwareBroker(final String instanceName) {
74 this.identifier = instanceName;
75 mountManager = new MountPointManagerImpl();
79 public String getIdentifier() {
83 public RootSalInstance getRoot() {
84 return controllerRoot;
87 public DataProviderService getDataBroker() {
88 return this.legacyDataBroker;
91 public NotificationProviderService getNotificationBroker() {
92 return this.notificationBroker;
95 public RpcProviderRegistry getRpcProviderRegistry() {
96 return this.rpcBroker;
99 public RpcProviderRegistry getRpcBroker() {
103 public void setRpcBroker(final RpcProviderRegistry rpcBroker) {
104 this.rpcBroker = rpcBroker;
107 public void setNotificationBroker(final NotificationProviderService notificationBroker) {
108 this.notificationBroker = notificationBroker;
111 public void setLegacyDataBroker(final DataProviderService dataBroker) {
112 this.legacyDataBroker = dataBroker;
115 public void start() {
116 checkState(controllerRoot == null, "Binding Aware Broker was already started.");
117 LOG.info("Starting Binding Aware Broker: {}", identifier);
119 controllerRoot = new RootSalInstance(getRpcProviderRegistry(), getNotificationBroker(), getDataBroker());
121 ImmutableClassToInstanceMap.Builder<BindingAwareService> consBuilder = ImmutableClassToInstanceMap.builder();
123 consBuilder.put(NotificationService.class, getRoot());
124 consBuilder.put(DataBrokerService.class, getRoot());
125 consBuilder.put(RpcConsumerRegistry.class, getRoot());
126 if(dataBroker != null) {
127 consBuilder.put(DataBroker.class, dataBroker);
129 consBuilder.put(MountService.class, mountManager).build();
130 supportedConsumerServices = consBuilder.build();
131 supportedProviderServices = ImmutableClassToInstanceMap.<BindingAwareService> builder()
132 .putAll(supportedConsumerServices).put(NotificationProviderService.class, getRoot())
133 .put(DataProviderService.class, getRoot()).put(RpcProviderRegistry.class, getRoot())
134 .put(MountProviderService.class, mountManager).build();
138 public ConsumerContext registerConsumer(final BindingAwareConsumer consumer, final BundleContext ctx) {
139 checkState(supportedConsumerServices != null, "Broker is not initialized.");
140 return BindingContextUtils.createConsumerContextAndInitialize(consumer, supportedConsumerServices);
144 public ProviderContext registerProvider(final BindingAwareProvider provider, final BundleContext ctx) {
145 checkState(supportedProviderServices != null, "Broker is not initialized.");
146 return BindingContextUtils.createProviderContextAndInitialize(provider, supportedProviderServices);
150 public void close() throws Exception {
151 // FIXME: Close all sessions
155 public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(final Class<T> type,
156 final T implementation) throws IllegalStateException {
157 return getRoot().addRoutedRpcImplementation(type, implementation);
161 public <T extends RpcService> RpcRegistration<T> addRpcImplementation(final Class<T> type, final T implementation)
162 throws IllegalStateException {
163 return getRoot().addRpcImplementation(type, implementation);
167 public <T extends RpcService> T getRpcService(final Class<T> module) {
168 return getRoot().getRpcService(module);
172 public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
174 return getRoot().registerRouteChangeListener(arg0);
177 public class RootSalInstance extends
178 AbstractBindingSalProviderInstance<DataProviderService, NotificationProviderService, RpcProviderRegistry> {
180 public RootSalInstance(final RpcProviderRegistry rpcRegistry,
181 final NotificationProviderService notificationBroker, final DataProviderService dataBroker) {
182 super(rpcRegistry, notificationBroker, dataBroker);
186 public void setDataBroker(final DataBroker asyncDataBroker) {
187 dataBroker = asyncDataBroker;