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 891784a29903aa7e3f27fc64548c9ae94f0a6f84..d24c3deb07336c99016c453b3810d1cf44f556d9 100644 (file)
@@ -14,6 +14,7 @@ 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;
@@ -42,6 +43,10 @@ public final class ParserIdentifier {
 
     private static final Logger LOG = LoggerFactory.getLogger(ParserIdentifier.class);
 
+    private ParserIdentifier() {
+        throw new UnsupportedOperationException("Util class.");
+    }
+
     /**
      * Make {@link InstanceIdentifierContext} from identifier.
      *
@@ -51,10 +56,10 @@ public final class ParserIdentifier {
      *            - {@link SchemaContext}
      * @return {@link InstanceIdentifierContext}
      */
-    public static InstanceIdentifierContext<?> toInstanceIdentifier(final String identifier,
+    public static InstanceIdentifierContext<?> toInstanceIdentifier(@Nullable final String identifier,
             final SchemaContext schemaContext) {
         final YangInstanceIdentifier deserialize;
-        if (identifier.contains(RestconfConstants.MOUNT)) {
+        if (identifier != null && identifier.contains(RestconfConstants.MOUNT)) {
             final String mountPointId = identifier.substring(0, identifier.indexOf("/" + RestconfConstants.MOUNT));
             deserialize = IdentifierCodec.deserialize(mountPointId, schemaContext);
         } else {
@@ -64,6 +69,18 @@ public final class ParserIdentifier {
         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);
+    }
+
     /**
      * Make a {@link QName} from identifier
      *
@@ -139,11 +156,18 @@ public final class ParserIdentifier {
             final StringBuilder pathBuilder = new StringBuilder();
             while (componentIter.hasNext()) {
                 final String current = componentIter.next();
-                pathBuilder.append("/");
-                pathBuilder.append(current);
+
                 if (RestconfConstants.MOUNT.equals(current)) {
+                    pathBuilder.append("/");
+                    pathBuilder.append(RestconfConstants.MOUNT);
                     break;
                 }
+
+                if (pathBuilder.length() != 0) {
+                    pathBuilder.append("/");
+                }
+
+                pathBuilder.append(current);
             }
             final InstanceIdentifierContext<?> point = ParserIdentifier
                     .toInstanceIdentifier(pathBuilder.toString(), schemaContext);