Untangle result processing logic. 01/75801/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 5 Sep 2018 19:33:55 +0000 (21:33 +0200)
committerRobert Varga <nite@hq.sk>
Thu, 6 Sep 2018 08:39:25 +0000 (08:39 +0000)
leafRefTargetNodeDataLog() is needlessly complex, where it is called
with 3 different sets of arguments from validateLeafRefTargetNodeData()
only.

Inline two of those callers, reducing nesting significantly and
making the code flow more obvious.

Change-Id: I68205d58677e2392989a5fc064a986f5c1716666
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit a538bafb5db59101206a5d1fd2a1d31e79c08de0)

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

index d62c359d24f178635a1c74d5f9b7e0a5a888107d..5af7fb3b26c7b8e6f8fd1df1d2363dfaf1066f56 100644 (file)
@@ -313,12 +313,13 @@ public final class LeafRefValidatation {
 
     private void validateLeafRefTargetNodeData(final NormalizedNode<?, ?> leaf, final LeafRefContext
             referencedByCtx, final ModificationType modificationType) {
-        final Map<LeafRefContext, Set<?>> leafRefsValues = new HashMap<>();
         if (validatedLeafRefCtx.contains(referencedByCtx)) {
-            leafRefTargetNodeDataLog(leaf, referencedByCtx, modificationType, leafRefsValues, null);
+            LOG.trace("Operation [{}] validate data of leafref TARGET node: name[{}] = value[{}] -> SKIP: Already "
+                    + "validated", modificationType, referencedByCtx.getNodeName(), leaf.getValue());
             return;
         }
 
+        final Map<LeafRefContext, Set<?>> leafRefsValues = new HashMap<>();
         for (final LeafRefContext leafRefContext : referencedByCtx.getAllReferencedByLeafRefCtxs().values()) {
             if (leafRefContext.isReferencing()) {
                 leafRefsValues.put(leafRefContext, extractRootValues(leafRefContext));
@@ -328,8 +329,6 @@ public final class LeafRefValidatation {
         if (!leafRefsValues.isEmpty()) {
             final Set<Object> values = extractRootValues(referencedByCtx);
             leafRefTargetNodeDataLog(leaf, referencedByCtx, modificationType, leafRefsValues, values);
-        } else {
-            leafRefTargetNodeDataLog(leaf, referencedByCtx, modificationType, null, null);
         }
         validatedLeafRefCtx.add(referencedByCtx);
     }
@@ -345,32 +344,25 @@ public final class LeafRefValidatation {
             final ModificationType modificationType, final Map<LeafRefContext, Set<?>> leafRefsValues,
             final Set<Object> leafRefTargetNodeValues) {
 
-        if (leafRefsValues != null && !leafRefsValues.isEmpty()) {
-            final Set<Entry<LeafRefContext, Set<?>>> entrySet = leafRefsValues.entrySet();
-            LOG.debug("Operation [{}] validate data of leafref TARGET node: name[{}] = value[{}]",
-                    modificationType, referencedByCtx.getNodeName(), leaf.getValue());
-            for (final Entry<LeafRefContext, Set<?>> entry : entrySet) {
-                final LeafRefContext leafRefContext = entry.getKey();
-                final Set<?> leafRefValuesSet = entry.getValue();
-                for (final Object leafRefsValue : leafRefValuesSet) {
-                    if (leafRefTargetNodeValues != null && !leafRefTargetNodeValues.contains(leafRefsValue)) {
-                        LOG.debug("Invalid leafref value [{}] allowed values {} by validation of leafref TARGET node: "
-                                + "{} path of invalid LEAFREF node: {} leafRef target path: {} {}", leafRefsValue,
-                                leafRefTargetNodeValues, leaf.getNodeType(), leafRefContext.getCurrentNodePath(),
-                                leafRefContext.getAbsoluteLeafRefTargetPath(), FAILED);
-                        errorsMessages.add(String.format("Invalid leafref value [%s] allowed values %s by validation "
-                                        + "of leafref TARGET node: %s path of invalid LEAFREF node: %s leafRef target "
-                                        + "path: %s %s", leafRefsValue, leafRefTargetNodeValues, leaf.getNodeType(),
-                                leafRefContext.getCurrentNodePath(), leafRefContext.getAbsoluteLeafRefTargetPath(),
-                                FAILED));
-                    } else {
-                        LOG.debug("Valid leafref value [{}] {}", leafRefsValue, SUCCESS);
-                    }
+        LOG.debug("Operation [{}] validate data of leafref TARGET node: name[{}] = value[{}]", modificationType,
+            referencedByCtx.getNodeName(), leaf.getValue());
+        for (final Entry<LeafRefContext, Set<?>> entry : leafRefsValues.entrySet()) {
+            for (final Object leafRefsValue : entry.getValue()) {
+                if (!leafRefTargetNodeValues.contains(leafRefsValue)) {
+                    final LeafRefContext leafRefContext = entry.getKey();
+                    LOG.debug("Invalid leafref value [{}] allowed values {} by validation of leafref TARGET node: "
+                            + "{} path of invalid LEAFREF node: {} leafRef target path: {} {}", leafRefsValue,
+                            leafRefTargetNodeValues, leaf.getNodeType(), leafRefContext.getCurrentNodePath(),
+                            leafRefContext.getAbsoluteLeafRefTargetPath(), FAILED);
+                    errorsMessages.add(String.format("Invalid leafref value [%s] allowed values %s by validation of "
+                            + "leafref TARGET node: %s path of invalid LEAFREF node: %s leafRef target path: %s %s",
+                            leafRefsValue, leafRefTargetNodeValues, leaf.getNodeType(),
+                            leafRefContext.getCurrentNodePath(), leafRefContext.getAbsoluteLeafRefTargetPath(),
+                            FAILED));
+                } else {
+                    LOG.trace("Valid leafref value [{}] {}", leafRefsValue, SUCCESS);
                 }
             }
-        } else if (leafRefsValues != null) {
-            LOG.debug("Operation [{}] validate data of leafref TARGET node: name[{}] = value[{}] "
-                    + "-> SKIP: Already validated", modificationType, referencedByCtx.getNodeName(), leaf.getValue());
         }
     }
 
@@ -382,18 +374,19 @@ public final class LeafRefValidatation {
 
         addValues(values, tree.getRootNode().getDataAfter(), pathFromRoot, current, QNameWithPredicate.ROOT);
 
-        if (!values.contains(leaf.getValue())) {
-            LOG.debug("Operation [{}] validate data of LEAFREF node: name[{}] = value[{}] {}",
-                    modificationType, referencingCtx.getNodeName(), leaf.getValue(), FAILED);
-            LOG.debug("Invalid leafref value [{}] allowed values {} of LEAFREF node: {} leafRef target path: {}",
-                    leaf.getValue(), values, leaf.getNodeType(), referencingCtx.getAbsoluteLeafRefTargetPath());
-            errorsMessages.add(String.format("Invalid leafref value [%s] allowed values %s of LEAFREF node: %s "
-                            + "leafRef target path: %s", leaf.getValue(), values, leaf.getNodeType(), referencingCtx
-                    .getAbsoluteLeafRefTargetPath()));
-        } else {
+        if (values.contains(leaf.getValue())) {
             LOG.debug("Operation [{}] validate data of LEAFREF node: name[{}] = value[{}] {}", modificationType,
-                    referencingCtx.getNodeName(), leaf.getValue(), SUCCESS);
+                referencingCtx.getNodeName(), leaf.getValue(), SUCCESS);
+            return;
         }
+
+        LOG.debug("Operation [{}] validate data of LEAFREF node: name[{}] = value[{}] {}", modificationType,
+            referencingCtx.getNodeName(), leaf.getValue(), FAILED);
+        LOG.debug("Invalid leafref value [{}] allowed values {} of LEAFREF node: {} leafRef target path: {}",
+            leaf.getValue(), values, leaf.getNodeType(), referencingCtx.getAbsoluteLeafRefTargetPath());
+        errorsMessages.add(String.format("Invalid leafref value [%s] allowed values %s of LEAFREF node: %s leafRef "
+                + "target path: %s", leaf.getValue(), values, leaf.getNodeType(),
+                referencingCtx.getAbsoluteLeafRefTargetPath()));
     }
 
     private void addValues(final Set<Object> values, final Optional<? extends NormalizedNode<?, ?>> optDataNode,