Bug 488: Removed requirement for DOM providers to supply BundleContext.
[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
24 import com.google.common.base.Optional;
25 import com.google.common.base.Preconditions;
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 Consumer consumer;
34
35     private BrokerImpl broker = null;
36     @GuardedBy("this")
37     private boolean closed = false;
38
39     public ConsumerContextImpl(final Consumer provider, final BrokerImpl brokerImpl) {
40         broker = brokerImpl;
41         consumer = provider;
42     }
43
44     @Override
45     public Future<RpcResult<CompositeNode>> rpc(final QName rpc,
46             final CompositeNode input) {
47         checkNotClosed();
48         return broker.invokeRpcAsync(rpc, input);
49     }
50
51     @Override
52     public <T extends BrokerService> T getService(final Class<T> service) {
53         checkNotClosed();
54         final T localProxy = instantiatedServices.getInstance(service);
55         if (localProxy != null) {
56             return localProxy;
57         }
58         final Optional<T> serviceImpl = broker.getGlobalService(service);
59         if(serviceImpl.isPresent()) {
60             final T ret = ProxyFactory.createProxy(null,serviceImpl.get());
61             instantiatedServices.putInstance(service, ret);
62             return ret;
63         } else {
64             return null;
65         }
66     }
67
68     @Override
69     public void close() {
70         synchronized (this) {
71             if (closed) {
72                 return;
73             }
74             this.closed = true;
75         }
76
77         Collection<BrokerService> toStop = instantiatedServices.values();
78         for (BrokerService brokerService : toStop) {
79             if (brokerService instanceof AbstractBrokerServiceProxy<?>) {
80                 ((AbstractBrokerServiceProxy<?>) brokerService).close();
81             }
82         }
83         broker.consumerSessionClosed(this);
84         broker = null;
85     }
86
87     @Override
88     public synchronized boolean isClosed() {
89         return closed;
90     }
91
92     /**
93      * @return the broker
94      */
95     protected final  BrokerImpl getBrokerChecked() {
96         checkNotClosed();
97         return broker;
98     }
99
100     /**
101      * @return the _consumer
102      */
103     public Consumer getConsumer() {
104         return consumer;
105     }
106
107     protected final void checkNotClosed()  {
108         Preconditions.checkState(!closed, "Session is closed.");
109     }
110 }

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