Changed mount point URI decoding in restconf
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / impl / RestconfImpl.xtend
index a65c0ff97aa73324083a0826186462d59c7006c6..5ad6f1eea88d1ec33fc1cc67c155c601d7f30880 100644 (file)
@@ -1,13 +1,17 @@
 package org.opendaylight.controller.sal.restconf.impl
 
+import com.google.common.base.Preconditions
+import java.net.URI
 import java.util.ArrayList
 import java.util.HashMap
 import java.util.List
 import java.util.Set
 import javax.ws.rs.core.Response
 import org.opendaylight.controller.md.sal.common.api.TransactionStatus
+import org.opendaylight.controller.sal.core.api.mount.MountInstance
 import org.opendaylight.controller.sal.rest.api.RestconfService
 import org.opendaylight.yangtools.yang.common.QName
+import org.opendaylight.yangtools.yang.common.RpcResult
 import org.opendaylight.yangtools.yang.data.api.CompositeNode
 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.InstanceIdentifierBuilder
@@ -21,11 +25,11 @@ import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode
 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode
 import org.opendaylight.yangtools.yang.model.api.Module
+import org.opendaylight.yangtools.yang.model.api.RpcDefinition
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition
 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition
 
 import static javax.ws.rs.core.Response.Status.*
