1 package org.opendaylight.controller.sal.dom.broker.impl
3 import org.opendaylight.controller.sal.dom.broker.spi.RpcRouter
4 import org.opendaylight.yangtools.concepts.Identifiable
5 import org.opendaylight.yangtools.yang.common.QName
6 import org.opendaylight.controller.sal.core.api.RpcImplementation
7 import org.opendaylight.yangtools.yang.data.api.CompositeNode
8 import static com.google.common.base.Preconditions.*;
10 import org.opendaylight.controller.sal.core.api.Broker.RpcRegistration
11 import java.util.concurrent.ConcurrentHashMap
13 import java.util.Collections
14 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration
15 import org.opendaylight.controller.md.sal.common.impl.ListenerRegistry
16 import org.opendaylight.controller.sal.core.api.RpcRegistrationListener
17 import org.slf4j.LoggerFactory
19 class RpcRouterImpl implements RpcRouter, Identifiable<String> {
21 static val log = LoggerFactory.getLogger(RpcRouterImpl)
23 Map<QName, RpcRegistration> implementations = new ConcurrentHashMap();
26 val Set<QName> supportedRpcs = Collections.unmodifiableSet(implementations.keySet);
28 private val rpcRegistrationListeners = new ListenerRegistry<RpcRegistrationListener>();
31 val String identifier;
37 override addRoutedRpcImplementation(QName rpcType, RpcImplementation implementation) {
40 override addRpcImplementation(QName rpcType, RpcImplementation implementation) throws IllegalArgumentException {
41 checkNotNull(rpcType, "Rpc Type should not be null");
42 checkNotNull(implementation, "Implementation should not be null.");
43 checkState(!implementations.containsKey(rpcType), "Provider for supplied rpc is already registered.");
44 val reg = new RpcRegistrationImpl(rpcType, implementation, this);
45 implementations.put(rpcType, reg)
47 for (listener : rpcRegistrationListeners.listeners) {
49 listener.instance.onRpcImplementationAdded(rpcType);
50 } catch (Exception e) {
51 log.error("Unhandled exception during invoking listener", e);
59 override invokeRpc(QName rpc, CompositeNode input) {
60 checkNotNull(rpc, "Rpc Type should not be null");
62 val impl = implementations.get(rpc);
63 checkState(impl !== null, "Provider for supplied rpc is not registered.");
65 return impl.instance.invokeRpc(rpc, input);
68 def remove(RpcRegistrationImpl impl) {
69 val existing = implementations.get(impl.type);
70 if (existing == impl) {
71 implementations.remove(impl.type);
73 for (listener : rpcRegistrationListeners.listeners) {
75 listener.instance.onRpcImplementationRemoved(impl.type);
76 } catch (Exception e) {
77 log.error("Unhandled exception during invoking listener", e);
82 override addRpcRegistrationListener(RpcRegistrationListener listener) {
83 rpcRegistrationListeners.register(listener);
88 class RpcRegistrationImpl extends AbstractObjectRegistration<RpcImplementation> implements RpcRegistration {
94 var RpcRouterImpl router;
96 new(QName type, RpcImplementation instance, RpcRouterImpl router) {
102 override protected removeRegistration() {