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

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