*/
package org.opendaylight.yangtools.yang.data.impl.leafref;
+import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import java.util.ArrayList;
import java.util.Collection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+// FIXME: 3.0.0: Rename to LeafRefValidation
public final class LeafRefValidatation {
private static final Logger LOG = LoggerFactory.getLogger(LeafRefValidatation.class);
}
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;
+ return tree.getRootNode().getDataAfter()
+ .map(root -> computeValues(root, context.getLeafRefNodePath().getPathFromRoot(), null))
+ .orElse(ImmutableSet.of());
}
private void validateLeafRefNodeData(final NormalizedNode<?, ?> leaf, final LeafRefContext referencingCtx,
final ModificationType modificationType, final YangInstanceIdentifier current) {
- final HashSet<Object> values = new HashSet<>();
- addValues(values, tree.getRootNode().getDataAfter(),
- referencingCtx.getAbsoluteLeafRefTargetPath().getPathFromRoot(), current, QNameWithPredicate.ROOT);
-
+ final Set<Object> values = tree.getRootNode().getDataAfter().map(
+ root -> computeValues(root, referencingCtx.getAbsoluteLeafRefTargetPath().getPathFromRoot(), current))
+ .orElse(ImmutableSet.of());
if (values.contains(leaf.getValue())) {
LOG.debug("Operation [{}] validate data of LEAFREF node: name[{}] = value[{}] {}", modificationType,
referencingCtx.getNodeName(), leaf.getValue(), SUCCESS);
referencingCtx.getAbsoluteLeafRefTargetPath()));
}
- private void addValues(final Set<Object> values, final Optional<? extends NormalizedNode<?, ?>> optDataNode,
+ private Set<Object> computeValues(final NormalizedNode<?, ?> node, final Iterable<QNameWithPredicate> path,
+ final YangInstanceIdentifier current) {
+ final HashSet<Object> values = new HashSet<>();
+ addValues(values, node, path, current, QNameWithPredicate.ROOT);
+ return values;
+ }
+
+ private void addValues(final Set<Object> values, final NormalizedNode<?, ?> node,
final Iterable<QNameWithPredicate> path, final YangInstanceIdentifier current,
final QNameWithPredicate previousQName) {
-
- if (!optDataNode.isPresent()) {
- return;
- }
- final NormalizedNode<?, ?> node = optDataNode.get();
if (node instanceof ValueNode) {
values.add(node.getValue());
return;
.getChild(pathArgument);
if (child.isPresent()) {
- addValues(values, child, nextLevel(path), current, qnameWithPredicate);
+ addValues(values, child.get(), nextLevel(path), current, qnameWithPredicate);
} else {
for (final ChoiceNode choiceNode : getChoiceNodes(dataContainerNode)) {
- addValues(values, Optional.of(choiceNode), path, current,
- qnameWithPredicate);
+ addValues(values, choiceNode, path, current, qnameWithPredicate);
}
}
.getChild(pathArgument);
if (child.isPresent()) {
- addValues(values, child, nextLevel(path), current, qnameWithPredicate);
+ addValues(values, child.get(), nextLevel(path), current, qnameWithPredicate);
} else {
for (final ChoiceNode choiceNode : getChoiceNodes(mapEntryNode)) {
- addValues(values, Optional.of(choiceNode), path, current, qnameWithPredicate);
+ addValues(values, choiceNode, path, current, qnameWithPredicate);
}
}
}
while (predicates.hasNext()) {
final QNamePredicate predicate = predicates.next();
final QName identifier = predicate.getIdentifier();
- final LeafRefPath predicatePathKeyExpression = predicate
- .getPathKeyExpression();
-
- final Set<?> pathKeyExprValues = getPathKeyExpressionValues(
- predicatePathKeyExpression, current);
+ final LeafRefPath predicatePathKeyExpression = predicate.getPathKeyExpression();
+ final Set<?> pathKeyExprValues = getPathKeyExpressionValues(predicatePathKeyExpression, current);
keyValues.put(identifier, pathKeyExprValues);
}
.getChild(pathArgument);
if (child.isPresent()) {
- addValues(values, child, nextLevel(path), current, qnameWithPredicate);
+ addValues(values, child.get(), nextLevel(path), current, qnameWithPredicate);
} else {
for (final ChoiceNode choiceNode : getChoiceNodes(mapEntryNode)) {
- addValues(values, Optional.of(choiceNode), path, current, qnameWithPredicate);
+ addValues(values, choiceNode, path, current, qnameWithPredicate);
}
}
}
private Set<?> getPathKeyExpressionValues(final LeafRefPath predicatePathKeyExpression,
final YangInstanceIdentifier current) {
- final Optional<NormalizedNode<?, ?>> parent = findParentNode(tree.getRootNode().getDataAfter(), current);
- final Set<Object> values = new HashSet<>();
- addValues(values, parent, nextLevel(predicatePathKeyExpression.getPathFromRoot()), null,
- QNameWithPredicate.ROOT);
-
- return values;
+ return findParentNode(tree.getRootNode().getDataAfter(), current)
+ .map(parent -> computeValues(parent, nextLevel(predicatePathKeyExpression.getPathFromRoot()), null))
+ .orElse(ImmutableSet.of());
}
private static Optional<NormalizedNode<?, ?>> findParentNode(