Merge "Resolved namespace from input data"
authorEd Warnicke <eaw@cisco.com>
Fri, 29 Nov 2013 12:18:33 +0000 (12:18 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Fri, 29 Nov 2013 12:18:33 +0000 (12:18 +0000)
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/BrokerFacade.xtend
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/CompositeNodeWrapper.java
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/ControllerContext.xtend
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/NodeWrapper.java
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.xtend
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/SimpleNodeWrapper.java

index 29ad7522c43436891ec6cb717ee6cbc713a04abd..1b0fda41ca723d67f640de2418ebd6e33e153751 100644 (file)
@@ -1,6 +1,5 @@
 package org.opendaylight.controller.sal.restconf.impl
 
-import javax.ws.rs.WebApplicationException
 import javax.ws.rs.core.Response
 import org.opendaylight.controller.md.sal.common.api.data.DataReader
 import org.opendaylight.controller.sal.core.api.Broker.ConsumerSession
index e000c7e29e21c0a39904088351940d6862e3f42d..c741c9aa19cbc1d6c27db7734aa79cb9d39b00de 100644 (file)
@@ -24,6 +24,7 @@ public final class CompositeNodeWrapper implements NodeWrapper<CompositeNode>, C
 
     private String localName;
     private URI namespace;
+    private QName name;
     private List<NodeWrapper<?>> values = new ArrayList<>();
     
     public CompositeNodeWrapper(String localName) {
@@ -34,6 +35,12 @@ public final class CompositeNodeWrapper implements NodeWrapper<CompositeNode>, C
         this(localName);
         this.namespace = namespace;
     }
+    
+    @Override
+    public void setQname(QName name) {
+        Preconditions.checkState(compositeNode == null, "Cannot change the object, due to data inconsistencies.");
+        this.name = name;
+    }
 
     @Override
     public String getLocalName() {
@@ -75,9 +82,11 @@ public final class CompositeNodeWrapper implements NodeWrapper<CompositeNode>, C
     @Override
     public CompositeNode unwrap(CompositeNode parent) {
         if (compositeNode == null) {
-            Preconditions.checkNotNull(namespace);
-            compositeNode = NodeFactory.createMutableCompositeNode(new QName(namespace, localName), 
-                    parent, new ArrayList<Node<?>>(), ModifyAction.CREATE, null);
+            if (name == null) {
+                Preconditions.checkNotNull(namespace);
+                name = new QName(namespace, localName);
+            }
+            compositeNode = NodeFactory.createMutableCompositeNode(name, parent, new ArrayList<Node<?>>(), null, null);
             
             List<Node<?>> nodeValues = new ArrayList<>();
             for (NodeWrapper<?> nodeWrapper : values) {
@@ -88,6 +97,7 @@ public final class CompositeNodeWrapper implements NodeWrapper<CompositeNode>, C
             values = null;
             namespace = null;
             localName = null;
+            name = null;
         }
         return compositeNode;
     }
index 065d01e8e9405058f563189e6f5117d30eed4857..882c73d001071dea11ee8bfb1d253887780a0875 100644 (file)
@@ -128,6 +128,25 @@ class ControllerContext implements SchemaServiceListener {
     private def dispatch CharSequence toRestconfIdentifier(PathArgument argument, DataSchemaNode node) {
         throw new IllegalArgumentException("Conversion of generic path argument is not supported");
     }
+    
+    def findModuleByNamespace(URI namespace) {
+        checkPreconditions
+        var module = uriToModuleName.get(namespace)
+        if (module === null) {
+            val moduleSchemas = schemas.findModuleByNamespace(namespace);
+            if(moduleSchemas === null) throw new IllegalArgumentException()
+            var latestModule = moduleSchemas.head
+            for (m : moduleSchemas) {
+                if (m.revision.after(latestModule.revision)) {
+                    latestModule = m
+                }
+            }
+            if(latestModule === null) throw new IllegalArgumentException()
+            uriToModuleName.put(namespace, latestModule.name)
+            module = latestModule.name;
+        }
+        return module
+    }
 
     def CharSequence toRestconfIdentifier(QName qname) {
         checkPreconditions
index 0a3616e494a22527f3d95d7294daccf733181bd4..db7770fc68797504dc969a89eaddda832aa5c1d4 100644 (file)
@@ -2,11 +2,14 @@ package org.opendaylight.controller.sal.restconf.impl;
 
 import java.net.URI;
 
+import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
 import org.opendaylight.yangtools.yang.data.api.Node;
 
 public interface NodeWrapper<T extends Node<?>> {
 
+    void setQname(QName name);
+    
     T unwrap(CompositeNode parent);
     
     URI getNamespace();
index b6426c65bfdd55c8f984a8433eaddbb9ccb20dbc..b7307544397b06e4bf544a71b0fd59471cfa72fd 100644 (file)
@@ -1,10 +1,12 @@
 package org.opendaylight.controller.sal.restconf.impl
 
 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.rest.api.RestconfService
 import org.opendaylight.yangtools.yang.data.api.CompositeNode
+import org.opendaylight.yangtools.yang.model.api.ChoiceNode
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode
 
@@ -77,7 +79,7 @@ class RestconfImpl implements RestconfService {
     
     override readConfigurationData(String identifier) {
         val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier
-        val data = broker.readOperationalData(instanceIdentifierWithSchemaNode.getInstanceIdentifier);
+        val data = broker.readConfigurationData(instanceIdentifierWithSchemaNode.getInstanceIdentifier);
         return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode)
     }
     
@@ -112,16 +114,43 @@ class RestconfImpl implements RestconfService {
     }
 
     private def void addNamespaceToNodeFromSchemaRecursively(NodeWrapper<?> nodeBuilder, DataSchemaNode schema) {
-        if (nodeBuilder.namespace === null) {
-            nodeBuilder.namespace = schema.QName.namespace
+        if (schema === null) {
+            throw new ResponseException(Response.Status.BAD_REQUEST,
+                "Data has bad format\n" + nodeBuilder.localName + " does not exist in yang schema.");
+        }
+        val moduleName = controllerContext.findModuleByNamespace(schema.QName.namespace);
+        if (nodeBuilder.namespace === null || nodeBuilder.namespace == schema.QName.namespace ||
+            nodeBuilder.namespace.path == moduleName) {
+            nodeBuilder.qname = schema.QName
+        } else {
+            throw new ResponseException(Response.Status.BAD_REQUEST,
+                "Data has bad format\nIf data is in XML format then namespace for " + nodeBuilder.localName +
+                    " should be " + schema.QName.namespace + ".\n If data is in Json format then module name for " +
+                    nodeBuilder.localName + " should be " + moduleName + ".");
         }
         if (nodeBuilder instanceof CompositeNodeWrapper) {
             val List<NodeWrapper<?>> children = (nodeBuilder as CompositeNodeWrapper).getValues
             for (child : children) {
                 addNamespaceToNodeFromSchemaRecursively(child,
-                    (schema as DataNodeContainer).childNodes.findFirst[n|n.QName.localName.equals(child.localName)])
+                    findFirstSchemaByLocalName(child.localName, (schema as DataNodeContainer).childNodes))
+            }
+        }
+    }
+    
+    private def DataSchemaNode findFirstSchemaByLocalName(String localName, Set<DataSchemaNode> schemas) {
+        for (schema : schemas) {
+            if (schema instanceof ChoiceNode) {
+                for (caze : (schema as ChoiceNode).cases) {
+                    val result =  findFirstSchemaByLocalName(localName, caze.childNodes)
+                    if (result !== null) {
+                        return result
+                    }
+                }
+            } else {
+                return schemas.findFirst[n|n.QName.localName.equals(localName)]
             }
         }
+        return null
     }
 
 }
index 4600d0890bcc41f30f6f43ff0907c978156352a3..1b103b43c25219f60a482fcc7b9a459666ece55f 100644 (file)
@@ -18,6 +18,7 @@ public final class SimpleNodeWrapper implements NodeWrapper<SimpleNode<?>>, Simp
     private String localName;
     private String value;
     private URI namespace;
+    private QName name;
 
     public SimpleNodeWrapper(String localName, String value) {
         this.localName = Preconditions.checkNotNull(localName);
@@ -29,6 +30,12 @@ public final class SimpleNodeWrapper implements NodeWrapper<SimpleNode<?>>, Simp
         this.namespace = namespace;
     }
     
+    @Override
+    public void setQname(QName name) {
+        Preconditions.checkState(simpleNode == null, "Cannot change the object, due to data inconsistencies.");
+        this.name = name;
+    }
+    
     @Override
     public String getLocalName() {
         if (simpleNode != null) {
@@ -54,12 +61,16 @@ public final class SimpleNodeWrapper implements NodeWrapper<SimpleNode<?>>, Simp
     @Override
     public SimpleNode<String> unwrap(CompositeNode parent) {
         if (simpleNode == null) {
-            Preconditions.checkNotNull(namespace);
-            simpleNode = NodeFactory.createImmutableSimpleNode(new QName(namespace, localName), parent, value);
+            if (name == null) {
+                Preconditions.checkNotNull(namespace);
+                name = new QName(namespace, localName);
+            }
+            simpleNode = NodeFactory.createImmutableSimpleNode(name, parent, value);
             
             value = null;
             namespace = null;
             localName = null;
+            name = null;
         }
         return simpleNode;
     }