Merge "ISSUE: Some changes to Authorization"
[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     private boolean closed = false;\r
30 \r
31     public Consumer getConsumer() {\r
32         return consumer;\r
33     }\r
34 \r
35     public ConsumerSessionImpl(BrokerImpl broker, Consumer consumer) {\r
36         this.broker = broker;\r
37         this.consumer = consumer;\r
38     }\r
39 \r
40     @Override\r
41     public Future<RpcResult<CompositeNode>> rpc(QName rpc, CompositeNode input) {\r
42         return broker.invokeRpc(rpc, input);\r
43     }\r
44 \r
45     @Override\r
46     public <T extends BrokerService> T getService(Class<T> service) {\r
47         BrokerService potential = instantiatedServices.get(service);\r
48         if (potential != null) {\r
49             @SuppressWarnings("unchecked")\r
50             T ret = (T) potential;\r
51             return ret;\r
52         }\r
53         T ret = this.broker.serviceFor(service, this);\r
54         if (ret != null) {\r
55             instantiatedServices.put(service, ret);\r
56         }\r
57         return ret;\r
58     }\r
59 \r
60     @Override\r
61     public void close() {\r
62         Collection<BrokerService> toStop = instantiatedServices.values();\r
63         this.closed  = true;\r
64         for (BrokerService brokerService : toStop) {\r
65             brokerService.closeSession();\r
66         }\r
67         broker.consumerSessionClosed(this);\r
68     }\r
69 \r
70     @Override\r
71     public boolean isClosed() {\r
72         return closed;\r
73     }\r
74 \r
75 }