7998bdecdd1847f6e86bdf28c5a40efda80b6112
[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.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ClassToInstanceMap;
13 import com.google.common.collect.ImmutableClassToInstanceMap;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import java.util.Collections;
16 import java.util.HashSet;
17 import java.util.Set;
18 import javax.annotation.Nonnull;
19 import javax.annotation.Nullable;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
21 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
23 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementation;
24 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationRegistration;
25 import org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService;
26 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
27 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
28 import org.opendaylight.controller.md.sal.dom.broker.impl.DOMRpcRouter;
29 import org.opendaylight.controller.sal.core.api.Broker;
30 import org.opendaylight.controller.sal.core.api.BrokerService;
31 import org.opendaylight.controller.sal.core.api.Consumer;
32 import org.opendaylight.controller.sal.core.api.Provider;
33 import org.opendaylight.yangtools.concepts.ListenerRegistration;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
36 import org.osgi.framework.BundleContext;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public class BrokerImpl implements Broker, DOMRpcProviderService, DOMRpcService, AutoCloseable {
41     private static final Logger LOG = LoggerFactory.getLogger(BrokerImpl.class);
42
43     // Broker Generic Context
44     private final Set<ConsumerContextImpl> sessions = Collections
45             .synchronizedSet(new HashSet<ConsumerContextImpl>());
46     private final Set<ProviderContextImpl> providerSessions = Collections
47             .synchronizedSet(new HashSet<ProviderContextImpl>());
48
49     private AutoCloseable deactivator;
50
51     private final DOMRpcService rpcService;
52     private final DOMRpcProviderService rpcProvider;
53
54     private final ClassToInstanceMap<BrokerService> services;
55
56     public BrokerImpl(final DOMRpcRouter router,final ClassToInstanceMap<BrokerService> services) {
57         this(router, router, services);
58     }
59
60     public BrokerImpl(final DOMRpcService rpcService, final DOMRpcProviderService rpcProvider,
61             final ClassToInstanceMap<BrokerService> services) {
62         this.rpcService = Preconditions.checkNotNull(rpcService, "DOMRpcService must not be null");
63         this.rpcProvider = Preconditions.checkNotNull(rpcProvider, "DOMRpcProviderService must not be null");
64         this.services = ImmutableClassToInstanceMap.copyOf(services);
65     }
66
67     @Override
68     public ConsumerSession registerConsumer(final Consumer consumer,
69             final BundleContext ctx) {
70         return registerConsumer(consumer);
71     }
72
73     @Override
74     public ProviderSession registerProvider(final Provider provider,
75             final BundleContext ctx) {
76         return registerProvider(provider);
77     }
78
79     // Validation
80     private void checkPredicates(final Provider prov) {
81         Preconditions.checkNotNull(prov, "Provider should not be null.");
82         synchronized (providerSessions) {
83             for (final ProviderContextImpl session : providerSessions) {
84                 if (prov.equals(session.getProvider())) {
85                     throw new IllegalStateException("Provider already registered");
86                 }
87             }
88         }
89
90     }
91
92     private void checkPredicates(final Consumer cons) {
93         Preconditions.checkNotNull(cons, "Consumer should not be null.");
94         synchronized (sessions) {
95             for (final ConsumerContextImpl session : sessions) {
96                 if (cons.equals(session.getConsumer())) {
97                     throw new IllegalStateException("Consumer already registered");
98                 }
99             }
100         }
101     }
102
103     // Private Factory methods
104     private ConsumerContextImpl newSessionFor(final Consumer provider) {
105         final ConsumerContextImpl ret = new ConsumerContextImpl(provider, this);
106         return ret;
107     }
108
109     private ProviderContextImpl newSessionFor(final Provider provider) {
110         final ProviderContextImpl ret = new ProviderContextImpl(provider, this);
111         return ret;
112     }
113
114     protected void consumerSessionClosed(
115             final ConsumerContextImpl consumerContextImpl) {
116         sessions.remove(consumerContextImpl);
117         providerSessions.remove(consumerContextImpl);
118     }
119
120     @Override
121     public void close() throws Exception {
122         if (deactivator != null) {
123             deactivator.close();
124             deactivator = null;
125         }
126     }
127
128     /**
129      * @return the deactivator
130      */
131     public AutoCloseable getDeactivator() {
132         return deactivator;
133     }
134
135     /**
136      * @param deactivator
137      *            the deactivator to set
138      */
139     public void setDeactivator(final AutoCloseable deactivator) {
140         this.deactivator = deactivator;
141     }
142
143     protected <T extends BrokerService> Optional<T> getGlobalService(final Class<T> service) {
144         return Optional.fromNullable(services.getInstance(service));
145     }
146
147
148     @Override
149     public ConsumerSession registerConsumer(final Consumer consumer) {
150         checkPredicates(consumer);
151         LOG.trace("Registering consumer {}", consumer);
152         final ConsumerContextImpl session = newSessionFor(consumer);
153         consumer.onSessionInitiated(session);
154         sessions.add(session);
155         return session;
156     }
157
158
159     @Override
160     public ProviderSession registerProvider(final Provider provider) {
161         checkPredicates(provider);
162         final ProviderContextImpl session = newSessionFor(provider);
163         provider.onSessionInitiated(session);
164         providerSessions.add(session);
165         return session;
166     }
167
168
169     @Nonnull
170     @Override
171     public <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(@Nonnull final T implementation, @Nonnull final DOMRpcIdentifier... rpcs) {
172         return rpcProvider.registerRpcImplementation(implementation, rpcs);
173     }
174
175     @Nonnull
176     @Override
177     public <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(@Nonnull final T implementation, @Nonnull final Set<DOMRpcIdentifier> rpcs) {
178         return rpcProvider.registerRpcImplementation(implementation, rpcs);
179     }
180
181     @Nonnull
182     @Override
183     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath type, @Nullable final NormalizedNode<?, ?> input) {
184         return rpcService.invokeRpc(type, input);
185     }
186
187     @Nonnull
188     @Override
189     public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(@Nonnull final T listener) {
190         return rpcService.registerRpcListener(listener);
191     }
192 }

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