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.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
20 import org.opendaylight.yangtools.yang.data.tree.api.TreeType;
21 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
24 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraintAware;
26 import org.opendaylight.yangtools.yang.model.api.MandatoryAware;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 // TODO: would making this Serializable be useful (for Functions and similar?)
31 final class MandatoryLeafEnforcer implements Immutable {
32 private static final Logger LOG = LoggerFactory.getLogger(MandatoryLeafEnforcer.class);
34 private final ImmutableList<MandatoryDescendant> mandatoryNodes;
36 private MandatoryLeafEnforcer(final ImmutableList<MandatoryDescendant> mandatoryNodes) {
37 this.mandatoryNodes = requireNonNull(mandatoryNodes);
40 static @Nullable MandatoryLeafEnforcer forContainer(final DataNodeContainer schema,
41 final DataTreeConfiguration treeConfig) {
42 if (!treeConfig.isMandatoryNodesValidationEnabled()) {
46 final var builder = ImmutableList.<MandatoryDescendant>builder();
47 findMandatoryNodes(builder, YangInstanceIdentifier.empty(), schema, treeConfig.getTreeType());
48 final var mandatoryNodes = builder.build();
49 return mandatoryNodes.isEmpty() ? null : new MandatoryLeafEnforcer(mandatoryNodes);
52 void enforceOnData(final NormalizedNode data) {
53 mandatoryNodes.forEach(node -> node.enforceOnData(data));
56 void enforceOnTreeNode(final TreeNode tree) {
57 enforceOnData(tree.getData());
60 private static void findMandatoryNodes(final Builder<MandatoryDescendant> builder, final YangInstanceIdentifier id,
61 final DataNodeContainer schema, final TreeType type) {
62 for (final DataSchemaNode child : schema.getChildNodes()) {
63 if (SchemaAwareApplyOperation.belongsToTree(type, child)) {
64 if (child instanceof ContainerSchemaNode container) {
65 if (!container.isPresenceContainer()) {
66 // the container is either:
67 // - not in an augmented subtree and not augmenting
68 // - in an augmented subtree
69 // in both cases just append the NodeID to the ongoing ID and continue the search.
70 findMandatoryNodes(builder, id.node(NodeIdentifier.create(container.getQName())), container,
74 boolean needEnforce = child instanceof MandatoryAware aware && aware.isMandatory();
75 if (!needEnforce && child instanceof ElementCountConstraintAware aware) {
76 needEnforce = aware.getElementCountConstraint()
78 final Integer min = constraint.getMinElements();
79 return min != null && min > 0;
81 .orElse(Boolean.FALSE);
84 final MandatoryDescendant desc = MandatoryDescendant.create(id, schema, child);
85 LOG.debug("Adding mandatory child {}", desc);