Do not compile augment/refine/deviate paths 64/27864/6
authorRobert Varga <rovarga@cisco.com>
Sat, 3 Oct 2015 01:41:35 +0000 (03:41 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Sat, 3 Oct 2015 13:03:50 +0000 (13:03 +0000)
These paths are not full XPaths, but rather SchemaNodeIdentifier paths.
Do not attempt to compile them, as we validate them by splitting and
look up of individual components.

Change-Id: If472ff6a2c795ce810794ad64dc643bf10a24199
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/AugmentStatementImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/AugmentUtils.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/DeviationStatementImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/RefineStatementImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/Utils.java

index 9e39f9009f4657b1bb91db333d90bc3c7bd15bff..12e1a06bb184d16c9b2d1fb7c984b40ab76142ae 100644 (file)
@@ -7,11 +7,9 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 
-import org.opendaylight.yangtools.yang.parser.spi.source.StmtOrderingNamespace;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
+import com.google.common.base.Preconditions;
 import java.util.Collection;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import java.util.regex.Pattern;
 import javax.annotation.Nonnull;
 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
@@ -24,24 +22,24 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
+import org.opendaylight.yangtools.yang.parser.spi.source.StmtOrderingNamespace;
 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.AugmentEffectiveStatementImpl;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-public class AugmentStatementImpl extends
-        AbstractDeclaredStatement<SchemaNodeIdentifier> implements
-        AugmentStatement {
-
-    private static final Logger LOG = LoggerFactory
-            .getLogger(AugmentStatementImpl.class);
+public class AugmentStatementImpl extends AbstractDeclaredStatement<SchemaNodeIdentifier> implements AugmentStatement {
+    private static final Logger LOG = LoggerFactory.getLogger(AugmentStatementImpl.class);
+    private static final Pattern PATH_REL_PATTERN1 = Pattern.compile("\\.\\.?\\s*/(.+)");
+    private static final Pattern PATH_REL_PATTERN2 = Pattern.compile("//.*");
 
-    protected AugmentStatementImpl(
-            StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> context) {
+    protected AugmentStatementImpl(final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> context) {
         super(context);
     }
 
-    public static class Definition
-            extends
+    public static class Definition extends
             AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> {
 
         public Definition() {
@@ -49,22 +47,23 @@ public class AugmentStatementImpl extends
         }
 
         @Override
-        public SchemaNodeIdentifier parseArgumentValue(
-                StmtContext<?, ?, ?> ctx, String value) throws SourceException {
-            return SchemaNodeIdentifier.create(
-                    AugmentUtils.parseAugmentPath(ctx, value),
-                    Utils.isXPathAbsolute(ctx, value));
+        public SchemaNodeIdentifier parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
+            Preconditions.checkArgument(!PATH_REL_PATTERN1.matcher(value).matches()
+                && !PATH_REL_PATTERN2.matcher(value).matches(),
+                "An argument for augment can be only absolute path; or descendant if used in uses");
+
+            return Utils.nodeIdentifierFromPath(ctx, value);
         }
 
         @Override
         public AugmentStatement createDeclared(
-                StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
+                final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
             return new AugmentStatementImpl(ctx);
         }
 
         @Override
         public EffectiveStatement<SchemaNodeIdentifier, AugmentStatement> createEffective(
-                StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
+                final StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
             return new AugmentEffectiveStatementImpl(ctx);
         }
 
index dd14bf87d90860cd2ea415a833ad71e8c254064d..3a068971d52c8a2b02a068f922d9a2355fb8a03b 100644 (file)
@@ -18,7 +18,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Set;
-import java.util.regex.Pattern;
 import javax.annotation.Nullable;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
@@ -46,21 +45,11 @@ import org.slf4j.LoggerFactory;
 public final class AugmentUtils {
 
     private static final Logger LOG = LoggerFactory.getLogger(AugmentUtils.class);
-    private static final Pattern PATH_REL_PATTERN1 = Pattern.compile("\\.\\.?\\s*/(.+)");
-    private static final Pattern PATH_REL_PATTERN2 = Pattern.compile("//.*");
 
     private AugmentUtils() {
         throw new UnsupportedOperationException();
     }
 
-    public static Iterable<QName> parseAugmentPath(final StmtContext<?, ?, ?> ctx, final String path) {
-        Preconditions.checkArgument(!PATH_REL_PATTERN1.matcher(path).matches()
-            && !PATH_REL_PATTERN2.matcher(path).matches(),
-            "An argument for augment can be only absolute path; or descendant if used in uses");
-
-        return Utils.parseXPath(ctx, path);
-    }
-
     public static void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceCtx,
             final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
 
index 8ba3ef03acfcb179b9e6b9d7fb615a089ed971e5..cc87db3b7fdaeb33d19ecf7fb47576706a878581 100644 (file)
@@ -7,8 +7,7 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeviationEffectiveStatementImpl;
-
+import javax.annotation.Nonnull;
 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.DeviationStatement;
@@ -16,13 +15,11 @@ import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
-import javax.annotation.Nonnull;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeviationEffectiveStatementImpl;
 
 public class DeviationStatementImpl extends AbstractDeclaredStatement<SchemaNodeIdentifier> implements DeviationStatement {
 
-    protected DeviationStatementImpl(
-            StmtContext<SchemaNodeIdentifier, DeviationStatement, ?> context) {
+    protected DeviationStatementImpl(final StmtContext<SchemaNodeIdentifier, DeviationStatement, ?> context) {
         super(context);
     }
 
@@ -32,18 +29,19 @@ public class DeviationStatementImpl extends AbstractDeclaredStatement<SchemaNode
             super(Rfc6020Mapping.DEVIATION);
         }
 
-        @Override public SchemaNodeIdentifier parseArgumentValue(
-                StmtContext<?, ?, ?> ctx, String value) throws SourceException {
-            return SchemaNodeIdentifier.create(Utils.parseXPath(ctx, value), Utils.isXPathAbsolute(ctx, value));
+        @Override
+        public SchemaNodeIdentifier parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
+            return Utils.nodeIdentifierFromPath(ctx, value);
         }
 
-        @Override public DeviationStatement createDeclared(
-                StmtContext<SchemaNodeIdentifier, DeviationStatement, ?> ctx) {
+        @Override
+        public DeviationStatement createDeclared(final StmtContext<SchemaNodeIdentifier, DeviationStatement, ?> ctx) {
             return new DeviationStatementImpl(ctx);
         }
 
-        @Override public EffectiveStatement<SchemaNodeIdentifier, DeviationStatement> createEffective(
-                StmtContext<SchemaNodeIdentifier, DeviationStatement, EffectiveStatement<SchemaNodeIdentifier, DeviationStatement>> ctx) {
+        @Override
+        public EffectiveStatement<SchemaNodeIdentifier, DeviationStatement> createEffective(
+                final StmtContext<SchemaNodeIdentifier, DeviationStatement, EffectiveStatement<SchemaNodeIdentifier, DeviationStatement>> ctx) {
             return new DeviationEffectiveStatementImpl(ctx);
         }
     }
index 35a79cab62d01f3277642cfabf5db14e90ecc1af..897b65f60a4cbbfc8233f26d0aac02713acc268e 100644 (file)
@@ -8,8 +8,7 @@
 
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.RefineEffectiveStatementImpl;
-
+import javax.annotation.Nullable;
 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionStatement;
@@ -19,13 +18,11 @@ import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
-import javax.annotation.Nullable;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.RefineEffectiveStatementImpl;
 
 public class RefineStatementImpl extends AbstractDeclaredStatement<SchemaNodeIdentifier> implements RefineStatement {
 
-    protected RefineStatementImpl(
-            StmtContext<SchemaNodeIdentifier, RefineStatement, ?> context) {
+    protected RefineStatementImpl(final StmtContext<SchemaNodeIdentifier, RefineStatement, ?> context) {
         super(context);
     }
 
@@ -37,17 +34,18 @@ public class RefineStatementImpl extends AbstractDeclaredStatement<SchemaNodeIde
         }
 
         @Override
-        public SchemaNodeIdentifier parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) {
-            return SchemaNodeIdentifier.create(Utils.parseXPath(ctx, value), Utils.isXPathAbsolute(ctx, value));
+        public SchemaNodeIdentifier parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
+            return Utils.nodeIdentifierFromPath(ctx, value);
         }
 
         @Override
-        public RefineStatement createDeclared(StmtContext<SchemaNodeIdentifier, RefineStatement, ?> ctx) {
+        public RefineStatement createDeclared(final StmtContext<SchemaNodeIdentifier, RefineStatement, ?> ctx) {
             return new RefineStatementImpl(ctx);
         }
 
         @Override
-        public EffectiveStatement<SchemaNodeIdentifier, RefineStatement> createEffective(StmtContext<SchemaNodeIdentifier, RefineStatement, EffectiveStatement<SchemaNodeIdentifier, RefineStatement>> ctx) {
+        public EffectiveStatement<SchemaNodeIdentifier, RefineStatement> createEffective(
+                final StmtContext<SchemaNodeIdentifier, RefineStatement, EffectiveStatement<SchemaNodeIdentifier, RefineStatement>> ctx) {
             return new RefineEffectiveStatementImpl(ctx);
         }
     }
index 5e56fecc7edf4efdc5fab31d23b386712af7d618..e4c43e0c30e2c935857dad5b55a5e0f95e4496aa 100644 (file)
@@ -115,10 +115,6 @@ public final class Utils {
         return keyNodes;
     }
 
-    public static List<String> splitPathToNodeNames(final String path) {
-        return SLASH_SPLITTER.splitToList(path);
-    }
-
     private static void compileXPath(final StmtContext<?, ?, ?> ctx, final String path) {
         final XPath xPath = XPATH_FACTORY.get().newXPath();
 
@@ -185,25 +181,20 @@ public final class Utils {
         return false;
     }
 
-    public static Iterable<QName> parseXPath(final StmtContext<?, ?, ?> ctx, final String path) {
-
-        String trimmedPath = trimSingleLastSlashFromXPath(path);
-
-        compileXPath(ctx, trimmedPath);
-
-        List<String> nodeNames = splitPathToNodeNames(trimmedPath);
-        List<QName> qNames = new ArrayList<>(nodeNames.size());
-
-        for (String nodeName : nodeNames) {
+    static SchemaNodeIdentifier nodeIdentifierFromPath(final StmtContext<?, ?, ?> ctx, final String path) {
+        // FIXME: is the path trimming really necessary??
+        final List<QName> qNames = new ArrayList<>();
+        for (String nodeName : SLASH_SPLITTER.split(trimSingleLastSlashFromXPath(path))) {
             try {
                 final QName qName = Utils.qNameFromArgument(ctx, nodeName);
                 qNames.add(qName);
             } catch (Exception e) {
-                throw new IllegalArgumentException(e);
+                throw new IllegalArgumentException(
+                    String.format("Failed to parse node '%s' in path '%s'", nodeName, path), e);
             }
         }
 
-        return qNames;
+        return SchemaNodeIdentifier.create(qNames, PATH_ABS.matcher(path).matches());
     }
 
     public static String stringFromStringContext(final YangStatementParser.ArgumentContext context) {