Merge "Bug 946: Fixed eclipse errors with nagasena projects"
[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.core.api.mount.MountInstance
15 import org.opendaylight.controller.sal.rest.impl.RestconfProvider
16 import org.opendaylight.controller.sal.streams.listeners.ListenerAdapter
17 import org.opendaylight.yangtools.yang.common.QName
18 import org.opendaylight.yangtools.yang.common.RpcResult
19 import org.opendaylight.yangtools.yang.data.api.CompositeNode
20 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
21 import org.slf4j.LoggerFactory
22
23 class BrokerFacade implements DataReader<InstanceIdentifier, CompositeNode> {
24
25
26     val static LOG = LoggerFactory.getLogger(BrokerFacade)
27     val static BrokerFacade INSTANCE = new BrokerFacade
28
29     @Property
30     private ConsumerSession context;
31
32     @Property
33     private DataBrokerService dataService;
34     
35     private new() {
36         if (INSTANCE !== null) {
37             throw new IllegalStateException("Already instantiated");
38         }
39     }
40
41     def static BrokerFacade getInstance() {
42         return INSTANCE
43     }
44
45     private def void checkPreconditions() {
46         if (context === null || dataService === null) {
47             throw new ResponseException(Response.Status.SERVICE_UNAVAILABLE, RestconfProvider::NOT_INITALIZED_MSG)
48         }
49     }
50
51     override readConfigurationData(InstanceIdentifier path) {
52         checkPreconditions
53         LOG.trace("Read Configuration via Restconf: {}", path)
54         return dataService.readConfigurationData(path);
55     }
56     
57     def readConfigurationDataBehindMountPoint(MountInstance mountPoint, InstanceIdentifier path) {
58         checkPreconditions
59         LOG.trace("Read Configuration via Restconf: {}", path)
60         return mountPoint.readConfigurationData(path);
61     }
62
63     override readOperationalData(InstanceIdentifier path) {
64         checkPreconditions
65         LOG.trace("Read Operational via Restconf: {}", path)
66         return dataService.readOperationalData(path);
67     }
68     
69     def readOperationalDataBehindMountPoint(MountInstance mountPoint, InstanceIdentifier path) {
70         checkPreconditions
71         LOG.trace("Read Operational via Restconf: {}", path)
72         return mountPoint.readOperationalData(path);
73     }
74
75     def RpcResult<CompositeNode> invokeRpc(QName type, CompositeNode payload) {
76         checkPreconditions
77         val future = context.rpc(type, payload);
78         return future.get;
79     }
80
81     def commitConfigurationDataPut(InstanceIdentifier path, CompositeNode payload) {
82         checkPreconditions
83         val transaction = dataService.beginTransaction;
84         LOG.trace("Put Configuration via Restconf: {}", path)
85         transaction.putConfigurationData(path, payload);
86         return transaction.commit
87     }
88     
89     def commitConfigurationDataPutBehindMountPoint(MountInstance mountPoint, InstanceIdentifier path, CompositeNode payload) {
90         checkPreconditions
91         val transaction = mountPoint.beginTransaction;
92         LOG.trace("Put Configuration via Restconf: {}", path)
93         transaction.putConfigurationData(path, payload);
94         return transaction.commit
95     }
96
97     def commitConfigurationDataPost(InstanceIdentifier path, CompositeNode payload) {
98         checkPreconditions
99         val transaction = dataService.beginTransaction;
100         transaction.putConfigurationData(path, payload);
101         if (payload == transaction.createdConfigurationData.get(path)) {
102             LOG.trace("Post Configuration via Restconf: {}", path)
103             return transaction.commit
104         }
105         LOG.trace("Post Configuration via Restconf was not executed because data already exists: {}", path)
106         return null;
107     }
108     
109     def commitConfigurationDataPostBehindMountPoint(MountInstance mountPoint, InstanceIdentifier path, CompositeNode payload) {
110         checkPreconditions
111         val transaction = mountPoint.beginTransaction;
112         transaction.putConfigurationData(path, payload);
113         if (payload == transaction.createdConfigurationData.get(path)) {
114             LOG.trace("Post Configuration via Restconf: {}", path)
115             return transaction.commit
116         }
117         LOG.trace("Post Configuration via Restconf was not executed because data already exists: {}", path)
118         return null;
119     }
120
121     def commitConfigurationDataDelete(InstanceIdentifier path) {
122         checkPreconditions
123         val transaction = dataService.beginTransaction;
124         LOG.info("Delete Configuration via Restconf: {}", path)
125         transaction.removeConfigurationData(path)
126         return transaction.commit
127     }
128     
129     def commitConfigurationDataDeleteBehindMountPoint(MountInstance mountPoint, InstanceIdentifier path) {
130         checkPreconditions
131         val transaction = mountPoint.beginTransaction;
132         LOG.info("Delete Configuration via Restconf: {}", path)
133         transaction.removeConfigurationData(path)
134         return transaction.commit
135     }
136
137     def registerToListenDataChanges(ListenerAdapter listener) {
138         checkPreconditions
139         if (listener.listening) {
140             return;
141         }
142         val registration = dataService.registerDataChangeListener(listener.path, listener)
143         listener.setRegistration(registration)
144     }
145
146 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.