Added DELETE support for Bridge and Port resources
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / sal / dom / broker / ProviderContextImpl.xtend
1 package org.opendaylight.controller.sal.dom.broker
2
3 import java.util.Collections
4 import java.util.HashMap
5 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession
6 import org.opendaylight.controller.sal.core.api.Provider
7 import org.opendaylight.controller.sal.core.api.RpcImplementation
8 import org.opendaylight.yangtools.yang.common.QName
9 import org.osgi.framework.BundleContext
10 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration
11 import org.opendaylight.controller.sal.core.api.Broker.RpcRegistration
12 import static java.util.Collections.*
13 import java.util.Collections
14 import java.util.HashMap
15 import org.opendaylight.controller.sal.core.api.RpcRegistrationListener
16
17 class ProviderContextImpl extends ConsumerContextImpl implements ProviderSession {
18
19     @Property
20     private val Provider provider;
21
22     private val rpcImpls = Collections.synchronizedMap(new HashMap<QName, RpcImplementation>());
23
24     new(Provider provider, BundleContext ctx) {
25         super(null, ctx);
26         this._provider = provider;
27     }
28
29     override addRpcImplementation(QName rpcType, RpcImplementation implementation) throws IllegalArgumentException {
30         if (rpcType == null) {
31             throw new IllegalArgumentException("rpcType must not be null");
32         }
33         if (implementation == null) {
34             throw new IllegalArgumentException("Implementation must not be null");
35         }
36         broker.addRpcImplementation(rpcType, implementation);
37         rpcImpls.put(rpcType, implementation);
38
39         return new RpcRegistrationImpl(rpcType, implementation, this);
40     }
41
42     def removeRpcImplementation(RpcRegistrationImpl implToRemove) throws IllegalArgumentException {
43         val localImpl = rpcImpls.get(implToRemove.type);
44         if (localImpl !== implToRemove.instance) {
45             throw new IllegalStateException("Implementation was not registered in this session");
46         }
47         broker.removeRpcImplementation(implToRemove.type, localImpl);
48         rpcImpls.remove(implToRemove.type);
49     }
50
51     override close() {
52         removeAllRpcImlementations
53         super.close
54     }
55
56     private def removeAllRpcImlementations() {
57         if (!rpcImpls.empty) {
58             for (entry : rpcImpls.entrySet) {
59                 broker.removeRpcImplementation(entry.key, entry.value);
60             }
61             rpcImpls.clear
62         }
63     }
64
65     override addMountedRpcImplementation(QName rpcType, RpcImplementation implementation) {
66         throw new UnsupportedOperationException("TODO: auto-generated method stub")
67     }
68
69     override addRoutedRpcImplementation(QName rpcType, RpcImplementation implementation) {
70         throw new UnsupportedOperationException("TODO: auto-generated method stub")
71     }
72
73     override getSupportedRpcs() {
74         broker.getSupportedRpcs();
75     }
76
77     override addRpcRegistrationListener(RpcRegistrationListener listener) {
78         broker.addRpcRegistrationListener(listener);
79     }
80 }
81
82 class RpcRegistrationImpl extends AbstractObjectRegistration<RpcImplementation> implements RpcRegistration {
83
84     @Property
85     val QName type
86
87     private var ProviderContextImpl context
88
89     new(QName type, RpcImplementation instance, ProviderContextImpl ctx) {
90         super(instance)
91         _type = type
92         context = ctx
93     }
94
95     override protected removeRegistration() {
96         context.removeRpcImplementation(this)
97         context = null
98     }
99
100 }