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

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