8230ecc0b5c655015b300d64a3c89fadf6cc4e61
[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.spi.TreeNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
33 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
34 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
35 import org.opendaylight.yangtools.yang.data.impl.schema.tree.AbstractNodeContainerModificationStrategy.Visible;
36 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
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> e : enforcer.getChildEntries()) {
61                     childBuilder.put(e.getKey(), SchemaAwareApplyOperation.from(e.getValue(), treeConfig));
62                     enforcerBuilder.put(e.getKey(), enforcer);
63                 }
64                 for (final Entry<AugmentationIdentifier, AugmentationSchemaNode> e
65                         : enforcer.getAugmentationEntries()) {
66                     childBuilder.put(e.getKey(), new AugmentationModificationStrategy(e.getValue(), caze, treeConfig));
67                     enforcerBuilder.put(e.getKey(), enforcer);
68                 }
69             }
70         }
71         childNodes = childBuilder.build();
72         caseEnforcers = enforcerBuilder.build();
73
74         final Map<CaseEnforcer, Collection<CaseEnforcer>> exclusionsBuilder = new HashMap<>();
75         for (final CaseEnforcer e : caseEnforcers.values()) {
76             exclusionsBuilder.put(e, ImmutableList.copyOf(
77                 Collections2.filter(caseEnforcers.values(), Predicates.not(Predicates.equalTo(e)))));
78         }
79         exclusions = ImmutableMap.copyOf(exclusionsBuilder);
80         emptyNode = ImmutableNodes.choiceNode(schema.getQName());
81     }
82
83     @Override
84     Optional<? extends TreeNode> apply(final ModifiedNode modification, final Optional<? extends TreeNode> storeMeta,
85             final Version version) {
86         return AutomaticLifecycleMixin.apply(super::apply, this::applyWrite, emptyNode, modification, storeMeta,
87             version);
88     }
89
90     @Override
91     TreeNode defaultTreeNode() {
92         return defaultTreeNode(emptyNode);
93     }
94
95     @Override
96     public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
97         return Optional.ofNullable(childNodes.get(child));
98     }
99
100     @Override
101     void optionalVerifyValueChildren(final NormalizedNode<?, ?> writtenValue) {
102         enforceCases(writtenValue);
103     }
104
105     private void enforceCases(final TreeNode tree) {
106         enforceCases(tree.getData());
107     }
108
109     private void enforceCases(final NormalizedNode<?, ?> normalizedNode) {
110         Verify.verify(normalizedNode instanceof ChoiceNode);
111         final Collection<DataContainerChild<?, ?>> children = ((ChoiceNode) normalizedNode).getValue();
112         if (!children.isEmpty()) {
113             final DataContainerChild<?, ?> firstChild = children.iterator().next();
114             final CaseEnforcer enforcer = Verify.verifyNotNull(caseEnforcers.get(firstChild.getIdentifier()),
115                 "Case enforcer cannot be null. Most probably, child node %s of choice node %s does not belong "
116                 + "in current tree type.", firstChild.getIdentifier(), normalizedNode.getIdentifier());
117
118             // Make sure no leaves from other cases are present
119             for (final CaseEnforcer other : exclusions.get(enforcer)) {
120                 for (final PathArgument id : other.getAllChildIdentifiers()) {
121                     final Optional<NormalizedNode<?, ?>> maybeChild = NormalizedNodes.getDirectChild(normalizedNode,
122                         id);
123                     Preconditions.checkArgument(!maybeChild.isPresent(),
124                         "Child %s (from case %s) implies non-presence of child %s (from case %s), which is %s",
125                         firstChild.getIdentifier(), enforcer, id, other, maybeChild.orElse(null));
126                 }
127             }
128
129             // Make sure all mandatory children are present
130             enforcer.enforceOnTreeNode(normalizedNode);
131         }
132     }
133
134     @Override
135     protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
136         final TreeNode ret = super.applyMerge(modification, currentMeta, version);
137         enforceCases(ret);
138         return ret;
139     }
140
141     @Override
142     protected TreeNode applyWrite(final ModifiedNode modification,  final NormalizedNode<?, ?> newValue,
143             final Optional<? extends TreeNode> currentMeta, final Version version) {
144         final TreeNode ret = super.applyWrite(modification, newValue, currentMeta, version);
145         enforceCases(ret);
146         return ret;
147     }
148
149     @Override
150     protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
151         final TreeNode ret = super.applyTouch(modification, currentMeta, version);
152         enforceCases(ret);
153         return ret;
154     }
155 }
156