Fix unique argument parser 32/75232/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 16 Aug 2018 12:30:11 +0000 (14:30 +0200)
committerRobert Varga <nite@hq.sk>
Thu, 16 Aug 2018 13:45:08 +0000 (13:45 +0000)
Unique argument can use horizontal tabs and line-breaks as separators
in addition to spaces. Deal with them instead of rejecting input.

Change-Id: I5fb6137b9b73c5c844a05cc59fb9261868834a24
JIRA: YANGTOOLS-893
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 68de00c780737c1844f7d045d6f1ecfd0e83f957)

yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/unique/UniqueStatementSupport.java
yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT893Test.java [new file with mode: 0644]
yang/yang-parser-rfc7950/src/test/resources/bugs/YT893/cr.yang [new file with mode: 0644]
yang/yang-parser-rfc7950/src/test/resources/bugs/YT893/crlf.yang [new file with mode: 0644]
yang/yang-parser-rfc7950/src/test/resources/bugs/YT893/ht.yang [new file with mode: 0644]
yang/yang-parser-rfc7950/src/test/resources/bugs/YT893/lf.yang [new file with mode: 0644]

index 1ecada93c60bf5123703e36684e20c525fccd811..1030e4a39aca7c3a5c469319536d5086dcc0a892 100644 (file)
@@ -7,11 +7,13 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.unique;
 
+import com.google.common.base.CharMatcher;
 import com.google.common.base.Splitter;
 import com.google.common.collect.ImmutableSet;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.Set;
+import java.util.regex.Pattern;
 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
@@ -23,10 +25,16 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
-public final class UniqueStatementSupport
-        extends AbstractStatementSupport<Collection<SchemaNodeIdentifier.Relative>, UniqueStatement,
-                EffectiveStatement<Collection<SchemaNodeIdentifier.Relative>, UniqueStatement>> {
-    private static final Splitter SPACE_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
+public final class UniqueStatementSupport extends AbstractStatementSupport<Collection<Relative>, UniqueStatement,
+        EffectiveStatement<Collection<Relative>, UniqueStatement>> {
+    /**
+     * Support 'sep' ABNF rule in RFC7950 section 14. CRLF pattern is used to squash line-break from CRLF to LF form
+     * and then we use SEP_SPLITTER, which can operate on single characters.
+     */
+    private static final Pattern CRLF_PATTERN = Pattern.compile("\r\n", Pattern.LITERAL);
+    private static final Splitter SEP_SPLITTER = Splitter.on(CharMatcher.anyOf(" \t\n").precomputed())
+            .omitEmptyStrings();
+
     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
         YangStmtMapping.UNIQUE)
         .build();
@@ -41,8 +49,7 @@ public final class UniqueStatementSupport
     }
 
     @Override