-import org.opendaylight.yangtools.yang.model.api.RpcDefinition
 
 class RestconfImpl implements RestconfService {
 
@@ -92,21 +96,36 @@ class RestconfImpl implements RestconfService {
     }
 
     override readData(String identifier) {
-        val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier
-        val data = broker.readOperationalData(instanceIdentifierWithSchemaNode.getInstanceIdentifier);
-        return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode)
+        val iiWithData = identifier.toInstanceIdentifier
+        var CompositeNode data = null;
+        if (iiWithData.mountPoint !== null) {
+            data = broker.readOperationalDataBehindMountPoint(iiWithData.mountPoint, iiWithData.instanceIdentifier)
+        } else {
+            data = broker.readOperationalData(iiWithData.getInstanceIdentifier);
+        }
+        return new StructuredData(data, iiWithData.schemaNode)
     }
 
     override readConfigurationData(String identifier) {
-        val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier
-        val data = broker.readConfigurationData(instanceIdentifierWithSchemaNode.getInstanceIdentifier);
-        return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode)
+        val iiWithData = identifier.toInstanceIdentifier
+        var CompositeNode data = null;
+        if (iiWithData.mountPoint !== null) {
+            data = broker.readConfigurationDataBehindMountPoint(iiWithData.mountPoint, iiWithData.getInstanceIdentifier)
+        } else {
+            data = broker.readConfigurationData(iiWithData.getInstanceIdentifier);
+        }
+        return new StructuredData(data, iiWithData.schemaNode)
     }
 
     override readOperationalData(String identifier) {
-        val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier
-        val data = broker.readOperationalData(instanceIdentifierWithSchemaNode.getInstanceIdentifier);
-        return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode)
+        val iiWithData = identifier.toInstanceIdentifier
+        var CompositeNode data = null;
+        if (iiWithData.mountPoint !== null) {
+            data = broker.readOperationalDataBehindMountPoint(iiWithData.mountPoint, iiWithData.getInstanceIdentifier)
+        } else {
+            data = broker.readOperationalData(iiWithData.getInstanceIdentifier);
+        }
+        return new StructuredData(data, iiWithData.schemaNode)
     }
 
     override updateConfigurationDataLegacy(String identifier, CompositeNode payload) {
@@ -114,9 +133,15 @@ class RestconfImpl implements RestconfService {
     }
 
     override updateConfigurationData(String identifier, CompositeNode payload) {
-        val identifierWithSchemaNode = identifier.resolveInstanceIdentifier
-        val value = normalizeNode(payload, identifierWithSchemaNode.schemaNode, identifierWithSchemaNode.mountPoint)
-        val status = broker.commitConfigurationDataPut(identifierWithSchemaNode.instanceIdentifier, value).get();
+        val iiWithData = identifier.toInstanceIdentifier
+        val value = normalizeNode(payload, iiWithData.schemaNode, iiWithData.mountPoint)
+        var RpcResult<TransactionStatus> status = null
+        if (iiWithData.mountPoint !== null) {
+            status = broker.commitConfigurationDataPutBehindMountPoint(iiWithData.mountPoint,
+                iiWithData.instanceIdentifier, value).get()
+        } else {
+            status = broker.commitConfigurationDataPut(iiWithData.instanceIdentifier, value).get();
+        }
         switch status.result {
             case TransactionStatus.COMMITED: Response.status(OK).build
             default: Response.status(INTERNAL_SERVER_ERROR).build
@@ -128,14 +153,21 @@ class RestconfImpl implements RestconfService {
     }
 
     override createConfigurationData(String identifier, CompositeNode payload) {
-        val uncompleteIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier
-        var schemaNode = (uncompleteIdentifierWithSchemaNode.schemaNode as DataNodeContainer).getSchemaChildNode(payload)
-        if (schemaNode === null) {
-            schemaNode = payload.findModule(uncompleteIdentifierWithSchemaNode.instanceIdentifier)?.getSchemaChildNode(payload)
-        }
-        val value = normalizeNode(payload, schemaNode, uncompleteIdentifierWithSchemaNode.instanceIdentifier)
-        val completeIdentifierWithSchemaNode = uncompleteIdentifierWithSchemaNode.addLastIdentifierFromData(value, schemaNode)
-        val status = broker.commitConfigurationDataPost(completeIdentifierWithSchemaNode.instanceIdentifier, value)?.get();
+        if (payload.namespace === null) {
+            throw new ResponseException(BAD_REQUEST,
+                "Data has bad format. Root element node must have namespace (XML format) or module name(JSON format)");
+        }
+        val uncompleteInstIdWithData = identifier.toInstanceIdentifier
+        val schemaNode = uncompleteInstIdWithData.mountPoint.findModule(payload)?.getSchemaChildNode(payload)
+        val value = normalizeNode(payload, schemaNode, uncompleteInstIdWithData.mountPoint)
+        val completeInstIdWithData = uncompleteInstIdWithData.addLastIdentifierFromData(value, schemaNode)
+        var RpcResult<TransactionStatus> status = null
+        if (completeInstIdWithData.mountPoint !== null) {
+            status = broker.commitConfigurationDataPostBehindMountPoint(completeInstIdWithData.mountPoint,
+                completeInstIdWithData.instanceIdentifier, value)?.get();
+        } else {
+            status = broker.commitConfigurationDataPost(completeInstIdWithData.instanceIdentifier, value)?.get();
+        }
         if (status === null) {
             return Response.status(ACCEPTED).build
         }
@@ -146,10 +178,20 @@ class RestconfImpl implements RestconfService {
     }
     
     override createConfigurationData(CompositeNode payload) {
-        val schemaNode = payload.findModule(null)?.getSchemaChildNode(payload)
+        if (payload.namespace === null) {
+            throw new ResponseException(BAD_REQUEST,
+                "Data has bad format. Root element node must have namespace (XML format) or module name(JSON format)");
+        }
+        val schemaNode = findModule(null, payload)?.getSchemaChildNode(payload)
         val value = normalizeNode(payload, schemaNode, null)
-        val identifierWithSchemaNode = addLastIdentifierFromData(null, value, schemaNode)
-        val status = broker.commitConfigurationDataPost(identifierWithSchemaNode.instanceIdentifier, value)?.get();
+        val iiWithData = addLastIdentifierFromData(null, value, schemaNode)
+        var RpcResult<TransactionStatus> status = null
+        if (iiWithData.mountPoint !== null) {
+            status = broker.commitConfigurationDataPostBehindMountPoint(iiWithData.mountPoint,
+                iiWithData.instanceIdentifier, value)?.get();
+        } else {
+            status = broker.commitConfigurationDataPost(iiWithData.instanceIdentifier, value)?.get();
+        }
         if (status === null) {
             return Response.status(ACCEPTED).build
         }
@@ -160,36 +202,43 @@ class RestconfImpl implements RestconfService {
     }
     
     override deleteConfigurationData(String identifier) {
-        val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier
-        val status = broker.commitConfigurationDataDelete(instanceIdentifierWithSchemaNode.getInstanceIdentifier).get;
+        val iiWithData = identifier.toInstanceIdentifier
+        var RpcResult<TransactionStatus> status = null
+        if (iiWithData.mountPoint !== null) {
+            status = broker.commitConfigurationDataDeleteBehindMountPoint(iiWithData.mountPoint,
+                iiWithData.getInstanceIdentifier).get;
+        } else {
+            status = broker.commitConfigurationDataDelete(iiWithData.getInstanceIdentifier).get;
+        }
         switch status.result {
             case TransactionStatus.COMMITED: Response.status(OK).build
             default: Response.status(INTERNAL_SERVER_ERROR).build
         }
     }
-
-    private def InstanceIdWithSchemaNode resolveInstanceIdentifier(String identifier) {
-        val identifierWithSchemaNode = identifier.toInstanceIdentifier
-        if (identifierWithSchemaNode === null) {
-            throw new ResponseException(BAD_REQUEST, "URI has bad format");
-        }
-        return identifierWithSchemaNode
+    
+    private def dispatch URI namespace(CompositeNode data) {
+        return data.nodeType.namespace
+    }
+    
+    private def dispatch URI namespace(CompositeNodeWrapper data) {
+        return data.namespace
     }
 
-    private def dispatch Module findModule(CompositeNode data, InstanceIdentifier partialPath) {
-        if (partialPath !== null && !partialPath.path.empty) {
-            return data.nodeType.namespace.findModuleByNamespace(partialPath)
+    private def dispatch Module findModule(MountInstance mountPoint, CompositeNode data) {
+        if (mountPoint !== null) {
+            return mountPoint.findModuleByNamespace(data.nodeType.namespace)
         } else {
-            return data.nodeType.namespace.findModuleByNamespace
+            return findModuleByNamespace(data.nodeType.namespace)
         }
     }
 
-    private def dispatch Module findModule(CompositeNodeWrapper data, InstanceIdentifier partialPath) {
+    private def dispatch Module findModule(MountInstance mountPoint, CompositeNodeWrapper data) {
+        Preconditions.checkNotNull(data.namespace)
         var Module module = null;
-        if (partialPath !== null && !partialPath.path.empty) {
-            module = data.namespace.findModuleByNamespace(partialPath) // namespace from XML
+        if (mountPoint !== null) {
+            module = mountPoint.findModuleByNamespace(data.namespace) // namespace from XML
             if (module === null) {
-                module = data.namespace.toString.findModuleByName(partialPath) // namespace (module name) from JSON
+                module = mountPoint.findModuleByName(data.namespace.toString) // namespace (module name) from JSON
             }
         } else {
             module = data.namespace.findModuleByNamespace // namespace from XML
@@ -208,10 +257,11 @@ class RestconfImpl implements RestconfService {
         return parentSchemaNode?.getDataChildByName(data.localName)
     }
 
-    private def InstanceIdWithSchemaNode addLastIdentifierFromData(InstanceIdWithSchemaNode identifierWithSchemaNode, CompositeNode data, DataSchemaNode schemaOfData) {
+    private def InstanceIdWithSchemaNode addLastIdentifierFromData(InstanceIdWithSchemaNode identifierWithSchemaNode,
+        CompositeNode data, DataSchemaNode schemaOfData) {
         val iiOriginal = identifierWithSchemaNode?.instanceIdentifier
-        var  InstanceIdentifierBuilder iiBuilder = null
-        if (iiOriginal === null) { 
+        var InstanceIdentifierBuilder iiBuilder = null
+        if (iiOriginal === null) {
             iiBuilder = InstanceIdentifier.builder
         } else {
             iiBuilder = InstanceIdentifier.builder(iiOriginal)
@@ -230,15 +280,20 @@ class RestconfImpl implements RestconfService {
         for (key : listNode.keyDefinition) {
             val dataNodeKeyValueObject = dataNode.getSimpleNodesByName(key.localName)?.head?.value
             if (dataNodeKeyValueObject === null) {
-                throw new ResponseException(BAD_REQUEST, "Data contains list \"" + dataNode.nodeType.localName + "\" which does not contain key: \"" + key.localName + "\"")
+                throw new ResponseException(BAD_REQUEST,
+                    "Data contains list \"" + dataNode.nodeType.localName + "\" which does not contain key: \"" +
+                        key.localName + "\"")
             }
             keyValues.put(key, dataNodeKeyValueObject);
         }
         return keyValues
     }
 
-    private def CompositeNode normalizeNode(CompositeNode node, DataSchemaNode schema, InstanceIdentifier mountPoint) {
-        if (schema !== null && !schema.containerOrList) {
+    private def CompositeNode normalizeNode(CompositeNode node, DataSchemaNode schema, MountInstance mountPoint) {
+        if (schema === null) {
+            throw new ResponseException(INTERNAL_SERVER_ERROR, "Data schema node was not found for " + node?.nodeType?.localName)
+        }
+        if (!(schema instanceof DataNodeContainer)) {
             throw new ResponseException(BAD_REQUEST, "Root element has to be container or list yang datatype.");
         }
         if (node instanceof CompositeNodeWrapper) {
@@ -250,12 +305,8 @@ class RestconfImpl implements RestconfService {
         return node
     }
 
-    private def isContainerOrList(DataSchemaNode schemaNode) {
-        return (schemaNode instanceof ContainerSchemaNode) || (schemaNode instanceof ListSchemaNode)
-    }
-
     private def void normalizeNode(NodeWrapper<?> nodeBuilder, DataSchemaNode schema, QName previousAugment,
-        InstanceIdentifier mountPoint) {
+        MountInstance mountPoint) {
         if (schema === null) {
             throw new ResponseException(BAD_REQUEST,
                 "Data has bad format.\n\"" + nodeBuilder.localName + "\" does not exist in yang schema.");
@@ -267,9 +318,11 @@ class RestconfImpl implements RestconfService {
         } else if (previousAugment !== null && schema.QName.namespace !== previousAugment.namespace) {
             validQName = QName.create(currentAugment, schema.QName.localName);
         }
-        var moduleName = controllerContext.findModuleNameByNamespace(validQName.namespace);
-        if (moduleName === null && mountPoint !== null && !mountPoint.path.empty) {
-            moduleName = controllerContext.findModuleByNamespace(validQName.namespace, mountPoint)?.name
+        var String moduleName = null;
+        if (mountPoint === null) {
+            moduleName = controllerContext.findModuleNameByNamespace(validQName.namespace);
+        } else {
+            moduleName = mountPoint.findModuleByNamespace(validQName.namespace)?.name
         }
         if (nodeBuilder.namespace === null || nodeBuilder.namespace == validQName.namespace ||
             nodeBuilder.namespace.toString == moduleName) {