Move LeafRefPath creation from fast path
[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.collect.Iterables;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Map.Entry;
19 import java.util.Optional;
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.AugmentationNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
28 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
35 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.ValueNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
38 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public final 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,
159             final LeafRefContext referencingCtx, final ModificationType modificationType,
160             final YangInstanceIdentifier current) {
161
162         if (node instanceof LeafNode) {
163             final LeafNode<?> leaf = (LeafNode<?>) node;
164
165             if (referencedByCtx != null && referencedByCtx.isReferenced()) {
166                 validateLeafRefTargetNodeData(leaf, referencedByCtx, modificationType);
167             }
168             if (referencingCtx != null && referencingCtx.isReferencing()) {
169                 validateLeafRefNodeData(leaf, referencingCtx, modificationType, current);
170             }
171
172             return;
173         }
174
175         if (node instanceof LeafSetNode) {
176             if (referencedByCtx == null && referencingCtx == null) {
177                 return;
178             }
179
180             final LeafSetNode<?> leafSet = (LeafSetNode<?>) node;
181             for (final NormalizedNode<?, ?> leafSetEntry : leafSet.getValue()) {
182                 if (referencedByCtx != null && referencedByCtx.isReferenced()) {
183                     validateLeafRefTargetNodeData(leafSetEntry, referencedByCtx, modificationType);
184                 }
185                 if (referencingCtx != null && referencingCtx.isReferencing()) {
186                     validateLeafRefNodeData(leafSetEntry, referencingCtx, modificationType, current);
187                 }
188             }
189
190             return;
191         }
192
193         if (node instanceof ChoiceNode) {
194             final ChoiceNode choice = (ChoiceNode) node;
195             for (final DataContainerChild<? extends PathArgument, ?> dataContainerChild : choice.getValue()) {
196                 final QName qname = dataContainerChild.getNodeType();
197
198                 final LeafRefContext childReferencedByCtx;
199                 if (referencedByCtx != null) {
200                     childReferencedByCtx = findReferencedByCtxUnderChoice(referencedByCtx, qname);
201                 } else {
202                     childReferencedByCtx = null;
203                 }
204
205                 final LeafRefContext childReferencingCtx;
206                 if (referencingCtx != null) {
207                     childReferencingCtx = findReferencingCtxUnderChoice(referencingCtx, qname);
208                 } else {
209                     childReferencingCtx = null;
210                 }
211
212                 if (childReferencedByCtx != null || childReferencingCtx != null) {
213                     final YangInstanceIdentifier childYangInstanceIdentifier = current
214                             .node(dataContainerChild.getIdentifier());
215                     validateNodeData(dataContainerChild, childReferencedByCtx,
216                             childReferencingCtx, modificationType, childYangInstanceIdentifier);
217                 }
218             }
219         } else if (node instanceof DataContainerNode) {
220             final DataContainerNode<?> dataContainerNode = (DataContainerNode<?>) node;
221
222             for (final DataContainerChild<? extends PathArgument, ?> child : dataContainerNode.getValue()) {
223                 if (child instanceof AugmentationNode) {
224                     validateNodeData(child, referencedByCtx, referencingCtx, modificationType, current
225                         .node(child.getIdentifier()));
226                     return;
227                 }
228
229                 final QName qname = child.getNodeType();
230                 final LeafRefContext childReferencedByCtx;
231                 if (referencedByCtx != null) {
232                     childReferencedByCtx = referencedByCtx.getReferencedChildByName(qname);
233                 } else {
234                     childReferencedByCtx = null;
235                 }
236
237                 final LeafRefContext childReferencingCtx;
238                 if (referencingCtx != null) {
239                     childReferencingCtx = referencingCtx.getReferencingChildByName(qname);
240                 } else {
241                     childReferencingCtx = null;
242                 }
243
244                 if (childReferencedByCtx != null || childReferencingCtx != null) {
245                     final YangInstanceIdentifier childYangInstanceIdentifier = current
246                             .node(child.getIdentifier());
247                     validateNodeData(child, childReferencedByCtx,
248                             childReferencingCtx, modificationType, childYangInstanceIdentifier);
249                 }
250             }
251         } else if (node instanceof MapNode) {
252             final MapNode map = (MapNode) node;
253
254             for (final MapEntryNode mapEntry : map.getValue()) {
255                 final YangInstanceIdentifier mapEntryYangInstanceIdentifier = current.node(mapEntry.getIdentifier());
256                 for (final DataContainerChild<? extends PathArgument, ?> mapEntryNode : mapEntry.getValue()) {
257                     if (mapEntryNode instanceof AugmentationNode) {
258                         validateNodeData(mapEntryNode, referencedByCtx, referencingCtx, modificationType, current
259                             .node(mapEntryNode.getIdentifier()));
260                         return;
261                     }
262
263                     final QName qname = mapEntryNode.getNodeType();
264                     final LeafRefContext childReferencedByCtx;
265                     if (referencedByCtx != null) {
266                         childReferencedByCtx = referencedByCtx.getReferencedChildByName(qname);
267                     } else {
268                         childReferencedByCtx = null;
269                     }
270
271                     final LeafRefContext childReferencingCtx;
272                     if (referencingCtx != null) {
273                         childReferencingCtx = referencingCtx.getReferencingChildByName(qname);
274                     } else {
275                         childReferencingCtx = null;
276                     }
277
278                     if (childReferencedByCtx != null || childReferencingCtx != null) {
279                         validateNodeData(mapEntryNode, childReferencedByCtx, childReferencingCtx, modificationType,
280                                 mapEntryYangInstanceIdentifier.node(mapEntryNode.getIdentifier()));
281                     }
282                 }
283             }
284         }
285         // FIXME: check UnkeyedListNode case
286     }
287
288     private static LeafRefContext findReferencingCtxUnderChoice(
289             final LeafRefContext referencingCtx, final QName qname) {
290
291         for (final LeafRefContext child : referencingCtx.getReferencingChilds().values()) {
292             final LeafRefContext referencingChildByName = child.getReferencingChildByName(qname);
293             if (referencingChildByName != null) {
294                 return referencingChildByName;
295             }
296         }
297
298         return null;
299     }
300
301     private static LeafRefContext findReferencedByCtxUnderChoice(
302             final LeafRefContext referencedByCtx, final QName qname) {
303
304         for (final LeafRefContext child : referencedByCtx.getReferencedByChilds().values()) {
305             final LeafRefContext referencedByChildByName = child.getReferencedChildByName(qname);
306             if (referencedByChildByName != null) {
307                 return referencedByChildByName;
308             }
309         }
310
311         return null;
312     }
313
314     private void validateLeafRefTargetNodeData(final NormalizedNode<?, ?> leaf, final LeafRefContext
315             referencedByCtx, final ModificationType modificationType) {
316         final Map<LeafRefContext, Set<?>> leafRefsValues = new HashMap<>();
317         if (validatedLeafRefCtx.contains(referencedByCtx)) {
318             leafRefTargetNodeDataLog(leaf, referencedByCtx, modificationType, leafRefsValues, null);
319             return;
320         }
321
322         for (final LeafRefContext leafRefContext : referencedByCtx.getAllReferencedByLeafRefCtxs().values()) {
323             if (leafRefContext.isReferencing()) {
324                 leafRefsValues.put(leafRefContext, extractRootValues(leafRefContext));
325             }
326         }
327
328         if (!leafRefsValues.isEmpty()) {
329             final Set<Object> values = extractRootValues(referencedByCtx);
330             leafRefTargetNodeDataLog(leaf, referencedByCtx, modificationType, leafRefsValues, values);
331         } else {
332             leafRefTargetNodeDataLog(leaf, referencedByCtx, modificationType, null, null);
333         }
334         validatedLeafRefCtx.add(referencedByCtx);
335     }
336
337     private Set<Object> extractRootValues(final LeafRefContext context) {
338         final Set<Object> values = new HashSet<>();
339         addValues(values, tree.getRootNode().getDataAfter(), context.getLeafRefNodePath().getPathFromRoot(), null,
340             QNameWithPredicate.ROOT);
341         return values;
342     }
343
344     private void leafRefTargetNodeDataLog(final NormalizedNode<?, ?> leaf, final LeafRefContext referencedByCtx,
345             final ModificationType modificationType, final Map<LeafRefContext, Set<?>> leafRefsValues,
346             final Set<Object> leafRefTargetNodeValues) {
347
348         if (leafRefsValues != null && !leafRefsValues.isEmpty()) {
349             final Set<Entry<LeafRefContext, Set<?>>> entrySet = leafRefsValues.entrySet();
350             LOG.debug("Operation [{}] validate data of leafref TARGET node: name[{}] = value[{}]",
351                     modificationType, referencedByCtx.getNodeName(), leaf.getValue());
352             for (final Entry<LeafRefContext, Set<?>> entry : entrySet) {
353                 final LeafRefContext leafRefContext = entry.getKey();
354                 final Set<?> leafRefValuesSet = entry.getValue();
355                 for (final Object leafRefsValue : leafRefValuesSet) {
356                     if (leafRefTargetNodeValues != null && !leafRefTargetNodeValues.contains(leafRefsValue)) {
357                         LOG.debug("Invalid leafref value [{}] allowed values {} by validation of leafref TARGET node: "
358                                 + "{} path of invalid LEAFREF node: {} leafRef target path: {} {}", leafRefsValue,
359                                 leafRefTargetNodeValues, leaf.getNodeType(), leafRefContext.getCurrentNodePath(),
360                                 leafRefContext.getAbsoluteLeafRefTargetPath(), FAILED);
361                         errorsMessages.add(String.format("Invalid leafref value [%s] allowed values %s by validation "
362                                         + "of leafref TARGET node: %s path of invalid LEAFREF node: %s leafRef target "
363                                         + "path: %s %s", leafRefsValue, leafRefTargetNodeValues, leaf.getNodeType(),
364                                 leafRefContext.getCurrentNodePath(), leafRefContext.getAbsoluteLeafRefTargetPath(),
365                                 FAILED));
366                     } else {
367                         LOG.debug("Valid leafref value [{}] {}", leafRefsValue, SUCCESS);
368                     }
369                 }
370             }
371         } else if (leafRefsValues != null) {
372             LOG.debug("Operation [{}] validate data of leafref TARGET node: name[{}] = value[{}] "
373                     + "-> SKIP: Already validated", modificationType, referencedByCtx.getNodeName(), leaf.getValue());
374         }
375     }
376
377     private void validateLeafRefNodeData(final NormalizedNode<?, ?> leaf, final LeafRefContext referencingCtx,
378             final ModificationType modificationType, final YangInstanceIdentifier current) {
379         final HashSet<Object> values = new HashSet<>();
380         final LeafRefPath targetPath = referencingCtx.getAbsoluteLeafRefTargetPath();
381         final Iterable<QNameWithPredicate> pathFromRoot = targetPath.getPathFromRoot();
382
383         addValues(values, tree.getRootNode().getDataAfter(), pathFromRoot, current, QNameWithPredicate.ROOT);
384
385         if (!values.contains(leaf.getValue())) {
386             LOG.debug("Operation [{}] validate data of LEAFREF node: name[{}] = value[{}] {}",
387                     modificationType, referencingCtx.getNodeName(), leaf.getValue(), FAILED);
388             LOG.debug("Invalid leafref value [{}] allowed values {} of LEAFREF node: {} leafRef target path: {}",
389                     leaf.getValue(), values, leaf.getNodeType(), referencingCtx.getAbsoluteLeafRefTargetPath());
390             errorsMessages.add(String.format("Invalid leafref value [%s] allowed values %s of LEAFREF node: %s "
391                             + "leafRef target path: %s", leaf.getValue(), values, leaf.getNodeType(), referencingCtx
392                     .getAbsoluteLeafRefTargetPath()));
393         } else {
394             LOG.debug("Operation [{}] validate data of LEAFREF node: name[{}] = value[{}] {}", modificationType,
395                     referencingCtx.getNodeName(), leaf.getValue(), SUCCESS);
396         }
397     }
398
399     private void addValues(final Set<Object> values, final Optional<? extends NormalizedNode<?, ?>> optDataNode,
400             final Iterable<QNameWithPredicate> path, final YangInstanceIdentifier current,
401             final QNameWithPredicate previousQName) {
402
403         if (!optDataNode.isPresent()) {
404             return;
405         }
406         final NormalizedNode<?, ?> node = optDataNode.get();
407         if (node instanceof ValueNode) {
408             values.add(node.getValue());
409             return;
410         }
411         if (node instanceof LeafSetNode<?>) {
412             for (final NormalizedNode<?, ?> entry : ((LeafSetNode<?>) node).getValue()) {
413                 values.add(entry.getValue());
414             }
415             return;
416         }
417
418         final Iterator<QNameWithPredicate> iterator = path.iterator();
419         if (!iterator.hasNext()) {
420             return;
421         }
422         final QNameWithPredicate qnameWithPredicate = iterator.next();
423         final QName qName = qnameWithPredicate.getQName();
424         final PathArgument pathArgument = new NodeIdentifier(qName);
425
426         if (node instanceof DataContainerNode) {
427             final DataContainerNode<?> dataContainerNode = (DataContainerNode<?>) node;
428             final Optional<DataContainerChild<? extends PathArgument, ?>> child = dataContainerNode
429                     .getChild(pathArgument);
430
431             if (child.isPresent()) {
432                 addValues(values, child, nextLevel(path), current, qnameWithPredicate);
433             } else {
434                 for (final ChoiceNode choiceNode : getChoiceNodes(dataContainerNode)) {
435                     addValues(values, Optional.of(choiceNode), path, current,
436                             qnameWithPredicate);
437                 }
438             }
439
440         } else if (node instanceof MapNode) {
441             final MapNode map = (MapNode) node;
442             final List<QNamePredicate> qNamePredicates = previousQName.getQNamePredicates();
443             if (qNamePredicates.isEmpty() || current == null) {
444                 final Iterable<MapEntryNode> value = map.getValue();
445                 for (final MapEntryNode mapEntryNode : value) {
446                     final Optional<DataContainerChild<? extends PathArgument, ?>> child = mapEntryNode
447                             .getChild(pathArgument);
448
449                     if (child.isPresent()) {
450                         addValues(values, child, nextLevel(path), current, qnameWithPredicate);
451                     } else {
452                         for (final ChoiceNode choiceNode : getChoiceNodes(mapEntryNode)) {
453                             addValues(values, Optional.of(choiceNode), path, current, qnameWithPredicate);
454                         }
455                     }
456                 }
457             } else {
458                 final Map<QName, Set<?>> keyValues = new HashMap<>();
459
460                 final Iterator<QNamePredicate> predicates = qNamePredicates.iterator();
461                 while (predicates.hasNext()) {
462                     final QNamePredicate predicate = predicates.next();
463                     final QName identifier = predicate.getIdentifier();
464                     final LeafRefPath predicatePathKeyExpression = predicate
465                             .getPathKeyExpression();
466
467                     final Set<?> pathKeyExprValues = getPathKeyExpressionValues(
468                             predicatePathKeyExpression, current);
469
470                     keyValues.put(identifier, pathKeyExprValues);
471                 }
472
473                 for (final MapEntryNode mapEntryNode : map.getValue()) {
474                     if (isMatchingPredicate(mapEntryNode, keyValues)) {
475                         final Optional<DataContainerChild<? extends PathArgument, ?>> child = mapEntryNode
476                                 .getChild(pathArgument);
477
478                         if (child.isPresent()) {
479                             addValues(values, child, nextLevel(path), current, qnameWithPredicate);
480                         } else {
481                             for (final ChoiceNode choiceNode : getChoiceNodes(mapEntryNode)) {
482                                 addValues(values, Optional.of(choiceNode),  path, current, qnameWithPredicate);
483                             }
484                         }
485                     }
486                 }
487             }
488         }
489     }
490
491     private static Iterable<ChoiceNode> getChoiceNodes(final DataContainerNode<?> dataContainerNode) {
492         final List<ChoiceNode> choiceNodes = new ArrayList<>();
493         for (final DataContainerChild<? extends PathArgument, ?> child : dataContainerNode.getValue()) {
494             if (child instanceof ChoiceNode) {
495                 choiceNodes.add((ChoiceNode) child);
496             }
497         }
498         return choiceNodes;
499     }
500
501     private static boolean isMatchingPredicate(final MapEntryNode mapEntryNode,
502             final Map<QName, Set<?>> allowedKeyValues) {
503         for (final Entry<QName, Object> entryKeyValue : mapEntryNode.getIdentifier().getKeyValues().entrySet()) {
504             final Set<?> allowedValues = allowedKeyValues.get(entryKeyValue.getKey());
505             if (allowedValues != null && !allowedValues.contains(entryKeyValue.getValue())) {
506                 return false;
507             }
508         }
509
510         return true;
511     }
512
513     private Set<?> getPathKeyExpressionValues(final LeafRefPath predicatePathKeyExpression,
514             final YangInstanceIdentifier current) {
515
516         final Optional<NormalizedNode<?, ?>> parent = findParentNode(tree.getRootNode().getDataAfter(), current);
517         final Iterable<QNameWithPredicate> predicatePathExpr = predicatePathKeyExpression.getPathFromRoot();
518         final Iterable<QNameWithPredicate> predicatePath = nextLevel(predicatePathExpr);
519
520         final Set<Object> values = new HashSet<>();
521         // FIXME: this null check does not look right
522         if (parent != null) {
523             addValues(values, parent, predicatePath, null, QNameWithPredicate.ROOT);
524         }
525
526         return values;
527     }
528
529     private static Optional<NormalizedNode<?, ?>> findParentNode(
530             final Optional<NormalizedNode<?, ?>> root, final YangInstanceIdentifier path) {
531         Optional<NormalizedNode<?, ?>> currentNode = root;
532         final Iterator<PathArgument> pathIterator = path.getPathArguments().iterator();
533         while (pathIterator.hasNext()) {
534             final PathArgument childPathArgument = pathIterator.next();
535             if (pathIterator.hasNext() && currentNode.isPresent()) {
536                 currentNode = NormalizedNodes.getDirectChild(currentNode.get(), childPathArgument);
537             } else {
538                 return currentNode;
539             }
540         }
541         return Optional.empty();
542     }
543
544     private static Iterable<QNameWithPredicate> nextLevel(final Iterable<QNameWithPredicate> path) {
545         return Iterables.skip(path, 1);
546     }
547 }