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