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