1 package org.opendaylight.controller.sal.restconf.impl
3 import java.util.ArrayList
6 import javax.ws.rs.core.Response
7 import org.opendaylight.controller.md.sal.common.api.TransactionStatus
8 import org.opendaylight.controller.sal.rest.api.RestconfService
9 import org.opendaylight.yangtools.yang.data.api.CompositeNode
10 import org.opendaylight.yangtools.yang.data.api.Node
11 import org.opendaylight.yangtools.yang.data.impl.NodeFactory
12 import org.opendaylight.yangtools.yang.model.api.ChoiceNode
13 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer
14 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode
16 class RestconfImpl implements RestconfService {
18 val static RestconfImpl INSTANCE = new RestconfImpl
24 extension ControllerContext controllerContext
27 if (INSTANCE !== null) {
28 throw new IllegalStateException("Already instantiated");
32 static def getInstance() {
36 override readAllData() {
37 // return broker.readOperationalData("".toInstanceIdentifier.getInstanceIdentifier);
38 throw new UnsupportedOperationException("Reading all data is currently not supported.")
41 override getModules() {
42 throw new UnsupportedOperationException("TODO: auto-generated method stub")
49 override readData(String identifier) {
50 val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier
51 val data = broker.readOperationalData(instanceIdentifierWithSchemaNode.getInstanceIdentifier);
52 return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode)
55 override createConfigurationData(String identifier, CompositeNode payload) {
56 val identifierWithSchemaNode = identifier.resolveInstanceIdentifier
57 val value = resolveNodeNamespaceBySchema(payload, identifierWithSchemaNode.schemaNode)
58 val status = broker.commitConfigurationDataPut(identifierWithSchemaNode.instanceIdentifier,value).get();
59 switch status.result {
60 case TransactionStatus.COMMITED: Response.status(Response.Status.OK).build
61 default: Response.status(Response.Status.INTERNAL_SERVER_ERROR).build
65 override updateConfigurationData(String identifier, CompositeNode payload) {
66 val identifierWithSchemaNode = identifier.resolveInstanceIdentifier
67 val value = resolveNodeNamespaceBySchema(payload, identifierWithSchemaNode.schemaNode)
68 val status = broker.commitConfigurationDataPut(identifierWithSchemaNode.instanceIdentifier,value).get();
69 switch status.result {
70 case TransactionStatus.COMMITED: Response.status(Response.Status.NO_CONTENT).build
71 default: Response.status(Response.Status.INTERNAL_SERVER_ERROR).build
75 override invokeRpc(String identifier, CompositeNode payload) {
76 val rpc = identifier.rpcDefinition
78 throw new ResponseException(Response.Status.NOT_FOUND, "RPC does not exist.");
80 val value = resolveNodeNamespaceBySchema(payload, rpc.input)
81 val List<Node<?>> input = new ArrayList
83 val rpcRequest = NodeFactory.createMutableCompositeNode(rpc.QName, null, input, null, null)
84 val rpcResult = broker.invokeRpc(rpc.QName, rpcRequest);
85 return new StructuredData(rpcResult.result, rpc.output);
88 override readConfigurationData(String identifier) {
89 val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier
90 val data = broker.readConfigurationData(instanceIdentifierWithSchemaNode.getInstanceIdentifier);
91 return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode)
94 override readOperationalData(String identifier) {
95 val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier
96 val data = broker.readOperationalData(instanceIdentifierWithSchemaNode.getInstanceIdentifier);
97 return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode)
100 override updateConfigurationDataLegacy(String identifier, CompositeNode payload) {
101 updateConfigurationData(identifier,payload);
104 override createConfigurationDataLegacy(String identifier, CompositeNode payload) {
105 createConfigurationData(identifier,payload);
108 private def InstanceIdWithSchemaNode resolveInstanceIdentifier(String identifier) {
109 val identifierWithSchemaNode = identifier.toInstanceIdentifier
110 if (identifierWithSchemaNode === null) {
111 throw new ResponseException(Response.Status.BAD_REQUEST, "URI has bad format");
113 return identifierWithSchemaNode
116 private def CompositeNode resolveNodeNamespaceBySchema(CompositeNode node, DataSchemaNode schema) {
117 if (node instanceof CompositeNodeWrapper) {
118 addNamespaceToNodeFromSchemaRecursively(node as CompositeNodeWrapper, schema)
119 return (node as CompositeNodeWrapper).unwrap()
124 private def void addNamespaceToNodeFromSchemaRecursively(NodeWrapper<?> nodeBuilder, DataSchemaNode schema) {
125 if (schema === null) {
126 throw new ResponseException(Response.Status.BAD_REQUEST,
127 "Data has bad format\n" + nodeBuilder.localName + " does not exist in yang schema.");
129 val moduleName = controllerContext.findModuleByNamespace(schema.QName.namespace);
130 if (nodeBuilder.namespace === null || nodeBuilder.namespace == schema.QName.namespace ||
131 nodeBuilder.namespace.path == moduleName) {
132 nodeBuilder.qname = schema.QName
134 throw new ResponseException(Response.Status.BAD_REQUEST,
135 "Data has bad format\nIf data is in XML format then namespace for " + nodeBuilder.localName +
136 " should be " + schema.QName.namespace + ".\n If data is in Json format then module name for " +
137 nodeBuilder.localName + " should be " + moduleName + ".");
139 if (nodeBuilder instanceof CompositeNodeWrapper) {
140 val List<NodeWrapper<?>> children = (nodeBuilder as CompositeNodeWrapper).getValues
141 for (child : children) {
142 addNamespaceToNodeFromSchemaRecursively(child,
143 findFirstSchemaByLocalName(child.localName, (schema as DataNodeContainer).childNodes))
148 private def DataSchemaNode findFirstSchemaByLocalName(String localName, Set<DataSchemaNode> schemas) {
149 for (schema : schemas) {
150 if (schema instanceof ChoiceNode) {
151 for (caze : (schema as ChoiceNode).cases) {
152 val result = findFirstSchemaByLocalName(localName, caze.childNodes)
153 if (result !== null) {
158 return schemas.findFirst[n|n.QName.localName.equals(localName)]