Use ValueNode in LeafRefValidation
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / leafref / LeafRefValidatation.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.impl.leafref;
9
10 import com.google.common.base.Optional;
11 import com.google.common.collect.Iterables;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Map.Entry;
20 import java.util.Set;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
25 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
27 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
34 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.ValueNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
37 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
39 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public class LeafRefValidatation {
44
45     private static final Logger LOG = LoggerFactory.getLogger(LeafRefValidatation.class);
46     private static final String FAILED = " -> FAILED";
47     private static final String SUCCESS = " -> OK";
48
49     private final Set<LeafRefContext> validatedLeafRefCtx = new HashSet<>();
50     private final List<String> errorsMessages = new ArrayList<>();
51     private final DataTreeCandidate tree;
52
53     private LeafRefValidatation(final DataTreeCandidate tree) {
54         this.tree = tree;
55     }
56
57     public static void validate(final DataTreeCandidate tree, final LeafRefContext rootLeafRefCtx)
58             throws LeafRefDataValidationFailedException {
59         new LeafRefValidatation(tree).validate0(rootLeafRefCtx);
60     }
61
62     private void validate0(final LeafRefContext rootLeafRefCtx) throws LeafRefDataValidationFailedException {
63         for (final DataTreeCandidateNode dataTreeCandidateNode : tree.getRootNode().getChildNodes()) {
64             if (dataTreeCandidateNode.getModificationType() != ModificationType.UNMODIFIED) {
65                 final PathArgument identifier = dataTreeCandidateNode.getIdentifier();
66                 final QName childQName = identifier.getNodeType();
67
68                 final LeafRefContext referencedByCtx = rootLeafRefCtx.getReferencedChildByName(childQName);
69                 final LeafRefContext referencingCtx = rootLeafRefCtx.getReferencingChildByName(childQName);
70                 if (referencedByCtx != null || referencingCtx != null) {
71                     final YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier
72                             .create(dataTreeCandidateNode.getIdentifier());
73                     validateNode(dataTreeCandidateNode, referencedByCtx, referencingCtx, yangInstanceIdentifier);
74                 }
75             }
76         }
77
78         if (!errorsMessages.isEmpty()) {
79             final StringBuilder message = new StringBuilder();
80             int errCount = 0;
81             for (final String errorMessage : errorsMessages) {
82                 message.append(errorMessage);
83                 errCount++;
84             }
85             throw new LeafRefDataValidationFailedException(message.toString(), errCount);
86         }
87     }
88
89     private void validateNode(final DataTreeCandidateNode node, final LeafRefContext referencedByCtx,
90         final LeafRefContext referencingCtx, final YangInstanceIdentifier current) {
91
92         if (node.getModificationType() == ModificationType.WRITE && node.getDataAfter().isPresent()) {
93             final Optional<NormalizedNode<?, ?>> dataAfter = node.getDataAfter();
94             final NormalizedNode<?, ?> normalizedNode = dataAfter.get();
95             validateNodeData(normalizedNode, referencedByCtx, referencingCtx,
96                     node.getModificationType(), current);
97             return;
98         }
99
100         if (node.getModificationType() == ModificationType.DELETE && referencedByCtx != null) {
101             final Optional<NormalizedNode<?, ?>> dataBefor = node.getDataBefore();
102             final NormalizedNode<?, ?> normalizedNode = dataBefor.get();
103             validateNodeData(normalizedNode, referencedByCtx, null,
104                     node.getModificationType(), current);
105             return;
106         }
107
108         final Collection<DataTreeCandidateNode> childNodes = node.getChildNodes();
109         for (final DataTreeCandidateNode childNode : childNodes) {
110             if (childNode.getModificationType() != ModificationType.UNMODIFIED) {
111                 final LeafRefContext childReferencedByCtx = getReferencedByCtxChild(referencedByCtx, childNode);
112                 final LeafRefContext childReferencingCtx = getReferencingCtxChild(referencingCtx, childNode);
113
114                 if (childReferencedByCtx != null || childReferencingCtx != null) {
115                     final YangInstanceIdentifier childYangInstanceIdentifier = current.node(childNode.getIdentifier());
116                     validateNode(childNode, childReferencedByCtx,childReferencingCtx, childYangInstanceIdentifier);
117                 }
118             }
119         }
120     }
121
122     private static LeafRefContext getReferencingCtxChild(final LeafRefContext referencingCtx,
123             final DataTreeCandidateNode childNode) {
124         if (referencingCtx == null) {
125             return null;
126         }
127
128         final QName childQName = childNode.getIdentifier().getNodeType();
129         LeafRefContext childReferencingCtx = referencingCtx.getReferencingChildByName(childQName);
130         if (childReferencingCtx == null) {
131             final NormalizedNode<?, ?> data = childNode.getDataAfter().get();
132             if (data instanceof MapEntryNode || data instanceof UnkeyedListEntryNode) {
133                 childReferencingCtx = referencingCtx;
134             }
135         }
136
137         return childReferencingCtx;
138     }
139
140     private static LeafRefContext getReferencedByCtxChild(final LeafRefContext referencedByCtx,
141             final DataTreeCandidateNode childNode) {
142         if (referencedByCtx == null) {
143             return null;
144         }
145
146         final QName childQName = childNode.getIdentifier().getNodeType();
147         LeafRefContext childReferencedByCtx = referencedByCtx.getReferencedChildByName(childQName);
148         if (childReferencedByCtx == null) {
149             final NormalizedNode<?, ?> data = childNode.getDataAfter().get();
150             if (data instanceof MapEntryNode || data instanceof UnkeyedListEntryNode) {
151                 childReferencedByCtx = referencedByCtx;
152             }
153         }
154
155         return childReferencedByCtx;
156     }
157
158     private void validateNodeData(final NormalizedNode<?, ?> node, final LeafRefContext referencedByCtx, final
159             LeafRefContext referencingCtx, final ModificationType modificationType, final YangInstanceIdentifier current) {
160
161         if (node instanceof LeafNode) {
162             final LeafNode<?> leaf = (LeafNode<?>) node;
163
164             if (referencedByCtx != null && referencedByCtx.isReferenced()) {
165                 validateLeafRefTargetNodeData(leaf, referencedByCtx, modificationType);
166             }
167             if (referencingCtx != null && referencingCtx.isReferencing()) {
168                 validateLeafRefNodeData(leaf, referencingCtx, modificationType, current);
169             }
170
171             return;
172         }
173
174         if (node instanceof LeafSetNode) {
175             if (referencedByCtx == null && referencingCtx == null) {
176                 return;
177             }
178
179             final LeafSetNode<?> leafSet = (LeafSetNode<?>) node;
180             for (final NormalizedNode<?, ?> leafSetEntry : leafSet.getValue()) {
181                 if (referencedByCtx != null && referencedByCtx.isReferenced()) {
182                     validateLeafRefTargetNodeData(leafSetEntry, referencedByCtx, modificationType);
183                 }
184                 if (referencingCtx != null && referencingCtx.isReferencing()) {
185                     validateLeafRefNodeData(leafSetEntry, referencingCtx, modificationType, current);
186                 }
187             }
188
189             return;
190         }
191
192         if (node instanceof ChoiceNode) {
193             final ChoiceNode choice = (ChoiceNode) node;
194             for (final DataContainerChild<? extends PathArgument, ?> dataContainerChild : choice.getValue()) {
195                 final QName qname = dataContainerChild.getNodeType();
196
197                 final LeafRefContext childReferencedByCtx;
198                 if (referencedByCtx != null) {
199                     childReferencedByCtx = findReferencedByCtxUnderChoice(referencedByCtx, qname);
200                 } else {
201                     childReferencedByCtx = null;
202                 }
203
204                 final LeafRefContext childReferencingCtx;
205                 if (referencingCtx != null) {
206                     childReferencingCtx = findReferencingCtxUnderChoice(referencingCtx, qname);
207                 } else {
208                     childReferencingCtx = null;
209                 }
210
211                 if (childReferencedByCtx != null || childReferencingCtx != null) {
212                     final YangInstanceIdentifier childYangInstanceIdentifier = current
213                             .node(dataContainerChild.getIdentifier());
214                     validateNodeData(dataContainerChild, childReferencedByCtx,
215                             childReferencingCtx, modificationType, childYangInstanceIdentifier);
216                 }
217             }
218         } else if (node instanceof DataContainerNode) {
219             final DataContainerNode<?> dataContainerNode = (DataContainerNode<?>) node;
220
221             for (final DataContainerChild<? extends PathArgument, ?> dataContainerChild : dataContainerNode.getValue()) {
222                 final QName qname = dataContainerChild.getNodeType();
223
224                 final LeafRefContext childReferencedByCtx;
225                 if (referencedByCtx != null) {
226                     childReferencedByCtx = referencedByCtx.getReferencedChildByName(qname);
227                 } else {
228                     childReferencedByCtx = null;
229                 }
230
231                 final LeafRefContext childReferencingCtx;
232                 if (referencingCtx != null) {
233                     childReferencingCtx = referencingCtx.getReferencingChildByName(qname);
234                 } else {
235                     childReferencingCtx = null;
236                 }
237
238                 if (childReferencedByCtx != null || childReferencingCtx != null) {
239                     final YangInstanceIdentifier childYangInstanceIdentifier = current
240                             .node(dataContainerChild.getIdentifier());
241                     validateNodeData(dataContainerChild, childReferencedByCtx,
242                             childReferencingCtx, modificationType, childYangInstanceIdentifier);
243                 }
244             }
245         } else if (node instanceof MapNode) {
246             final MapNode map = (MapNode) node;
247
248             for (final MapEntryNode mapEntry : map.getValue()) {
249                 final YangInstanceIdentifier mapEntryYangInstanceIdentifier = current.node(mapEntry.getIdentifier());
250                 for (final DataContainerChild<? extends PathArgument, ?> mapEntryNode : mapEntry.getValue()) {
251                     final QName qname = mapEntryNode.getNodeType();
252
253                     final LeafRefContext childReferencedByCtx;
254                     if (referencedByCtx != null) {
255                         childReferencedByCtx = referencedByCtx.getReferencedChildByName(qname);
256                     } else {
257                         childReferencedByCtx = null;
258                     }
259
260                     final LeafRefContext childReferencingCtx;
261                     if (referencingCtx != null) {
262                         childReferencingCtx = referencingCtx.getReferencingChildByName(qname);
263                     } else {
264                         childReferencingCtx = null;
265                     }
266
267                     if (childReferencedByCtx != null || childReferencingCtx != null) {
268                         final YangInstanceIdentifier mapEntryNodeYangInstanceIdentifier = mapEntryYangInstanceIdentifier
269                                 .node(mapEntryNode.getIdentifier());
270                         validateNodeData(mapEntryNode, childReferencedByCtx,
271                                 childReferencingCtx, modificationType,
272                                 mapEntryNodeYangInstanceIdentifier);
273                     }
274                 }
275             }
276         }
277         // FIXME if (node instance of UnkeyedListNode ...
278     }
279
280     private static LeafRefContext findReferencingCtxUnderChoice(
281             final LeafRefContext referencingCtx, final QName qname) {
282
283         for (final LeafRefContext child : referencingCtx.getReferencingChilds().values()) {
284             final LeafRefContext referencingChildByName = child.getReferencingChildByName(qname);
285             if (referencingChildByName != null) {
286                 return referencingChildByName;
287             }
288         }
289
290         return null;
291     }
292
293     private static LeafRefContext findReferencedByCtxUnderChoice(
294             final LeafRefContext referencedByCtx, final QName qname) {
295
296         for (final LeafRefContext child : referencedByCtx.getReferencedByChilds().values()) {
297             final LeafRefContext referencedByChildByName = child.getReferencedChildByName(qname);
298             if (referencedByChildByName != null) {
299                 return referencedByChildByName;
300             }
301         }
302
303         return null;
304     }
305
306     private void validateLeafRefTargetNodeData(final NormalizedNode<?, ?> leaf, final LeafRefContext
307             referencedByCtx, final ModificationType modificationType) {
308         final Map<LeafRefContext, Set<?>> leafRefsValues = new HashMap<>();
309         if (validatedLeafRefCtx.contains(referencedByCtx)) {
310             leafRefTargetNodeDataLog(leaf, referencedByCtx, modificationType, leafRefsValues, null);
311             return;
312         }
313
314         final Map<QName, LeafRefContext> allReferencedByLeafRefCtxs = referencedByCtx.getAllReferencedByLeafRefCtxs();
315         for (final LeafRefContext leafRefContext : allReferencedByLeafRefCtxs.values()) {
316             if (leafRefContext.isReferencing()) {
317                 final Set<Object> values = new HashSet<>();
318
319                 final SchemaPath leafRefNodeSchemaPath = leafRefContext.getCurrentNodePath();
320                 final LeafRefPath leafRefNodePath = LeafRefUtils.schemaPathToLeafRefPath(leafRefNodeSchemaPath,
321                                 leafRefContext.getLeafRefContextModule());
322                 final Iterable<QNameWithPredicate> pathFromRoot = leafRefNodePath.getPathFromRoot();
323                 addValues(values, tree.getRootNode().getDataAfter(), pathFromRoot, null, QNameWithPredicate.ROOT);
324                 leafRefsValues.put(leafRefContext, values);
325             }
326         }
327
328         if (!leafRefsValues.isEmpty()) {
329             final Set<Object> leafRefTargetNodeValues = new HashSet<>();
330             final SchemaPath nodeSchemaPath = referencedByCtx.getCurrentNodePath();
331             final LeafRefPath nodePath = LeafRefUtils.schemaPathToLeafRefPath(nodeSchemaPath, referencedByCtx
332                     .getLeafRefContextModule());
333             addValues(leafRefTargetNodeValues, tree.getRootNode().getDataAfter(), nodePath.getPathFromRoot(), null,
334                     QNameWithPredicate.ROOT);
335             leafRefTargetNodeDataLog(leaf, referencedByCtx, modificationType, leafRefsValues,
336                     leafRefTargetNodeValues);
337         } else {
338             leafRefTargetNodeDataLog(leaf, referencedByCtx, modificationType, null, null);
339         }
340         validatedLeafRefCtx.add(referencedByCtx);
341     }
342
343     private void leafRefTargetNodeDataLog(final NormalizedNode<?, ?> leaf, final LeafRefContext referencedByCtx,
344             final ModificationType modificationType, final Map<LeafRefContext, Set<?>> leafRefsValues,
345             final Set<Object> leafRefTargetNodeValues) {
346
347         if (leafRefsValues != null && !leafRefsValues.isEmpty()) {
348             final Set<Entry<LeafRefContext, Set<?>>> entrySet = leafRefsValues.entrySet();
349             LOG.debug("Operation [{}] validate data of leafref TARGET node: name[{}] = value[{}]",
350                     modificationType, referencedByCtx.getNodeName(), leaf.getValue());
351             for (final Entry<LeafRefContext, Set<?>> entry : entrySet) {
352                 final LeafRefContext leafRefContext = entry.getKey();
353                 final Set<?> leafRefValuesSet = entry.getValue();
354                 for (final Object leafRefsValue : leafRefValuesSet) {
355                     if (leafRefTargetNodeValues != null && !leafRefTargetNodeValues.contains(leafRefsValue)) {
356                         LOG.debug("Invalid leafref value [{}] allowed values {} by validation of leafref TARGET node:" +
357                                 " {} path of invalid LEAFREF node: {} leafRef target path: {} {}", leafRefsValue,
358                                 leafRefTargetNodeValues, leaf.getNodeType(), leafRefContext.getCurrentNodePath(),
359                                 leafRefContext.getAbsoluteLeafRefTargetPath(), FAILED);
360                         errorsMessages.add(String.format("Invalid leafref value [%s] allowed values %s by validation " +
361                                         "of  leafref TARGET node: %s path of invalid LEAFREF node: %s leafRef target " +
362                                         "path: %s %s", leafRefsValue, leafRefTargetNodeValues, leaf.getNodeType(),
363                                 leafRefContext.getCurrentNodePath(), leafRefContext.getAbsoluteLeafRefTargetPath(),
364                                 FAILED));
365                     } else {
366                         LOG.debug("Valid leafref value [{}] {}", leafRefsValue, SUCCESS);
367                     }
368                 }
369             }
370         } else if (leafRefsValues != null) {
371             LOG.debug("Operation [{}] validate data of leafref TARGET node: name[{}] = value[{}] -> SKIP: Already validated",
372                     modificationType, referencedByCtx.getNodeName(), leaf.getValue());
373         }
374     }
375
376     private void validateLeafRefNodeData(final NormalizedNode<?, ?> leaf, final LeafRefContext referencingCtx,
377             final ModificationType modificationType, final YangInstanceIdentifier current) {
378         final HashSet<Object> values = new HashSet<>();
379         final LeafRefPath targetPath = referencingCtx.getAbsoluteLeafRefTargetPath();
380         final Iterable<QNameWithPredicate> pathFromRoot = targetPath.getPathFromRoot();
381
382         addValues(values, tree.getRootNode().getDataAfter(), pathFromRoot, current, QNameWithPredicate.ROOT);
383
384         if (!values.contains(leaf.getValue())) {
385             LOG.debug("Operation [{}] validate data of LEAFREF node: name[{}] = value[{}] {}",
386                     modificationType, referencingCtx.getNodeName(), leaf.getValue(), FAILED);
387             LOG.debug("Invalid leafref value [{}] allowed values {} of LEAFREF node: {} leafRef target path: {}",
388                     leaf.getValue(), values, leaf.getNodeType(), referencingCtx.getAbsoluteLeafRefTargetPath());
389             errorsMessages.add(String.format("Invalid leafref value [%s] allowed values %s of LEAFREF node: %s " +
390                             "leafRef  target path: %s", leaf.getValue(), values, leaf.getNodeType(), referencingCtx
391                     .getAbsoluteLeafRefTargetPath()));
392         } else {
393             LOG.debug("Operation [{}] validate data of LEAFREF node: name[{}] = value[{}] {}", modificationType,
394                     referencingCtx.getNodeName(), leaf.getValue(), SUCCESS);
395         }
396     }
397
398     private void addValues(final Set<Object> values, final Optional<? extends NormalizedNode<?, ?>> optDataNode,
399             final Iterable<QNameWithPredicate> path, final YangInstanceIdentifier current, final QNameWithPredicate previousQName) {
400
401         if (!optDataNode.isPresent()) {
402             return;
403         }
404         final NormalizedNode<?, ?> node = optDataNode.get();
405         if (node instanceof ValueNode) {
406             values.add(node.getValue());
407             return;
408         }
409         if (node instanceof LeafSetNode<?>) {
410             for (final NormalizedNode<?, ?> entry : ((LeafSetNode<?>) node).getValue()) {
411                 values.add(entry.getValue());
412             }
413             return;
414         }
415
416         final Iterator<QNameWithPredicate> iterator = path.iterator();
417         if (!iterator.hasNext()) {
418             return;
419         }
420         final QNameWithPredicate qnameWithPredicate = iterator.next();
421         final QName qName = qnameWithPredicate.getQName();
422         final PathArgument pathArgument = new NodeIdentifier(qName);
423
424         if (node instanceof DataContainerNode) {
425             final DataContainerNode<?> dataContainerNode = (DataContainerNode<?>) node;
426             final Optional<DataContainerChild<? extends PathArgument, ?>> child = dataContainerNode
427                     .getChild(pathArgument);
428
429             if (child.isPresent()) {
430                 addValues(values, child, nextLevel(path), current, qnameWithPredicate);
431             } else {
432                 for (final ChoiceNode choiceNode : getChoiceNodes(dataContainerNode)) {
433                     addValues(values, Optional.of(choiceNode), path, current,
434                             qnameWithPredicate);
435                 }
436             }
437
438         } else if (node instanceof MapNode) {
439             final MapNode map = (MapNode) node;
440             final List<QNamePredicate> qNamePredicates = previousQName.getQNamePredicates();
441             if (qNamePredicates.isEmpty() || current == null) {
442                 final Iterable<MapEntryNode> value = map.getValue();
443                 for (final MapEntryNode mapEntryNode : value) {
444                     final Optional<DataContainerChild<? extends PathArgument, ?>> child = mapEntryNode
445                             .getChild(pathArgument);
446
447                     if (child.isPresent()) {
448                         addValues(values, child, nextLevel(path), current, qnameWithPredicate);
449                     } else {
450                         for (final ChoiceNode choiceNode : getChoiceNodes(mapEntryNode)) {
451                             addValues(values, Optional.of(choiceNode), path, current, qnameWithPredicate);
452                         }
453                     }
454                 }
455             } else {
456                 final Map<QName, Set<?>> keyValues = new HashMap<>();
457
458                 final Iterator<QNamePredicate> predicates = qNamePredicates.iterator();
459                 while (predicates.hasNext()) {
460                     final QNamePredicate predicate = predicates.next();
461                     final QName identifier = predicate.getIdentifier();
462                     final LeafRefPath predicatePathKeyExpression = predicate
463                             .getPathKeyExpression();
464
465                     final Set<?> pathKeyExprValues = getPathKeyExpressionValues(
466                             predicatePathKeyExpression, current);
467
468                     keyValues.put(identifier, pathKeyExprValues);
469                 }
470
471                 for (final MapEntryNode mapEntryNode : map.getValue()) {
472                     if (isMatchingPredicate(mapEntryNode, keyValues)) {
473                         final Optional<DataContainerChild<? extends PathArgument, ?>> child = mapEntryNode
474                                 .getChild(pathArgument);
475
476                         if (child.isPresent()) {
477                             addValues(values, child, nextLevel(path), current, qnameWithPredicate);
478                         } else {
479                             for (final ChoiceNode choiceNode : getChoiceNodes(mapEntryNode)) {
480                                 addValues(values, Optional.of(choiceNode),  path, current, qnameWithPredicate);
481                             }
482                         }
483                     }
484                 }
485             }
486         }
487     }
488
489     private static Iterable<ChoiceNode> getChoiceNodes(final DataContainerNode<?> dataContainerNode) {
490         final List<ChoiceNode> choiceNodes = new ArrayList<>();
491         for (final DataContainerChild<? extends PathArgument, ?> child : dataContainerNode.getValue()) {
492             if (child instanceof ChoiceNode) {
493                 choiceNodes.add((ChoiceNode) child);
494             }
495         }
496         return choiceNodes;
497     }
498
499     private static boolean isMatchingPredicate(final MapEntryNode mapEntryNode, final Map<QName, Set<?>> allowedKeyValues) {
500         for (final Entry<QName, Object> entryKeyValue : mapEntryNode.getIdentifier().getKeyValues().entrySet()) {
501             final Set<?> allowedValues = allowedKeyValues.get(entryKeyValue.getKey());
502             if (allowedValues != null && !allowedValues.contains(entryKeyValue.getValue())) {
503                 return false;
504             }
505         }
506
507         return true;
508     }
509
510     private Set<?> getPathKeyExpressionValues(final LeafRefPath predicatePathKeyExpression,
511             final YangInstanceIdentifier current) {
512
513         final Optional<NormalizedNode<?, ?>> parent = findParentNode(tree.getRootNode().getDataAfter(), current);
514
515         final Iterable<QNameWithPredicate> predicatePathExpr = predicatePathKeyExpression.getPathFromRoot();
516         final Iterable<QNameWithPredicate> predicatePath = nextLevel(predicatePathExpr);
517
518         final Set<Object> values = new HashSet<>();
519         if (parent != null) {
520             addValues(values, parent, predicatePath, null,QNameWithPredicate.ROOT);
521         }
522
523         return values;
524     }
525
526     private static Optional<NormalizedNode<?, ?>> findParentNode(
527             final Optional<NormalizedNode<?, ?>> root, final YangInstanceIdentifier path) {
528         Optional<NormalizedNode<?, ?>> currentNode = root;
529         final Iterator<PathArgument> pathIterator = path.getPathArguments().iterator();
530         while (pathIterator.hasNext()) {
531             final PathArgument childPathArgument = pathIterator.next();
532             if (pathIterator.hasNext() && currentNode.isPresent()) {
533                 currentNode = NormalizedNodes.getDirectChild(currentNode.get(), childPathArgument);
534             } else {
535                 return currentNode;
536             }
537         }
538         return Optional.absent();
539     }
540
541     private static Iterable<QNameWithPredicate> nextLevel(final Iterable<QNameWithPredicate> path) {
542         return Iterables.skip(path, 1);
543     }
544 }