Make empty lists and choices disappear
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / 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.impl.schema.tree;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Predicates;
14 import com.google.common.base.Verify;
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.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
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.api.schema.tree.DataTreeConfiguration;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
35 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
36 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
37 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
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 AbstractNodeContainerModificationStrategy {
44     private final Map<PathArgument, ModificationApplyOperation> childNodes;
45     // FIXME: enforce leaves not coming from two case statements at the same time
46     private final Map<CaseEnforcer, Collection<CaseEnforcer>> exclusions;
47     private final Map<PathArgument, CaseEnforcer> caseEnforcers;
48     private final ChoiceNode emptyNode;
49
50     ChoiceModificationStrategy(final ChoiceSchemaNode schema, final DataTreeConfiguration treeConfig) {
51         super(ChoiceNode.class, treeConfig);
52
53         final Builder<PathArgument, ModificationApplyOperation> childBuilder = ImmutableMap.builder();
54         final Builder<PathArgument, CaseEnforcer> enforcerBuilder = ImmutableMap.builder();
55         for (final CaseSchemaNode caze : schema.getCases().values()) {
56             final CaseEnforcer enforcer = CaseEnforcer.forTree(caze, treeConfig);
57             if (enforcer != null) {
58                 for (final Entry<NodeIdentifier, DataSchemaNode> e : enforcer.getChildEntries()) {
59                     childBuilder.put(e.getKey(), SchemaAwareApplyOperation.from(e.getValue(), treeConfig));
60                     enforcerBuilder.put(e.getKey(), enforcer);
61                 }
62                 for (final Entry<AugmentationIdentifier, AugmentationSchemaNode> e
63                         : enforcer.getAugmentationEntries()) {
64                     childBuilder.put(e.getKey(), new AugmentationModificationStrategy(e.getValue(), caze, treeConfig));
65                     enforcerBuilder.put(e.getKey(), enforcer);
66                 }
67             }
68         }
69         childNodes = childBuilder.build();
70         caseEnforcers = enforcerBuilder.build();
71
72         final Map<CaseEnforcer, Collection<CaseEnforcer>> exclusionsBuilder = new HashMap<>();
73         for (final CaseEnforcer e : caseEnforcers.values()) {
74             exclusionsBuilder.put(e, ImmutableList.copyOf(
75                 Collections2.filter(caseEnforcers.values(), Predicates.not(Predicates.equalTo(e)))));
76         }
77         exclusions = ImmutableMap.copyOf(exclusionsBuilder);
78         emptyNode = ImmutableNodes.choiceNode(schema.getQName());
79     }
80
81
82     @Override
83     Optional<TreeNode> apply(final ModifiedNode modification, final Optional<TreeNode> storeMeta,
84             final Version version) {
85         return AutomaticLifecycleMixin.apply(super::apply, emptyNode, modification, storeMeta, version);
86     }
87
88     @Override
89     void checkApplicable(final ModificationPath path, final NodeModification modification,
90             final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
91         AutomaticLifecycleMixin.checkApplicable(super::checkApplicable, emptyNode, path, modification, current,
92             version);
93     }
94
95     @Override
96     public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
97         return Optional.ofNullable(childNodes.get(child));
98     }
99
100     @Override
101     @SuppressWarnings("rawtypes")
102     protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
103         checkArgument(original instanceof ChoiceNode);
104         return ImmutableChoiceNodeBuilder.create((ChoiceNode) original);
105     }
106
107     @Override
108     void verifyStructure(final NormalizedNode<?, ?> writtenValue, final boolean verifyChildren) {
109         if (verifyChildrenStructure() && verifyChildren) {
110             enforceCases(writtenValue);
111         }
112         super.verifyStructure(writtenValue, verifyChildren);
113     }
114
115     private void enforceCases(final TreeNode tree) {
116         enforceCases(tree.getData());
117     }
118
119     private void enforceCases(final NormalizedNode<?, ?> normalizedNode) {
120         Verify.verify(normalizedNode instanceof ChoiceNode);
121         final Collection<DataContainerChild<?, ?>> children = ((ChoiceNode) normalizedNode).getValue();
122         if (!children.isEmpty()) {
123             final DataContainerChild<?, ?> firstChild = children.iterator().next();
124             final CaseEnforcer enforcer = Verify.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 : exclusions.get(enforcer)) {
130                 for (final PathArgument id : other.getAllChildIdentifiers()) {
131                     final Optional<NormalizedNode<?, ?>> maybeChild = NormalizedNodes.getDirectChild(normalizedNode,
132                         id);
133                     Preconditions.checkArgument(!maybeChild.isPresent(),
134                         "Child %s (from case %s) implies non-presence of child %s (from case %s), which is %s",
135                         firstChild.getIdentifier(), enforcer, id, other, maybeChild.orElse(null));
136                 }
137             }
138
139             // Make sure all mandatory children are present
140             enforcer.enforceOnTreeNode(normalizedNode);
141         }
142     }
143
144     @Override
145     protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
146         final TreeNode ret = super.applyMerge(modification, currentMeta, version);
147         enforceCases(ret);
148         return ret;
149     }
150
151     @Override
152     protected TreeNode applyWrite(final ModifiedNode modification, final Optional<TreeNode> currentMeta,
153             final Version version) {
154         final TreeNode ret = super.applyWrite(modification, currentMeta, version);
155         enforceCases(ret);
156         return ret;
157     }
158
159     @Override
160     protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
161         final TreeNode ret = super.applyTouch(modification, currentMeta, version);
162         enforceCases(ret);
163         return ret;
164     }
165
166     @Override
167     protected NormalizedNode<?, ?> createEmptyValue(final NormalizedNode<?, ?> original) {
168         checkArgument(original instanceof ChoiceNode);
169         return ImmutableChoiceNodeBuilder.create().withNodeIdentifier(((ChoiceNode) original).getIdentifier()).build();
170     }
171 }
172