Introduce WhenConditionAware 11/64811/4
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 27 Oct 2017 15:59:34 +0000 (17:59 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 27 Oct 2017 19:54:30 +0000 (21:54 +0200)
getWhenCondition() is used by multiple interfaces, extract it to
a dedicated interface so the individual definitions are consistent.

Change-Id: I177f8dcddd1ffa150ffe50c106475ee624343a20
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
16 files changed:
yang/yang-data-util/src/main/java/org/opendaylight/yangtools/yang/data/util/ConstraintDefinitions.java
yang/yang-data-util/src/main/java/org/opendaylight/yangtools/yang/data/util/EmptyConstraintDefinition.java
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/AugmentationSchemaNode.java
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/ConstraintDefinition.java
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/UsesNode.java
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/WhenConditionAware.java [new file with mode: 0644]
yang/yang-model-export/src/main/java/org/opendaylight/yangtools/yang/model/export/SchemaContextEmitter.java
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/EffectiveAugmentationSchema.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/AugmentEffectiveStatementImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveConstraintDefinitionImpl.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/AugmentTest.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug5481Test.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6180Test.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/MustAndWhenStmtTest.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserSimpleTest.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/yin/YinFileChoiceStmtTest.java

index e7b93721f2af1dc3062e18f0b812281cc4045fdb..a5edb1c890d2661e3e54c74e46dd80b7accf6795 100644 (file)
@@ -58,7 +58,7 @@ public final class ConstraintDefinitions {
 
     public static String toString(final ConstraintDefinition def) {
         return MoreObjects.toStringHelper(def).omitNullValues()
-                .add("whenCondition", def.getWhenCondition())
+                .add("whenCondition", def.getWhenCondition().orElse(null))
                 .add("mustConstraints", def.getMustConstraints())
                 .add("mandatory", def.isMandatory())
                 .add("minElements", def.getMinElements())
index 8050a1fcb2ebf61751b59c4c84945ad9acc04ff6..ab348855a245b35217b7e48e8550045e5ab5cef3 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.yangtools.yang.data.util;
 
 import com.google.common.collect.ImmutableSet;
+import java.util.Optional;
 import java.util.Set;
 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
@@ -39,8 +40,8 @@ public abstract class EmptyConstraintDefinition implements ConstraintDefinition
     }
 
     @Override
-    public final RevisionAwareXPath getWhenCondition() {
-        return null;
+    public final Optional<RevisionAwareXPath> getWhenCondition() {
+        return Optional.empty();
     }
 
     @Override
index 5e280997dc5247e6073c7affb65aa180112a1079..dcb64f77250b45cd143d14932ea95b086bfa2c0e 100644 (file)
@@ -16,21 +16,7 @@ import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
  * from a grouping in a "uses" statement.
  */
 public interface AugmentationSchemaNode extends DataNodeContainer, NotificationNodeContainer, ActionNodeContainer,
-        WithStatus {
-    /**
-     * Returns when statement.
-     *
-     * <p>
-     * If when condition is present node defined by the parent data definition
-     * statement is only valid when the returned XPath
-     * expression conceptually evaluates to "true"
-     * for a particular instance, then the node defined by the parent data
-     * definition statement is valid; otherwise, it is not.
-     *
-     * @return XPath condition
-     */
-    RevisionAwareXPath getWhenCondition();
-
+        WhenConditionAware, WithStatus {
     /**
      * Returns augmentation schema path.
      *
index 6da5c0e1160f08cfa8607a0ebe98a729ebdd023f..1f9ee7277125c11c94a2ef49e4a4b5f151b3302b 100644 (file)
@@ -16,17 +16,7 @@ import javax.annotation.Nullable;
  * YANG element therefore if the constraint doesn't have sense for some element
  * then the method returns <code>null</code> value.
  */
-public interface ConstraintDefinition {
-
-    /**
-     * Specifies the condition when the data node which contains
-     * <code>when</code> YANG substatement has to be present. If XPath
-     * expression is evaluated as true then the data node has to be present.
-     *
-     * @return XPath expression.
-     */
-    RevisionAwareXPath getWhenCondition();
-
+public interface ConstraintDefinition extends WhenConditionAware {
     /**
      * Specifies the rules which the node which contains <code>must</code> YANG
      * substatement has to match.
index e494db7580406b6b73d7790776b0192ed5ae47cc..b96d2b2bc0449e73e44eaa60cb36fece65980514 100644 (file)
@@ -8,15 +8,14 @@
 package org.opendaylight.yangtools.yang.model.api;
 
 import java.util.Map;
-import java.util.Optional;
 import java.util.Set;
 import javax.annotation.Nonnull;
+import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
 
 /**
- * Contains the methods for getting data and checking properties of the YANG
- * <code>uses</code> substatement.
+ * Contains the methods for getting data and checking properties of the YANG <code>uses</code> substatement.
  */
-public interface UsesNode extends DocumentedNode.WithStatus {
+public interface UsesNode extends WhenConditionAware, WithStatus {
 
     /**
      * Returns the schema path to used grouping.
@@ -58,19 +57,4 @@ public interface UsesNode extends DocumentedNode.WithStatus {
      *         refined node
      */
     @Nonnull Map<SchemaPath, SchemaNode> getRefines();
-
-    /**
-     * Returns when statement.
-     *
-     * <p>
-     * If when condition is present node defined by the parent data definition
-     * statement is only valid when the returned XPath expression conceptually
-     * evaluates to "true" for a particular instance, then the node defined by
-     * the parent data definition statement is valid; otherwise, it is not.
-     *
-     * @return Optional of XPath condition
-     */
-    default @Nonnull Optional<RevisionAwareXPath> getWhenCondition() {
-        return Optional.empty();
-    }
 }
diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/WhenConditionAware.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/WhenConditionAware.java
new file mode 100644 (file)
index 0000000..1b07f48
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2017 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.model.api;
+
+import com.google.common.annotations.Beta;
+import java.util.Optional;
+
+/**
+ * Mix-in interface for nodes which can be conditional on a when statement.
+ *
+ * @author Robert Varga
+ */
+@Beta
+public interface WhenConditionAware {
+    /**
+     * Returns when statement.
+     *
+     * <p>
+     * If when condition is present node defined by the parent data definition
+     * statement is only valid when the returned XPath
+     * expression conceptually evaluates to "true"
+     * for a particular instance, then the node defined by the parent data
+     * definition statement is valid; otherwise, it is not.
+     *
+     * @return XPath condition
+     */
+    Optional<RevisionAwareXPath> getWhenCondition();
+
+}
index d6e557d78fdf51baba14bfdd32bb34a62307e8fc..414aaa10861aa2c8e2f761746eae37bed6a07263 100644 (file)
@@ -1814,7 +1814,7 @@ abstract class SchemaContextEmitter {
         }
 
         private void emitConstraints(final ConstraintDefinition constraints) {
-            emitWhen(constraints.getWhenCondition());
+            constraints.getWhenCondition().ifPresent(this::emitWhen);
             for (final MustDefinition mustCondition : constraints.getMustConstraints()) {
                 emitMust(mustCondition);
             }
@@ -1822,7 +1822,7 @@ abstract class SchemaContextEmitter {
 
         private void emitLeaf(final LeafSchemaNode child) {
             super.writer.startLeafNode(child.getQName());
-            emitWhen(child.getConstraints().getWhenCondition());
+            child.getConstraints().getWhenCondition().ifPresent(this::emitWhen);
             // FIXME: BUG-2444: *(ifFeatureNode )
             emitTypeNode(child.getPath(), child.getType());
             emitUnitsNode(child.getUnits());
@@ -1839,7 +1839,7 @@ abstract class SchemaContextEmitter {
         private void emitLeafList(final LeafListSchemaNode child) {
             super.writer.startLeafListNode(child.getQName());
 
-            emitWhen(child.getConstraints().getWhenCondition());
+            child.getConstraints().getWhenCondition().ifPresent(this::emitWhen);
             // FIXME: BUG-2444: *(ifFeatureNode )
             emitTypeNode(child.getPath(), child.getType());
             emitUnitsNode(child.getType().getUnits());
@@ -1858,7 +1858,7 @@ abstract class SchemaContextEmitter {
 
         private void emitList(final ListSchemaNode child) {
             super.writer.startListNode(child.getQName());
-            emitWhen(child.getConstraints().getWhenCondition());
+            child.getConstraints().getWhenCondition().ifPresent(this::emitWhen);
 
             // FIXME: BUG-2444: *(ifFeatureNode )
             emitMustNodes(child.getConstraints().getMustConstraints());
@@ -1903,7 +1903,7 @@ abstract class SchemaContextEmitter {
 
         private void emitChoice(final ChoiceSchemaNode choice) {
             super.writer.startChoiceNode(choice.getQName());
-            emitWhen(choice.getConstraints().getWhenCondition());
+            choice.getConstraints().getWhenCondition().ifPresent(this::emitWhen);
             // FIXME: BUG-2444: *(ifFeatureNode )
             // FIXME: BUG-2444: defaultNode //Optional
             emitConfigNode(choice.isConfiguration());
@@ -1922,7 +1922,7 @@ abstract class SchemaContextEmitter {
                 return;
             }
             super.writer.startCaseNode(caze.getQName());
-            emitWhen(caze.getConstraints().getWhenCondition());
+            caze.getConstraints().getWhenCondition().ifPresent(this::emitWhen);
             // FIXME: BUG-2444: *(ifFeatureNode )
             emitDocumentedNode(caze);
             emitDataNodeContainer(caze);
@@ -1944,7 +1944,7 @@ abstract class SchemaContextEmitter {
         }
 
         private void emitBodyOfDataSchemaNode(final DataSchemaNode dataSchemaNode) {
-            emitWhen(dataSchemaNode.getConstraints().getWhenCondition());
+            dataSchemaNode.getConstraints().getWhenCondition().ifPresent(this::emitWhen);
             // FIXME: BUG-2444: *(ifFeatureNode )
             emitMustNodes(dataSchemaNode.getConstraints().getMustConstraints());
             emitConfigNode(dataSchemaNode.isConfiguration());
index fdb09d08b3b09bf114fb7adf236ee4277341d6c4..0f4e019b5cfa7263bf2ecec5d1db73823d8c4f81 100644 (file)
@@ -50,7 +50,7 @@ public final class EffectiveAugmentationSchema implements AugmentationSchemaNode
     }
 
     @Override
-    public RevisionAwareXPath getWhenCondition() {
+    public Optional<RevisionAwareXPath> getWhenCondition() {
         return delegate.getWhenCondition();
     }
 
index f05bef46a189873237acbc66a218c4a621a47560..62edcab92a553d2b2d1244e4d4a4be6f4252cc71 100644 (file)
@@ -87,8 +87,8 @@ public final class AugmentEffectiveStatementImpl
     }
 
     @Override
-    public RevisionAwareXPath getWhenCondition() {
-        return whenCondition;
+    public Optional<RevisionAwareXPath> getWhenCondition() {
+        return Optional.ofNullable(whenCondition);
     }
 
     @Nonnull
index b29451b9b0f8ad97348fc5724d91963eef0bbbdd..dfcf434a5f56fab137b32ac71828ce574a5f2851 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.collect.ImmutableSet;
+import java.util.Optional;
 import java.util.Set;
 import org.opendaylight.yangtools.yang.data.util.ConstraintDefinitions;
 import org.opendaylight.yangtools.yang.data.util.EmptyConstraintDefinition;
@@ -75,8 +76,8 @@ final class EffectiveConstraintDefinitionImpl implements ConstraintDefinition {
     }
 
     @Override
-    public RevisionAwareXPath getWhenCondition() {
-        return whenCondition;
+    public Optional<RevisionAwareXPath> getWhenCondition() {
+        return Optional.ofNullable(whenCondition);
     }
 
     @Override
index 7bd9b1ab24988ac4b3f81ba8a566e5f9574b97e3..300111b7a879241c66df7344f1dba28e5f3f78a3 100644 (file)
@@ -133,11 +133,11 @@ public class AugmentTest {
         AugmentationSchemaNode augment2 = null;
         AugmentationSchemaNode augment3 = null;
         for (final AugmentationSchemaNode as : augmentations) {
-            if (as.getWhenCondition() == null) {
+            if (!as.getWhenCondition().isPresent()) {
                 augment3 = as;
-            } else if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
+            } else if ("if:ifType='ds0'".equals(as.getWhenCondition().get().toString())) {
                 augment1 = as;
-            } else if ("if:ifType='ds2'".equals(as.getWhenCondition().toString())) {
+            } else if ("if:ifType='ds2'".equals(as.getWhenCondition().get().toString())) {
                 augment2 = as;
             }
         }
index 6d09dfe0088e2195059ad176970cc886f61cd82e..dd4e0889abab78c3ccfd1c5c2a34389a4aa8b3a8 100644 (file)
@@ -9,6 +9,7 @@
 package org.opendaylight.yangtools.yang.stmt;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
@@ -39,9 +40,8 @@ public class Bug5481Test {
         assertTrue(dataChildByName instanceof ContainerSchemaNode);
 
         ContainerSchemaNode topContainer = (ContainerSchemaNode) dataChildByName;
-        RevisionAwareXPath whenConditionTopContainer = topContainer.getConstraints().getWhenCondition();
 
-        assertNull(whenConditionTopContainer);
+        assertFalse(topContainer.getConstraints().getWhenCondition().isPresent());
         assertEquals(Status.CURRENT, topContainer.getStatus());
         assertNull(topContainer.getDescription());
         assertNull(topContainer.getReference());
@@ -54,7 +54,7 @@ public class Bug5481Test {
         assertTrue(dataChildByName2 instanceof LeafSchemaNode);
 
         LeafSchemaNode extendedLeaf = (LeafSchemaNode) dataChildByName2;
-        RevisionAwareXPath whenConditionExtendedLeaf = extendedLeaf.getConstraints().getWhenCondition();
+        RevisionAwareXPath whenConditionExtendedLeaf = extendedLeaf.getConstraints().getWhenCondition().get();
 
         assertEquals(new RevisionAwareXPathImpl("module1:top = 'extended'", false), whenConditionExtendedLeaf);
         assertEquals(Status.DEPRECATED, extendedLeaf.getStatus());
index 260b1f080af6c011dbed0e5c6b1edc14f7219dd3..2700ef44f1f61e36b97e2598526550bd4b3c7ac1 100644 (file)
@@ -70,7 +70,7 @@ public class Bug6180Test {
         final DataSchemaNode dataNodeBar = schemaContext.getDataChildByName(QName.create("foo", "2016-07-11", "bar"));
         assertTrue(dataNodeBar instanceof ContainerSchemaNode);
         final ContainerSchemaNode bar = (ContainerSchemaNode) dataNodeBar;
-        final RevisionAwareXPath whenCondition = bar.getConstraints().getWhenCondition();
+        final RevisionAwareXPath whenCondition = bar.getConstraints().getWhenCondition().get();
         assertEquals("/foo != \"bar\"", whenCondition.toString());
 
         final Set<TypeDefinition<?>> typeDefinitions = schemaContext.getTypeDefinitions();
@@ -88,7 +88,7 @@ public class Bug6180Test {
         final DataSchemaNode dataNodeBar = schemaContext.getDataChildByName(QName.create("foo", "2016-07-11", "bar"));
         assertTrue(dataNodeBar instanceof ContainerSchemaNode);
         final ContainerSchemaNode bar = (ContainerSchemaNode) dataNodeBar;
-        final RevisionAwareXPath whenCondition = bar.getConstraints().getWhenCondition();
+        final RevisionAwareXPath whenCondition = bar.getConstraints().getWhenCondition().get();
         assertEquals("/foo != 'bar'", whenCondition.toString());
 
         final Set<TypeDefinition<?>> typeDefinitions = schemaContext.getTypeDefinitions();
index 2f3390d1e9ca113538b578690a6615f9da1de715..f6bc785260e91b81aaa12a284876359afcfe58b8 100644 (file)
@@ -82,6 +82,7 @@ public class MustAndWhenStmtTest {
         final ContainerSchemaNode container = (ContainerSchemaNode) testModule.getDataChildByName(
             QName.create(testModule.getQNameModule(), "test-container"));
         assertNotNull(container);
-        assertEquals("conditional-leaf = 'autumn-leaf'", container.getConstraints().getWhenCondition().toString());
+        assertEquals("conditional-leaf = 'autumn-leaf'", container.getConstraints().getWhenCondition()
+            .get().toString());
     }
 }
index 6a072559b1e2f0702991ccb85cac92dc742ed542..a0f6230341fa6e7ed283c5ddfc86ccecd9426070 100644 (file)
@@ -71,7 +71,7 @@ public class YangParserSimpleTest {
         assertFalse(data.isAugmenting());
         assertFalse(data.isConfiguration());
         final ConstraintDefinition constraints = data.getConstraints();
-        assertEquals("class != 'wheel'", constraints.getWhenCondition().toString());
+        assertEquals("class != 'wheel'", constraints.getWhenCondition().get().toString());
         final Set<MustDefinition> mustConstraints = constraints.getMustConstraints();
         assertEquals(2, constraints.getMustConstraints().size());
 
@@ -119,7 +119,7 @@ public class YangParserSimpleTest {
 
         // constraints
         final ConstraintDefinition constraints = nodes.getConstraints();
-        assertEquals("class != 'wheel'", constraints.getWhenCondition().toString());
+        assertEquals("class != 'wheel'", constraints.getWhenCondition().get().toString());
         final Set<MustDefinition> mustConstraints = constraints.getMustConstraints();
         assertEquals(2, constraints.getMustConstraints().size());
 
index 72065a976d622000ab07f8e7e1ece4e9f1a391ec..61351d5b8e92e27598abaf88151c045590d9c67c 100644 (file)
@@ -23,7 +23,6 @@ import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
 import org.opendaylight.yangtools.yang.stmt.TestUtils;
@@ -67,8 +66,7 @@ public class YinFileChoiceStmtTest {
         assertEquals("main-impl", caseNode.getQName().getLocalName());
         assertEquals(13, caseNode.getChildNodes().size());
 
-        final RevisionAwareXPath whenCondition = caseNode.getConstraints().getWhenCondition();
-        assertNotNull(whenCondition);
+        assertTrue(caseNode.getConstraints().getWhenCondition().isPresent());
 
         choice = (ChoiceSchemaNode) list.getDataChildByName(QName.create(testModule.getQNameModule(), "state"));
         assertNotNull(choice);