2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.dom.broker;
10 import java.util.Collection;
11 import java.util.concurrent.Future;
13 import javax.annotation.concurrent.GuardedBy;
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;
26 import com.google.common.collect.ClassToInstanceMap;
27 import com.google.common.collect.MutableClassToInstanceMap;
29 class ConsumerContextImpl implements ConsumerSession {
31 private final ClassToInstanceMap<BrokerService> instantiatedServices = MutableClassToInstanceMap
33 private final BundleContext context;
34 private final Consumer consumer;
36 private BrokerImpl broker = null;
38 private boolean closed = false;
40 public ConsumerContextImpl(final Consumer consumer, final BundleContext ctx) {
41 this.consumer = consumer;
46 public Future<RpcResult<CompositeNode>> rpc(final QName rpc,
47 final CompositeNode input) {
48 return broker.invokeRpcAsync(rpc, input);
52 public <T extends BrokerService> T getService(final Class<T> service) {
53 final T localProxy = instantiatedServices.getInstance(service);
54 if (localProxy != null) {
57 final ServiceReference<T> serviceRef = context
58 .getServiceReference(service);
59 if (serviceRef == null) {
62 final T serviceImpl = context.getService(serviceRef);
63 final T ret = ProxyFactory.createProxy(serviceRef, serviceImpl);
65 instantiatedServices.putInstance(service, ret);
79 Collection<BrokerService> toStop = instantiatedServices.values();
80 for (BrokerService brokerService : toStop) {
81 if (brokerService instanceof AbstractBrokerServiceProxy<?>) {
82 ((AbstractBrokerServiceProxy<?>) brokerService).close();
85 broker.consumerSessionClosed(this);
89 public synchronized boolean isClosed() {
96 public BrokerImpl getBroker() {
104 public void setBroker(final BrokerImpl broker) {
105 this.broker = broker;
109 * @return the _consumer
111 public Consumer getConsumer() {