2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.restconf.impl
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
22 class BrokerFacade implements DataReader<InstanceIdentifier, CompositeNode> {
25 val static LOG = LoggerFactory.getLogger(BrokerFacade)
26 val static BrokerFacade INSTANCE = new BrokerFacade
29 private ConsumerSession context;
32 private DataBrokerService dataService;
35 if (INSTANCE !== null) {
36 throw new IllegalStateException("Already instantiated");
40 def static BrokerFacade getInstance() {
44 private def void checkPreconditions() {
45 if (context === null || dataService === null) {
46 throw new ResponseException(Response.Status.SERVICE_UNAVAILABLE, RestconfProvider::NOT_INITALIZED_MSG)
50 override readConfigurationData(InstanceIdentifier path) {
52 LOG.trace("Read Configuration via Restconf: {}", path)
53 return dataService.readConfigurationData(path);
56 def readConfigurationDataBehindMountPoint(MountInstance mountPoint, InstanceIdentifier path) {
58 LOG.trace("Read Configuration via Restconf: {}", path)
59 return mountPoint.readConfigurationData(path);
62 override readOperationalData(InstanceIdentifier path) {
64 LOG.trace("Read Operational via Restconf: {}", path)
65 return dataService.readOperationalData(path);
68 def readOperationalDataBehindMountPoint(MountInstance mountPoint, InstanceIdentifier path) {
70 LOG.trace("Read Operational via Restconf: {}", path)
71 return mountPoint.readOperationalData(path);
74 def RpcResult<CompositeNode> invokeRpc(QName type, CompositeNode payload) {
76 val future = context.rpc(type, payload);
80 def commitConfigurationDataPut(InstanceIdentifier path, CompositeNode payload) {
82 val transaction = dataService.beginTransaction;
83 LOG.trace("Put Configuration via Restconf: {}", path)
84 transaction.putConfigurationData(path, payload);
85 return transaction.commit
88 def commitConfigurationDataPutBehindMountPoint(MountInstance mountPoint, InstanceIdentifier path, CompositeNode payload) {
90 val transaction = mountPoint.beginTransaction;
91 LOG.trace("Put Configuration via Restconf: {}", path)
92 transaction.putConfigurationData(path, payload);
93 return transaction.commit
96 def commitConfigurationDataPost(InstanceIdentifier path, CompositeNode payload) {
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
104 LOG.trace("Post Configuration via Restconf was not executed because data already exists: {}", path)
108 def commitConfigurationDataPostBehindMountPoint(MountInstance mountPoint, InstanceIdentifier path, CompositeNode payload) {
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
116 LOG.trace("Post Configuration via Restconf was not executed because data already exists: {}", path)
120 def commitConfigurationDataDelete(InstanceIdentifier path) {
122 val transaction = dataService.beginTransaction;
123 LOG.info("Delete Configuration via Restconf: {}", path)
124 transaction.removeConfigurationData(path)
125 return transaction.commit
128 def commitConfigurationDataDeleteBehindMountPoint(MountInstance mountPoint, InstanceIdentifier path) {
130 val transaction = mountPoint.beginTransaction;
131 LOG.info("Delete Configuration via Restconf: {}", path)
132 transaction.removeConfigurationData(path)
133 return transaction.commit