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