5ee19a0e8fec876faf5d465ee3a140d373e53cd3
[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.sal.core.api.RpcRegistrationListener
16 import org.slf4j.LoggerFactory
17 import org.opendaylight.yangtools.concepts.util.ListenerRegistry
18 import org.opendaylight.controller.sal.core.api.Broker.RoutedRpcRegistration
19 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
20
21 class RpcRouterImpl implements RpcRouter, Identifiable<String> {
22
23     static val log = LoggerFactory.getLogger(RpcRouterImpl)
24
25     Map<QName, RpcRegistration> implementations = new ConcurrentHashMap();
26
27     @Property
28     val Set<QName> supportedRpcs = Collections.unmodifiableSet(implementations.keySet);
29
30     private val rpcRegistrationListeners = new ListenerRegistry<RpcRegistrationListener>();
31
32     @Property
33     val String identifier;
34
35     new(String name) {
36         _identifier = name;
37     }
38
39     override addRoutedRpcImplementation(QName rpcType, RpcImplementation implementation) {
40                 checkNotNull(rpcType, "Rpc Type should not be null");
41         checkNotNull(implementation, "Implementation should not be null.");
42         val reg = new RoutedRpcRegistrationImpl(rpcType, implementation, this);
43         implementations.put(rpcType, reg)
44
45         for (listener : rpcRegistrationListeners.listeners) {
46             try {
47                 listener.instance.onRpcImplementationAdded(rpcType);
48             } catch (Exception e) {
49                 log.error("Unhandled exception during invoking listener", e);
50             }
51         }
52
53         return reg;
54     }
55
56     override addRpcImplementation(QName rpcType, RpcImplementation implementation) throws IllegalArgumentException {
57         checkNotNull(rpcType, "Rpc Type should not be null");
58         checkNotNull(implementation, "Implementation should not be null.");
59         checkState(!implementations.containsKey(rpcType), "Provider for supplied rpc is already registered.");
60         val reg = new RpcRegistrationImpl(rpcType, implementation, this);
61         implementations.put(rpcType, reg)
62
63         for (listener : rpcRegistrationListeners.listeners) {
64             try {
65                 listener.instance.onRpcImplementationAdded(rpcType);
66             } catch (Exception e) {
67                 log.error("Unhandled exception during invoking listener", e);
68             }
69         }
70
71         return reg;
72
73     }
74
75     override invokeRpc(QName rpc, CompositeNode input) {
76         checkNotNull(rpc, "Rpc Type should not be null");
77
78         val impl = implementations.get(rpc);
79         checkState(impl !== null, "Provider for supplied rpc is not registered.");
80
81         return impl.instance.invokeRpc(rpc, input);
82     }
83
84     def remove(RpcRegistrationImpl impl) {
85         val existing = implementations.get(impl.type);
86         if (existing == impl) {
87             implementations.remove(impl.type);
88         }
89         for (listener : rpcRegistrationListeners.listeners) {
90             try {
91                 listener.instance.onRpcImplementationRemoved(impl.type);
92             } catch (Exception e) {
93                 log.error("Unhandled exception during invoking listener", e);
94             }
95         }
96     }
97     
98     override addRpcRegistrationListener(RpcRegistrationListener listener) {
99         rpcRegistrationListeners.register(listener);
100     }
101
102 }
103
104 class RpcRegistrationImpl extends AbstractObjectRegistration<RpcImplementation> implements RpcRegistration {
105
106     @Property
107     val QName type;
108
109     @Property
110     var RpcRouterImpl router;
111
112     new(QName type, RpcImplementation instance, RpcRouterImpl router) {
113         super(instance)
114         _type = type
115         _router = router
116     }
117
118     override protected removeRegistration() {
119         router.remove(this);
120     }
121 }
122 class RoutedRpcRegistrationImpl extends RpcRegistrationImpl implements RoutedRpcRegistration {
123
124
125     new(QName type, RpcImplementation instance, RpcRouterImpl router) {
126         super(type,instance,router)
127     }
128
129     override protected removeRegistration() {
130         router.remove(this);
131     }
132     override registerPath(QName context, InstanceIdentifier path) {
133         //
134         
135     }
136
137     override unregisterPath(QName context, InstanceIdentifier path) {
138         //
139     }
140 }