Correct mandatory leaf 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 com.google.common.base.Preconditions;
11 import com.google.common.base.Predicates;
12 import com.google.common.base.Verify;
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.ImmutableMap.Builder;
17 import java.util.Collection;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Map.Entry;
21 import java.util.Optional;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
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.DataTreeConfiguration;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
34 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
35 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
36 import org.opendaylight.yangtools.yang.data.impl.schema.tree.AbstractNodeContainerModificationStrategy.Visible;
37 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
41
42 final class ChoiceModificationStrategy extends Visible<ChoiceSchemaNode> {
43     private static final NormalizedNodeContainerSupport<NodeIdentifier, ChoiceNode> SUPPORT =
44             new NormalizedNodeContainerSupport<>(ChoiceNode.class, ImmutableChoiceNodeBuilder::create,
45                     ImmutableChoiceNodeBuilder::create);
46
47     private final ImmutableMap<PathArgument, ModificationApplyOperation> childNodes;
48     // FIXME: enforce leaves not coming from two case statements at the same time
49     private final ImmutableMap<CaseEnforcer, Collection<CaseEnforcer>> exclusions;
50     private final ImmutableMap<PathArgument, CaseEnforcer> caseEnforcers;
51     private final @NonNull ChoiceNode emptyNode;
52
53     ChoiceModificationStrategy(final ChoiceSchemaNode schema, final DataTreeConfiguration treeConfig) {
54         super(SUPPORT, treeConfig, schema);
55
56         final Builder<PathArgument, ModificationApplyOperation> childBuilder = ImmutableMap.builder();
57         final Builder<PathArgument, CaseEnforcer> enforcerBuilder = ImmutableMap.builder();
58         for (final CaseSchemaNode caze : schema.getCases().values()) {
59             final CaseEnforcer enforcer = CaseEnforcer.forTree(caze, treeConfig);
60             if (enforcer != null) {
61                 for (final Entry<NodeIdentifier, DataSchemaNode> e : enforcer.getChildEntries()) {
62                     childBuilder.put(e.getKey(), SchemaAwareApplyOperation.from(e.getValue(), treeConfig));
63                     enforcerBuilder.put(e.getKey(), enforcer);
64                 }
65                 for (final Entry<AugmentationIdentifier, AugmentationSchemaNode> e
66                         : enforcer.getAugmentationEntries()) {
67                     childBuilder.put(e.getKey(), new AugmentationModificationStrategy(e.getValue(), caze, treeConfig));
68                     enforcerBuilder.put(e.getKey(), enforcer);
69                 }
70             }
71         }
72         childNodes = childBuilder.build();
73         caseEnforcers = enforcerBuilder.build();
74
75         final Map<CaseEnforcer, Collection<CaseEnforcer>> exclusionsBuilder = new HashMap<>();
76         for (final CaseEnforcer e : caseEnforcers.values()) {
77             exclusionsBuilder.put(e, ImmutableList.copyOf(
78                 Collections2.filter(caseEnforcers.values(), Predicates.not(Predicates.equalTo(e)))));
79         }
80         exclusions = ImmutableMap.copyOf(exclusionsBuilder);
81         emptyNode = ImmutableNodes.choiceNode(schema.getQName());
82     }
83
84     @Override
85     Optional<TreeNode> apply(final ModifiedNode modification, final Optional<TreeNode> storeMeta,
86             final Version version) {
87         return AutomaticLifecycleMixin.apply(super::apply, this::applyWrite, emptyNode, modification, storeMeta,
88             version);
89     }
90
91     @Override
92     void checkApplicable(final ModificationPath path, final NodeModification modification,
93             final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
94         AutomaticLifecycleMixin.checkApplicable(super::checkApplicable, emptyNode, path, modification, current,
95             version);
96     }
97
98     @Override
99     public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
100         return Optional.ofNullable(childNodes.get(child));
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.verify(normalizedNode instanceof ChoiceNode);
114         final Collection<DataContainerChild<?, ?>> children = ((ChoiceNode) normalizedNode).getValue();
115         if (!children.isEmpty()) {
116             final DataContainerChild<?, ?> firstChild = children.iterator().next();
117             final CaseEnforcer enforcer = Verify.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 : exclusions.get(enforcer)) {
123                 for (final PathArgument id : other.getAllChildIdentifiers()) {
124                     final Optional<NormalizedNode<?, ?>> maybeChild = NormalizedNodes.getDirectChild(normalizedNode,
125                         id);
126                     Preconditions.checkArgument(!maybeChild.isPresent(),
127                         "Child %s (from case %s) implies non-presence of child %s (from case %s), which is %s",
128                         firstChild.getIdentifier(), enforcer, id, other, maybeChild.orElse(null));
129                 }
130             }
131
132             // Make sure all mandatory children are present
133             enforcer.enforceOnTreeNode(normalizedNode);
134         }
135     }
136
137     @Override
138     protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
139         final TreeNode ret = super.applyMerge(modification, currentMeta, version);
140         enforceCases(ret);
141         return ret;
142     }
143
144     @Override
145     protected TreeNode applyWrite(final ModifiedNode modification,  final NormalizedNode<?, ?> newValue,
146             final Optional<TreeNode> currentMeta, final Version version) {
147         final TreeNode ret = super.applyWrite(modification, newValue, currentMeta, version);
148         enforceCases(ret);
149         return ret;
150     }
151
152     @Override
153     protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
154         final TreeNode ret = super.applyTouch(modification, currentMeta, version);
155         enforceCases(ret);
156         return ret;
157     }
158 }
159