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