Verify exclusion consistency
[yangtools.git] / data / yang-data-tree-ri / src / main / java / org / opendaylight / yangtools / yang / data / tree / impl / ChoiceModificationStrategy.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 com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verify;
12 import static com.google.common.base.Verify.verifyNotNull;
13
14 import com.google.common.base.Predicates;
15 import com.google.common.collect.Collections2;
16 import com.google.common.collect.ImmutableList;
17 import com.google.common.collect.ImmutableMap;
18 import com.google.common.collect.ImmutableMap.Builder;
19 import java.util.Collection;
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.Map.Entry;
23 import java.util.Optional;
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
28 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
32 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
34 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
35 import org.opendaylight.yangtools.yang.data.tree.impl.AbstractNodeContainerModificationStrategy.Visible;
36 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
37 import org.opendaylight.yangtools.yang.data.tree.impl.node.Version;
38 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
42
43 final class ChoiceModificationStrategy extends Visible<ChoiceSchemaNode> {
44     private static final NormalizedNodeContainerSupport<NodeIdentifier, ChoiceNode> SUPPORT =
45             new NormalizedNodeContainerSupport<>(ChoiceNode.class, ImmutableChoiceNodeBuilder::create,
46                     ImmutableChoiceNodeBuilder::create);
47
48     private final ImmutableMap<PathArgument, ModificationApplyOperation> childNodes;
49     // FIXME: enforce leaves not coming from two case statements at the same time
50     private final ImmutableMap<CaseEnforcer, Collection<CaseEnforcer>> exclusions;
51     private final ImmutableMap<PathArgument, CaseEnforcer> caseEnforcers;
52     private final @NonNull ChoiceNode emptyNode;
53
54     ChoiceModificationStrategy(final ChoiceSchemaNode schema, final DataTreeConfiguration treeConfig) {
55         super(SUPPORT, treeConfig, schema);
56
57         final Builder<PathArgument, ModificationApplyOperation> childBuilder = ImmutableMap.builder();
58         final Builder<PathArgument, CaseEnforcer> enforcerBuilder = ImmutableMap.builder();
59         for (final CaseSchemaNode caze : schema.getCases()) {
60             final CaseEnforcer enforcer = CaseEnforcer.forTree(caze, treeConfig);
61             if (enforcer != null) {
62                 for (final Entry<NodeIdentifier, DataSchemaNode> entry : enforcer.getChildEntries()) {
63                     final ModificationApplyOperation childOper;
64                     try {
65                         childOper = SchemaAwareApplyOperation.from(entry.getValue(), treeConfig);
66                     } catch (ExcludedDataSchemaNodeException e) {
67                         // This should never happen as enforcer performs filtering
68                         throw new IllegalStateException("Enforcer references out-of-tree child " + entry, e);
69                     }
70
71                     childBuilder.put(entry.getKey(), childOper);
72                     enforcerBuilder.put(entry.getKey(), enforcer);
73                 }
74                 for (final Entry<AugmentationIdentifier, AugmentationSchemaNode> e
75                         : enforcer.getAugmentationEntries()) {
76                     childBuilder.put(e.getKey(), new AugmentationModificationStrategy(e.getValue(), caze, treeConfig));
77                     enforcerBuilder.put(e.getKey(), enforcer);
78                 }
79             }
80         }
81         childNodes = childBuilder.build();
82         caseEnforcers = enforcerBuilder.build();
83
84         final Map<CaseEnforcer, Collection<CaseEnforcer>> exclusionsBuilder = new HashMap<>();
85         for (final CaseEnforcer e : caseEnforcers.values()) {
86             exclusionsBuilder.put(e, ImmutableList.copyOf(
87                 Collections2.filter(caseEnforcers.values(), Predicates.not(Predicates.equalTo(e)))));
88         }
89         exclusions = ImmutableMap.copyOf(exclusionsBuilder);
90         emptyNode = ImmutableNodes.choiceNode(schema.getQName());
91     }
92
93     @Override
94     Optional<? extends TreeNode> apply(final ModifiedNode modification, final Optional<? extends TreeNode> storeMeta,
95             final Version version) {
96         return AutomaticLifecycleMixin.apply(super::apply, this::applyWrite, emptyNode, modification, storeMeta,
97             version);
98     }
99
100     @Override
101     TreeNode defaultTreeNode() {
102         return defaultTreeNode(emptyNode);
103     }
104
105     @Override
106     public ModificationApplyOperation childByArg(final PathArgument arg) {
107         return childNodes.get(arg);
108     }
109
110     @Override
111     void optionalVerifyValueChildren(final NormalizedNode writtenValue) {
112         enforceCases(writtenValue);
113     }
114
115     private void enforceCases(final TreeNode tree) {
116         enforceCases(tree.getData());
117     }
118
119     private void enforceCases(final NormalizedNode normalizedNode) {
120         verify(normalizedNode instanceof ChoiceNode);
121         final var children = ((ChoiceNode) normalizedNode).body();
122         if (!children.isEmpty()) {
123             final DataContainerChild firstChild = children.iterator().next();
124             final CaseEnforcer enforcer = verifyNotNull(caseEnforcers.get(firstChild.getIdentifier()),
125                 "Case enforcer cannot be null. Most probably, child node %s of choice node %s does not belong "
126                 + "in current tree type.", firstChild.getIdentifier(), normalizedNode.getIdentifier());
127
128             // Make sure no leaves from other cases are present
129             for (final CaseEnforcer other : verifyNotNull(exclusions.get(enforcer))) {
130                 for (final PathArgument id : other.getAllChildIdentifiers()) {
131                     final Optional<NormalizedNode> maybeChild = NormalizedNodes.getDirectChild(normalizedNode, id);
132                     checkArgument(!maybeChild.isPresent(),
133                         "Child %s (from case %s) implies non-presence of child %s (from case %s), which is %s",
134                         firstChild.getIdentifier(), enforcer, id, other, maybeChild.orElse(null));
135                 }
136             }
137
138             // Make sure all mandatory children are present
139             enforcer.enforceOnTreeNode(normalizedNode);
140         }
141     }
142
143     @Override
144     protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
145         final TreeNode ret = super.applyMerge(modification, currentMeta, version);
146         enforceCases(ret);
147         return ret;
148     }
149
150     @Override
151     protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode newValue,
152             final Optional<? extends TreeNode> currentMeta, final Version version) {
153         final TreeNode ret = super.applyWrite(modification, newValue, currentMeta, version);
154         enforceCases(ret);
155         return ret;
156     }
157
158     @Override
159     protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
160         final TreeNode ret = super.applyTouch(modification, currentMeta, version);
161         enforceCases(ret);
162         return ret;
163     }
164 }
165