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.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
23 class BrokerFacade implements DataReader<InstanceIdentifier, CompositeNode> {
26 val static LOG = LoggerFactory.getLogger(BrokerFacade)
27 val static BrokerFacade INSTANCE = new BrokerFacade
30 private ConsumerSession context;
33 private DataBrokerService dataService;
36 if (INSTANCE !== null) {
37 throw new IllegalStateException("Already instantiated");
41 def static BrokerFacade getInstance() {
45 private def void checkPreconditions() {
46 if (context === null || dataService === null) {
47 throw new ResponseException(Response.Status.SERVICE_UNAVAILABLE, RestconfProvider::NOT_INITALIZED_MSG)
51 override readConfigurationData(InstanceIdentifier path) {
53 LOG.trace("Read Configuration via Restconf: {}", path)
54 return dataService.readConfigurationData(path);
57 def readConfigurationDataBehindMountPoint(MountInstance mountPoint, InstanceIdentifier path) {
59 LOG.trace("Read Configuration via Restconf: {}", path)
60 return mountPoint.readConfigurationData(path);
63 override readOperationalData(InstanceIdentifier path) {
65 LOG.trace("Read Operational via Restconf: {}", path)
66 return dataService.readOperationalData(path);
69 def readOperationalDataBehindMountPoint(MountInstance mountPoint, InstanceIdentifier path) {
71 LOG.trace("Read Operational via Restconf: {}", path)
72 return mountPoint.readOperationalData(path);
75 def RpcResult<CompositeNode> invokeRpc(QName type, CompositeNode payload) {
77 val future = context.rpc(type, payload);
81 def commitConfigurationDataPut(InstanceIdentifier path, CompositeNode payload) {
83 val transaction = dataService.beginTransaction;
84 LOG.trace("Put Configuration via Restconf: {}", path)
85 transaction.putConfigurationData(path, payload);
86 return transaction.commit
89 def commitConfigurationDataPutBehindMountPoint(MountInstance mountPoint, InstanceIdentifier path, CompositeNode payload) {
91 val transaction = mountPoint.beginTransaction;
92 LOG.trace("Put Configuration via Restconf: {}", path)
93 transaction.putConfigurationData(path, payload);
94 return transaction.commit
97 def commitConfigurationDataPost(InstanceIdentifier path, CompositeNode payload) {
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
105 LOG.trace("Post Configuration via Restconf was not executed because data already exists: {}", path)
109 def commitConfigurationDataPostBehindMountPoint(MountInstance mountPoint, InstanceIdentifier path, CompositeNode payload) {
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
117 LOG.trace("Post Configuration via Restconf was not executed because data already exists: {}", path)
121 def commitConfigurationDataDelete(InstanceIdentifier path) {
123 val transaction = dataService.beginTransaction;
124 LOG.info("Delete Configuration via Restconf: {}", path)
125 transaction.removeConfigurationData(path)
126 return transaction.commit
129 def commitConfigurationDataDeleteBehindMountPoint(MountInstance mountPoint, InstanceIdentifier path) {
131 val transaction = mountPoint.beginTransaction;
132 LOG.info("Delete Configuration via Restconf: {}", path)
133 transaction.removeConfigurationData(path)
134 return transaction.commit
137 def registerToListenDataChanges(ListenerAdapter listener) {
139 if (listener.listening) {
142 val registration = dataService.registerDataChangeListener(listener.path, listener)
143 listener.setRegistration(registration)