Prepare RevisionAwareXPath for XPath integration 01/81201/3
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 26 Mar 2019 17:07:10 +0000 (18:07 +0100)
committerRobert Varga <nite@hq.sk>
Tue, 26 Mar 2019 19:53:12 +0000 (19:53 +0000)
This fixes up the mistake of using toString() to expose the original
XPath and defines a WithExpression extension, which allows exposing
the parsed expression.

Change-Id: Iac0c36f6bde106f92b91a4bef768de55ad9e6d41
JIRA: YANGTOOLS-966
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/RevisionAwareXPath.java
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/RevisionAwareXPathImpl.java
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/must/MustEffectiveStatementImpl.java
yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/AugmentTest.java
yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug5518Test.java
yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6180Test.java
yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/MustAndWhenStmtTest.java
yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserSimpleTest.java

index 62a349b99b5a2ed2b6d42bd09ce37fc7e11cfa2d..8096eba9b135befc0c5dddfbad7390f844937489 100644 (file)
@@ -7,28 +7,40 @@
  */
 package org.opendaylight.yangtools.yang.model.api;
 
+import com.google.common.annotations.Beta;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.yang.xpath.api.YangXPathExpression;
+import org.opendaylight.yangtools.yang.xpath.api.YangXPathExpression.QualifiedBound;
+
 /**
- * Contains methods for getting data (concrete XPath) and metadata (is XPath
- * absolute) from XPath instance.
+ * Contains methods for getting data (concrete XPath) and metadata (is XPath absolute) from XPath instance.
  */
+// FIXME: 4.0.0: find a better name for this interface
 public interface RevisionAwareXPath {
-
     /**
-     * Returns <code>true</code> if the XPapth starts in root of Yang model,
-     * otherwise returns <code>false</code>.
+     * Returns <code>true</code> if the XPapth starts in root of Yang model, otherwise returns <code>false</code>.
      *
-     * @return <code>true</code> if the XPapth starts in root of Yang model,
-     *         otherwise returns <code>false</code>
+     * @return <code>true</code> if the XPapth starts in root of Yang model, otherwise returns <code>false</code>
      */
     boolean isAbsolute();
 
     /**
-     * Returns the XPath formatted string as is defined in model. <br>
-     * For example:
+     * Returns the XPath formatted string as is defined in model. For example:
      * /prefix:container/prefix:container::cond[when()=foo]/prefix:leaf
      *
      * @return the XPath formatted string as is defined in model.
      */
-    @Override
-    String toString();
+    @NonNull String getOriginalString();
+
+    @Beta
+    // FIXME: 4.0.0: integrate this interface into RevisionAwareXPath
+    interface WithExpression extends RevisionAwareXPath {
+        /**
+         * Return the {@link YangXPathExpression} of this XPath. The expression is required to be at least
+         * Qualified-bound.
+         *
+         * @return The location path
+         */
+        @NonNull QualifiedBound getXPathExpression();
+    }
 }
index 813f38d0570aaca2dd4247eaba7c0f055eef0b3f..a4e81b7c624faa6d263085019e51ceef4f82c82d 100644 (file)
@@ -7,22 +7,23 @@
  */
 package org.opendaylight.yangtools.yang.model.util;
 
+import static java.util.Objects.requireNonNull;
+
 import java.util.Objects;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
 
 /**
- * The <code>helper</code> implementation of Instance Rewision Aware XPath
- * interface.
+ * The <code>helper</code> implementation of Instance Revision Aware XPath interface.
  *
  * @see RevisionAwareXPath
  */
 public class RevisionAwareXPathImpl implements RevisionAwareXPath {
-
-    private final String xpath;
+    private final @NonNull String xpath;
     private final boolean absolute;
 
     public RevisionAwareXPathImpl(final String xpath, final boolean absolute) {
-        this.xpath = xpath;
+        this.xpath = requireNonNull(xpath);
         this.absolute = absolute;
     }
 
@@ -56,7 +57,7 @@ public class RevisionAwareXPathImpl implements RevisionAwareXPath {
     }
 
     @Override
-    public String toString() {
+    public String getOriginalString() {
         return xpath;
     }
 }
index d64fbbf2992bebf7f3c692c36bdd382ce610d5fd..d5fdbe8d6f0f3140ac45c04ead7a0ebd6ab0b02d 100644 (file)
@@ -83,6 +83,6 @@ final class MustEffectiveStatementImpl extends DeclaredEffectiveStatementBase<Re
 
     @Override
     public String toString() {
-        return xpath.toString();
+        return xpath.getOriginalString();
     }
 }
