-/*\r
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.\r
- *\r
- * This program and the accompanying materials are made available under the\r
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
- * and is available at http://www.eclipse.org/legal/epl-v10.html\r
- */\r
-package org.opendaylight.controller.sal.core.impl;\r
-\r
-import java.util.Collection;\r
-import java.util.HashMap;\r
-import java.util.Map;\r
-import java.util.concurrent.Future;\r
-\r
-import org.opendaylight.controller.sal.core.api.BrokerService;\r
-import org.opendaylight.controller.sal.core.api.Consumer;\r
-import org.opendaylight.controller.sal.core.api.Broker.ConsumerSession;\r
-import org.opendaylight.controller.yang.common.QName;\r
-import org.opendaylight.controller.yang.common.RpcResult;\r
-import org.opendaylight.controller.yang.data.api.CompositeNode;\r
-\r
-\r
-public class ConsumerSessionImpl implements ConsumerSession {\r
-\r
- private final BrokerImpl broker;\r
- private final Consumer consumer;\r
-\r
- private Map<Class<? extends BrokerService>, BrokerService> instantiatedServices = new HashMap<Class<? extends BrokerService>, BrokerService>();\r
- private boolean closed = false;\r
-\r
- public Consumer getConsumer() {\r
- return consumer;\r
- }\r
-\r
- public ConsumerSessionImpl(BrokerImpl broker, Consumer consumer) {\r
- this.broker = broker;\r
- this.consumer = consumer;\r
- }\r
-\r
- @Override\r
- public Future<RpcResult<CompositeNode>> rpc(QName rpc, CompositeNode input) {\r
- return broker.invokeRpc(rpc, input);\r
- }\r
-\r
- @Override\r
- public <T extends BrokerService> T getService(Class<T> service) {\r
- BrokerService potential = instantiatedServices.get(service);\r
- if (potential != null) {\r
- @SuppressWarnings("unchecked")\r
- T ret = (T) potential;\r
- return ret;\r
- }\r
- T ret = this.broker.serviceFor(service, this);\r
- if (ret != null) {\r
- instantiatedServices.put(service, ret);\r
- }\r
- return ret;\r
- }\r
-\r
- @Override\r
- public void close() {\r
- Collection<BrokerService> toStop = instantiatedServices.values();\r
- this.closed = true;\r
- for (BrokerService brokerService : toStop) {\r
- brokerService.closeSession();\r
- }\r
- broker.consumerSessionClosed(this);\r
- }\r
-\r
- @Override\r
- public boolean isClosed() {\r
- return closed;\r
- }\r
-\r
-}