813f52b67d4bab0632fdefb6a9fe7d23d8054792
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / sal / dom / broker / ConsumerContextImpl.xtend
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.sal.dom.broker
9
10 import org.opendaylight.controller.sal.core.api.Broker.ConsumerSession
11 import org.opendaylight.controller.sal.core.api.BrokerService
12 import org.opendaylight.controller.sal.core.api.Consumer
13 import org.osgi.framework.BundleContext
14 import org.opendaylight.yangtools.yang.common.QName
15 import org.opendaylight.yangtools.yang.data.api.CompositeNode
16 import org.opendaylight.controller.sal.dom.broker.osgi.AbstractBrokerServiceProxy
17 import com.google.common.collect.ClassToInstanceMap
18 import com.google.common.collect.MutableClassToInstanceMap
19 import org.opendaylight.controller.sal.dom.broker.osgi.ProxyFactory
20
21 class ConsumerContextImpl implements ConsumerSession {
22
23     @Property
24     private val Consumer consumer;
25
26     @Property
27     private var BrokerImpl broker;
28
29     private val ClassToInstanceMap<BrokerService> instantiatedServices = MutableClassToInstanceMap.create();
30     private boolean closed = false;
31
32     private BundleContext context;
33
34     public new(Consumer consumer, BundleContext ctx) {
35         this._consumer = consumer;
36         this.context = ctx;
37     }
38
39     override rpc(QName rpc, CompositeNode input) {
40         return broker.invokeRpcAsync(rpc, input);
41     }
42
43     override <T extends BrokerService> T getService(Class<T> service) {
44         val localProxy = instantiatedServices.getInstance(service);
45         if(localProxy != null) {
46             return localProxy;
47         }
48         val serviceRef = context.getServiceReference(service);
49         if(serviceRef == null) {
50             return null;
51         }
52         val serviceImpl = context.getService(serviceRef);
53         
54         
55         val ret = ProxyFactory.createProxy(serviceRef,serviceImpl);
56         if(ret != null) {
57             instantiatedServices.putInstance(service, ret);
58         }
59         return ret;
60     }
61
62     override close() {
63         val toStop = instantiatedServices.values();
64         this.closed = true;
65         for (BrokerService brokerService : toStop) {
66             if(brokerService instanceof AbstractBrokerServiceProxy<?>) {
67                 (brokerService as AutoCloseable).close();
68             } 
69         }
70         broker.consumerSessionClosed(this);
71     }
72
73     override isClosed() {
74         return closed;
75     }
76 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.