2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.leafref;
10 import com.google.common.base.Optional;
11 import com.google.common.collect.Iterables;
12 import java.util.Collection;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.Iterator;
16 import java.util.LinkedList;
17 import java.util.List;
19 import java.util.Map.Entry;
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.NodeIdentifierWithPredicates;
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.LeafSetEntryNode;
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.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;
43 public class LeafRefValidatation {
45 private static final Logger LOG = LoggerFactory.getLogger(LeafRefValidatation.class);
46 private static final String NEW_LINE = System.getProperty("line.separator");
48 private final DataTreeCandidate tree;
49 private final LinkedList<String> errorsMessages = new LinkedList<String>();
50 private final HashSet<LeafRefContext> validatedLeafRefCtx = new HashSet<LeafRefContext>();
52 private LeafRefValidatation(final DataTreeCandidate tree) {
56 public static void validate(final DataTreeCandidate tree, final LeafRefContext rootLeafRefCtx)
57 throws LeafRefDataValidationFailedException {
58 new LeafRefValidatation(tree).validate0(rootLeafRefCtx);
60 private void validate0(final LeafRefContext rootLeafRefCtx)
61 throws LeafRefDataValidationFailedException {
63 final DataTreeCandidateNode rootNode = tree.getRootNode();
65 final Collection<DataTreeCandidateNode> childNodes = rootNode.getChildNodes();
66 for (final DataTreeCandidateNode dataTreeCandidateNode : childNodes) {
68 final ModificationType modificationType = dataTreeCandidateNode
69 .getModificationType();
70 if (modificationType != ModificationType.UNMODIFIED) {
72 final PathArgument identifier = dataTreeCandidateNode.getIdentifier();
73 final QName childQName = identifier.getNodeType();
75 final LeafRefContext referencedByCtx = rootLeafRefCtx
76 .getReferencedChildByName(childQName);
77 final LeafRefContext referencingCtx = rootLeafRefCtx
78 .getReferencingChildByName(childQName);
79 if (referencedByCtx != null || referencingCtx != null) {
80 final YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier
81 .create(dataTreeCandidateNode.getIdentifier());
82 validateNode(dataTreeCandidateNode, referencedByCtx,
83 referencingCtx, yangInstanceIdentifier);
89 if (!errorsMessages.isEmpty()) {
90 final StringBuilder message = new StringBuilder();
92 for (final String errorMessage : errorsMessages) {
93 message.append(errorMessage);
96 throw new LeafRefDataValidationFailedException(message.toString(),
102 private void validateNode(final DataTreeCandidateNode node,
103 final LeafRefContext referencedByCtx, final LeafRefContext referencingCtx,
104 final YangInstanceIdentifier current) {
106 if ((node.getModificationType() == ModificationType.WRITE)
107 && node.getDataAfter().isPresent()) {
108 final Optional<NormalizedNode<?, ?>> dataAfter = node.getDataAfter();
109 final NormalizedNode<?, ?> normalizedNode = dataAfter.get();
110 validateNodeData(normalizedNode, referencedByCtx, referencingCtx,
111 node.getModificationType(), current);
115 if (node.getModificationType() == ModificationType.DELETE
116 && referencedByCtx != null) {
117 final Optional<NormalizedNode<?, ?>> dataBefor = node.getDataBefore();
118 final NormalizedNode<?, ?> normalizedNode = dataBefor.get();
119 validateNodeData(normalizedNode, referencedByCtx, null,
120 node.getModificationType(), current);
124 final Collection<DataTreeCandidateNode> childNodes = node.getChildNodes();
125 for (final DataTreeCandidateNode childNode : childNodes) {
126 final ModificationType modificationType = childNode.getModificationType();
128 if (modificationType != ModificationType.UNMODIFIED) {
130 final LeafRefContext childReferencedByCtx = getReferencedByCtxChild(
131 referencedByCtx, childNode);
132 final LeafRefContext childReferencingCtx = getReferencingCtxChild(
133 referencingCtx, childNode);
135 if (childReferencedByCtx != null || childReferencingCtx != null) {
136 final YangInstanceIdentifier childYangInstanceIdentifier = current
137 .node(childNode.getIdentifier());
138 validateNode(childNode, childReferencedByCtx,
139 childReferencingCtx, childYangInstanceIdentifier);
147 private LeafRefContext getReferencingCtxChild(
148 final LeafRefContext referencingCtx, final DataTreeCandidateNode childNode) {
150 LeafRefContext childReferencingCtx = null;
151 if (referencingCtx != null) {
152 final PathArgument identifier = childNode.getIdentifier();
153 final QName childQName = identifier.getNodeType();
155 childReferencingCtx = referencingCtx
156 .getReferencingChildByName(childQName);
158 if (childReferencingCtx == null) {
159 final NormalizedNode<?, ?> data = childNode.getDataAfter().get();
160 if (data instanceof MapEntryNode
161 || data instanceof UnkeyedListEntryNode) {
162 childReferencingCtx = referencingCtx;
167 return childReferencingCtx;
170 private LeafRefContext getReferencedByCtxChild(
171 final LeafRefContext referencedByCtx, final DataTreeCandidateNode childNode) {
173 LeafRefContext childReferencedByCtx = null;
174 if (referencedByCtx != null) {
175 final PathArgument identifier = childNode.getIdentifier();
176 final QName childQName = identifier.getNodeType();
178 childReferencedByCtx = referencedByCtx
179 .getReferencedChildByName(childQName);
180 if (childReferencedByCtx == null) {
181 final NormalizedNode<?, ?> data = childNode.getDataAfter().get();
182 if (data instanceof MapEntryNode
183 || data instanceof UnkeyedListEntryNode) {
184 childReferencedByCtx = referencedByCtx;
189 return childReferencedByCtx;
192 private void validateNodeData(final NormalizedNode<?, ?> node,
193 final LeafRefContext referencedByCtx, final LeafRefContext referencingCtx,
194 final ModificationType modificationType, final YangInstanceIdentifier current) {
196 if (node instanceof LeafNode) {
197 final LeafNode<?> leaf = (LeafNode<?>) node;
199 if (referencedByCtx != null && referencedByCtx.isReferenced()) {
200 validateLeafRefTargetNodeData(leaf, referencedByCtx,
203 if (referencingCtx != null && referencingCtx.isReferencing()) {
204 validateLeafRefNodeData(leaf, referencingCtx, modificationType,
211 if (node instanceof LeafSetNode) {
212 final LeafSetNode<?> leafSet = (LeafSetNode<?>) node;
214 if (referencedByCtx == null && referencingCtx == null) {
218 final Iterable<? extends NormalizedNode<?, ?>> leafSetEntries = leafSet
220 for (final NormalizedNode<?, ?> leafSetEntry : leafSetEntries) {
221 if (referencedByCtx != null && referencedByCtx.isReferenced()) {
222 validateLeafRefTargetNodeData(leafSetEntry,
223 referencedByCtx, modificationType);
225 if (referencingCtx != null && referencingCtx.isReferencing()) {
226 validateLeafRefNodeData(leafSetEntry, referencingCtx,
227 modificationType, current);
234 if (node instanceof ChoiceNode) {
235 final ChoiceNode choice = (ChoiceNode) node;
236 final Iterable<DataContainerChild<? extends PathArgument, ?>> childs = choice
238 for (final DataContainerChild<? extends PathArgument, ?> dataContainerChild : childs) {
239 final QName qname = dataContainerChild.getNodeType();
241 LeafRefContext childReferencedByCtx = null;
242 LeafRefContext childReferencingCtx = null;
243 if (referencedByCtx != null) {
244 childReferencedByCtx = findReferencedByCtxUnderChoice(
245 referencedByCtx, qname);
247 if (referencingCtx != null) {
248 childReferencingCtx = findReferencingCtxUnderChoice(
249 referencingCtx, qname);
251 if (childReferencedByCtx != null || childReferencingCtx != null) {
252 final YangInstanceIdentifier childYangInstanceIdentifier = current
253 .node(dataContainerChild.getIdentifier());
254 validateNodeData(dataContainerChild, childReferencedByCtx,
255 childReferencingCtx, modificationType,
256 childYangInstanceIdentifier);
259 } else if (node instanceof DataContainerNode) {
260 final DataContainerNode<?> dataContainerNode = (DataContainerNode<?>) node;
261 final Iterable<DataContainerChild<? extends PathArgument, ?>> dataContainerChilds = dataContainerNode
264 for (final DataContainerChild<? extends PathArgument, ?> dataContainerChild : dataContainerChilds) {
265 final QName qname = dataContainerChild.getNodeType();
267 LeafRefContext childReferencedByCtx = null;
268 LeafRefContext childReferencingCtx = null;
269 if (referencedByCtx != null) {
270 childReferencedByCtx = referencedByCtx
271 .getReferencedChildByName(qname);
273 if (referencingCtx != null) {
274 childReferencingCtx = referencingCtx
275 .getReferencingChildByName(qname);
277 if (childReferencedByCtx != null || childReferencingCtx != null) {
278 final YangInstanceIdentifier childYangInstanceIdentifier = current
279 .node(dataContainerChild.getIdentifier());
280 validateNodeData(dataContainerChild, childReferencedByCtx,
281 childReferencingCtx, modificationType,
282 childYangInstanceIdentifier);
285 } else if (node instanceof MapNode) {
286 final MapNode map = (MapNode) node;
287 final Iterable<MapEntryNode> mapEntries = map.getValue();
288 for (final MapEntryNode mapEntry : mapEntries) {
289 final Iterable<DataContainerChild<? extends PathArgument, ?>> mapEntryNodes = mapEntry
291 final YangInstanceIdentifier mapEntryYangInstanceIdentifier = current
292 .node(mapEntry.getIdentifier());
293 for (final DataContainerChild<? extends PathArgument, ?> mapEntryNode : mapEntryNodes) {
294 final QName qname = mapEntryNode.getNodeType();
296 LeafRefContext childReferencedByCtx = null;
297 LeafRefContext childReferencingCtx = null;
298 if (referencedByCtx != null) {
299 childReferencedByCtx = referencedByCtx
300 .getReferencedChildByName(qname);
302 if (referencingCtx != null) {
303 childReferencingCtx = referencingCtx
304 .getReferencingChildByName(qname);
306 if (childReferencedByCtx != null
307 || childReferencingCtx != null) {
308 final YangInstanceIdentifier mapEntryNodeYangInstanceIdentifier = mapEntryYangInstanceIdentifier
309 .node(mapEntryNode.getIdentifier());
310 validateNodeData(mapEntryNode, childReferencedByCtx,
311 childReferencingCtx, modificationType,
312 mapEntryNodeYangInstanceIdentifier);
318 // FIXME if(node instance of UnkeyedListNode ...
321 private LeafRefContext findReferencingCtxUnderChoice(
322 final LeafRefContext referencingCtx, final QName qname) {
324 final Map<QName, LeafRefContext> referencingChilds = referencingCtx
325 .getReferencingChilds();
326 final Set<Entry<QName, LeafRefContext>> childs = referencingChilds.entrySet();
327 for (final Entry<QName, LeafRefContext> child : childs) {
328 final LeafRefContext referencingChildByName = child.getValue()
329 .getReferencingChildByName(qname);
330 if (referencingChildByName != null) {
331 return referencingChildByName;
338 private LeafRefContext findReferencedByCtxUnderChoice(
339 final LeafRefContext referencedByCtx, final QName qname) {
341 final Map<QName, LeafRefContext> referencedByChilds = referencedByCtx
342 .getReferencedByChilds();
343 final Set<Entry<QName, LeafRefContext>> childs = referencedByChilds
345 for (final Entry<QName, LeafRefContext> child : childs) {
346 final LeafRefContext referencedByChildByName = child.getValue()
347 .getReferencedChildByName(qname);
348 if (referencedByChildByName != null) {
349 return referencedByChildByName;
356 @SuppressWarnings("rawtypes")
357 private void validateLeafRefTargetNodeData(final NormalizedNode<?, ?> leaf,
358 final LeafRefContext referencedByCtx, final ModificationType modificationType) {
360 final StringBuilder header_log = new StringBuilder();
361 final StringBuilder log = new StringBuilder();
362 header_log.append("Operation [" + modificationType
363 + "] validate data of leafref TARGET node: name["
364 + referencedByCtx.getNodeName() + "] = value["
365 + leaf.getValue() + "]");
367 if (validatedLeafRefCtx.contains(referencedByCtx)) {
368 header_log.append(" -> SKIP: Already validated");
369 LOG.debug(header_log.toString());
373 final Map<QName, LeafRefContext> allReferencedByLeafRefCtxs = referencedByCtx
374 .getAllReferencedByLeafRefCtxs();
376 final HashMap<LeafRefContext, HashSet> leafRefsValues = new HashMap<LeafRefContext, HashSet>();
377 final Collection<LeafRefContext> leafrefs = allReferencedByLeafRefCtxs
379 for (final LeafRefContext leafRefContext : leafrefs) {
380 if (leafRefContext.isReferencing()) {
381 final HashSet<Object> values = new HashSet<>();
383 final SchemaPath leafRefNodeSchemaPath = leafRefContext
384 .getCurrentNodePath();
385 final LeafRefPath leafRefNodePath = LeafRefUtils
386 .schemaPathToLeafRefPath(leafRefNodeSchemaPath,
387 leafRefContext.getLeafRefContextModule());
388 final Iterable<QNameWithPredicate> pathFromRoot = leafRefNodePath
390 addValues(values, tree.getRootNode().getDataAfter(),
391 pathFromRoot, null, QNameWithPredicate.ROOT);
392 leafRefsValues.put(leafRefContext, values);
396 final HashSet<Object> leafRefTargetNodeValues = new HashSet<>();
397 final SchemaPath nodeSchemaPath = referencedByCtx.getCurrentNodePath();
398 final LeafRefPath nodePath = LeafRefUtils.schemaPathToLeafRefPath(
399 nodeSchemaPath, referencedByCtx.getLeafRefContextModule());
400 addValues(leafRefTargetNodeValues, tree.getRootNode().getDataAfter(),
401 nodePath.getPathFromRoot(), null, QNameWithPredicate.ROOT);
403 boolean valid = true;
404 final Set<Entry<LeafRefContext, HashSet>> entrySet = leafRefsValues
406 for (final Entry<LeafRefContext, HashSet> entry : entrySet) {
407 final LeafRefContext leafRefContext = entry.getKey();
408 final HashSet leafRefValuesSet = entry.getValue();
409 for (final Object leafRefsValue : leafRefValuesSet) {
410 if (!leafRefTargetNodeValues.contains(leafRefsValue)) {
412 final StringBuilder sb = createInvalidTargetMessage(leaf,
413 leafRefTargetNodeValues, leafRefContext,
415 log.append(NEW_LINE);
416 log.append(sb.toString());
417 log.append(" -> FAILED");
420 errorsMessages.add(sb.toString());
424 log.append(NEW_LINE);
425 log.append("Valid leafref value [");
426 log.append(leafRefsValue);
428 log.append(" -> OK");
433 header_log.append(valid ? " -> OK" : " -> FAILED");
434 LOG.debug(header_log.append(log.toString()).toString());
436 validatedLeafRefCtx.add(referencedByCtx);
439 private StringBuilder createInvalidTargetMessage(final NormalizedNode<?, ?> leaf,
440 final HashSet<?> leafRefTargetNodeValues, final LeafRefContext leafRefContext,
441 final Object leafRefsValue) {
442 final StringBuilder sb = new StringBuilder();
443 sb.append("Invalid leafref value [");
444 sb.append(leafRefsValue);
446 sb.append(" allowed values ");
447 sb.append(leafRefTargetNodeValues);
448 sb.append(" by validation of leafref TARGET node: ");
449 sb.append(leaf.getNodeType());
450 sb.append(" path of invalid LEAFREF node: ");
451 sb.append(leafRefContext.getCurrentNodePath());
452 sb.append(" leafRef target path: ");
453 sb.append(leafRefContext.getAbsoluteLeafRefTargetPath());
457 private void validateLeafRefNodeData(final NormalizedNode<?, ?> leaf,
458 final LeafRefContext referencingCtx, final ModificationType modificationType,
459 final YangInstanceIdentifier current) {
461 final StringBuilder header_log = new StringBuilder();
462 final StringBuilder log = new StringBuilder();
464 header_log.append("Operation [" + modificationType
465 + "] validate data of LEAFREF node: name["
466 + referencingCtx.getNodeName() + "] = value["
467 + leaf.getValue() + "]");
469 final HashSet<Object> values = new HashSet<>();
470 final LeafRefPath targetPath = referencingCtx.getAbsoluteLeafRefTargetPath();
471 final Iterable<QNameWithPredicate> pathFromRoot = targetPath
474 addValues(values, tree.getRootNode().getDataAfter(), pathFromRoot,
475 current, QNameWithPredicate.ROOT);
477 if (!values.contains(leaf.getValue())) {
478 final StringBuilder sb = createInvalidLeafRefMessage(leaf,
479 referencingCtx, values);
480 errorsMessages.add(sb.toString());
482 header_log.append(" -> FAILED");
483 log.append(sb.toString());
485 header_log.append(" -> OK");
488 LOG.debug(header_log.toString());
489 if (!log.toString().equals(""))
490 LOG.debug(log.toString());
493 private StringBuilder createInvalidLeafRefMessage(
494 final NormalizedNode<?, ?> leaf, final LeafRefContext referencingCtx,
495 final Set<?> values) {
496 final StringBuilder sb = new StringBuilder();
497 sb.append("Invalid leafref value [");
498 sb.append(leaf.getValue());
500 sb.append(" allowed values ");
502 sb.append(" of LEAFREF node: ");
503 sb.append(leaf.getNodeType());
504 sb.append(" leafRef target path: ");
505 sb.append(referencingCtx.getAbsoluteLeafRefTargetPath());
510 private void addValues(final Set<Object> values,
511 final Optional<? extends NormalizedNode<?, ?>> optDataNode,
512 final Iterable<QNameWithPredicate> path, final YangInstanceIdentifier current,
513 final QNameWithPredicate previousQName) {
515 if (!optDataNode.isPresent()) {
518 final NormalizedNode<?, ?> node = optDataNode.get();
520 if (node instanceof LeafNode || node instanceof LeafSetEntryNode) {
521 values.add(node.getValue());
523 } else if (node instanceof LeafSetNode<?>) {
524 final LeafSetNode<?> leafSetNode = (LeafSetNode<?>) node;
525 final Iterable<? extends NormalizedNode<?, ?>> entries = leafSetNode
527 for (final NormalizedNode<?, ?> entry : entries) {
528 values.add(entry.getValue());
533 final Iterator<QNameWithPredicate> iterator = path.iterator();
534 if (!iterator.hasNext()) {
537 final QNameWithPredicate qnameWithPredicate = iterator.next();
538 final QName qName = qnameWithPredicate.getQName();
539 final PathArgument pathArgument = toPathArgument(qName);
541 if (node instanceof DataContainerNode) {
542 final DataContainerNode<?> dataContainerNode = (DataContainerNode<?>) node;
543 final Optional<DataContainerChild<? extends PathArgument, ?>> child = dataContainerNode
544 .getChild(pathArgument);
546 if (child.isPresent()) {
547 addValues(values, child, nextLevel(path), current,
550 final Iterable<ChoiceNode> choiceNodes = getChoiceNodes(dataContainerNode);
551 for (final ChoiceNode choiceNode : choiceNodes) {
552 addValues(values, Optional.of(choiceNode), path, current,
557 } else if (node instanceof MapNode) {
558 final MapNode map = (MapNode) node;
559 final List<QNamePredicate> qNamePredicates = previousQName
560 .getQNamePredicates();
561 if (qNamePredicates.isEmpty() || current == null) {
562 final Iterable<MapEntryNode> value = map.getValue();
563 for (final MapEntryNode mapEntryNode : value) {
564 final Optional<DataContainerChild<? extends PathArgument, ?>> child = mapEntryNode
565 .getChild(pathArgument);
567 if (child.isPresent()) {
568 addValues(values, child, nextLevel(path), current,
571 final Iterable<ChoiceNode> choiceNodes = getChoiceNodes(mapEntryNode);
572 for (final ChoiceNode choiceNode : choiceNodes) {
573 addValues(values, Optional.of(choiceNode), path,
574 current, qnameWithPredicate);
579 final Map<QName, Set<?>> keyValues = new HashMap<QName, Set<?>>();
581 final Iterator<QNamePredicate> predicates = qNamePredicates
583 while (predicates.hasNext()) {
584 final QNamePredicate predicate = predicates.next();
585 final QName identifier = predicate.getIdentifier();
586 final LeafRefPath predicatePathKeyExpression = predicate
587 .getPathKeyExpression();
589 final Set<?> pathKeyExprValues = getPathKeyExpressionValues(
590 predicatePathKeyExpression, current);
592 keyValues.put(identifier, pathKeyExprValues);
595 final Iterable<MapEntryNode> mapEntryNodes = map.getValue();
596 for (final MapEntryNode mapEntryNode : mapEntryNodes) {
597 if (isMatchingPredicate(mapEntryNode, keyValues)) {
598 final Optional<DataContainerChild<? extends PathArgument, ?>> child = mapEntryNode
599 .getChild(pathArgument);
601 if (child.isPresent()) {
602 addValues(values, child, nextLevel(path), current,
605 final Iterable<ChoiceNode> choiceNodes = getChoiceNodes(mapEntryNode);
606 for (final ChoiceNode choiceNode : choiceNodes) {
607 addValues(values, Optional.of(choiceNode),
608 path, current, qnameWithPredicate);
618 private Iterable<ChoiceNode> getChoiceNodes(
619 final DataContainerNode<?> dataContainerNode) {
621 final LinkedList<ChoiceNode> choiceNodes = new LinkedList<ChoiceNode>();
623 final Iterable<DataContainerChild<? extends PathArgument, ?>> childs = dataContainerNode
625 for (final DataContainerChild<? extends PathArgument, ?> child : childs) {
626 if (child instanceof ChoiceNode) {
627 choiceNodes.add((ChoiceNode) child);
633 private boolean isMatchingPredicate(final MapEntryNode mapEntryNode,
634 final Map<QName, Set<?>> allowedKeyValues) {
636 final NodeIdentifierWithPredicates identifier = mapEntryNode.getIdentifier();
637 final Map<QName, Object> entryKeyValues = identifier.getKeyValues();
639 final Set<Entry<QName, Object>> entryKeyValueSet = entryKeyValues.entrySet();
640 for (final Entry<QName, Object> entryKeyValue : entryKeyValueSet) {
641 final QName key = entryKeyValue.getKey();
642 final Object value = entryKeyValue.getValue();
644 final Set<?> allowedValues = allowedKeyValues.get(key);
645 if (allowedValues != null && !allowedValues.contains(value)) {
654 private Set<?> getPathKeyExpressionValues(
655 final LeafRefPath predicatePathKeyExpression,
656 final YangInstanceIdentifier current) {
658 final Optional<NormalizedNode<?, ?>> parent = findParentNode(tree
659 .getRootNode().getDataAfter(), current);
661 final Iterable<QNameWithPredicate> predicatePathExpr = predicatePathKeyExpression
663 final Iterable<QNameWithPredicate> predicatePath = nextLevel(predicatePathExpr);
665 final Set<Object> values = new HashSet<>();
666 if (parent != null) {
667 addValues(values, parent, predicatePath, null,
668 QNameWithPredicate.ROOT);
674 private Optional<NormalizedNode<?, ?>> findParentNode(
675 final Optional<NormalizedNode<?, ?>> root, final YangInstanceIdentifier path) {
676 Optional<NormalizedNode<?, ?>> currentNode = root;
677 final Iterator<PathArgument> pathIterator = path.getPathArguments()
679 while (pathIterator.hasNext()) {
680 final PathArgument childPathArgument = pathIterator.next();
681 if (pathIterator.hasNext() && currentNode.isPresent()) {
682 currentNode = NormalizedNodes.getDirectChild(currentNode.get(),
688 return Optional.absent();
691 private Iterable<QNameWithPredicate> nextLevel(
692 final Iterable<QNameWithPredicate> path) {
693 return Iterables.skip(path, 1);
696 private PathArgument toPathArgument(final QName qName) {
697 return YangInstanceIdentifier.of(qName).getLastPathArgument();