index c4ef3c754b2e2c15003f1ab27e173fd753537866..b2f2f4556b567364e7dec4a77b5d6b0fc92e7c6e 100644 (file)
@@ -134,9 +134,9 @@ public class AugmentTest {
         for (final AugmentationSchemaNode as : augmentations) {
             if (!as.getWhenCondition().isPresent()) {
                 augment3 = as;
-            } else if ("if:ifType='ds0'".equals(as.getWhenCondition().get().toString())) {
+            } else if ("if:ifType='ds0'".equals(as.getWhenCondition().get().getOriginalString())) {
                 augment1 = as;
-            } else if ("if:ifType='ds2'".equals(as.getWhenCondition().get().toString())) {
+            } else if ("if:ifType='ds2'".equals(as.getWhenCondition().get().getOriginalString())) {
                 augment2 = as;
             }
         }
index 316f99958fd72677fcbb0616defdf0fafa7beff2..2464c156431c71e8458d1d823c4673b60001a387 100644 (file)
@@ -31,6 +31,6 @@ public class Bug5518Test {
         final Collection<MustDefinition> mustConstraints = root.getMustConstraints();
         assertEquals(1, mustConstraints.size());
         final MustDefinition must = mustConstraints.iterator().next();
-        assertEquals("not(deref(.)/../same-pass)", must.getXpath().toString());
+        assertEquals("not(deref(.)/../same-pass)", must.getXpath().getOriginalString());
     }
 }
index 60f5f3b8997282dd2bf2872c5215d8848f660f2d..c4185796eb1c33f9f30c2e9c62a9117d61a548db 100644 (file)
@@ -72,7 +72,7 @@ public class Bug6180Test {
         assertTrue(dataNodeBar instanceof ContainerSchemaNode);
         final ContainerSchemaNode bar = (ContainerSchemaNode) dataNodeBar;
         final RevisionAwareXPath whenCondition = bar.getWhenCondition().get();
-        assertEquals("/foo != \"bar\"", whenCondition.toString());
+        assertEquals("/foo != \"bar\"", whenCondition.getOriginalString());
 
         final Set<TypeDefinition<?>> typeDefinitions = schemaContext.getTypeDefinitions();
         assertEquals(1, typeDefinitions.size());
@@ -90,7 +90,7 @@ public class Bug6180Test {
         assertTrue(dataNodeBar instanceof ContainerSchemaNode);
         final ContainerSchemaNode bar = (ContainerSchemaNode) dataNodeBar;
         final RevisionAwareXPath whenCondition = bar.getWhenCondition().get();
-        assertEquals("/foo != 'bar'", whenCondition.toString());
+        assertEquals("/foo != 'bar'", whenCondition.getOriginalString());
 
         final Set<TypeDefinition<?>> typeDefinitions = schemaContext.getTypeDefinitions();
         assertEquals(1, typeDefinitions.size());
index aef2927745a9fb38f610d66be8fc973b00bb773b..98e6656c3f78f3fabfa6815bdd1c7ba0b25addf2 100644 (file)
@@ -49,14 +49,14 @@ public class MustAndWhenStmtTest {
 
         final Iterator<MustDefinition> mustsIterator = musts.iterator();
         MustDefinition mustStmt = mustsIterator.next();
-        assertThat(mustStmt.getXpath().toString(), anyOf(is("ifType != 'ethernet' or (ifType = 'ethernet' and "
+        assertThat(mustStmt.getXpath().getOriginalString(), anyOf(is("ifType != 'ethernet' or (ifType = 'ethernet' and "
                 + "ifMTU = 1500)"), is("ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)")));
         assertThat(mustStmt.getErrorMessage(), anyOf(is(Optional.of("An ethernet MTU must be 1500")),
             is(Optional.of("An atm MTU must be 64 .. 17966"))));
         assertThat(mustStmt.getErrorAppTag(), anyOf(is(Optional.of("An ethernet error")),
             is(Optional.of("An atm error"))));
         mustStmt = mustsIterator.next();
-        assertThat(mustStmt.getXpath().toString(), anyOf(
+        assertThat(mustStmt.getXpath().getOriginalString(), anyOf(
             is("ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)"),
             is("ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)")));
         assertThat(mustStmt.getErrorMessage(), anyOf(is(Optional.of("An ethernet MTU must be 1500")),
@@ -78,6 +78,6 @@ public class MustAndWhenStmtTest {
         final ContainerSchemaNode container = (ContainerSchemaNode) testModule.getDataChildByName(
             QName.create(testModule.getQNameModule(), "test-container"));
         assertNotNull(container);
-        assertEquals("conditional-leaf = 'autumn-leaf'", container.getWhenCondition().get().toString());
+        assertEquals("conditional-leaf = 'autumn-leaf'", container.getWhenCondition().get().getOriginalString());
     }
 }
index 08da5b773367508e79f7b6a59d46f94a6fe930aa..54acd650dd97f75aad05e732cf241506d5fab92a 100644 (file)
@@ -72,7 +72,7 @@ public class YangParserSimpleTest {
         assertFalse(data.isConfiguration());
 
         assertTrue(data.isMandatory());
-        assertEquals("class != 'wheel'", data.getWhenCondition().get().toString());
+        assertEquals("class != 'wheel'", data.getWhenCondition().get().getOriginalString());
         final Collection<MustDefinition> mustConstraints = data.getMustConstraints();
         assertEquals(2, mustConstraints.size());
 
@@ -113,7 +113,7 @@ public class YangParserSimpleTest {
         assertFalse(nodes.isConfiguration());
 
         // constraints
-        assertEquals("class != 'wheel'", nodes.getWhenCondition().get().toString());
+        assertEquals("class != 'wheel'", nodes.getWhenCondition().get().getOriginalString());
         final Collection<MustDefinition> mustConstraints = nodes.getMustConstraints();
         assertEquals(2, mustConstraints.size());