Add NormalizedNode.BuilderFactory
[yangtools.git] / data / yang-data-tree-ri / src / main / java / org / opendaylight / yangtools / yang / data / tree / impl / MandatoryLeafEnforcer.java
1 /*
2  * Copyright (c) 2014 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.tree.impl;
9
10 import static java.util.Objects.requireNonNull;
11
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;
30
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);
34
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:
37     //
38     //          container foo {
39     //            presence "something";
40     //            container bar {
41     //              leaf baz { type string; mandatory true; }
42     //              leaf xyzzy { type string; mandatory true; }
43     //            }
44     //          }
45     //
46     //        we will have populated:
47     //          [ foo, bar ]
48     //          [ foo, xyzzy ]
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:
51     //
52     //          non-terminal(foo):
53     //            terminal(bar)
54     //            terminal(xyzzy)
55     //
56     //        And then we would have a List of (unique) mandatory immediate descendants -- and recurse through them.
57     //
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.
61     //
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
67     //        went through.
68     private final ImmutableList<ImmutableList<PathArgument>> mandatoryNodes;
69
70     private MandatoryLeafEnforcer(final ImmutableList<ImmutableList<PathArgument>> mandatoryNodes) {
71         this.mandatoryNodes = requireNonNull(mandatoryNodes);
72     }
73
74     static @Nullable MandatoryLeafEnforcer forContainer(final DataNodeContainer schema,
75             final DataTreeConfiguration treeConfig) {
76         if (!treeConfig.isMandatoryNodesValidationEnabled()) {
77             return null;
78         }
79
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);
84     }
85
86     void enforceOnData(final NormalizedNode data) {
87         for (var path : mandatoryNodes) {
88             if (NormalizedNodes.findNode(data, path).isEmpty()) {
89                 throw new IllegalArgumentException("Node " + data.name() + " is missing mandatory descendant "
90                     + YangInstanceIdentifier.of(path));
91             }
92         }
93     }
94
95     void enforceOnTreeNode(final TreeNode tree) {
96         enforceOnData(tree.getData());
97     }
98
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,
110                             type);
111                     }
112                 } else {
113                     boolean needEnforce = child instanceof MandatoryAware aware && aware.isMandatory();
114                     if (!needEnforce && child instanceof ElementCountConstraintAware aware) {
115                         needEnforce = aware.getElementCountConstraint()
116                             .map(constraint -> {
117                                 final Integer min = constraint.getMinElements();
118                                 return min != null && min > 0;
119                             })
120                             .orElse(Boolean.FALSE);
121                     }
122                     if (needEnforce) {
123                         final var desc = id.node(NodeIdentifier.create(child.getQName()));
124                         LOG.debug("Adding mandatory child {}", desc);
125                         builder.add(ImmutableList.copyOf(desc.getPathArguments()));
126                     }
127                 }
128             }
129         }
130     }
131 }