Initial opendaylight infrastructure commit!!
[controller.git] / opendaylight / sal / yang-prototype / sal / sal-broker-impl / src / main / java / org / opendaylight / controller / sal / core / impl / ConsumerSessionImpl.java
1 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.controller.sal.core.impl;\r
9 \r
10 import java.util.Collection;\r
11 import java.util.HashMap;\r
12 import java.util.Map;\r
13 import java.util.concurrent.Future;\r
14 \r
15 import org.opendaylight.controller.sal.core.api.BrokerService;\r
16 import org.opendaylight.controller.sal.core.api.Consumer;\r
17 import org.opendaylight.controller.sal.core.api.Broker.ConsumerSession;\r
18 import org.opendaylight.controller.yang.common.QName;\r
19 import org.opendaylight.controller.yang.common.RpcResult;\r
20 import org.opendaylight.controller.yang.data.api.CompositeNode;\r
21 \r
22 \r
23 public class ConsumerSessionImpl implements ConsumerSession {\r
24 \r
25     private final BrokerImpl broker;\r
26     private final Consumer consumer;\r
27 \r
28     private Map<Class<? extends BrokerService>, BrokerService> instantiatedServices = new HashMap<Class<? extends BrokerService>, BrokerService>();\r
29 \r
30     public Consumer getConsumer() {\r
31         return consumer;\r
32     }\r
33 \r
34     public ConsumerSessionImpl(BrokerImpl broker, Consumer consumer) {\r
35         this.broker = broker;\r
36         this.consumer = consumer;\r
37     }\r
38 \r
39     @Override\r
40     public Future<RpcResult<CompositeNode>> rpc(QName rpc, CompositeNode input) {\r
41         return broker.invokeRpc(rpc, input);\r
42     }\r
43 \r
44     @Override\r
45     public <T extends BrokerService> T getService(Class<T> service) {\r
46         BrokerService potential = instantiatedServices.get(service);\r
47         if (potential != null) {\r
48             @SuppressWarnings("unchecked")\r
49             T ret = (T) potential;\r
50             return ret;\r
51         }\r
52         T ret = this.broker.serviceFor(service, this);\r
53         if (ret != null) {\r
54             instantiatedServices.put(service, ret);\r
55         }\r
56         return ret;\r
57     }\r
58 \r
59     @Override\r
60     public void close() {\r
61         Collection<BrokerService> toStop = instantiatedServices.values();\r
62         for (BrokerService brokerService : toStop) {\r
63             brokerService.closeSession();\r
64         }\r
65         broker.consumerSessionClosed(this);\r
66     }\r
67 \r
68 }\r
69