Merge "Bug:129 Connection Manager Dashlet"
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / sal / dom / broker / impl / RpcRouterImpl.xtend
1 package org.opendaylight.controller.sal.dom.broker.impl
2
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.*;
9 import java.util.Map
10 import org.opendaylight.controller.sal.core.api.Broker.RpcRegistration
11 import java.util.concurrent.ConcurrentHashMap
12 import java.util.Set
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
18
19 class RpcRouterImpl implements RpcRouter, Identifiable<String> {
20
21     static val log = LoggerFactory.getLogger(RpcRouterImpl)
22
23     Map<QName, RpcRegistration> implementations = new ConcurrentHashMap();
24
25     @Property
26     val Set<QName> supportedRpcs = Collections.unmodifiableSet(implementations.keySet);
27
28     private val rpcRegistrationListeners = new ListenerRegistry<RpcRegistrationListener>();
29
30     @Property
31     val String identifier;
32
33     new(String name) {
34         _identifier = name;
35     }
36
37     override addRoutedRpcImplementation(QName rpcType, RpcImplementation implementation) {
38     }
39
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)
46
47         for (listener : rpcRegistrationListeners.listeners) {
48             try {
49                 listener.instance.onRpcImplementationAdded(rpcType);
50             } catch (Exception e) {
51                 log.error("Unhandled exception during invoking listener", e);
52             }
53         }
54
55         return reg;
56
57     }
58
59     override invokeRpc(QName rpc, CompositeNode input) {
60         checkNotNull(rpc, "Rpc Type should not be null");
61
62         val impl = implementations.get(rpc);
63         checkState(impl !== null, "Provider for supplied rpc is not registered.");
64
65         return impl.instance.invokeRpc(rpc, input);
66     }
67
68     def remove(RpcRegistrationImpl impl) {
69         val existing = implementations.get(impl.type);
70         if (existing == impl) {
71             implementations.remove(impl.type);
72         }
73         for (listener : rpcRegistrationListeners.listeners) {
74             try {
75                 listener.instance.onRpcImplementationRemoved(impl.type);
76             } catch (Exception e) {
77                 log.error("Unhandled exception during invoking listener", e);
78             }
79         }
80     }
81     
82     override addRpcRegistrationListener(RpcRegistrationListener listener) {
83         rpcRegistrationListeners.register(listener);
84     }
85
86 }
87
88 class RpcRegistrationImpl extends AbstractObjectRegistration<RpcImplementation> implements RpcRegistration {
89
90     @Property
91     val QName type;
92
93     @Property
94     var RpcRouterImpl router;
95
96     new(QName type, RpcImplementation instance, RpcRouterImpl router) {
97         super(instance)
98         _type = type
99         _router = router
100     }
101
102     override protected removeRegistration() {
103         router.remove(this);
104     }
105
106 }