Do not validate non-existent roots 09/75809/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 5 Sep 2018 21:39:18 +0000 (23:39 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 6 Sep 2018 09:28:27 +0000 (11:28 +0200)
If we have no data there is no point to validate anything, and
having the root value as an invariant makes code a lot simpler.

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

yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefValidatation.java

index 10265d3a3d5e334fa6b139c091dece197990245d..3f32ca7b02f0b1a5f7eea2cca995ca1b53a120e9 100644 (file)
@@ -51,19 +51,23 @@ public final class LeafRefValidatation {
 
     private final Set<LeafRefContext> validatedLeafRefCtx = new HashSet<>();
     private final List<String> errorsMessages = new ArrayList<>();
-    private final DataTreeCandidate tree;
+    private final NormalizedNode<?, ?> root;
 
-    private LeafRefValidatation(final DataTreeCandidate tree) {
-        this.tree = tree;
+    private LeafRefValidatation(final NormalizedNode<?, ?> root) {
+        this.root = root;
     }
 
     public static void validate(final DataTreeCandidate tree, final LeafRefContext rootLeafRefCtx)
             throws LeafRefDataValidationFailedException {
-        new LeafRefValidatation(tree).validate0(rootLeafRefCtx);
+        final Optional<NormalizedNode<?, ?>> root = tree.getRootNode().getDataAfter();
+        if (root.isPresent()) {
+            new LeafRefValidatation(root.get()).validateChildren(rootLeafRefCtx, tree.getRootNode().getChildNodes());
+        }
     }
 
-    private void validate0(final LeafRefContext rootLeafRefCtx) throws LeafRefDataValidationFailedException {
-        for (final DataTreeCandidateNode dataTreeCandidateNode : tree.getRootNode().getChildNodes()) {
+    private void validateChildren(final LeafRefContext rootLeafRefCtx, final Collection<DataTreeCandidateNode> children)
+            throws LeafRefDataValidationFailedException {
+        for (final DataTreeCandidateNode dataTreeCandidateNode : children) {
             if (dataTreeCandidateNode.getModificationType() != ModificationType.UNMODIFIED) {
                 final PathArgument identifier = dataTreeCandidateNode.getIdentifier();
                 final QName childQName = identifier.getNodeType();
@@ -352,16 +356,13 @@ public final class LeafRefValidatation {
     }
 
     private Set<Object> extractRootValues(final LeafRefContext context) {
-        return tree.getRootNode().getDataAfter()
-                .map(root -> computeValues(root, context.getLeafRefNodePath().getPathFromRoot(), null))
-                .orElse(ImmutableSet.of());
+        return computeValues(root, context.getLeafRefNodePath().getPathFromRoot(), null);
     }
 
     private void validateLeafRefNodeData(final NormalizedNode<?, ?> leaf, final LeafRefContext referencingCtx,
             final ModificationType modificationType, final YangInstanceIdentifier current) {
-        final Set<Object> values = tree.getRootNode().getDataAfter().map(
-            root -> computeValues(root, referencingCtx.getAbsoluteLeafRefTargetPath().getPathFromRoot(), current))
-                .orElse(ImmutableSet.of());
+        final Set<Object> values = computeValues(root, referencingCtx.getAbsoluteLeafRefTargetPath().getPathFromRoot(),
+            current);
         if (values.contains(leaf.getValue())) {
             LOG.debug("Operation [{}] validate data of LEAFREF node: name[{}] = value[{}] {}", modificationType,
                 referencingCtx.getNodeName(), leaf.getValue(), SUCCESS);
@@ -491,7 +492,7 @@ public final class LeafRefValidatation {
 
     private Set<?> getPathKeyExpressionValues(final LeafRefPath predicatePathKeyExpression,
             final YangInstanceIdentifier current) {
-        return findParentNode(tree.getRootNode().getDataAfter(), current)
+        return findParentNode(Optional.of(root), current)
                 .map(parent -> computeValues(parent, nextLevel(predicatePathKeyExpression.getPathFromRoot()), null))
                 .orElse(ImmutableSet.of());
     }