1 package org.opendaylight.controller.sal.restconf.impl
4 import javax.ws.rs.core.Response
5 import org.opendaylight.controller.sal.rest.api.RestconfService
6 import org.opendaylight.yangtools.yang.data.api.CompositeNode
7 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer
8 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode
9 import org.opendaylight.controller.md.sal.common.api.TransactionStatus
11 class RestconfImpl implements RestconfService {
13 val static RestconfImpl INSTANCE = new RestconfImpl
19 extension ControllerContext controllerContext
22 if (INSTANCE != null) {
23 throw new IllegalStateException("Already instantiated");
27 static def getInstance() {
31 override readAllData() {
32 // return broker.readOperationalData("".toInstanceIdentifier.getInstanceIdentifier);
33 throw new UnsupportedOperationException("Reading all data is currently not supported.")
36 override getModules() {
37 throw new UnsupportedOperationException("TODO: auto-generated method stub")
44 override readData(String identifier) {
45 val instanceIdentifierWithSchemaNode = identifier.toInstanceIdentifier
46 val data = broker.readOperationalData(instanceIdentifierWithSchemaNode.getInstanceIdentifier);
47 return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode)
50 override createConfigurationData(String identifier, CompositeNode payload) {
51 val identifierWithSchemaNode = identifier.toInstanceIdentifier
52 val value = resolveNodeNamespaceBySchema(payload, identifierWithSchemaNode.schemaNode)
53 val status = broker.commitConfigurationDataPut(identifierWithSchemaNode.instanceIdentifier,value).get();
54 switch status.result {
55 case TransactionStatus.COMMITED: Response.status(Response.Status.OK).build
56 default: Response.status(Response.Status.INTERNAL_SERVER_ERROR).build
60 override updateConfigurationData(String identifier, CompositeNode payload) {
61 val identifierWithSchemaNode = identifier.toInstanceIdentifier
62 val value = resolveNodeNamespaceBySchema(payload, identifierWithSchemaNode.schemaNode)
63 val status = broker.commitConfigurationDataPut(identifierWithSchemaNode.instanceIdentifier,value).get();
64 switch status.result {
65 case TransactionStatus.COMMITED: Response.status(Response.Status.NO_CONTENT).build
66 default: Response.status(Response.Status.INTERNAL_SERVER_ERROR).build
70 override invokeRpc(String identifier, CompositeNode payload) {
71 val rpc = identifier.toQName;
72 val value = resolveNodeNamespaceBySchema(payload, controllerContext.getRpcInputSchema(rpc))
73 val rpcResult = broker.invokeRpc(rpc, value);
74 val schema = controllerContext.getRpcOutputSchema(rpc);
75 return new StructuredData(rpcResult.result, schema);
78 override readConfigurationData(String identifier) {
79 val instanceIdentifierWithSchemaNode = identifier.toInstanceIdentifier
80 val data = broker.readOperationalData(instanceIdentifierWithSchemaNode.getInstanceIdentifier);
81 return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode)
84 override readOperationalData(String identifier) {
85 val instanceIdentifierWithSchemaNode = identifier.toInstanceIdentifier
86 val data = broker.readOperationalData(instanceIdentifierWithSchemaNode.getInstanceIdentifier);
87 return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode)
90 override updateConfigurationDataLegacy(String identifier, CompositeNode payload) {
91 updateConfigurationData(identifier,payload);
94 override createConfigurationDataLegacy(String identifier, CompositeNode payload) {
95 createConfigurationData(identifier,payload);
98 override createOperationalData(String identifier, CompositeNode payload) {
99 val identifierWithSchemaNode = identifier.toInstanceIdentifier
100 val value = resolveNodeNamespaceBySchema(payload, identifierWithSchemaNode.schemaNode)
101 val status = broker.commitOperationalDataPut(identifierWithSchemaNode.instanceIdentifier,value).get();
102 switch status.result {
103 case TransactionStatus.COMMITED: Response.status(Response.Status.OK).build
104 default: Response.status(Response.Status.INTERNAL_SERVER_ERROR).build
108 override updateOperationalData(String identifier, CompositeNode payload) {
109 val identifierWithSchemaNode = identifier.toInstanceIdentifier
110 val value = resolveNodeNamespaceBySchema(payload, identifierWithSchemaNode.schemaNode)
111 val status = broker.commitOperationalDataPut(identifierWithSchemaNode.instanceIdentifier,value).get();
112 switch status.result {
113 case TransactionStatus.COMMITED: Response.status(Response.Status.NO_CONTENT).build
114 default: Response.status(Response.Status.INTERNAL_SERVER_ERROR).build
118 private def CompositeNode resolveNodeNamespaceBySchema(CompositeNode node, DataSchemaNode schema) {
119 if (node instanceof CompositeNodeWrapper) {
120 addNamespaceToNodeFromSchemaRecursively(node as CompositeNodeWrapper, schema)
121 return (node as CompositeNodeWrapper).unwrap(null)
126 private def void addNamespaceToNodeFromSchemaRecursively(NodeWrapper<?> nodeBuilder, DataSchemaNode schema) {
127 if (nodeBuilder.namespace == null) {
128 nodeBuilder.namespace = schema.QName.namespace
130 if (nodeBuilder instanceof CompositeNodeWrapper) {
131 val List<NodeWrapper<?>> children = (nodeBuilder as CompositeNodeWrapper).getValues
132 for (child : children) {
133 addNamespaceToNodeFromSchemaRecursively(child,
134 (schema as DataNodeContainer).childNodes.findFirst[n|n.QName.localName.equals(child.localName)])