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 org.opendaylight.controller.sal.core.api.Broker.ConsumerSession
11 import org.opendaylight.controller.sal.core.api.BrokerService
12 import org.opendaylight.controller.sal.core.api.Consumer
13 import org.osgi.framework.BundleContext
14 import org.opendaylight.yangtools.yang.common.QName
15 import org.opendaylight.yangtools.yang.data.api.CompositeNode
16 import org.opendaylight.controller.sal.dom.broker.osgi.AbstractBrokerServiceProxy
17 import com.google.common.collect.ClassToInstanceMap
18 import com.google.common.collect.MutableClassToInstanceMap
19 import org.opendaylight.controller.sal.dom.broker.osgi.ProxyFactory
21 class ConsumerContextImpl implements ConsumerSession {
24 private val Consumer consumer;
27 private var BrokerImpl broker;
29 private val ClassToInstanceMap<BrokerService> instantiatedServices = MutableClassToInstanceMap.create();
30 private boolean closed = false;
32 private BundleContext context;
34 public new(Consumer consumer, BundleContext ctx) {
35 this._consumer = consumer;
39 override rpc(QName rpc, CompositeNode input) {
40 return broker.invokeRpc(rpc, input);
43 override <T extends BrokerService> T getService(Class<T> service) {
44 val localProxy = instantiatedServices.getInstance(service);
45 if(localProxy != null) {
48 val serviceRef = context.getServiceReference(service);
49 if(serviceRef == null) {
52 val serviceImpl = context.getService(serviceRef);
55 val ret = ProxyFactory.createProxy(serviceRef,serviceImpl);
57 instantiatedServices.putInstance(service, ret);
63 val toStop = instantiatedServices.values();
65 for (BrokerService brokerService : toStop) {
66 if(brokerService instanceof AbstractBrokerServiceProxy<?>) {
67 (brokerService as AutoCloseable).close();
70 broker.consumerSessionClosed(this);