68066778dbd29308dbb6093b6c59d98e62dce080
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / sal / dom / broker / BrokerImpl.java
1 /*
2  * Copyright (c) 2013 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.dom.broker;
9
10 import java.util.Collections;
11 import java.util.HashSet;
12 import java.util.Set;
13 import java.util.concurrent.Future;
14
15 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
16 import org.opendaylight.controller.sal.core.api.Broker;
17 import org.opendaylight.controller.sal.core.api.BrokerService;
18 import org.opendaylight.controller.sal.core.api.Consumer;
19 import org.opendaylight.controller.sal.core.api.Provider;
20 import org.opendaylight.controller.sal.core.api.RoutedRpcDefaultImplementation;
21 import org.opendaylight.controller.sal.core.api.RpcImplementation;
22 import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry;
23 import org.opendaylight.controller.sal.core.api.RpcRegistrationListener;
24 import org.opendaylight.controller.sal.core.api.RpcRoutingContext;
25 import org.opendaylight.controller.sal.dom.broker.spi.RpcRouter;
26 import org.opendaylight.yangtools.concepts.ListenerRegistration;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.common.RpcResult;
29 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
30 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
31 import org.osgi.framework.BundleContext;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import com.google.common.base.Optional;
36 import com.google.common.base.Preconditions;
37 import com.google.common.collect.ClassToInstanceMap;
38 import com.google.common.collect.ImmutableClassToInstanceMap;
39 import com.google.common.util.concurrent.ListenableFuture;
40
41 public class BrokerImpl implements Broker, RpcProvisionRegistry, AutoCloseable {
42     private final static Logger log = LoggerFactory.getLogger(BrokerImpl.class);
43
44     // Broker Generic Context
45     private final Set<ConsumerContextImpl> sessions = Collections
46             .synchronizedSet(new HashSet<ConsumerContextImpl>());
47     private final Set<ProviderContextImpl> providerSessions = Collections
48             .synchronizedSet(new HashSet<ProviderContextImpl>());
49
50     private AutoCloseable deactivator = null;
51
52     private RpcRouter router = null;
53
54     private final ClassToInstanceMap<BrokerService> services;
55
56     public  BrokerImpl(final RpcRouter router,final ClassToInstanceMap<BrokerService> services) {
57         this.router = Preconditions.checkNotNull(router, "RPC Router must not be null");
58         this.services = ImmutableClassToInstanceMap.copyOf(services);
59     }
60
61
62     @Override
63     public ConsumerSession registerConsumer(final Consumer consumer,
64             final BundleContext ctx) {
65         return registerConsumer(consumer);
66     }
67
68     @Override
69     public ProviderSession registerProvider(final Provider provider,
70             final BundleContext ctx) {
71         return registerProvider(provider);
72     }
73
74     protected Future<RpcResult<CompositeNode>> invokeRpcAsync(final QName rpc,
75             final CompositeNode input) {
76         return router.invokeRpc(rpc, input);
77     }
78
79     // Validation
80     private void checkPredicates(final Provider prov) {
81         Preconditions.checkNotNull(prov, "Provider should not be null.");
82         for (ProviderContextImpl session : providerSessions) {
83             if (prov.equals(session.getProvider())) {
84                 throw new IllegalStateException("Provider already registered");
85             }
86         }
87
88     }
89
90     private void checkPredicates(final Consumer cons) {
91         Preconditions.checkNotNull(cons, "Consumer should not be null.");
92         for (ConsumerContextImpl session : sessions) {
93             if (cons.equals(session.getConsumer())) {
94                 throw new IllegalStateException("Consumer already registered");
95             }
96         }
97     }
98
99     // Private Factory methods
100     private ConsumerContextImpl newSessionFor(final Consumer provider) {
101         ConsumerContextImpl ret = new ConsumerContextImpl(provider, this);
102         return ret;
103     }
104
105     private ProviderContextImpl newSessionFor(final Provider provider) {
106         ProviderContextImpl ret = new ProviderContextImpl(provider, this);
107         return ret;
108     }
109
110     protected void consumerSessionClosed(
111             final ConsumerContextImpl consumerContextImpl) {
112         sessions.remove(consumerContextImpl);
113         providerSessions.remove(consumerContextImpl);
114     }
115
116     @Override
117     public void close() throws Exception {
118         if (deactivator != null) {
119             deactivator.close();
120             deactivator = null;
121         }
122     }
123
124     @Override
125     public RpcRegistration addRpcImplementation(final QName rpcType,
126             final RpcImplementation implementation)
127             throws IllegalArgumentException {
128         return router.addRpcImplementation(rpcType, implementation);
129     }
130
131     @Override
132     public RoutedRpcRegistration addRoutedRpcImplementation(
133             final QName rpcType, final RpcImplementation implementation) {
134         return router.addRoutedRpcImplementation(rpcType, implementation);
135     }
136
137     @Override
138     public void setRoutedRpcDefaultDelegate(
139             final RoutedRpcDefaultImplementation defaultImplementation) {
140         router.setRoutedRpcDefaultDelegate(defaultImplementation);
141     }
142
143     @Override
144     public ListenerRegistration<RpcRegistrationListener> addRpcRegistrationListener(
145             final RpcRegistrationListener listener) {
146         return router.addRpcRegistrationListener(listener);
147     }
148
149     @Override
150     public <L extends RouteChangeListener<RpcRoutingContext, InstanceIdentifier>> ListenerRegistration<L> registerRouteChangeListener(
151             final L listener) {
152         return router.registerRouteChangeListener(listener);
153     }
154
155     @Override
156     public Set<QName> getSupportedRpcs() {
157         return router.getSupportedRpcs();
158     }
159
160     @Override
161     public ListenableFuture<RpcResult<CompositeNode>> invokeRpc(
162             final QName rpc, final CompositeNode input) {
163         return router.invokeRpc(rpc, input);
164     }
165
166     /**
167      * @return the deactivator
168      */
169     public AutoCloseable getDeactivator() {
170         return deactivator;
171     }
172
173     /**
174      * @param deactivator
175      *            the deactivator to set
176      */
177     public void setDeactivator(final AutoCloseable deactivator) {
178         this.deactivator = deactivator;
179     }
180
181     /**
182      * @return the router
183      */
184     public RpcRouter getRouter() {
185         return router;
186     }
187
188     /**
189      * @param router
190      *            the router to set
191      */
192     public void setRouter(final RpcRouter router) {
193         this.router = router;
194     }
195
196     protected <T extends BrokerService> Optional<T> getGlobalService(final Class<T> service) {
197         return Optional.fromNullable(services.getInstance(service));
198     }
199
200
201     @Override
202     public ConsumerSession registerConsumer(Consumer consumer) {
203         checkPredicates(consumer);
204         log.trace("Registering consumer {}", consumer);
205         final ConsumerContextImpl session = newSessionFor(consumer);
206         consumer.onSessionInitiated(session);
207         sessions.add(session);
208         return session;
209     }
210
211
212     @Override
213     public ProviderSession registerProvider(Provider provider) {
214         checkPredicates(provider);
215         final ProviderContextImpl session = newSessionFor(provider);
216         provider.onSessionInitiated(session);
217         providerSessions.add(session);
218         return session;
219     }
220
221 }

©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.