BUG-4355: mandatory node presence enforcement
[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.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.base.Predicates;
15 import com.google.common.base.Verify;
16 import com.google.common.collect.Collections2;
17 import com.google.common.collect.ImmutableList;
18 import com.google.common.collect.ImmutableMap;
19 import com.google.common.collect.ImmutableMap.Builder;
20 import java.util.Collection;
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.Map.Entry;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
26 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
34 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
35 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
36 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
38
39 final class ChoiceModificationStrategy extends AbstractNodeContainerModificationStrategy {
40     private final Map<PathArgument, ModificationApplyOperation> childNodes;
41     // FIXME: enforce leaves not coming from two case statements at the same time
42     private final Map<CaseEnforcer, Collection<CaseEnforcer>> exclusions;
43     private final Map<PathArgument, CaseEnforcer> caseEnforcers;
44
45     ChoiceModificationStrategy(final ChoiceSchemaNode schemaNode, final TreeType treeType) {
46         super(ChoiceNode.class, treeType);
47
48         final Builder<PathArgument, ModificationApplyOperation> childBuilder = ImmutableMap.builder();
49         final Builder<PathArgument, CaseEnforcer> enforcerBuilder = ImmutableMap.builder();
50         for (final ChoiceCaseNode caze : schemaNode.getCases()) {
51             final CaseEnforcer enforcer = CaseEnforcer.forTree(caze, treeType);
52             if (enforcer != null) {
53                 for (final Entry<NodeIdentifier, DataSchemaNode> e : enforcer.getChildEntries()) {
54                     childBuilder.put(e.getKey(), SchemaAwareApplyOperation.from(e.getValue(), treeType));
55                     enforcerBuilder.put(e.getKey(), enforcer);
56                 }
57             }
58         }
59         childNodes = childBuilder.build();
60         caseEnforcers = enforcerBuilder.build();
61
62         final Map<CaseEnforcer, Collection<CaseEnforcer>> exclusionsBuilder = new HashMap<>();
63         for (CaseEnforcer e : caseEnforcers.values()) {
64             exclusionsBuilder.put(e, ImmutableList.copyOf(
65                 Collections2.filter(caseEnforcers.values(), Predicates.not(Predicates.equalTo(e)))));
66         }
67         exclusions = ImmutableMap.copyOf(exclusionsBuilder);
68     }
69
70     @Override
71     public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
72         return Optional.fromNullable(childNodes.get(child));
73     }
74
75     @Override
76     @SuppressWarnings("rawtypes")
77     protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
78         checkArgument(original instanceof ChoiceNode);
79         return ImmutableChoiceNodeBuilder.create((ChoiceNode) original);
80     }
81
82     @Override
83     void verifyStructure(final NormalizedNode<?, ?> writtenValue, final boolean verifyChildren) {
84         if(verifyChildrenStructure() && verifyChildren) {
85             enforceCases(writtenValue);
86         }
87         super.verifyStructure(writtenValue, verifyChildren);
88     }
89
90     private void enforceCases(final TreeNode tree) {
91         enforceCases(tree.getData());
92     }
93
94     private void enforceCases(final NormalizedNode<?, ?> normalizedNode) {
95         Verify.verify(normalizedNode instanceof ChoiceNode);
96         final Collection<DataContainerChild<?, ?>> children = ((ChoiceNode) normalizedNode).getValue();
97         if (!children.isEmpty()) {
98             final DataContainerChild<?, ?> firstChild = children.iterator().next();
99             final CaseEnforcer enforcer = caseEnforcers.get(firstChild.getIdentifier());
100             Verify.verifyNotNull(enforcer, "Case enforcer cannot be null. Most probably, child node %s of choice node %s does not belong in current tree type.", firstChild.getIdentifier(), normalizedNode.getIdentifier());
101
102             // Make sure no leaves from other cases are present
103             for (CaseEnforcer other : exclusions.get(enforcer)) {
104                 for (NodeIdentifier id : other.getChildIdentifiers()) {
105                     final Optional<NormalizedNode<?, ?>> maybeChild = NormalizedNodes.getDirectChild(normalizedNode, id);
106                     Preconditions.checkArgument(!maybeChild.isPresent(),
107                         "Child %s (from case %s) implies non-presence of child %s (from case %s), which is %s",
108                         firstChild.getIdentifier(), enforcer, id, other, maybeChild.orNull());
109                 }
110             }
111
112             // Make sure all mandatory children are present
113             enforcer.enforceOnTreeNode(normalizedNode);
114         }
115     }
116
117     @Override
118     protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
119         final TreeNode ret = super.applyMerge(modification, currentMeta, version);
120         enforceCases(ret);
121         return ret;
122     }
123
124     @Override
125     protected TreeNode applyWrite(final ModifiedNode modification, final Optional<TreeNode> currentMeta,
126             final Version version) {
127         final TreeNode ret = super.applyWrite(modification, currentMeta, version);
128         enforceCases(ret);
129         return ret;
130     }
131
132     @Override
133     protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
134         final TreeNode ret = super.applyTouch(modification, currentMeta, version);
135         enforceCases(ret);
136         return ret;
137     }
138 }