2 * Copyright (c) 2014 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.tree.impl;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableList.Builder;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.yangtools.concepts.Immutable;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
21 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
22 import org.opendaylight.yangtools.yang.data.tree.api.TreeType;
23 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
26 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraintAware;
27 import org.opendaylight.yangtools.yang.model.api.MandatoryAware;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 // TODO: would making this Serializable be useful (for Functions and similar?)
32 final class MandatoryLeafEnforcer implements Immutable {
33 private static final Logger LOG = LoggerFactory.getLogger(MandatoryLeafEnforcer.class);
35 // FIXME: Well, there is still room to optimize footprint and performance. This list of lists can have overlaps,
36 // i.e. in the case of:
39 // presence "something";
41 // leaf baz { type string; mandatory true; }
42 // leaf xyzzy { type string; mandatory true; }
46 // we will have populated:
49 // and end up looking up 'foo' twice. A better alternative to the inner list would be a recursive tree
50 // structure with terminal and non-terminal nodes:
56 // And then we would have a List of (unique) mandatory immediate descendants -- and recurse through them.
58 // For absolute best footprint this means keeping a PathArgument (for terminal) and perhaps an
59 // ImmutableList<Object> (for non-terminal) nodes -- since PathArgument and ImmutableList are disjunct, we
60 // can make a quick instanceof determination to guide us.
62 // We then can short-circuit to using NormalizedNodes.getDirectChild(), or better yet, we can talk directly
63 // to DistinctNodeContainer.childByArg() -- which seems to be the context in which we are invoked.
64 // At any rate, we would perform lookups step by step -- and throw an exception when to find a child.
65 // Obviously in that case we need to reconstruct the full path -- but that is an error path and we can expend
66 // some time to get at that. A simple Deque<PathArgument> seems like a reasonable compromise to track what we
68 private final ImmutableList<ImmutableList<PathArgument>> mandatoryNodes;
70 private MandatoryLeafEnforcer(final ImmutableList<ImmutableList<PathArgument>> mandatoryNodes) {
71 this.mandatoryNodes = requireNonNull(mandatoryNodes);
74 static @Nullable MandatoryLeafEnforcer forContainer(final DataNodeContainer schema,
75 final DataTreeConfiguration treeConfig) {
76 if (!treeConfig.isMandatoryNodesValidationEnabled()) {
80 final var builder = ImmutableList.<ImmutableList<PathArgument>>builder();
81 findMandatoryNodes(builder, YangInstanceIdentifier.of(), schema, treeConfig.getTreeType());
82 final var mandatoryNodes = builder.build();
83 return mandatoryNodes.isEmpty() ? null : new MandatoryLeafEnforcer(mandatoryNodes);
86 void enforceOnData(final NormalizedNode data) {
87 for (var path : mandatoryNodes) {
88 if (!NormalizedNodes.findNode(data, path).isPresent()) {
89 throw new IllegalArgumentException(String.format("Node %s is missing mandatory descendant %s",
90 data.name(), YangInstanceIdentifier.of(path)));
95 void enforceOnTreeNode(final TreeNode tree) {
96 enforceOnData(tree.getData());
99 private static void findMandatoryNodes(final Builder<ImmutableList<PathArgument>> builder,
100 final YangInstanceIdentifier id, final DataNodeContainer schema, final TreeType type) {
101 for (var child : schema.getChildNodes()) {
102 if (SchemaAwareApplyOperation.belongsToTree(type, child)) {
103 if (child instanceof ContainerSchemaNode container) {
104 if (!container.isPresenceContainer()) {
105 // the container is either:
106 // - not in an augmented subtree and not augmenting
107 // - in an augmented subtree
108 // in both cases just append the NodeID to the ongoing ID and continue the search.
109 findMandatoryNodes(builder, id.node(NodeIdentifier.create(container.getQName())), container,
113 boolean needEnforce = child instanceof MandatoryAware aware && aware.isMandatory();
114 if (!needEnforce && child instanceof ElementCountConstraintAware aware) {
115 needEnforce = aware.getElementCountConstraint()
117 final Integer min = constraint.getMinElements();
118 return min != null && min > 0;
120 .orElse(Boolean.FALSE);
123 final var desc = id.node(NodeIdentifier.create(child.getQName()));
124 LOG.debug("Adding mandatory child {}", desc);
125 builder.add(ImmutableList.copyOf(desc.getPathArguments()));