33fc2446196d5473f9742883155b92d25110fc77
[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         for (final 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 (final 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         final ConsumerContextImpl ret = new ConsumerContextImpl(provider, this);
102         return ret;
103     }
104
105     private ProviderContextImpl newSessionFor(final Provider provider) {
106         final 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     /**
125      * @return the deactivator
126      */
127     public AutoCloseable getDeactivator() {
128         return deactivator;
129     }
130
131     /**
132      * @param deactivator
133      *            the deactivator to set
134      */
135     public void setDeactivator(final AutoCloseable deactivator) {
136         this.deactivator = deactivator;
137     }
138
139     protected <T extends BrokerService> Optional<T> getGlobalService(final Class<T> service) {
140         return Optional.fromNullable(services.getInstance(service));
141     }
142
143
144     @Override
145     public ConsumerSession registerConsumer(final Consumer consumer) {
146         checkPredicates(consumer);
147         LOG.trace("Registering consumer {}", consumer);
148         final ConsumerContextImpl session = newSessionFor(consumer);
149         consumer.onSessionInitiated(session);
150         sessions.add(session);
151         return session;
152     }
153
154
155     @Override
156     public ProviderSession registerProvider(final Provider provider) {
157         checkPredicates(provider);
158         final ProviderContextImpl session = newSessionFor(provider);
159         provider.onSessionInitiated(session);
160         providerSessions.add(session);
161         return session;
162     }
163
164
165     @Nonnull
166     @Override
167     public <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(@Nonnull final T implementation, @Nonnull final DOMRpcIdentifier... rpcs) {
168         return rpcProvider.registerRpcImplementation(implementation, rpcs);
169     }
170
171     @Nonnull
172     @Override
173     public <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(@Nonnull final T implementation, @Nonnull final Set<DOMRpcIdentifier> rpcs) {
174         return rpcProvider.registerRpcImplementation(implementation, rpcs);
175     }
176
177     @Nonnull
178     @Override
179     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath type, @Nullable final NormalizedNode<?, ?> input) {
180         return rpcService.invokeRpc(type, input);
181     }
182
183     @Nonnull
184     @Override
185     public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(@Nonnull final T listener) {
186         return rpcService.registerRpcListener(listener);
187     }
188 }

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