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
15 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec
16 import org.opendaylight.yangtools.yang.model.api.TypeDefinition
17 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode
18 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode
19 import org.opendaylight.yangtools.yang.common.QName
20 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode
21 import static javax.ws.rs.core.Response.Status.*
23 class RestconfImpl implements RestconfService {
25 val static RestconfImpl INSTANCE = new RestconfImpl
31 extension ControllerContext controllerContext
34 if (INSTANCE !== null) {
35 throw new IllegalStateException("Already instantiated");
39 static def getInstance() {
43 override readAllData() {
44 // return broker.readOperationalData("".toInstanceIdentifier.getInstanceIdentifier);
45 throw new UnsupportedOperationException("Reading all data is currently not supported.")
48 override getModules() {
49 throw new UnsupportedOperationException("TODO: auto-generated method stub")
56 override readData(String identifier) {
57 val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier
58 val data = broker.readOperationalData(instanceIdentifierWithSchemaNode.getInstanceIdentifier);
59 return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode)
62 override createConfigurationData(String identifier, CompositeNode payload) {
63 val identifierWithSchemaNode = identifier.resolveInstanceIdentifier
64 val value = normalizeNode(payload, identifierWithSchemaNode.schemaNode)
65 val status = broker.commitConfigurationDataPut(identifierWithSchemaNode.instanceIdentifier, value).get();
66 switch status.result {
67 case TransactionStatus.COMMITED: Response.status(NO_CONTENT).build
68 default: Response.status(INTERNAL_SERVER_ERROR).build
72 override updateConfigurationData(String identifier, CompositeNode payload) {
73 val identifierWithSchemaNode = identifier.resolveInstanceIdentifier
74 val value = normalizeNode(payload, identifierWithSchemaNode.schemaNode)
75 val status = broker.commitConfigurationDataPut(identifierWithSchemaNode.instanceIdentifier, value).get();
76 switch status.result {
77 case TransactionStatus.COMMITED: Response.status(OK).build
78 default: Response.status(INTERNAL_SERVER_ERROR).build
82 override invokeRpc(String identifier, CompositeNode payload) {
83 val rpc = identifier.rpcDefinition
85 throw new ResponseException(NOT_FOUND, "RPC does not exist.");
87 val value = normalizeNode(payload, rpc.input)
88 val List<Node<?>> input = new ArrayList
90 val rpcRequest = NodeFactory.createMutableCompositeNode(rpc.QName, null, input, null, null)
91 val rpcResult = broker.invokeRpc(rpc.QName, rpcRequest);
92 if (!rpcResult.successful) {
93 throw new ResponseException(INTERNAL_SERVER_ERROR, "Operation failed")
95 if (rpcResult.result === null) {
98 return new StructuredData(rpcResult.result, rpc.output)
101 override readConfigurationData(String identifier) {
102 val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier
103 val data = broker.readConfigurationData(instanceIdentifierWithSchemaNode.getInstanceIdentifier);
104 return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode)
107 override readOperationalData(String identifier) {
108 val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier
109 val data = broker.readOperationalData(instanceIdentifierWithSchemaNode.getInstanceIdentifier);
110 return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode)
113 override updateConfigurationDataLegacy(String identifier, CompositeNode payload) {
114 updateConfigurationData(identifier, payload);
117 override createConfigurationDataLegacy(String identifier, CompositeNode payload) {
118 createConfigurationData(identifier, payload);
121 private def InstanceIdWithSchemaNode resolveInstanceIdentifier(String identifier) {
122 val identifierWithSchemaNode = identifier.toInstanceIdentifier
123 if (identifierWithSchemaNode === null) {
124 throw new ResponseException(BAD_REQUEST, "URI has bad format");
126 return identifierWithSchemaNode
129 private def CompositeNode normalizeNode(CompositeNode node, DataSchemaNode schema) {
130 if (node instanceof CompositeNodeWrapper) {
131 normalizeNode(node as CompositeNodeWrapper, schema, null)
132 return (node as CompositeNodeWrapper).unwrap()
137 private def void normalizeNode(NodeWrapper<?> nodeBuilder, DataSchemaNode schema, QName previousAugment) {
138 if (schema === null) {
139 throw new ResponseException(BAD_REQUEST,
140 "Data has bad format\n" + nodeBuilder.localName + " does not exist in yang schema.");
142 var validQName = schema.QName
143 var currentAugment = previousAugment;
144 if (schema.augmenting) {
145 currentAugment = schema.QName
146 } else if (previousAugment !== null && schema.QName.namespace !== previousAugment.namespace) {
147 validQName = QName.create(currentAugment, schema.QName.localName);
149 val moduleName = controllerContext.findModuleByNamespace(validQName.namespace);
150 if (nodeBuilder.namespace === null || nodeBuilder.namespace == validQName.namespace ||
151 nodeBuilder.namespace.path == moduleName) {
152 nodeBuilder.qname = validQName
154 throw new ResponseException(BAD_REQUEST,
155 "Data has bad format\nIf data is in XML format then namespace for " + nodeBuilder.localName +
156 " should be " + schema.QName.namespace + ".\n If data is in Json format then module name for " +
157 nodeBuilder.localName + " should be " + moduleName + ".");
160 if (nodeBuilder instanceof CompositeNodeWrapper) {
161 val List<NodeWrapper<?>> children = (nodeBuilder as CompositeNodeWrapper).getValues
162 for (child : children) {
164 findFirstSchemaByLocalName(child.localName, (schema as DataNodeContainer).childNodes),
167 } else if (nodeBuilder instanceof SimpleNodeWrapper) {
168 val simpleNode = (nodeBuilder as SimpleNodeWrapper)
169 val stringValue = simpleNode.value as String;
171 val objectValue = TypeDefinitionAwareCodec.from(schema.typeDefinition)?.deserialize(stringValue);
172 simpleNode.setValue(objectValue)
173 } else if (nodeBuilder instanceof EmptyNodeWrapper) {
174 val emptyNodeBuilder = nodeBuilder as EmptyNodeWrapper
175 if (schema instanceof LeafSchemaNode) {
176 emptyNodeBuilder.setComposite(false);
177 } else if (schema instanceof ContainerSchemaNode) {
179 // FIXME: Add presence check
180 emptyNodeBuilder.setComposite(true);
185 private def dispatch TypeDefinition<?> typeDefinition(LeafSchemaNode node) {
189 private def dispatch TypeDefinition<?> typeDefinition(LeafListSchemaNode node) {
193 private def DataSchemaNode findFirstSchemaByLocalName(String localName, Set<DataSchemaNode> schemas) {
194 for (schema : schemas) {
195 if (schema instanceof ChoiceNode) {
196 for (caze : (schema as ChoiceNode).cases) {
197 val result = findFirstSchemaByLocalName(localName, caze.childNodes)
198 if (result !== null) {
203 val result = schemas.findFirst[n|n.QName.localName.equals(localName)]
204 if (result !== null) {