Split out AbstractValidation 00/93900/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 19 Nov 2020 21:30:20 +0000 (22:30 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 20 Nov 2020 16:56:35 +0000 (17:56 +0100)
MinMaxElementsValidation performs a useful role as a template for
how we can create simple subtree validations. Split the common bits
into AbstractValidation and elinate code duplication by doing some
trickery.

JIRA: YANGTOOLS-1177
Change-Id: I5adbed16e85f9752a0ce5061b0159c8124583346
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit ffe8f4ea13bd2bab97514aa1d6ee257a1d7b39ff)

yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/AbstractValidation.java [new file with mode: 0644]
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/MinMaxElementsValidation.java
yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/ListConstraintsValidation.java
yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/YT776Test.java

diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/AbstractValidation.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/AbstractValidation.java
new file mode 100644 (file)
index 0000000..98ff474
--- /dev/null
@@ -0,0 +1,139 @@
+/*
+ * Copyright (c) 2020 PANTHEON.tech, 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.data.impl.schema.tree;
+
+import static com.google.common.base.Verify.verifyNotNull;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.base.MoreObjects.ToStringHelper;
+import java.util.Optional;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A forwarding {@link ModificationApplyOperation}. Useful for strategies which do not deal with data layout, but rather
+ * perform additional validation.
+ */
+abstract class AbstractValidation extends ModificationApplyOperation {
+    private static final Logger LOG = LoggerFactory.getLogger(AbstractValidation.class);
+
+    private final @NonNull ModificationApplyOperation delegate;
+
+    AbstractValidation(final ModificationApplyOperation delegate) {
+        this.delegate = requireNonNull(delegate);
+    }
+
+    @Override
+    public final Optional<ModificationApplyOperation> getChild(final PathArgument child) {
+        return delegate.getChild(child);
+    }
+
+    @Override
+    final ChildTrackingPolicy getChildPolicy() {
+        return delegate.getChildPolicy();
+    }
+
+    @Override
+    final void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode<?, ?> value, final Version version) {
+        delegate.mergeIntoModifiedNode(node, value, version);
+    }
+
+    @Override
+    final void quickVerifyStructure(final NormalizedNode<?, ?> modification) {
+        delegate.quickVerifyStructure(modification);
+    }
+
+    @Override
+    final void recursivelyVerifyStructure(final NormalizedNode<?, ?> value) {
+        delegate.recursivelyVerifyStructure(value);
+    }
+
+    @Override
+    final Optional<? extends TreeNode> apply(final ModifiedNode modification,
+            final Optional<? extends TreeNode> storeMeta, final Version version) {
+        Optional<? extends TreeNode> ret = modification.getValidatedNode(this, storeMeta);
+        if (ret == null) {
+            // This might also mean the delegate is maintaining validation
+            if (delegate instanceof AbstractValidation) {
+                ret = modification.getValidatedNode(delegate, storeMeta);
+                if (ret != null) {
+                    return ret;
+                }
+            }
+
+            // Deal with the result moving on us
+            ret = delegate.apply(modification, storeMeta, version);
+            ret.ifPresent(meta -> enforceOnData(meta.getData()));
+        }
+        return ret;
+    }
+
+    @Override
+    final void checkApplicable(final ModificationPath path, final NodeModification modification,
+            final Optional<? extends TreeNode> current, final Version version) throws DataValidationFailedException {
+        delegate.checkApplicable(path, modification, current, version);
+        if (!(modification instanceof ModifiedNode)) {
+            // FIXME: 7.0.0: turn this into a verify?
+            LOG.debug("Could not validate {}, does not implement expected class {}", modification, ModifiedNode.class);
+            return;
+        }
+
+        final ModifiedNode modified = (ModifiedNode) modification;
+        if (delegate instanceof AbstractValidation) {
+            checkApplicable(path, verifyNotNull(modified.getValidatedNode(delegate, current)));
+            return;
+        }
+
+        // We need to actually perform the operation to deal with merge in a sane manner. We know the modification
+        // is immutable, so the result of validation will probably not change. Note we should not be checking number
+        final Optional<? extends TreeNode> applied = delegate.apply(modified, current, version);
+        checkApplicable(path, applied);
+
+        // Everything passed. We now have a snapshot of the result node, it would be too bad if we just threw it out.
+        // We know what the result of an apply operation is going to be *if* the following are kept unchanged:
+        // - the 'current' node
+        // - the effective model context (therefore, the fact this object is associated with the modification)
+        //
+        // So let's stash the result. We will pick it up during apply operation.
+        modified.setValidatedNode(this, current, applied);
+    }
+
+    private void checkApplicable(final ModificationPath path, final Optional<? extends TreeNode> applied)
+            throws DataValidationFailedException {
+        if (applied.isPresent()) {
+            // We only enforce min/max on present data and rely on MandatoryLeafEnforcer to take care of the empty case
+            enforceOnData(path, applied.orElseThrow().getData());
+        }
+    }
+
+    @Override
+    void fullVerifyStructure(final NormalizedNode<?, ?> modification) {
+        delegate.fullVerifyStructure(modification);
+        enforceOnData(modification);
+    }
+
+    final @NonNull ModificationApplyOperation delegate() {
+        return delegate;
+    }
+
+    abstract void enforceOnData(ModificationPath path, NormalizedNode<?, ?> value)
+        throws DataValidationFailedException;
+
+    abstract void enforceOnData(@NonNull NormalizedNode<?, ?> data);
+
+    @Override
+    ToStringHelper addToStringAttributes(final ToStringHelper helper) {
+        return helper.add("delegate", delegate);
+    }
+}
index 8e1e91c5633060d19c12e420622351199ea91dc2..f5c75a6524eafc0d2b56ed2ccc19025f79aa9450 100644 (file)
@@ -7,36 +7,26 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
 
