721b1e5f0986b940b1094b2646b40243e5426831
[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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ClassToInstanceMap;
13 import com.google.common.collect.MutableClassToInstanceMap;
14 import org.opendaylight.controller.sal.core.api.Broker.ConsumerSession;
15 import org.opendaylight.controller.sal.core.api.BrokerService;
16 import org.opendaylight.controller.sal.core.api.Consumer;
17 import org.opendaylight.controller.sal.dom.broker.osgi.AbstractBrokerServiceProxy;
18 import org.opendaylight.controller.sal.dom.broker.osgi.ProxyFactory;
19
20 import javax.annotation.concurrent.GuardedBy;
21 import java.util.Collection;
22
23 class ConsumerContextImpl implements ConsumerSession {
24
25     private final ClassToInstanceMap<BrokerService> instantiatedServices = MutableClassToInstanceMap
26             .create();
27     private final Consumer consumer;
28
29     private BrokerImpl broker = null;
30     @GuardedBy("this")
31     private boolean closed = false;
32
33     public ConsumerContextImpl(final Consumer provider, final BrokerImpl brokerImpl) {
34         broker = brokerImpl;
35         consumer = provider;
36     }
37
38     @Override
39     public <T extends BrokerService> T getService(final Class<T> service) {
40         checkNotClosed();
41         final T localProxy = instantiatedServices.getInstance(service);
42         if (localProxy != null) {
43             return localProxy;
44         }
45         final Optional<T> serviceImpl = broker.getGlobalService(service);
46         if(serviceImpl.isPresent()) {
47             final T ret = ProxyFactory.createProxy(null,serviceImpl.get());
48             instantiatedServices.putInstance(service, ret);
49             return ret;
50         } else {
51             return null;
52         }
53     }
54
55     @Override
56     public void close() {
57         synchronized (this) {
58             if (closed) {
59                 return;
60             }
61             this.closed = true;
62         }
63
64         Collection<BrokerService> toStop = instantiatedServices.values();
65         for (BrokerService brokerService : toStop) {
66             if (brokerService instanceof AbstractBrokerServiceProxy<?>) {
67                 ((AbstractBrokerServiceProxy<?>) brokerService).close();
68             }
69         }
70         broker.consumerSessionClosed(this);
71         broker = null;
72     }
73
74
75     @Override
76     public synchronized boolean isClosed() {
77         return closed;
78     }
79
80     /**
81      * @return the broker
82      */
83     protected final  BrokerImpl getBrokerChecked() {
84         checkNotClosed();
85         return broker;
86     }
87
88     /**
89      * @return the _consumer
90      */
91     public Consumer getConsumer() {
92         return consumer;
93     }
94
95     protected final void checkNotClosed()  {
96         Preconditions.checkState(!closed, "Session is closed.");
97     }
98 }

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