9c0c3ac14c9eabb9757ac526b505822e573a0824
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / impl / BrokerFacade.xtend
1 package org.opendaylight.controller.sal.restconf.impl
2
3 import javax.ws.rs.core.Response
4 import org.opendaylight.controller.md.sal.common.api.data.DataReader
5 import org.opendaylight.controller.sal.core.api.Broker.ConsumerSession
6 import org.opendaylight.controller.sal.core.api.data.DataBrokerService
7 import org.opendaylight.controller.sal.rest.impl.RestconfProvider
8 import org.opendaylight.yangtools.yang.common.QName
9 import org.opendaylight.yangtools.yang.common.RpcResult
10 import org.opendaylight.yangtools.yang.data.api.CompositeNode
11 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
12 import org.slf4j.LoggerFactory
13 import org.opendaylight.controller.sal.core.api.mount.MountInstance
14
15 class BrokerFacade implements DataReader<InstanceIdentifier, CompositeNode> {
16
17
18     val static LOG = LoggerFactory.getLogger(BrokerFacade)
19     val static BrokerFacade INSTANCE = new BrokerFacade
20
21     @Property
22     private ConsumerSession context;
23
24     @Property
25     private DataBrokerService dataService;
26     
27     private new() {
28         if (INSTANCE !== null) {
29             throw new IllegalStateException("Already instantiated");
30         }
31     }
32
33     def static BrokerFacade getInstance() {
34         return INSTANCE
35     }
36
37     private def void checkPreconditions() {
38         if (context === null || dataService === null) {
39             throw new ResponseException(Response.Status.SERVICE_UNAVAILABLE, RestconfProvider::NOT_INITALIZED_MSG)
40         }
41     }
42
43     override readConfigurationData(InstanceIdentifier path) {
44         checkPreconditions
45         LOG.info("Read Configuration via Restconf: {}", path)
46         return dataService.readConfigurationData(path);
47     }
48     
49     def readConfigurationDataBehindMountPoint(MountInstance mountPoint, InstanceIdentifier path) {
50         checkPreconditions
51         LOG.info("Read Configuration via Restconf: {}", path)
52         return mountPoint.readConfigurationData(path);
53     }
54
55     override readOperationalData(InstanceIdentifier path) {
56         checkPreconditions
57         LOG.info("Read Operational via Restconf: {}", path)
58         return dataService.readOperationalData(path);
59     }
60     
61     def readOperationalDataBehindMountPoint(MountInstance mountPoint, InstanceIdentifier path) {
62         checkPreconditions
63         LOG.info("Read Operational via Restconf: {}", path)
64         return mountPoint.readOperationalData(path);
65     }
66
67     def RpcResult<CompositeNode> invokeRpc(QName type, CompositeNode payload) {
68         checkPreconditions
69         val future = context.rpc(type, payload);
70         return future.get;
71     }
72
73     def commitConfigurationDataPut(InstanceIdentifier path, CompositeNode payload) {
74         checkPreconditions
75         val transaction = dataService.beginTransaction;
76         LOG.info("Put Configuration via Restconf: {}", path)
77         transaction.putConfigurationData(path, payload);
78         return transaction.commit
79     }
80     
81     def commitConfigurationDataPutBehindMountPoint(MountInstance mountPoint, InstanceIdentifier path, CompositeNode payload) {
82         checkPreconditions
83         val transaction = mountPoint.beginTransaction;
84         LOG.info("Put Configuration via Restconf: {}", path)
85         transaction.putConfigurationData(path, payload);
86         return transaction.commit
87     }
88
89     def commitConfigurationDataPost(InstanceIdentifier path, CompositeNode payload) {
90         checkPreconditions
91         val transaction = dataService.beginTransaction;
92         transaction.putConfigurationData(path, payload);
93         if (payload == transaction.createdConfigurationData.get(path)) {
94             LOG.info("Post Configuration via Restconf: {}", path)
95             return transaction.commit
96         }
97         LOG.info("Post Configuration via Restconf was not executed because data already exists: {}", path)
98         return null;
99     }
100     
101     def commitConfigurationDataPostBehindMountPoint(MountInstance mountPoint, InstanceIdentifier path, CompositeNode payload) {
102         checkPreconditions
103         val transaction = mountPoint.beginTransaction;
104         transaction.putConfigurationData(path, payload);
105         if (payload == transaction.createdConfigurationData.get(path)) {
106             LOG.info("Post Configuration via Restconf: {}", path)
107             return transaction.commit
108         }
109         LOG.info("Post Configuration via Restconf was not executed because data already exists: {}", path)
110         return null;
111     }
112
113     def commitConfigurationDataDelete(InstanceIdentifier path) {
114         checkPreconditions
115         val transaction = dataService.beginTransaction;
116         LOG.info("Delete Configuration via Restconf: {}", path)
117         transaction.removeConfigurationData(path)
118         return transaction.commit
119     }
120     
121     def commitConfigurationDataDeleteBehindMountPoint(MountInstance mountPoint, InstanceIdentifier path) {
122         checkPreconditions
123         val transaction = mountPoint.beginTransaction;
124         LOG.info("Delete Configuration via Restconf: {}", path)
125         transaction.removeConfigurationData(path)
126         return transaction.commit
127     }
128
129 }