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.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;
37 public class RootBindingAwareBroker implements Mutable, Identifiable<String>, BindingAwareBroker, AutoCloseable,
40 private static final Logger LOG = LoggerFactory.getLogger(RootBindingAwareBroker.class);
42 RootSalInstance controllerRoot;
44 private final String identifier;
46 private RpcProviderRegistry rpcBroker;
48 private NotificationProviderService notificationBroker;
50 private NotificationPublishService notificationPublishService;
52 private DataBroker dataBroker;
54 private ImmutableClassToInstanceMap<BindingAwareService> supportedConsumerServices;
56 private ImmutableClassToInstanceMap<BindingAwareService> supportedProviderServices;
58 private MountPointService mountService;
60 public RootBindingAwareBroker(final String instanceName) {
61 this.identifier = instanceName;
65 public String getIdentifier() {
69 public RootSalInstance getRoot() {
70 return controllerRoot;
73 public NotificationProviderService getNotificationBroker() {
74 return this.notificationBroker;
77 public NotificationPublishService getNotificationPublishService() {
78 return this.notificationPublishService;
81 public RpcProviderRegistry getRpcProviderRegistry() {
82 return this.rpcBroker;
85 public RpcProviderRegistry getRpcBroker() {
89 public MountPointService getMountService() {
93 public void setDataBroker(final DataBroker asyncDataBroker) {
94 dataBroker = asyncDataBroker;
97 public void setMountService(final MountPointService mount) {
98 this.mountService = mount;
101 public void setRpcBroker(final RpcProviderRegistry rpcBroker) {
102 this.rpcBroker = rpcBroker;
105 public void setNotificationBroker(final NotificationProviderService notificationBroker) {
106 this.notificationBroker = notificationBroker;
109 public void setNotificationPublishService(final NotificationPublishService notificationPublishService) {
110 this.notificationPublishService = notificationPublishService;
113 public void start() {
114 checkState(controllerRoot == null, "Binding Aware Broker was already started.");
115 LOG.info("Starting Binding Aware Broker: {}", identifier);
117 controllerRoot = new RootSalInstance(getRpcProviderRegistry(), getNotificationBroker());
119 final ImmutableClassToInstanceMap.Builder<BindingAwareService> consBuilder = ImmutableClassToInstanceMap
122 consBuilder.put(NotificationService.class, getRoot());
123 consBuilder.put(RpcConsumerRegistry.class, getRoot());
124 if (dataBroker != null) {
125 consBuilder.put(DataBroker.class, dataBroker);
127 consBuilder.put(MountPointService.class, mountService);
129 supportedConsumerServices = consBuilder.build();
130 final ImmutableClassToInstanceMap.Builder<BindingAwareService> provBuilder = ImmutableClassToInstanceMap
132 provBuilder.putAll(supportedConsumerServices).put(NotificationProviderService.class, getRoot())
133 .put(RpcProviderRegistry.class, getRoot());
134 if (notificationPublishService != null) {
135 provBuilder.put(NotificationPublishService.class, notificationPublishService);
138 supportedProviderServices = provBuilder.build();
142 public ConsumerContext registerConsumer(final BindingAwareConsumer consumer, final BundleContext ctx) {
143 return registerConsumer(consumer);
147 public ConsumerContext registerConsumer(final BindingAwareConsumer consumer) {
148 checkState(supportedConsumerServices != null, "Broker is not initialized.");
149 return BindingContextUtils.createConsumerContextAndInitialize(consumer, supportedConsumerServices);
153 public ProviderContext registerProvider(final BindingAwareProvider provider, final BundleContext ctx) {
154 return registerProvider(provider);
158 public ProviderContext registerProvider(final BindingAwareProvider provider) {
159 checkState(supportedProviderServices != null, "Broker is not initialized.");
160 return BindingContextUtils.createProviderContextAndInitialize(provider, supportedProviderServices);
164 public void close() throws Exception {
165 // FIXME: Close all sessions
169 public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(final Class<T> type,
170 final T implementation) throws IllegalStateException {
171 return getRoot().addRoutedRpcImplementation(type, implementation);
175 public <T extends RpcService> RpcRegistration<T> addRpcImplementation(final Class<T> type, final T implementation)
176 throws IllegalStateException {
177 return getRoot().addRpcImplementation(type, implementation);
181 public <T extends RpcService> T getRpcService(final Class<T> module) {
182 return getRoot().getRpcService(module);
186 public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L>
187 registerRouteChangeListener(final L listener) {
188 return getRoot().registerRouteChangeListener(listener);
191 public static class RootSalInstance extends
192 AbstractBindingSalProviderInstance<NotificationProviderService, RpcProviderRegistry> {
194 public RootSalInstance(final RpcProviderRegistry rpcRegistry,
195 final NotificationProviderService notificationBroker) {
196 super(rpcRegistry, notificationBroker);