-import static com.google.common.base.Preconditions.checkArgument;
-import static java.util.Objects.requireNonNull;
-
 import com.google.common.base.MoreObjects.ToStringHelper;
 import java.util.Optional;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.RequiredElementCountException;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint;
 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraintAware;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 final class MinMaxElementsValidation<T extends DataSchemaNode & ElementCountConstraintAware>
-        extends ModificationApplyOperation {
-    private static final Logger LOG = LoggerFactory.getLogger(MinMaxElementsValidation.class);
-
-    private final SchemaAwareApplyOperation<T> delegate;
+        extends AbstractValidation {
     private final int minElements;
     private final int maxElements;
 
     private MinMaxElementsValidation(final SchemaAwareApplyOperation<T> delegate, final Integer minElements,
             final Integer maxElements) {
-        this.delegate = requireNonNull(delegate);
+        super(delegate);
         this.minElements = minElements != null ? minElements : 0;
         this.maxElements = maxElements != null ? maxElements : Integer.MAX_VALUE;
     }
@@ -53,105 +43,39 @@ final class MinMaxElementsValidation<T extends DataSchemaNode & ElementCountCons
     }
 
     @Override
-    Optional<? extends TreeNode> apply(final ModifiedNode modification, final Optional<? extends TreeNode> storeMeta,
-            final Version version) {
-        Optional<? extends TreeNode> ret = modification.getValidatedNode(this, storeMeta);
-        if (ret == null) {
-            // Deal with the result moving on us
-            ret = delegate.apply(modification, storeMeta, version);
-            if (ret.isPresent()) {
-                checkChildren(ret.get().getData());
-            }
-        }
-
-        return ret;
-    }
-
-    @Override
-    void checkApplicable(final ModificationPath path, final NodeModification modification,
-            final Optional<? extends TreeNode> current, final Version version) throws DataValidationFailedException {
-        delegate.checkApplicable(path, modification, current, version);
-
-        if (!(modification instanceof ModifiedNode)) {
-            LOG.debug("Could not validate {}, does not implement expected class {}", modification, ModifiedNode.class);
-            return;
-        }
-        final ModifiedNode modified = (ModifiedNode) modification;
-
-        // We need to actually perform the operation to deal with merge in a sane manner. We know the modification
-        // is immutable, so the result of validation will probably not change. Note we should not be checking number
-        final Optional<? extends TreeNode> maybeApplied = delegate.apply(modified, current, version);
-        if (maybeApplied.isPresent()) {
-            // We only enforce min/max on present data and rely on MandatoryLeafEnforcer to take care of the empty case
-            validateMinMaxElements(path, maybeApplied.get().getData());
-        }
-
-        // Everything passed. We now have a snapshot of the result node, it would be too bad if we just threw it out.
-        // We know what the result of an apply operation is going to be *if* the following are kept unchanged:
-        // - the 'current' node
-        // - the schemacontext (therefore, the fact this object is associated with the modification)
-        //
-        // So let's stash the result. We will pick it up during apply operation.
-        modified.setValidatedNode(this, current, maybeApplied);
-    }
-
-    @Override
-    void fullVerifyStructure(final NormalizedNode<?, ?> modification) {
-        delegate.fullVerifyStructure(modification);
-        checkChildren(modification);
-    }
-
-    @Override
-    public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
-        return delegate.getChild(child);
-    }
-
-    @Override
-    ChildTrackingPolicy getChildPolicy() {
-        return delegate.getChildPolicy();
-    }
-
-    @Override
-    void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode<?, ?> value, final Version version) {
-        delegate.mergeIntoModifiedNode(node, value, version);
+    void enforceOnData(final NormalizedNode<?, ?> data) {
+        enforceOnData(data, (actual, message) -> new IllegalArgumentException(message));
     }
 
     @Override
-    void quickVerifyStructure(final NormalizedNode<?, ?> modification) {
-        delegate.quickVerifyStructure(modification);
+    void enforceOnData(final ModificationPath path, final NormalizedNode<?, ?> data)
+            throws RequiredElementCountException {
+        enforceOnData(data, (actual, message) -> new RequiredElementCountException(path.toInstanceIdentifier(),
+            minElements, maxElements, actual, message));
     }
 
-    @Override
-    void recursivelyVerifyStructure(final NormalizedNode<?, ?> value) {
-        delegate.recursivelyVerifyStructure(value);
-    }
-
-    @Override
-    ToStringHelper addToStringAttributes(final ToStringHelper helper) {
-        return helper.add("min", minElements).add("max", maxElements).add("delegate", delegate);
+    @FunctionalInterface
+    @NonNullByDefault
+    interface ExceptionSupplier<T extends Exception> {
+        T get(int actual, String message);
     }
 
-    private void validateMinMaxElements(final ModificationPath path, final NormalizedNode<?, ?> value)
-            throws DataValidationFailedException {
-        final PathArgument id = value.getIdentifier();
+    private <X extends @NonNull Exception> void enforceOnData(final NormalizedNode<?, ?> value,
+            final ExceptionSupplier<X> exceptionSupplier) throws X {
         final int children = numOfChildrenFromValue(value);
         if (minElements > children) {
-            throw new RequiredElementCountException(path.toInstanceIdentifier(), minElements, maxElements, children,
-                "%s does not have enough elements (%s), needs at least %s", id, children, minElements);
+            throw exceptionSupplier.get(children, value.getIdentifier()
+                + " does not have enough elements (" + children + "), needs at least " + minElements);
         }
         if (maxElements < children) {
-            throw new RequiredElementCountException(path.toInstanceIdentifier(), minElements, maxElements, children,
-                "%s has too many elements (%s), can have at most %s", id, children, maxElements);
+            throw exceptionSupplier.get(children, value.getIdentifier()
+                + " has too many elements (" + children + "), can have at most " + maxElements);
         }
     }
 
-    private void checkChildren(final NormalizedNode<?, ?> value) {
-        final PathArgument id = value.getIdentifier();
-        final int children = numOfChildrenFromValue(value);
-        checkArgument(minElements <= children, "Node %s does not have enough elements (%s), needs at least %s", id,
-                children, minElements);
-        checkArgument(maxElements >= children, "Node %s has too many elements (%s), can have at most %s", id, children,
-                maxElements);
+    @Override
+    ToStringHelper addToStringAttributes(final ToStringHelper helper) {
+        return super.addToStringAttributes(helper.add("min", minElements).add("max", maxElements));
     }
 
     private static int numOfChildrenFromValue(final NormalizedNode<?, ?> value) {
index 8d172f5446e9afc4b3c6a0abfa05b184facfd451..e21bd1ae3f4b7b8540e2c7a67064012583659e4b 100644 (file)
@@ -243,7 +243,7 @@ public class ListConstraintsValidation {
             modificationTree.ready();
             fail("Should have failed with IAE");
         } catch (IllegalArgumentException e) {
-            assertEquals("Node (urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model?"
+            assertEquals("(urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model?"
                     + "revision=2015-02-02)min-max-leaf-list has too many elements (4), can have at most 3",
                     e.getMessage());
         }
@@ -298,7 +298,7 @@ public class ListConstraintsValidation {
             modificationTree.ready();
             fail("Should have failed with IAE");
         } catch (IllegalArgumentException e) {
-            assertEquals("Node (urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model?"
+            assertEquals("(urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model?"
                     + "revision=2015-02-02)unkeyed-list has too many elements (2), can have at most 1", e.getMessage());
         }
     }
index 2e8f7d7b9645110059ab0335edae6890d8d77de1..d6295726a3569bf9330e8d2848cf71f51a8cd732 100644 (file)
@@ -114,7 +114,7 @@ public class YT776Test {
             mod.ready();
             fail("Should fail with IAE");
         } catch (IllegalArgumentException e) {
-            assertEquals("Node (yt776)attributes does not have enough elements (0), needs at least 1", e.getMessage());
+            assertEquals("(yt776)attributes does not have enough elements (0), needs at least 1", e.getMessage());
         }
     }
 
@@ -166,7 +166,7 @@ public class YT776Test {
             mod.ready();
             fail("Should fail with IAE");
         } catch (IllegalArgumentException e) {
-            assertEquals("Node (yt776)attributes has too many elements (3), can have at most 2", e.getMessage());
+            assertEquals("(yt776)attributes has too many elements (3), can have at most 2", e.getMessage());
         }
     }