Untangle result processing logic.
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / leafref / LeafRefValidatation.java
index 42006157e59916fc5071c588df53176a5beaf901..5af7fb3b26c7b8e6f8fd1df1d2363dfaf1066f56 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -7,47 +7,48 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.leafref;
 
-import com.google.common.base.Optional;
 import com.google.common.collect.Iterables;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Optional;
 import java.util.Set;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
-import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.ValueNode;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class LeafRefValidatation {
+public final class LeafRefValidatation {
 
     private static final Logger LOG = LoggerFactory.getLogger(LeafRefValidatation.class);
-    private static final String NEW_LINE = System.getProperty("line.separator");
+    private static final String FAILED = " -> FAILED";
+    private static final String SUCCESS = " -> OK";
 
+    private final Set<LeafRefContext> validatedLeafRefCtx = new HashSet<>();
+    private final List<String> errorsMessages = new ArrayList<>();
     private final DataTreeCandidate tree;
-    private final LinkedList<String> errorsMessages =  new LinkedList<String>();
-    private final HashSet<LeafRefContext> validatedLeafRefCtx =  new HashSet<LeafRefContext>();
 
     private LeafRefValidatation(final DataTreeCandidate tree) {
         this.tree = tree;
@@ -57,33 +58,21 @@ public class LeafRefValidatation {
             throws LeafRefDataValidationFailedException {
         new LeafRefValidatation(tree).validate0(rootLeafRefCtx);
     }
-    private void validate0(final LeafRefContext rootLeafRefCtx)
-            throws LeafRefDataValidationFailedException {
-
-        final DataTreeCandidateNode rootNode = tree.getRootNode();
-
-        final Collection<DataTreeCandidateNode> childNodes = rootNode.getChildNodes();
-        for (final DataTreeCandidateNode dataTreeCandidateNode : childNodes) {
-
-            final ModificationType modificationType = dataTreeCandidateNode
-                    .getModificationType();
-            if (modificationType != ModificationType.UNMODIFIED) {
 
+    private void validate0(final LeafRefContext rootLeafRefCtx) throws LeafRefDataValidationFailedException {
+        for (final DataTreeCandidateNode dataTreeCandidateNode : tree.getRootNode().getChildNodes()) {
+            if (dataTreeCandidateNode.getModificationType() != ModificationType.UNMODIFIED) {
                 final PathArgument identifier = dataTreeCandidateNode.getIdentifier();
                 final QName childQName = identifier.getNodeType();
 
-                final LeafRefContext referencedByCtx = rootLeafRefCtx
-                        .getReferencedChildByName(childQName);
-                final LeafRefContext referencingCtx = rootLeafRefCtx
-                        .getReferencingChildByName(childQName);
+                final LeafRefContext referencedByCtx = rootLeafRefCtx.getReferencedChildByName(childQName);
+                final LeafRefContext referencingCtx = rootLeafRefCtx.getReferencingChildByName(childQName);
                 if (referencedByCtx != null || referencingCtx != null) {
                     final YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier
                             .create(dataTreeCandidateNode.getIdentifier());
-                    validateNode(dataTreeCandidateNode, referencedByCtx,
-                            referencingCtx, yangInstanceIdentifier);
+                    validateNode(dataTreeCandidateNode, referencedByCtx, referencingCtx, yangInstanceIdentifier);
                 }
             }
-
         }
 
         if (!errorsMessages.isEmpty()) {
@@ -93,18 +82,14 @@ public class LeafRefValidatation {
                 message.append(errorMessage);
                 errCount++;
             }
-            throw new LeafRefDataValidationFailedException(message.toString(),
-                    errCount);
+            throw new LeafRefDataValidationFailedException(message.toString(), errCount);
         }
-
     }
 
-    private void validateNode(final DataTreeCandidateNode node,
-            final LeafRefContext referencedByCtx, final LeafRefContext referencingCtx,
-            final YangInstanceIdentifier current) {
+    private void validateNode(final DataTreeCandidateNode node, final LeafRefContext referencedByCtx,
+        final LeafRefContext referencingCtx, final YangInstanceIdentifier current) {
 
-        if ((node.getModificationType() == ModificationType.WRITE)
-                && node.getDataAfter().isPresent()) {
+        if (node.getModificationType() == ModificationType.WRITE && node.getDataAfter().isPresent()) {
             final Optional<NormalizedNode<?, ?>> dataAfter = node.getDataAfter();
             final NormalizedNode<?, ?> normalizedNode = dataAfter.get();
             validateNodeData(normalizedNode, referencedByCtx, referencingCtx,
@@ -112,8 +97,7 @@ public class LeafRefValidatation {
             return;
         }
 
-        if (node.getModificationType() == ModificationType.DELETE
-                && referencedByCtx != null) {
+        if (node.getModificationType() == ModificationType.DELETE && referencedByCtx != null) {
             final Optional<NormalizedNode<?, ?>> dataBefor = node.getDataBefore();
             final NormalizedNode<?, ?> normalizedNode = dataBefor.get();
             validateNodeData(normalizedNode, referencedByCtx, null,
@@ -123,108 +107,83 @@ public class LeafRefValidatation {
 
         final Collection<DataTreeCandidateNode> childNodes = node.getChildNodes();
         for (final DataTreeCandidateNode childNode : childNodes) {
-            final ModificationType modificationType = childNode.getModificationType();
-
-            if (modificationType != ModificationType.UNMODIFIED) {
-
-                final LeafRefContext childReferencedByCtx = getReferencedByCtxChild(
-                        referencedByCtx, childNode);
-                final LeafRefContext childReferencingCtx = getReferencingCtxChild(
-                        referencingCtx, childNode);
+            if (childNode.getModificationType() != ModificationType.UNMODIFIED) {
+                final LeafRefContext childReferencedByCtx = getReferencedByCtxChild(referencedByCtx, childNode);
+                final LeafRefContext childReferencingCtx = getReferencingCtxChild(referencingCtx, childNode);
 
                 if (childReferencedByCtx != null || childReferencingCtx != null) {
-                    final YangInstanceIdentifier childYangInstanceIdentifier = current
-                            .node(childNode.getIdentifier());
-                    validateNode(childNode, childReferencedByCtx,
-                            childReferencingCtx, childYangInstanceIdentifier);
+                    final YangInstanceIdentifier childYangInstanceIdentifier = current.node(childNode.getIdentifier());
+                    validateNode(childNode, childReferencedByCtx,childReferencingCtx, childYangInstanceIdentifier);
                 }
             }
-
         }
-
     }
 
-    private LeafRefContext getReferencingCtxChild(
-            final LeafRefContext referencingCtx, final DataTreeCandidateNode childNode) {
-
-        LeafRefContext childReferencingCtx = null;
-        if (referencingCtx != null) {
-            final PathArgument identifier = childNode.getIdentifier();
-            final QName childQName = identifier.getNodeType();
-
-            childReferencingCtx = referencingCtx
-                    .getReferencingChildByName(childQName);
+    private static LeafRefContext getReferencingCtxChild(final LeafRefContext referencingCtx,
+            final DataTreeCandidateNode childNode) {
+        if (referencingCtx == null) {
+            return null;
+        }
 
-            if (childReferencingCtx == null) {
-                final NormalizedNode<?, ?> data = childNode.getDataAfter().get();
-                if (data instanceof MapEntryNode
-                        || data instanceof UnkeyedListEntryNode) {
-                    childReferencingCtx = referencingCtx;
-                }
+        final QName childQName = childNode.getIdentifier().getNodeType();
+        LeafRefContext childReferencingCtx = referencingCtx.getReferencingChildByName(childQName);
+        if (childReferencingCtx == null) {
+            final NormalizedNode<?, ?> data = childNode.getDataAfter().get();
+            if (data instanceof MapEntryNode || data instanceof UnkeyedListEntryNode) {
+                childReferencingCtx = referencingCtx;
             }
         }
 
         return childReferencingCtx;
     }
 
-    private LeafRefContext getReferencedByCtxChild(
-            final LeafRefContext referencedByCtx, final DataTreeCandidateNode childNode) {
-
-        LeafRefContext childReferencedByCtx = null;
-        if (referencedByCtx != null) {
-            final PathArgument identifier = childNode.getIdentifier();
-            final QName childQName = identifier.getNodeType();
-
-            childReferencedByCtx = referencedByCtx
-                    .getReferencedChildByName(childQName);
-            if (childReferencedByCtx == null) {
-                final NormalizedNode<?, ?> data = childNode.getDataAfter().get();
-                if (data instanceof MapEntryNode
-                        || data instanceof UnkeyedListEntryNode) {
-                    childReferencedByCtx = referencedByCtx;
-                }
+    private static LeafRefContext getReferencedByCtxChild(final LeafRefContext referencedByCtx,
+            final DataTreeCandidateNode childNode) {
+        if (referencedByCtx == null) {
+            return null;
+        }
+
+        final QName childQName = childNode.getIdentifier().getNodeType();
+        LeafRefContext childReferencedByCtx = referencedByCtx.getReferencedChildByName(childQName);
+        if (childReferencedByCtx == null) {
+            final NormalizedNode<?, ?> data = childNode.getDataAfter().get();
+            if (data instanceof MapEntryNode || data instanceof UnkeyedListEntryNode) {
+                childReferencedByCtx = referencedByCtx;
             }
         }
 
         return childReferencedByCtx;
     }
 
-    private void validateNodeData(final NormalizedNode<?, ?> node,
-            final LeafRefContext referencedByCtx, final LeafRefContext referencingCtx,
-            final ModificationType modificationType, final YangInstanceIdentifier current) {
+    private void validateNodeData(final NormalizedNode<?, ?> node, final LeafRefContext referencedByCtx,
+            final LeafRefContext referencingCtx, final ModificationType modificationType,
+            final YangInstanceIdentifier current) {
 
         if (node instanceof LeafNode) {
             final LeafNode<?> leaf = (LeafNode<?>) node;
 
             if (referencedByCtx != null && referencedByCtx.isReferenced()) {
-                validateLeafRefTargetNodeData(leaf, referencedByCtx,
-                        modificationType);
+                validateLeafRefTargetNodeData(leaf, referencedByCtx, modificationType);
             }
             if (referencingCtx != null && referencingCtx.isReferencing()) {
-                validateLeafRefNodeData(leaf, referencingCtx, modificationType,
-                        current);
+                validateLeafRefNodeData(leaf, referencingCtx, modificationType, current);
             }
 
             return;
         }
 
         if (node instanceof LeafSetNode) {
-            final LeafSetNode<?> leafSet = (LeafSetNode<?>) node;
-
             if (referencedByCtx == null && referencingCtx == null) {
                 return;
             }
 
-            final Iterable<? extends NormalizedNode<?, ?>> leafSetEntries = leafSet
-                    .getValue();
-            for (final NormalizedNode<?, ?> leafSetEntry : leafSetEntries) {
+            final LeafSetNode<?> leafSet = (LeafSetNode<?>) node;
+            for (final NormalizedNode<?, ?> leafSetEntry : leafSet.getValue()) {
                 if (referencedByCtx != null && referencedByCtx.isReferenced()) {
-                    validateLeafRefTargetNodeData(leafSetEntry,
-                            referencedByCtx, modificationType);
+                    validateLeafRefTargetNodeData(leafSetEntry, referencedByCtx, modificationType);
                 }
                 if (referencingCtx != null && referencingCtx.isReferencing()) {
-                    validateLeafRefNodeData(leafSetEntry, referencingCtx,
-                            modificationType, current);
+                    validateLeafRefNodeData(leafSetEntry, referencingCtx, modificationType, current);
                 }
             }
 
@@ -233,100 +192,104 @@ public class LeafRefValidatation {
 
         if (node instanceof ChoiceNode) {
             final ChoiceNode choice = (ChoiceNode) node;
-            final Iterable<DataContainerChild<? extends PathArgument, ?>> childs = choice
-                    .getValue();
-            for (final DataContainerChild<? extends PathArgument, ?> dataContainerChild : childs) {
+            for (final DataContainerChild<? extends PathArgument, ?> dataContainerChild : choice.getValue()) {
                 final QName qname = dataContainerChild.getNodeType();
 
-                LeafRefContext childReferencedByCtx = null;
-                LeafRefContext childReferencingCtx = null;
+                final LeafRefContext childReferencedByCtx;
                 if (referencedByCtx != null) {
-                    childReferencedByCtx = findReferencedByCtxUnderChoice(
-                            referencedByCtx, qname);
+                    childReferencedByCtx = findReferencedByCtxUnderChoice(referencedByCtx, qname);
+                } else {
+                    childReferencedByCtx = null;
                 }
+
+                final LeafRefContext childReferencingCtx;
                 if (referencingCtx != null) {
-                    childReferencingCtx = findReferencingCtxUnderChoice(
-                            referencingCtx, qname);
+                    childReferencingCtx = findReferencingCtxUnderChoice(referencingCtx, qname);
+                } else {
+                    childReferencingCtx = null;
                 }
+
                 if (childReferencedByCtx != null || childReferencingCtx != null) {
                     final YangInstanceIdentifier childYangInstanceIdentifier = current
                             .node(dataContainerChild.getIdentifier());
                     validateNodeData(dataContainerChild, childReferencedByCtx,
-                            childReferencingCtx, modificationType,
-                            childYangInstanceIdentifier);
+                            childReferencingCtx, modificationType, childYangInstanceIdentifier);
                 }
             }
         } else if (node instanceof DataContainerNode) {
             final DataContainerNode<?> dataContainerNode = (DataContainerNode<?>) node;
-            final Iterable<DataContainerChild<? extends PathArgument, ?>> dataContainerChilds = dataContainerNode
-                    .getValue();
 
-            for (final DataContainerChild<? extends PathArgument, ?> dataContainerChild : dataContainerChilds) {
-                final QName qname = dataContainerChild.getNodeType();
+            for (final DataContainerChild<? extends PathArgument, ?> child : dataContainerNode.getValue()) {
+                if (child instanceof AugmentationNode) {
+                    validateNodeData(child, referencedByCtx, referencingCtx, modificationType, current
+                        .node(child.getIdentifier()));
+                    return;
+                }
 
-                LeafRefContext childReferencedByCtx = null;
-                LeafRefContext childReferencingCtx = null;
+                final QName qname = child.getNodeType();
+                final LeafRefContext childReferencedByCtx;
                 if (referencedByCtx != null) {
-                    childReferencedByCtx = referencedByCtx
-                            .getReferencedChildByName(qname);
+                    childReferencedByCtx = referencedByCtx.getReferencedChildByName(qname);
+                } else {
+                    childReferencedByCtx = null;
                 }
+
+                final LeafRefContext childReferencingCtx;
                 if (referencingCtx != null) {
-                    childReferencingCtx = referencingCtx
-                            .getReferencingChildByName(qname);
+                    childReferencingCtx = referencingCtx.getReferencingChildByName(qname);
+                } else {
+                    childReferencingCtx = null;
                 }
+
                 if (childReferencedByCtx != null || childReferencingCtx != null) {
                     final YangInstanceIdentifier childYangInstanceIdentifier = current
-                            .node(dataContainerChild.getIdentifier());
-                    validateNodeData(dataContainerChild, childReferencedByCtx,
-                            childReferencingCtx, modificationType,
-                            childYangInstanceIdentifier);
+                            .node(child.getIdentifier());
+                    validateNodeData(child, childReferencedByCtx,
+                            childReferencingCtx, modificationType, childYangInstanceIdentifier);
                 }
             }
         } else if (node instanceof MapNode) {
             final MapNode map = (MapNode) node;
-            final Iterable<MapEntryNode> mapEntries = map.getValue();
-            for (final MapEntryNode mapEntry : mapEntries) {
-                final Iterable<DataContainerChild<? extends PathArgument, ?>> mapEntryNodes = mapEntry
-                        .getValue();
-                final YangInstanceIdentifier mapEntryYangInstanceIdentifier = current
-                        .node(mapEntry.getIdentifier());
-                for (final DataContainerChild<? extends PathArgument, ?> mapEntryNode : mapEntryNodes) {
-                    final QName qname = mapEntryNode.getNodeType();
 
-                    LeafRefContext childReferencedByCtx = null;
-                    LeafRefContext childReferencingCtx = null;
+            for (final MapEntryNode mapEntry : map.getValue()) {
+                final YangInstanceIdentifier mapEntryYangInstanceIdentifier = current.node(mapEntry.getIdentifier());
+                for (final DataContainerChild<? extends PathArgument, ?> mapEntryNode : mapEntry.getValue()) {
+                    if (mapEntryNode instanceof AugmentationNode) {
+                        validateNodeData(mapEntryNode, referencedByCtx, referencingCtx, modificationType, current
+                            .node(mapEntryNode.getIdentifier()));
+                        return;
+                    }
+
+                    final QName qname = mapEntryNode.getNodeType();
+                    final LeafRefContext childReferencedByCtx;
                     if (referencedByCtx != null) {
-                        childReferencedByCtx = referencedByCtx
-                                .getReferencedChildByName(qname);
+                        childReferencedByCtx = referencedByCtx.getReferencedChildByName(qname);
+                    } else {
+                        childReferencedByCtx = null;
                     }
+
+                    final LeafRefContext childReferencingCtx;
                     if (referencingCtx != null) {
-                        childReferencingCtx = referencingCtx
-                                .getReferencingChildByName(qname);
+                        childReferencingCtx = referencingCtx.getReferencingChildByName(qname);
+                    } else {
+                        childReferencingCtx = null;
                     }
-                    if (childReferencedByCtx != null
-                            || childReferencingCtx != null) {
-                        final YangInstanceIdentifier mapEntryNodeYangInstanceIdentifier = mapEntryYangInstanceIdentifier
-                                .node(mapEntryNode.getIdentifier());
-                        validateNodeData(mapEntryNode, childReferencedByCtx,
-                                childReferencingCtx, modificationType,
-                                mapEntryNodeYangInstanceIdentifier);
+
+                    if (childReferencedByCtx != null || childReferencingCtx != null) {
+                        validateNodeData(mapEntryNode, childReferencedByCtx, childReferencingCtx, modificationType,
+                                mapEntryYangInstanceIdentifier.node(mapEntryNode.getIdentifier()));
                     }
                 }
             }
-
         }
-        // FIXME if(node instance of UnkeyedListNode ...
+        // FIXME: check UnkeyedListNode case
     }
 
-    private LeafRefContext findReferencingCtxUnderChoice(
+    private static LeafRefContext findReferencingCtxUnderChoice(
             final LeafRefContext referencingCtx, final QName qname) {
 
-        final Map<QName, LeafRefContext> referencingChilds = referencingCtx
-                .getReferencingChilds();
-        final Set<Entry<QName, LeafRefContext>> childs = referencingChilds.entrySet();
-        for (final Entry<QName, LeafRefContext> child : childs) {
-            final LeafRefContext referencingChildByName = child.getValue()
-                    .getReferencingChildByName(qname);
+        for (final LeafRefContext child : referencingCtx.getReferencingChilds().values()) {
+            final LeafRefContext referencingChildByName = child.getReferencingChildByName(qname);
             if (referencingChildByName != null) {
                 return referencingChildByName;
             }
@@ -335,16 +298,11 @@ public class LeafRefValidatation {
         return null;
     }
 
-    private LeafRefContext findReferencedByCtxUnderChoice(
+    private static LeafRefContext findReferencedByCtxUnderChoice(
             final LeafRefContext referencedByCtx, final QName qname) {
 
-        final Map<QName, LeafRefContext> referencedByChilds = referencedByCtx
-                .getReferencedByChilds();
-        final Set<Entry<QName, LeafRefContext>> childs = referencedByChilds
-                .entrySet();
-        for (final Entry<QName, LeafRefContext> child : childs) {
-            final LeafRefContext referencedByChildByName = child.getValue()
-                    .getReferencedChildByName(qname);
+        for (final LeafRefContext child : referencedByCtx.getReferencedByChilds().values()) {
+            final LeafRefContext referencedByChildByName = child.getReferencedChildByName(qname);
             if (referencedByChildByName != null) {
                 return referencedByChildByName;
             }
@@ -353,162 +311,85 @@ public class LeafRefValidatation {
         return null;
     }
 
-    @SuppressWarnings("rawtypes")
-    private void validateLeafRefTargetNodeData(final NormalizedNode<?, ?> leaf,
-            final LeafRefContext referencedByCtx, final ModificationType modificationType) {
-
-        final StringBuilder header_log = new StringBuilder();
-        final StringBuilder log = new StringBuilder();
-        header_log.append("Operation [" + modificationType
-                + "] validate data of leafref TARGET node: name["
-                + referencedByCtx.getNodeName() + "] = value["
-                + leaf.getValue() + "]");
-
+    private void validateLeafRefTargetNodeData(final NormalizedNode<?, ?> leaf, final LeafRefContext
+            referencedByCtx, final ModificationType modificationType) {
         if (validatedLeafRefCtx.contains(referencedByCtx)) {
-            header_log.append(" -> SKIP: Already validated");
-            LOG.debug(header_log.toString());
+            LOG.trace("Operation [{}] validate data of leafref TARGET node: name[{}] = value[{}] -> SKIP: Already "
+                    + "validated", modificationType, referencedByCtx.getNodeName(), leaf.getValue());
             return;
         }
 
-        final Map<QName, LeafRefContext> allReferencedByLeafRefCtxs = referencedByCtx
-                .getAllReferencedByLeafRefCtxs();
-
-        final HashMap<LeafRefContext, HashSet> leafRefsValues = new HashMap<LeafRefContext, HashSet>();
-        final Collection<LeafRefContext> leafrefs = allReferencedByLeafRefCtxs
-                .values();
-        for (final LeafRefContext leafRefContext : leafrefs) {
+        final Map<LeafRefContext, Set<?>> leafRefsValues = new HashMap<>();
+        for (final LeafRefContext leafRefContext : referencedByCtx.getAllReferencedByLeafRefCtxs().values()) {
             if (leafRefContext.isReferencing()) {
-                final HashSet<Object> values = new HashSet<>();
-
-                final SchemaPath leafRefNodeSchemaPath = leafRefContext
-                        .getCurrentNodePath();
-                final LeafRefPath leafRefNodePath = LeafRefUtils
-                        .schemaPathToLeafRefPath(leafRefNodeSchemaPath,
-                                leafRefContext.getLeafRefContextModule());
-                final Iterable<QNameWithPredicate> pathFromRoot = leafRefNodePath
-                        .getPathFromRoot();
-                addValues(values, tree.getRootNode().getDataAfter(),
-                        pathFromRoot, null, QNameWithPredicate.ROOT);
-                leafRefsValues.put(leafRefContext, values);
+                leafRefsValues.put(leafRefContext, extractRootValues(leafRefContext));
             }
         }
 
-        final HashSet<Object> leafRefTargetNodeValues = new HashSet<>();
-        final SchemaPath nodeSchemaPath = referencedByCtx.getCurrentNodePath();
-        final LeafRefPath nodePath = LeafRefUtils.schemaPathToLeafRefPath(
-                nodeSchemaPath, referencedByCtx.getLeafRefContextModule());
-        addValues(leafRefTargetNodeValues, tree.getRootNode().getDataAfter(),
-                nodePath.getPathFromRoot(), null, QNameWithPredicate.ROOT);
-
-        boolean valid = true;
-        final Set<Entry<LeafRefContext, HashSet>> entrySet = leafRefsValues
-                .entrySet();
-        for (final Entry<LeafRefContext, HashSet> entry : entrySet) {
-            final LeafRefContext leafRefContext = entry.getKey();
-            final HashSet leafRefValuesSet = entry.getValue();
-            for (final Object leafRefsValue : leafRefValuesSet) {
-                if (!leafRefTargetNodeValues.contains(leafRefsValue)) {
+        if (!leafRefsValues.isEmpty()) {
+            final Set<Object> values = extractRootValues(referencedByCtx);
+            leafRefTargetNodeDataLog(leaf, referencedByCtx, modificationType, leafRefsValues, values);
+        }
+        validatedLeafRefCtx.add(referencedByCtx);
+    }
 
-                    final StringBuilder sb = createInvalidTargetMessage(leaf,
-                            leafRefTargetNodeValues, leafRefContext,
-                            leafRefsValue);
-                    log.append(NEW_LINE);
-                    log.append(sb.toString());
-                    log.append(" -> FAILED");
+    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;
+    }
 
-                    sb.append(NEW_LINE);
-                    errorsMessages.add(sb.toString());
+    private void leafRefTargetNodeDataLog(final NormalizedNode<?, ?> leaf, final LeafRefContext referencedByCtx,
+            final ModificationType modificationType, final Map<LeafRefContext, Set<?>> leafRefsValues,
+            final Set<Object> leafRefTargetNodeValues) {
 
-                    valid = false;
+        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.append(NEW_LINE);
-                    log.append("Valid leafref value [");
-                    log.append(leafRefsValue);
-                    log.append("]");
-                    log.append(" -> OK");
+                    LOG.trace("Valid leafref value [{}] {}", leafRefsValue, SUCCESS);
                 }
             }
         }
-
-        header_log.append(valid ? " -> OK" : " -> FAILED");
-        LOG.debug(header_log.append(log.toString()).toString());
-
-        validatedLeafRefCtx.add(referencedByCtx);
     }
 
-    private StringBuilder createInvalidTargetMessage(final NormalizedNode<?, ?> leaf,
-            final HashSet<?> leafRefTargetNodeValues, final LeafRefContext leafRefContext,
-            final Object leafRefsValue) {
-        final StringBuilder sb = new StringBuilder();
-        sb.append("Invalid leafref value [");
-        sb.append(leafRefsValue);
-        sb.append("]");
-        sb.append(" allowed values ");
-        sb.append(leafRefTargetNodeValues);
-        sb.append(" by validation of leafref TARGET node: ");
-        sb.append(leaf.getNodeType());
-        sb.append(" path of invalid LEAFREF node: ");
-        sb.append(leafRefContext.getCurrentNodePath());
-        sb.append(" leafRef target path: ");
-        sb.append(leafRefContext.getAbsoluteLeafRefTargetPath());
-        return sb;
-    }
-
-    private void validateLeafRefNodeData(final NormalizedNode<?, ?> leaf,
-            final LeafRefContext referencingCtx, final ModificationType modificationType,
-            final YangInstanceIdentifier current) {
-
-        final StringBuilder header_log = new StringBuilder();
-        final StringBuilder log = new StringBuilder();
-
-        header_log.append("Operation [" + modificationType
-                + "] validate data of LEAFREF node: name["
-                + referencingCtx.getNodeName() + "] = value["
-                + leaf.getValue() + "]");
-
+    private void validateLeafRefNodeData(final NormalizedNode<?, ?> leaf, final LeafRefContext referencingCtx,
+            final ModificationType modificationType, final YangInstanceIdentifier current) {
         final HashSet<Object> values = new HashSet<>();
         final LeafRefPath targetPath = referencingCtx.getAbsoluteLeafRefTargetPath();
-        final Iterable<QNameWithPredicate> pathFromRoot = targetPath
-                .getPathFromRoot();
-
-        addValues(values, tree.getRootNode().getDataAfter(), pathFromRoot,
-                current, QNameWithPredicate.ROOT);
+        final Iterable<QNameWithPredicate> pathFromRoot = targetPath.getPathFromRoot();
 
-        if (!values.contains(leaf.getValue())) {
-            final StringBuilder sb = createInvalidLeafRefMessage(leaf,
-                    referencingCtx, values);
-            errorsMessages.add(sb.toString());
+        addValues(values, tree.getRootNode().getDataAfter(), pathFromRoot, current, QNameWithPredicate.ROOT);
 
-            header_log.append(" -> FAILED");
-            log.append(sb.toString());
-        } else {
-            header_log.append(" -> OK");
+        if (values.contains(leaf.getValue())) {
+            LOG.debug("Operation [{}] validate data of LEAFREF node: name[{}] = value[{}] {}", modificationType,
+                referencingCtx.getNodeName(), leaf.getValue(), SUCCESS);
+            return;
         }
 
-        LOG.debug(header_log.toString());
-        if (!log.toString().equals(""))
-            LOG.debug(log.toString());
-    }
-
-    private StringBuilder createInvalidLeafRefMessage(
-            final NormalizedNode<?, ?> leaf, final LeafRefContext referencingCtx,
-            final Set<?> values) {
-        final StringBuilder sb = new StringBuilder();
-        sb.append("Invalid leafref value [");
-        sb.append(leaf.getValue());
-        sb.append("]");
-        sb.append(" allowed values ");
-        sb.append(values);
-        sb.append(" of LEAFREF node: ");
-        sb.append(leaf.getNodeType());
-        sb.append(" leafRef target path: ");
-        sb.append(referencingCtx.getAbsoluteLeafRefTargetPath());
-        sb.append(NEW_LINE);
-        return sb;
+        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,
+    private void addValues(final Set<Object> values, final Optional<? extends NormalizedNode<?, ?>> optDataNode,
             final Iterable<QNameWithPredicate> path, final YangInstanceIdentifier current,
             final QNameWithPredicate previousQName) {
 
@@ -516,15 +397,12 @@ public class LeafRefValidatation {
             return;
         }
         final NormalizedNode<?, ?> node = optDataNode.get();
-
-        if (node instanceof LeafNode || node instanceof LeafSetEntryNode) {
+        if (node instanceof ValueNode) {
             values.add(node.getValue());
             return;
-        } else if (node instanceof LeafSetNode<?>) {
-            final LeafSetNode<?> leafSetNode = (LeafSetNode<?>) node;
-            final Iterable<? extends NormalizedNode<?, ?>> entries = leafSetNode
-                    .getValue();
-            for (final NormalizedNode<?, ?> entry : entries) {
+        }
+        if (node instanceof LeafSetNode<?>) {
+            for (final NormalizedNode<?, ?> entry : ((LeafSetNode<?>) node).getValue()) {
                 values.add(entry.getValue());
             }
             return;
@@ -536,7 +414,7 @@ public class LeafRefValidatation {
         }
         final QNameWithPredicate qnameWithPredicate = iterator.next();
         final QName qName = qnameWithPredicate.getQName();
-        final PathArgument pathArgument = toPathArgument(qName);
+        final PathArgument pathArgument = new NodeIdentifier(qName);
 
         if (node instanceof DataContainerNode) {
             final DataContainerNode<?> dataContainerNode = (DataContainerNode<?>) node;
@@ -544,11 +422,9 @@ public class LeafRefValidatation {
                     .getChild(pathArgument);
 
             if (child.isPresent()) {
-                addValues(values, child, nextLevel(path), current,
-                        qnameWithPredicate);
+                addValues(values, child, nextLevel(path), current, qnameWithPredicate);
             } else {
-                final Iterable<ChoiceNode> choiceNodes = getChoiceNodes(dataContainerNode);
-                for (final ChoiceNode choiceNode : choiceNodes) {
+                for (final ChoiceNode choiceNode : getChoiceNodes(dataContainerNode)) {
                     addValues(values, Optional.of(choiceNode), path, current,
                             qnameWithPredicate);
                 }
@@ -556,8 +432,7 @@ public class LeafRefValidatation {
 
         } else if (node instanceof MapNode) {
             final MapNode map = (MapNode) node;
-            final List<QNamePredicate> qNamePredicates = previousQName
-                    .getQNamePredicates();
+            final List<QNamePredicate> qNamePredicates = previousQName.getQNamePredicates();
             if (qNamePredicates.isEmpty() || current == null) {
                 final Iterable<MapEntryNode> value = map.getValue();
                 for (final MapEntryNode mapEntryNode : value) {
@@ -565,21 +440,17 @@ public class LeafRefValidatation {
                             .getChild(pathArgument);
 
                     if (child.isPresent()) {
-                        addValues(values, child, nextLevel(path), current,
-                                qnameWithPredicate);
+                        addValues(values, child, nextLevel(path), current, qnameWithPredicate);
                     } else {
-                        final Iterable<ChoiceNode> choiceNodes = getChoiceNodes(mapEntryNode);
-                        for (final ChoiceNode choiceNode : choiceNodes) {
-                            addValues(values, Optional.of(choiceNode), path,
-                                    current, qnameWithPredicate);
+                        for (final ChoiceNode choiceNode : getChoiceNodes(mapEntryNode)) {
+                            addValues(values, Optional.of(choiceNode), path, current, qnameWithPredicate);
                         }
                     }
                 }
             } else {
-                final Map<QName, Set<?>> keyValues = new HashMap<QName, Set<?>>();
+                final Map<QName, Set<?>> keyValues = new HashMap<>();
 
-                final Iterator<QNamePredicate> predicates = qNamePredicates
-                        .iterator();
+                final Iterator<QNamePredicate> predicates = qNamePredicates.iterator();
                 while (predicates.hasNext()) {
                     final QNamePredicate predicate = predicates.next();
                     final QName identifier = predicate.getIdentifier();
@@ -592,37 +463,27 @@ public class LeafRefValidatation {
                     keyValues.put(identifier, pathKeyExprValues);
                 }
 
-                final Iterable<MapEntryNode> mapEntryNodes = map.getValue();
-                for (final MapEntryNode mapEntryNode : mapEntryNodes) {
+                for (final MapEntryNode mapEntryNode : map.getValue()) {
                     if (isMatchingPredicate(mapEntryNode, keyValues)) {
                         final Optional<DataContainerChild<? extends PathArgument, ?>> child = mapEntryNode
                                 .getChild(pathArgument);
 
                         if (child.isPresent()) {
-                            addValues(values, child, nextLevel(path), current,
-                                    qnameWithPredicate);
+                            addValues(values, child, nextLevel(path), current, qnameWithPredicate);
                         } else {
-                            final Iterable<ChoiceNode> choiceNodes = getChoiceNodes(mapEntryNode);
-                            for (final ChoiceNode choiceNode : choiceNodes) {
-                                addValues(values, Optional.of(choiceNode),
-                                        path, current, qnameWithPredicate);
+                            for (final ChoiceNode choiceNode : getChoiceNodes(mapEntryNode)) {
+                                addValues(values, Optional.of(choiceNode),  path, current, qnameWithPredicate);
                             }
                         }
                     }
                 }
-
             }
         }
     }
 
-    private Iterable<ChoiceNode> getChoiceNodes(
-            final DataContainerNode<?> dataContainerNode) {
-
-        final LinkedList<ChoiceNode> choiceNodes = new LinkedList<ChoiceNode>();
-
-        final Iterable<DataContainerChild<? extends PathArgument, ?>> childs = dataContainerNode
-                .getValue();
-        for (final DataContainerChild<? extends PathArgument, ?> child : childs) {
+    private static Iterable<ChoiceNode> getChoiceNodes(final DataContainerNode<?> dataContainerNode) {
+        final List<ChoiceNode> choiceNodes = new ArrayList<>();
+        for (final DataContainerChild<? extends PathArgument, ?> child : dataContainerNode.getValue()) {
             if (child instanceof ChoiceNode) {
                 choiceNodes.add((ChoiceNode) child);
             }
@@ -630,70 +491,50 @@ public class LeafRefValidatation {
         return choiceNodes;
     }
 
-    private boolean isMatchingPredicate(final MapEntryNode mapEntryNode,
+    private static boolean isMatchingPredicate(final MapEntryNode mapEntryNode,
             final Map<QName, Set<?>> allowedKeyValues) {
-
-        final NodeIdentifierWithPredicates identifier = mapEntryNode.getIdentifier();
-        final Map<QName, Object> entryKeyValues = identifier.getKeyValues();
-
-        final Set<Entry<QName, Object>> entryKeyValueSet = entryKeyValues.entrySet();
-        for (final Entry<QName, Object> entryKeyValue : entryKeyValueSet) {
-            final QName key = entryKeyValue.getKey();
-            final Object value = entryKeyValue.getValue();
-
-            final Set<?> allowedValues = allowedKeyValues.get(key);
-            if (allowedValues != null && !allowedValues.contains(value)) {
+        for (final Entry<QName, Object> entryKeyValue : mapEntryNode.getIdentifier().getKeyValues().entrySet()) {
+            final Set<?> allowedValues = allowedKeyValues.get(entryKeyValue.getKey());
+            if (allowedValues != null && !allowedValues.contains(entryKeyValue.getValue())) {
                 return false;
             }
-
         }
 
         return true;
     }
 
-    private Set<?> getPathKeyExpressionValues(
-            final LeafRefPath predicatePathKeyExpression,
+    private Set<?> getPathKeyExpressionValues(final LeafRefPath predicatePathKeyExpression,
             final YangInstanceIdentifier current) {
 
-        final Optional<NormalizedNode<?, ?>> parent = findParentNode(tree
-                .getRootNode().getDataAfter(), current);
-
-        final Iterable<QNameWithPredicate> predicatePathExpr = predicatePathKeyExpression
-                .getPathFromRoot();
+        final Optional<NormalizedNode<?, ?>> parent = findParentNode(tree.getRootNode().getDataAfter(), current);
+        final Iterable<QNameWithPredicate> predicatePathExpr = predicatePathKeyExpression.getPathFromRoot();
         final Iterable<QNameWithPredicate> predicatePath = nextLevel(predicatePathExpr);
 
         final Set<Object> values = new HashSet<>();
+        // FIXME: this null check does not look right
         if (parent != null) {
-            addValues(values, parent, predicatePath, null,
-                    QNameWithPredicate.ROOT);
+            addValues(values, parent, predicatePath, null, QNameWithPredicate.ROOT);
         }
 
         return values;
     }
 
-    private Optional<NormalizedNode<?, ?>> findParentNode(
+    private static Optional<NormalizedNode<?, ?>> findParentNode(
             final Optional<NormalizedNode<?, ?>> root, final YangInstanceIdentifier path) {
         Optional<NormalizedNode<?, ?>> currentNode = root;
-        final Iterator<PathArgument> pathIterator = path.getPathArguments()
-                .iterator();
+        final Iterator<PathArgument> pathIterator = path.getPathArguments().iterator();
         while (pathIterator.hasNext()) {
             final PathArgument childPathArgument = pathIterator.next();
             if (pathIterator.hasNext() && currentNode.isPresent()) {
-                currentNode = NormalizedNodes.getDirectChild(currentNode.get(),
-                        childPathArgument);
+                currentNode = NormalizedNodes.getDirectChild(currentNode.get(), childPathArgument);
             } else {
                 return currentNode;
             }
         }
-        return Optional.absent();
+        return Optional.empty();
     }
 
-    private Iterable<QNameWithPredicate> nextLevel(
-            final Iterable<QNameWithPredicate> path) {
+    private static Iterable<QNameWithPredicate> nextLevel(final Iterable<QNameWithPredicate> path) {
         return Iterables.skip(path, 1);
     }
-
-    private PathArgument toPathArgument(final QName qName) {
-        return YangInstanceIdentifier.of(qName).getLastPathArgument();
-    }
 }