fa81bc9040849c6b198187c4d846de308bfbe685
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / sal / dom / broker / ConsumerContextImpl.java
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 java.util.Collection;
11 import java.util.concurrent.Future;
12
13 import javax.annotation.concurrent.GuardedBy;
14
15 import org.opendaylight.controller.sal.core.api.Broker.ConsumerSession;
16 import org.opendaylight.controller.sal.core.api.BrokerService;
17 import org.opendaylight.controller.sal.core.api.Consumer;
18 import org.opendaylight.controller.sal.dom.broker.osgi.AbstractBrokerServiceProxy;
19 import org.opendaylight.controller.sal.dom.broker.osgi.ProxyFactory;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.RpcResult;
22 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
23 import org.osgi.framework.BundleContext;
24 import org.osgi.framework.ServiceReference;
25
26 import com.google.common.collect.ClassToInstanceMap;
27 import com.google.common.collect.MutableClassToInstanceMap;
28
29 class ConsumerContextImpl implements ConsumerSession {
30
31     private final ClassToInstanceMap<BrokerService> instantiatedServices = MutableClassToInstanceMap
32             .create();
33     private final BundleContext context;
34     private final Consumer consumer;
35
36     private BrokerImpl broker = null;
37     @GuardedBy("this")
38     private boolean closed = false;
39
40     public ConsumerContextImpl(final Consumer consumer, final BundleContext ctx) {
41         this.consumer = consumer;
42         this.context = ctx;
43     }
44
45     @Override
46     public Future<RpcResult<CompositeNode>> rpc(final QName rpc,
47             final CompositeNode input) {
48         return broker.invokeRpcAsync(rpc, input);
49     }
50
51     @Override
52     public <T extends BrokerService> T getService(final Class<T> service) {
53         final T localProxy = instantiatedServices.getInstance(service);
54         if (localProxy != null) {
55             return localProxy;
56         }
57         final ServiceReference<T> serviceRef = context
58                 .getServiceReference(service);
59         if (serviceRef == null) {
60             return null;
61         }
62         final T serviceImpl = context.getService(serviceRef);
63         final T ret = ProxyFactory.createProxy(serviceRef, serviceImpl);
64         if (ret != null) {
65             instantiatedServices.putInstance(service, ret);
66         }
67         return ret;
68     }
69
70     @Override
71     public void close() {
72         synchronized (this) {
73             if (closed) {
74                 return;
75             }
76             this.closed = true;
77         }
78
79         Collection<BrokerService> toStop = instantiatedServices.values();
80         for (BrokerService brokerService : toStop) {
81             if (brokerService instanceof AbstractBrokerServiceProxy<?>) {
82                 ((AbstractBrokerServiceProxy<?>) brokerService).close();
83             }
84         }
85         broker.consumerSessionClosed(this);
86     }
87
88     @Override
89     public synchronized boolean isClosed() {
90         return closed;
91     }
92
93     /**
94      * @return the broker
95      */
96     public BrokerImpl getBroker() {
97         return broker;
98     }
99
100     /**
101      * @param broker
102      *            the broker to set
103      */
104     public void setBroker(final BrokerImpl broker) {
105         this.broker = broker;
106     }
107
108     /**
109      * @return the _consumer
110      */
111     public Consumer getConsumer() {
112         return consumer;
113     }
114 }

©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.