Add LeafRefValidatation.computeValues() 05/75805/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 5 Sep 2018 21:24:30 +0000 (23:24 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 6 Sep 2018 09:27:49 +0000 (11:27 +0200)
This separates out the heavy-weight value computation, simplifying
callers a bit.

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

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

index bf7f2a74ab5458918263758920d6c45d2d54e947..10265d3a3d5e334fa6b139c091dece197990245d 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.leafref;
 
+import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -41,6 +42,7 @@ import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+// FIXME: 3.0.0: Rename to LeafRefValidation
 public final class LeafRefValidatation {
 
     private static final Logger LOG = LoggerFactory.getLogger(LeafRefValidatation.class);
@@ -350,18 +352,16 @@ public final class LeafRefValidatation {
     }
 
     private Set<Object> extractRootValues(final LeafRefContext context) {
-        final Set<Object> values = new HashSet<>();
-        addValues(values, tree.getRootNode().getDataAfter(), context.getLeafRefNodePath().getPathFromRoot(), null,
-            QNameWithPredicate.ROOT);
-        return values;
+        return tree.getRootNode().getDataAfter()
+                .map(root -> computeValues(root, context.getLeafRefNodePath().getPathFromRoot(), null))
+                .orElse(ImmutableSet.of());
     }
 
     private void validateLeafRefNodeData(final NormalizedNode<?, ?> leaf, final LeafRefContext referencingCtx,
             final ModificationType modificationType, final YangInstanceIdentifier current) {
-        final HashSet<Object> values = new HashSet<>();
-        addValues(values, tree.getRootNode().getDataAfter(),
-            referencingCtx.getAbsoluteLeafRefTargetPath().getPathFromRoot(), current, QNameWithPredicate.ROOT);
-
+        final Set<Object> values = tree.getRootNode().getDataAfter().map(
+            root -> computeValues(root, referencingCtx.getAbsoluteLeafRefTargetPath().getPathFromRoot(), current))
+                .orElse(ImmutableSet.of());
         if (values.contains(leaf.getValue())) {
             LOG.debug("Operation [{}] validate data of LEAFREF node: name[{}] = value[{}] {}", modificationType,
                 referencingCtx.getNodeName(), leaf.getValue(), SUCCESS);
@@ -377,14 +377,16 @@ public final class LeafRefValidatation {
                 referencingCtx.getAbsoluteLeafRefTargetPath()));
     }
 
-    private void addValues(final Set<Object> values, final Optional<? extends NormalizedNode<?, ?>> optDataNode,
+    private Set<Object> computeValues(final NormalizedNode<?, ?> node, final Iterable<QNameWithPredicate> path,
+            final YangInstanceIdentifier current) {
+        final HashSet<Object> values = new HashSet<>();
+        addValues(values, node, path, current, QNameWithPredicate.ROOT);
+        return values;
+    }
+
+    private void addValues(final Set<Object> values, final NormalizedNode<?, ?> node,
             final Iterable<QNameWithPredicate> path, final YangInstanceIdentifier current,
             final QNameWithPredicate previousQName) {
-
-        if (!optDataNode.isPresent()) {
-            return;
-        }
-        final NormalizedNode<?, ?> node = optDataNode.get();
         if (node instanceof ValueNode) {
             values.add(node.getValue());
             return;
@@ -410,11 +412,10 @@ public final class LeafRefValidatation {
                     .getChild(pathArgument);
 
             if (child.isPresent()) {
-                addValues(values, child, nextLevel(path), current, qnameWithPredicate);
+                addValues(values, child.get(), nextLevel(path), current, qnameWithPredicate);
             } else {
                 for (final ChoiceNode choiceNode : getChoiceNodes(dataContainerNode)) {
-                    addValues(values, Optional.of(choiceNode), path, current,
-                            qnameWithPredicate);
+                    addValues(values, choiceNode, path, current, qnameWithPredicate);
                 }
             }
 
@@ -428,10 +429,10 @@ public final class LeafRefValidatation {
                             .getChild(pathArgument);
 
                     if (child.isPresent()) {
-                        addValues(values, child, nextLevel(path), current, qnameWithPredicate);
+                        addValues(values, child.get(), nextLevel(path), current, qnameWithPredicate);
                     } else {
                         for (final ChoiceNode choiceNode : getChoiceNodes(mapEntryNode)) {
-                            addValues(values, Optional.of(choiceNode), path, current, qnameWithPredicate);
+                            addValues(values, choiceNode, path, current, qnameWithPredicate);
                         }
                     }
                 }
@@ -442,11 +443,8 @@ public final class LeafRefValidatation {
                 while (predicates.hasNext()) {
                     final QNamePredicate predicate = predicates.next();
                     final QName identifier = predicate.getIdentifier();
-                    final LeafRefPath predicatePathKeyExpression = predicate
-                            .getPathKeyExpression();
-
-                    final Set<?> pathKeyExprValues = getPathKeyExpressionValues(
-                            predicatePathKeyExpression, current);
+                    final LeafRefPath predicatePathKeyExpression = predicate.getPathKeyExpression();
+                    final Set<?> pathKeyExprValues = getPathKeyExpressionValues(predicatePathKeyExpression, current);
 
                     keyValues.put(identifier, pathKeyExprValues);
                 }
@@ -457,10 +455,10 @@ public final class LeafRefValidatation {
                                 .getChild(pathArgument);
 
                         if (child.isPresent()) {
-                            addValues(values, child, nextLevel(path), current, qnameWithPredicate);
+                            addValues(values, child.get(), nextLevel(path), current, qnameWithPredicate);
                         } else {
                             for (final ChoiceNode choiceNode : getChoiceNodes(mapEntryNode)) {
-                                addValues(values, Optional.of(choiceNode),  path, current, qnameWithPredicate);
+                                addValues(values, choiceNode,  path, current, qnameWithPredicate);
                             }
                         }
                     }
@@ -493,12 +491,9 @@ public final class LeafRefValidatation {
 
     private Set<?> getPathKeyExpressionValues(final LeafRefPath predicatePathKeyExpression,
             final YangInstanceIdentifier current) {
-        final Optional<NormalizedNode<?, ?>> parent = findParentNode(tree.getRootNode().getDataAfter(), current);
-        final Set<Object> values = new HashSet<>();
-        addValues(values, parent, nextLevel(predicatePathKeyExpression.getPathFromRoot()), null,
-            QNameWithPredicate.ROOT);
-
-        return values;
+        return findParentNode(tree.getRootNode().getDataAfter(), current)
+                .map(parent -> computeValues(parent, nextLevel(predicatePathKeyExpression.getPathFromRoot()), null))
+                .orElse(ImmutableSet.of());
     }
 
     private static Optional<NormalizedNode<?, ?>> findParentNode(