RPC input is wrapped into RPC name element 31/3331/3
authormsunal <msunal@cisco.com>
Mon, 2 Dec 2013 11:31:00 +0000 (12:31 +0100)
committermsunal <msunal@cisco.com>
Mon, 2 Dec 2013 16:06:26 +0000 (17:06 +0100)
- input RPC section is wrapped into CompositeNode with RPC name due to MD-SAL

Change-Id: I7a49f2c5c93e53e5985606230d5d2c5b2a884ce4
Signed-off-by: Martin Sunal <msunal@cisco.com>
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
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromJsonToCompositeNodeTest.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromXmlToCompositeNodeTest.java

index c741c9aa19cbc1d6c27db7734aa79cb9d39b00de..fc146256e8ed7d2a0168944f3d16170fbd85bd07 100644 (file)
@@ -26,11 +26,11 @@ public final class CompositeNodeWrapper implements NodeWrapper<CompositeNode>, C
     private URI namespace;
     private QName name;
     private List<NodeWrapper<?>> values = new ArrayList<>();
-    
+
     public CompositeNodeWrapper(String localName) {
         this.localName = Preconditions.checkNotNull(localName);
     }
-    
+
     public CompositeNodeWrapper(URI namespace, String localName) {
         this(localName);
         this.namespace = namespace;
@@ -80,19 +80,18 @@ public final class CompositeNodeWrapper implements NodeWrapper<CompositeNode>, C
     }
 
     @Override
