Update to MD-SAL APIs
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / sal / dom / broker / ConsumerContextImpl.xtend
1 package org.opendaylight.controller.sal.dom.broker
2
3 import java.util.Collections
4 import org.opendaylight.controller.sal.core.api.Broker.ConsumerSession
5 import java.util.HashMap
6 import org.opendaylight.controller.sal.core.api.BrokerService
7 import org.opendaylight.controller.sal.core.api.Consumer
8 import org.osgi.framework.BundleContext
9 import org.opendaylight.yangtools.yang.common.QName
10 import org.opendaylight.yangtools.yang.data.api.CompositeNode
11
12 class ConsumerContextImpl implements ConsumerSession {
13
14     @Property
15     private val Consumer consumer;
16
17     @Property
18     private var BrokerImpl broker;
19
20     private val instantiatedServices = Collections.synchronizedMap(
21         new HashMap<Class<? extends BrokerService>, BrokerService>());
22     private boolean closed = false;
23
24     private BundleContext context;
25
26     public new(Consumer consumer, BundleContext ctx) {
27         this._consumer = consumer;
28         this.context = ctx;
29     }
30
31     override rpc(QName rpc, CompositeNode input) {
32         return broker.invokeRpc(rpc, input);
33     }
34
35     override <T extends BrokerService> T getService(Class<T> service) {
36         val potential = instantiatedServices.get(service);
37         if(potential != null) {
38             val ret = potential as T;
39             return ret;
40         }
41         val ret = broker.serviceFor(service, this);
42         if(ret != null) {
43             instantiatedServices.put(service, ret);
44         }
45         return ret;
46     }
47
48     override close() {
49         val toStop = instantiatedServices.values();
50         this.closed = true;
51         for (BrokerService brokerService : toStop) {
52             //brokerService.closeSession();
53         }
54         broker.consumerSessionClosed(this);
55     }
56
57     override isClosed() {
58         return closed;
59     }
60 }