BUG 2412 - restconf @POST invokeRpc with nopayload method
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / impl / ControllerContext.java
index 5f6604c68de3692343668266cadd4fcdb1e5a956..5566f01674a3112e2f0dd09c8287b17a9e0752b4 100644 (file)
@@ -43,6 +43,7 @@ import org.opendaylight.yangtools.concepts.Codec;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
@@ -251,24 +252,34 @@ public class ControllerContext implements SchemaContextListener {
         return node;
     }
 
-    public String toFullRestconfIdentifier(final YangInstanceIdentifier path) {
+    public String toFullRestconfIdentifier(final YangInstanceIdentifier path, final DOMMountPoint mount) {
         checkPreconditions();
 
         final Iterable<PathArgument> elements = path.getPathArguments();
         final StringBuilder builder = new StringBuilder();
         final PathArgument head = elements.iterator().next();
         final QName startQName = head.getNodeType();
-        final Module initialModule = globalSchema.findModuleByNamespaceAndRevision(startQName.getNamespace(),
+        final SchemaContext schemaContext;
+        if (mount != null) {
+            schemaContext = mount.getSchemaContext();
+        } else {
+            schemaContext = globalSchema;
+        }
+        final Module initialModule = schemaContext.findModuleByNamespaceAndRevision(startQName.getNamespace(),
                 startQName.getRevision());
         DataNodeContainer node = initialModule;
         for (final PathArgument element : elements) {
-            final QName _nodeType = element.getNodeType();
-            final DataSchemaNode potentialNode = ControllerContext.childByQName(node, _nodeType);
-            if (!ControllerContext.isListOrContainer(potentialNode)) {
-                return null;
+            if (!(element instanceof AugmentationIdentifier)) {
+                final QName _nodeType = element.getNodeType();
+                final DataSchemaNode potentialNode = ControllerContext.childByQName(node, _nodeType);
+                if (!(element instanceof NodeIdentifier && potentialNode instanceof ListSchemaNode)) {
+                    if (!ControllerContext.isListOrContainer(potentialNode)) {
+                        return null;
+                    }
+                    builder.append(convertToRestconfIdentifier(element, (DataNodeContainer) potentialNode, mount));
+                    node = (DataNodeContainer) potentialNode;
+                }
             }
-            node = ((DataNodeContainer) potentialNode);
-            builder.append(this.convertToRestconfIdentifier(element, node));
         }
 
         return builder.toString();
@@ -313,6 +324,18 @@ public class ControllerContext implements SchemaContextListener {
         return schema == null ? null : schema.getName() + ':' + qname.getLocalName();
     }
 
+    public CharSequence toRestconfIdentifier(final QName qname, final DOMMountPoint mount) {
+        final SchemaContext schema;
+        if (mount != null) {
+            schema = mount.getSchemaContext();
+        } else {
+            checkPreconditions();
+            schema = globalSchema;
+        }
+
+        return toRestconfIdentifier(schema, qname);
+    }
+
     public CharSequence toRestconfIdentifier(final QName qname) {
         checkPreconditions();
 
@@ -462,8 +485,9 @@ public class ControllerContext implements SchemaContextListener {
         return ret;
     }
 
-    private String toUriString(final Object object) throws UnsupportedEncodingException {
-        return object == null ? "" : URLEncoder.encode(object.toString(), ControllerContext.URI_ENCODING_CHAR_SET);
+    private String toUriString(final Object object, final LeafSchemaNode leafNode, final DOMMountPoint mount) throws UnsupportedEncodingException {
+        final Codec<Object, Object> codec = RestCodec.from(leafNode.getType(), mount);
+        return object == null ? "" : URLEncoder.encode(codec.serialize(object).toString(), ControllerContext.URI_ENCODING_CHAR_SET);
     }
 
     private InstanceIdentifierContext collectPathArguments(final InstanceIdentifierBuilder builder,
@@ -563,6 +587,15 @@ public class ControllerContext implements SchemaContextListener {
             }
 
             targetNode = findInstanceDataChildByNameAndNamespace(parentNode, nodeName, module.getNamespace());
+
+            if (targetNode == null && parentNode instanceof Module) {
+                final RpcDefinition rpc = ControllerContext.getInstance().getRpcDefinition(head);
+                if (rpc != null) {
+                    return new InstanceIdentifierContext<RpcDefinition>(builder.build(), rpc, mountPoint,
+                            mountPoint != null ? mountPoint.getSchemaContext() : globalSchema);
+                }
+            }
+
             if (targetNode == null) {
                 throw new RestconfDocumentedException("URI has bad format. Possible reasons:\n" + " 1. \"" + head
                         + "\" was not found in parent data node.\n" + " 2. \"" + head
@@ -823,11 +856,11 @@ public class ControllerContext implements SchemaContextListener {
         return null;
     }
 
-    private CharSequence convertToRestconfIdentifier(final PathArgument argument, final DataNodeContainer node) {
+    private CharSequence convertToRestconfIdentifier(final PathArgument argument, final DataNodeContainer node, final DOMMountPoint mount) {
         if (argument instanceof NodeIdentifier && node instanceof ContainerSchemaNode) {
             return convertToRestconfIdentifier((NodeIdentifier) argument, (ContainerSchemaNode) node);
         } else if (argument instanceof NodeIdentifierWithPredicates && node instanceof ListSchemaNode) {
-            return convertToRestconfIdentifier((NodeIdentifierWithPredicates) argument, (ListSchemaNode) node);
+            return convertToRestconfIdentifier(argument, node, mount);
         } else if (argument != null && node != null) {
             throw new IllegalArgumentException("Conversion of generic path argument is not supported");
         } else {
@@ -841,9 +874,9 @@ public class ControllerContext implements SchemaContextListener {
     }
 
     private CharSequence convertToRestconfIdentifier(final NodeIdentifierWithPredicates argument,
-            final ListSchemaNode node) {
+            final ListSchemaNode node, final DOMMountPoint mount) {
         final QName nodeType = argument.getNodeType();
-        final CharSequence nodeIdentifier = this.toRestconfIdentifier(nodeType);
+        final CharSequence nodeIdentifier = this.toRestconfIdentifier(nodeType, mount);
         final Map<QName, Object> keyValues = argument.getKeyValues();
 
         final StringBuilder builder = new StringBuilder();
@@ -854,17 +887,23 @@ public class ControllerContext implements SchemaContextListener {
         final List<QName> keyDefinition = node.getKeyDefinition();
         boolean hasElements = false;
         for (final QName key : keyDefinition) {
-            if (!hasElements) {
-                hasElements = true;
-            } else {
-                builder.append('/');
-            }
+            for (final DataSchemaNode listChild : node.getChildNodes()) {
+                if (listChild.getQName().equals(key)) {
+                    if (!hasElements) {
+                        hasElements = true;
+                    } else {
+                        builder.append('/');
+                    }
 
-            try {
-                builder.append(toUriString(keyValues.get(key)));
-            } catch (final UnsupportedEncodingException e) {
-                LOG.error("Error parsing URI: {}", keyValues.get(key), e);
-                return null;
+                    try {
+                        Preconditions.checkState(listChild instanceof LeafSchemaNode, "List key has to consist of leaves");
+                        builder.append(toUriString(keyValues.get(key), (LeafSchemaNode)listChild, mount));
+                    } catch (final UnsupportedEncodingException e) {
+                        LOG.error("Error parsing URI: {}", keyValues.get(key), e);
+                        return null;
+                    }
+                    break;
+                }
             }
         }