-    public CompositeNode unwrap(CompositeNode parent) {
+    public CompositeNode unwrap() {
         if (compositeNode == 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) {
-                nodeValues.add(nodeWrapper.unwrap(compositeNode));
+                nodeValues.add(nodeWrapper.unwrap());
             }
-            compositeNode.setValue(nodeValues);
+            compositeNode = NodeFactory.createMutableCompositeNode(name, null, nodeValues, null, null);
             
             values = null;
             namespace = null;
@@ -104,132 +103,132 @@ public final class CompositeNodeWrapper implements NodeWrapper<CompositeNode>, C
 
     @Override
     public QName getNodeType() {
-        return unwrap(null).getNodeType();
+        return unwrap().getNodeType();
     }
 
     @Override
     public CompositeNode getParent() {
-        return unwrap(null).getParent();
+        return unwrap().getParent();
     }
 
     @Override
     public List<Node<?>> getValue() {
-        return unwrap(null).getValue();
+        return unwrap().getValue();
     }
 
     @Override
     public ModifyAction getModificationAction() {
-        return unwrap(null).getModificationAction();
+        return unwrap().getModificationAction();
     }
 
     @Override
     public List<Node<?>> getChildren() {
-        return unwrap(null).getChildren();
+        return unwrap().getChildren();
     }
 
     @Override
     public List<CompositeNode> getCompositesByName(QName children) {
-        return unwrap(null).getCompositesByName(children);
+        return unwrap().getCompositesByName(children);
     }
 
     @Override
     public List<CompositeNode> getCompositesByName(String children) {
-        return unwrap(null).getCompositesByName(children);
+        return unwrap().getCompositesByName(children);
     }
 
     @Override
     public List<SimpleNode<?>> getSimpleNodesByName(QName children) {
-        return unwrap(null).getSimpleNodesByName(children);
+        return unwrap().getSimpleNodesByName(children);
     }
 
     @Override
     public List<SimpleNode<?>> getSimpleNodesByName(String children) {
-        return unwrap(null).getSimpleNodesByName(children);
+        return unwrap().getSimpleNodesByName(children);
     }
 
     @Override
     public CompositeNode getFirstCompositeByName(QName container) {
-        return unwrap(null).getFirstCompositeByName(container);
+        return unwrap().getFirstCompositeByName(container);
     }
 
     @Override
     public SimpleNode<?> getFirstSimpleByName(QName leaf) {
-        return unwrap(null).getFirstSimpleByName(leaf);
+        return unwrap().getFirstSimpleByName(leaf);
     }
 
     @Override
     public MutableCompositeNode asMutable() {
-        return unwrap(null).asMutable();
+        return unwrap().asMutable();
     }
 
     @Override
     public QName getKey() {
-        return unwrap(null).getKey();
+        return unwrap().getKey();
     }
 
     @Override
     public List<Node<?>> setValue(List<Node<?>> value) {
-        return unwrap(null).setValue(value);
+        return unwrap().setValue(value);
     }
 
     @Override
     public int size() {
-        return unwrap(null).size();
+        return unwrap().size();
     }
 
     @Override
     public boolean isEmpty() {
-        return unwrap(null).isEmpty();
+        return unwrap().isEmpty();
     }
 
     @Override
     public boolean containsKey(Object key) {
-        return unwrap(null).containsKey(key);
+        return unwrap().containsKey(key);
     }
 
     @Override
     public boolean containsValue(Object value) {
-        return unwrap(null).containsValue(value);
+        return unwrap().containsValue(value);
     }
 
     @Override
     public List<Node<?>> get(Object key) {
-        return unwrap(null).get(key);
+        return unwrap().get(key);
     }
 
     @Override
     public List<Node<?>> put(QName key, List<Node<?>> value) {
-        return unwrap(null).put(key, value);
+        return unwrap().put(key, value);
     }
 
     @Override
     public List<Node<?>> remove(Object key) {
-        return unwrap(null).remove(key);
+        return unwrap().remove(key);
     }
 
     @Override
     public void putAll(Map<? extends QName, ? extends List<Node<?>>> m) {
-        unwrap(null).putAll(m);
+        unwrap().putAll(m);
     }
 
     @Override
     public void clear() {
-        unwrap(null).clear();
+        unwrap().clear();
     }
 
     @Override
     public Set<QName> keySet() {
-        return unwrap(null).keySet();
+        return unwrap().keySet();
     }
 
     @Override
     public Collection<List<Node<?>>> values() {
-        return unwrap(null).values();
+        return unwrap().values();
     }
 
     @Override
     public Set<java.util.Map.Entry<QName, List<Node<?>>>> entrySet() {
-        return unwrap(null).entrySet();
+        return unwrap().entrySet();
     }
 
 }
index 882c73d001071dea11ee8bfb1d253887780a0875..eca4bd257298b78706cbd10321e8484ea06cab87 100644 (file)
@@ -36,8 +36,7 @@ class ControllerContext implements SchemaServiceListener {
 
     val static NULL_VALUE = "null"
 
-    @Property
-    SchemaContext schemas;
+    var SchemaContext schemas;
 
     private val BiMap<URI, String> uriToModuleName = HashBiMap.create();
     private val Map<String, URI> moduleNameToUri = uriToModuleName.inverse();
@@ -60,6 +59,10 @@ class ControllerContext implements SchemaServiceListener {
         }
     }
 
+    def setSchemas(SchemaContext schemas) {
+        onGlobalContextUpdated(schemas)
+    }
+
     public def InstanceIdWithSchemaNode toInstanceIdentifier(String restconfInstance) {
         val ret = InstanceIdentifier.builder();
         val pathArgs = restconfInstance.split("/");
@@ -73,7 +76,7 @@ class ControllerContext implements SchemaServiceListener {
         if (schemaNode === null) {
             return null
         }
-        new InstanceIdWithSchemaNode(ret.toInstance, schemaNode)
+        return new InstanceIdWithSchemaNode(ret.toInstance, schemaNode)
     }
 
     private def findModule(String restconfInstance) {
@@ -85,13 +88,13 @@ class ControllerContext implements SchemaServiceListener {
         }
         val modulWithFirstYangStatement = pathArgs.filter[s|s.contains(":")].head
         val startModule = modulWithFirstYangStatement.toModuleName();
-        schemas.getLatestModule(startModule)
+        return getLatestModule(startModule)
     }
 
-    private def getLatestModule(SchemaContext schema, String moduleName) {
-        checkNotNull(schema)
+    private def getLatestModule(String moduleName) {
+        checkPreconditions
         checkArgument(moduleName !== null && !moduleName.empty)
-        val modules = schema.modules.filter[m|m.name == moduleName]
+        val modules = schemas.modules.filter[m|m.name == moduleName]
         var latestModule = modules.head
         for (module : modules) {
             if (module.revision.after(latestModule.revision)) {
@@ -308,13 +311,17 @@ class ControllerContext implements SchemaServiceListener {
             return str;
         }
     }
-
-    public def QName toQName(String name) {
+    
+    private def QName toQName(String name) {
         val module = name.toModuleName;
         val node = name.toNodeName;
         val namespace = moduleNameToUri.get(module);
         return new QName(namespace,null,node);
     }
+    
+    def getRpcDefinition(String name) {
+        return qnameToRpc.get(name.toQName)
+    }
 
     override onGlobalContextUpdated(SchemaContext context) {
         this.schemas = context;
@@ -324,12 +331,4 @@ class ControllerContext implements SchemaServiceListener {
         }
     }
     
-    def ContainerSchemaNode getRpcOutputSchema(QName name) {
-        qnameToRpc.get(name)?.output;
-    }
-    
-    def ContainerSchemaNode getRpcInputSchema(QName name) {
-        qnameToRpc.get(name)?.input;
-    }
-
 }
index db7770fc68797504dc969a89eaddda832aa5c1d4..675e1194396147334509e35d5a6b4ebb21e40b35 100644 (file)
@@ -3,14 +3,13 @@ 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);
+    T unwrap();
     
     URI getNamespace();
 
index b7307544397b06e4bf544a71b0fd59471cfa72fd..a6a71fef55e6d3055901406734be06e05aca6c6d 100644 (file)
@@ -1,11 +1,14 @@
 package org.opendaylight.controller.sal.restconf.impl
 
+import java.util.ArrayList
 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.data.api.Node
+import org.opendaylight.yangtools.yang.data.impl.NodeFactory
 import org.opendaylight.yangtools.yang.model.api.ChoiceNode
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode
@@ -70,11 +73,16 @@ class RestconfImpl implements RestconfService {
     }
 
     override invokeRpc(String identifier, CompositeNode payload) {
-        val rpc = identifier.toQName;
-        val value = resolveNodeNamespaceBySchema(payload, controllerContext.getRpcInputSchema(rpc))
-        val rpcResult = broker.invokeRpc(rpc, value);
-        val schema = controllerContext.getRpcOutputSchema(rpc);
-        return new StructuredData(rpcResult.result, schema);
+        val rpc = identifier.rpcDefinition
+        if (rpc === null) {
+            throw new ResponseException(Response.Status.NOT_FOUND, "RPC does not exist.");
+        }
+        val value = resolveNodeNamespaceBySchema(payload, rpc.input)
+        val List<Node<?>> input = new ArrayList
+        input.add(value)
+        val rpcRequest = NodeFactory.createMutableCompositeNode(rpc.QName, null, input, null, null)
+        val rpcResult = broker.invokeRpc(rpc.QName, rpcRequest);
+        return new StructuredData(rpcResult.result, rpc.output);
     }
     
     override readConfigurationData(String identifier) {
@@ -108,7 +116,7 @@ class RestconfImpl implements RestconfService {
     private def CompositeNode resolveNodeNamespaceBySchema(CompositeNode node, DataSchemaNode schema) {
         if (node instanceof CompositeNodeWrapper) {
             addNamespaceToNodeFromSchemaRecursively(node as CompositeNodeWrapper, schema)
-            return (node as CompositeNodeWrapper).unwrap(null)
+            return (node as CompositeNodeWrapper).unwrap()
         }
         return node
     }
index 1b103b43c25219f60a482fcc7b9a459666ece55f..c1576895fd70bc8fc5c23a706c1fe26501af4b03 100644 (file)
@@ -59,13 +59,13 @@ public final class SimpleNodeWrapper implements NodeWrapper<SimpleNode<?>>, Simp
     }
 
     @Override
-    public SimpleNode<String> unwrap(CompositeNode parent) {
+    public SimpleNode<String> unwrap() {
         if (simpleNode == null) {
             if (name == null) {
                 Preconditions.checkNotNull(namespace);
                 name = new QName(namespace, localName);
             }
-            simpleNode = NodeFactory.createImmutableSimpleNode(name, parent, value);
+            simpleNode = NodeFactory.createImmutableSimpleNode(name, null, value);
             
             value = null;
             namespace = null;
@@ -77,37 +77,37 @@ public final class SimpleNodeWrapper implements NodeWrapper<SimpleNode<?>>, Simp
 
     @Override
     public QName getNodeType() {
-        return unwrap(null).getNodeType();
+        return unwrap().getNodeType();
     }
 
     @Override
     public CompositeNode getParent() {
-        return unwrap(null).getParent();
+        return unwrap().getParent();
     }
 
     @Override
     public String getValue() {
-        return unwrap(null).getValue();
+        return unwrap().getValue();
     }
 
     @Override
     public ModifyAction getModificationAction() {
-        return unwrap(null).getModificationAction();
+        return unwrap().getModificationAction();
     }
 
     @Override
     public MutableSimpleNode<String> asMutable() {
-        return unwrap(null).asMutable();
+        return unwrap().asMutable();
     }
 
     @Override
     public QName getKey() {
-        return unwrap(null).getKey();
+        return unwrap().getKey();
     }
 
     @Override
     public String setValue(String value) {
-        return unwrap(null).setValue(value);
+        return unwrap().setValue(value);
     }
 
 
index 2e8b07151914b12c9b0c2a360b1c9b075c0a880a..f55e92f9c9afbfefb1707d0f96ca4f071d9b1d03 100644 (file)
@@ -3,25 +3,24 @@ package org.opendaylight.controller.sal.restconf.impl.test;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
-import static org.mockito.Matchers.any;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
 
-import java.io.*;
-import java.net.*;
-import java.util.*;
-import java.util.concurrent.*;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URISyntaxException;
+import java.util.List;
 
 import javax.ws.rs.WebApplicationException;
 
-import org.junit.*;
+import org.junit.Test;
 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
-import org.opendaylight.controller.sal.restconf.impl.*;
-import org.opendaylight.yangtools.yang.data.api.*;
-import org.slf4j.*;
-import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
-import org.opendaylight.yangtools.yang.common.RpcResult;
-import org.opendaylight.yangtools.yang.model.api.*;
+import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
+import org.opendaylight.yangtools.yang.data.api.CompositeNode;
+import org.opendaylight.yangtools.yang.data.api.Node;
+import org.opendaylight.yangtools.yang.data.api.SimpleNode;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import com.google.gson.JsonSyntaxException;
 
@@ -203,7 +202,7 @@ public class FromJsonToCompositeNodeTest {
         TestUtils.supplementNamespace(dataSchemaNode1, compositeNode);
 
         assertTrue(compositeNode instanceof CompositeNodeWrapper);
-        CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap(null);
+        CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap();
 
         assertEquals("lst", compNode.getNodeType().getLocalName());
         verifyCompositeNode(compNode, "simple:list:yang1");
@@ -232,7 +231,7 @@ public class FromJsonToCompositeNodeTest {
         TestUtils.supplementNamespace(dataSchemaNode, compositeNode);
 
         assertTrue(compositeNode instanceof CompositeNodeWrapper);
-        CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap(null);
+        CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap();
 
         assertEquals(topLevelElementName, compNode.getNodeType().getLocalName());
         verifyCompositeNode(compNode, namespace);
@@ -334,7 +333,7 @@ public class FromJsonToCompositeNodeTest {
             if (dummyNamespaces) {
                 try {
                     TestUtils.addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode);
-                    return ((CompositeNodeWrapper) compositeNode).unwrap(null);
+                    return ((CompositeNodeWrapper) compositeNode).unwrap();
                 } catch (URISyntaxException e) {
                     LOG.error(e.getMessage());
                     assertTrue(e.getMessage(), false);
index ef122dd8d7f68cba55088831d9ebe639c869b646..5aa6d0339db6264e8e1c38157d118fd7dc2e8121 100644 (file)
@@ -262,7 +262,7 @@ public class FromXmlToCompositeNodeTest {
             if (dummyNamespaces) {
                 try {
                     TestUtils.addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode);
-                    return ((CompositeNodeWrapper) compositeNode).unwrap(null);
+                    return ((CompositeNodeWrapper) compositeNode).unwrap();
                 } catch (URISyntaxException e) {
                     LOG.error(e.getMessage());
                     assertTrue(e.getMessage(), false);