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