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