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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.