Bug 6291 - Fix bugs of new Restconf 11 implementation
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / restconf / utils / parser / ParserIdentifier.java
index 355b9a31d54b70e8c73592bf7153b85b5a3e2a21..d24c3deb07336c99016c453b3810d1cf44f556d9 100644 (file)
@@ -14,16 +14,24 @@ import java.text.ParseException;
 import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
+import javax.annotation.Nullable;
+import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
+import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
 import org.opendaylight.netconf.md.sal.rest.schema.SchemaExportContext;
 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
+import org.opendaylight.restconf.parser.IdentifierCodec;
 import org.opendaylight.restconf.utils.RestconfConstants;
 import org.opendaylight.restconf.utils.validation.RestconfValidation;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
+import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -35,8 +43,42 @@ public final class ParserIdentifier {
 
     private static final Logger LOG = LoggerFactory.getLogger(ParserIdentifier.class);
 
-    public static InstanceIdentifierContext<?> toInstanceIdentifier(final String identifier) {
-        throw new UnsupportedOperationException("Not yet implemented");
+    private ParserIdentifier() {
+        throw new UnsupportedOperationException("Util class.");
+    }
+
+    /**
+     * Make {@link InstanceIdentifierContext} from identifier.
+     *
+     * @param identifier
+     *            - path identifier
+     * @param schemaContext
+     *            - {@link SchemaContext}
+     * @return {@link InstanceIdentifierContext}
+     */
+    public static InstanceIdentifierContext<?> toInstanceIdentifier(@Nullable final String identifier,
+            final SchemaContext schemaContext) {
+        final YangInstanceIdentifier deserialize;
+        if (identifier != null && identifier.contains(RestconfConstants.MOUNT)) {
+            final String mountPointId = identifier.substring(0, identifier.indexOf("/" + RestconfConstants.MOUNT));
+            deserialize = IdentifierCodec.deserialize(mountPointId, schemaContext);
+        } else {
+            deserialize = IdentifierCodec.deserialize(identifier, schemaContext);
+        }
+        final DataSchemaContextNode<?> child = DataSchemaContextTree.from(schemaContext).getChild(deserialize);
+        return new InstanceIdentifierContext<SchemaNode>(deserialize, child.getDataSchemaNode(), null, schemaContext);
+    }
+
+    /**
+     * Make {@link String} from {@link YangInstanceIdentifier}
+     *
+     * @param instanceIdentifier
+     * @param schemaContext
+     * @return
+     */
+    public static String stringFromYangInstanceIdentifier(final YangInstanceIdentifier instanceIdentifier,
+            final SchemaContext schemaContext) {
+        return IdentifierCodec.serialize(instanceIdentifier, schemaContext);
     }
 
     /**
@@ -47,18 +89,28 @@ public final class ParserIdentifier {
      * @return {@link QName}
      */
     public static QName makeQNameFromIdentifier(final String identifier) {
+        // check if more than one slash is not used as path separator
+        if (identifier.contains(
+                String.valueOf(RestconfConstants.SLASH).concat(String.valueOf(RestconfConstants.SLASH)))) {
+            LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
+            throw new RestconfDocumentedException(
+                    "URI has bad format. End of URI should be in format \'moduleName/yyyy-MM-dd\'", ErrorType.PROTOCOL,
+                    ErrorTag.INVALID_VALUE);
+        }
+
         final int mountIndex = identifier.indexOf(RestconfConstants.MOUNT);
         String moduleNameAndRevision = "";
         if (mountIndex >= 0) {
-            moduleNameAndRevision = identifier.substring(mountIndex + RestconfConstants.MOUNT.length());
+            moduleNameAndRevision = identifier.substring(mountIndex + RestconfConstants.MOUNT.length())
+                    .replaceFirst(String.valueOf(RestconfConstants.SLASH), "");
         } else {
             moduleNameAndRevision = identifier;
         }
 
-        final Splitter splitter = Splitter.on("/").omitEmptyStrings();
+        final Splitter splitter = Splitter.on(RestconfConstants.SLASH);
         final Iterable<String> split = splitter.split(moduleNameAndRevision);
-        final List<String> pathArgs = Lists.<String> newArrayList(split);
-        if (pathArgs.size() < 2) {
+        final List<String> pathArgs = Lists.newArrayList(split);
+        if (pathArgs.size() != 2) {
             LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
             throw new RestconfDocumentedException(
                     "URI has bad format. End of URI should be in format \'moduleName/yyyy-MM-dd\'", ErrorType.PROTOCOL,
@@ -87,10 +139,12 @@ public final class ParserIdentifier {
      *            - {@link SchemaContext}
      * @param identifier
      *            - path parameter
+     * @param domMountPointService
+     *            - {@link DOMMountPointService}
      * @return {@link SchemaExportContext}
      */
     public static SchemaExportContext toSchemaExportContextFromIdentifier(final SchemaContext schemaContext,
-            final String identifier) {
+            final String identifier, final DOMMountPointService domMountPointService) {
         final Iterable<String> pathComponents = RestconfConstants.SLASH_SPLITTER.split(identifier);
         final Iterator<String> componentIter = pathComponents.iterator();
         if (!Iterables.contains(pathComponents, RestconfConstants.MOUNT)) {
@@ -102,16 +156,22 @@ public final class ParserIdentifier {
             final StringBuilder pathBuilder = new StringBuilder();
             while (componentIter.hasNext()) {
                 final String current = componentIter.next();
+
+                if (RestconfConstants.MOUNT.equals(current)) {
+                    pathBuilder.append("/");
+                    pathBuilder.append(RestconfConstants.MOUNT);
+                    break;
+                }
+
                 if (pathBuilder.length() != 0) {
                     pathBuilder.append("/");
                 }
+
                 pathBuilder.append(current);
-                if (RestconfConstants.MOUNT.equals(current)) {
-                    break;
-                }
             }
-            final InstanceIdentifierContext<?> mountPoint = ParserIdentifier
-                    .toInstanceIdentifier(pathBuilder.toString());
+            final InstanceIdentifierContext<?> point = ParserIdentifier
+                    .toInstanceIdentifier(pathBuilder.toString(), schemaContext);
+            final DOMMountPoint mountPoint = domMountPointService.getMountPoint(point.getInstanceIdentifier()).get();
             final String moduleName = RestconfValidation.validateAndGetModulName(componentIter);
             final Date revision = RestconfValidation.validateAndGetRevision(componentIter);
             final Module module = mountPoint.getSchemaContext().findModuleByName(moduleName, revision);