-    public Collection<SchemaNodeIdentifier.Relative> parseArgumentValue(final StmtContext<?, ?, ?> ctx,
-            final String value) {
+    public Collection<Relative> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
         final Collection<Relative> uniqueConstraints = parseUniqueConstraintArgument(ctx, value);
         SourceException.throwIf(uniqueConstraints.isEmpty(), ctx.getStatementSourceReference(),
                 "Invalid argument value '%s' of unique statement. The value must contains at least "
@@ -57,8 +64,8 @@ public final class UniqueStatementSupport
 
     @Override
     public EffectiveStatement<Collection<Relative>, UniqueStatement> createEffective(
-            final StmtContext<Collection<Relative>, UniqueStatement,
-            EffectiveStatement<Collection<Relative>, UniqueStatement>> ctx) {
+            final StmtContext<Collection<Relative>, UniqueStatement, EffectiveStatement<Collection<Relative>,
+            UniqueStatement>> ctx) {
         return new UniqueEffectiveStatementImpl(ctx);
     }
 
@@ -67,15 +74,18 @@ public final class UniqueStatementSupport
         return SUBSTATEMENT_VALIDATOR;
     }
 
-    private static Collection<SchemaNodeIdentifier.Relative> parseUniqueConstraintArgument(
-            final StmtContext<?, ?, ?> ctx, final String argumentValue) {
-        final Set<SchemaNodeIdentifier.Relative> uniqueConstraintNodes = new HashSet<>();
-        for (final String uniqueArgToken : SPACE_SPLITTER.split(argumentValue)) {
+    private static Collection<Relative> parseUniqueConstraintArgument(final StmtContext<?, ?, ?> ctx,
+            final String argumentValue) {
+        // deal with 'line-break' rule, which is either "\n" or "\r\n", but not "\r"
+        final String nocrlf = CRLF_PATTERN.matcher(argumentValue).replaceAll("\n");
+
+        final Set<Relative> uniqueConstraintNodes = new HashSet<>();
+        for (final String uniqueArgToken : SEP_SPLITTER.split(nocrlf)) {
             final SchemaNodeIdentifier nodeIdentifier = ArgumentUtils.nodeIdentifierFromPath(ctx, uniqueArgToken);
             SourceException.throwIf(nodeIdentifier.isAbsolute(), ctx.getStatementSourceReference(),
                     "Unique statement argument '%s' contains schema node identifier '%s' "
                             + "which is not in the descendant node identifier form.", argumentValue, uniqueArgToken);
-            uniqueConstraintNodes.add((SchemaNodeIdentifier.Relative) nodeIdentifier);
+            uniqueConstraintNodes.add((Relative) nodeIdentifier);
         }
         return ImmutableSet.copyOf(uniqueConstraintNodes);
     }
diff --git a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT893Test.java b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT893Test.java
new file mode 100644 (file)
index 0000000..4c40428
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.stmt;
+
+import static org.junit.Assert.assertNotNull;
+
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
+
+public class YT893Test {
+    @Test(expected = SomeModifiersUnresolvedException.class)
+    public void testCR() throws Exception {
+        StmtTestUtils.parseYangSource("/bugs/YT893/cr.yang");
+    }
+
+    @Test
+    public void testCRLF() throws Exception {
+        assertNotNull(StmtTestUtils.parseYangSource("/bugs/YT893/crlf.yang"));
+    }
+
+    @Test
+    public void testHTAB() throws Exception {
+        assertNotNull(StmtTestUtils.parseYangSource("/bugs/YT893/ht.yang"));
+    }
+
+    @Test
+    public void testLF() throws Exception {
+        assertNotNull(StmtTestUtils.parseYangSource("/bugs/YT893/lf.yang"));
+    }
+}
diff --git a/yang/yang-parser-rfc7950/src/test/resources/bugs/YT893/cr.yang b/yang/yang-parser-rfc7950/src/test/resources/bugs/YT893/cr.yang
new file mode 100644 (file)
index 0000000..ed724ac
--- /dev/null
@@ -0,0 +1,15 @@
+module cr {
+    namespace "urn:cr";
+    prefix cr;
+
+    list foo {
+        unique "a\rb";
+
+        leaf a {
+            type string;
+        }
+        leaf b {
+            type string;
+        }
+    }
+}
diff --git a/yang/yang-parser-rfc7950/src/test/resources/bugs/YT893/crlf.yang b/yang/yang-parser-rfc7950/src/test/resources/bugs/YT893/crlf.yang
new file mode 100644 (file)
index 0000000..063f54a
--- /dev/null
@@ -0,0 +1,16 @@
+module crlf {
+    namespace "urn:crlf";
+    prefix crlf;
+
+    list foo {
+        unique "a\r
+                b";
+
+        leaf a {
+            type string;
+        }
+        leaf b {
+            type string;
+        }
+    }
+}
diff --git a/yang/yang-parser-rfc7950/src/test/resources/bugs/YT893/ht.yang b/yang/yang-parser-rfc7950/src/test/resources/bugs/YT893/ht.yang
new file mode 100644 (file)
index 0000000..8e035fe
--- /dev/null
@@ -0,0 +1,15 @@
+module ht {
+    namespace "urn:ht";
+    prefix ht;
+
+    list foo {
+        unique "a      b";
+
+        leaf a {
+            type string;
+        }
+        leaf b {
+            type string;
+        }
+    }
+}
diff --git a/yang/yang-parser-rfc7950/src/test/resources/bugs/YT893/lf.yang b/yang/yang-parser-rfc7950/src/test/resources/bugs/YT893/lf.yang
new file mode 100644 (file)
index 0000000..b1d559f
--- /dev/null
@@ -0,0 +1,16 @@
+module lf {
+    namespace "urn:lf";
+    prefix lf;
+
+    list foo {
+        unique "a
+                b";
+
+        leaf a {
+            type string;
+        }
+        leaf b {
+            type string;
+        }
+    }
+}