2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
10 import static com.google.common.base.Preconditions.checkArgument;
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;
23 import java.util.Map.Entry;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
28 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
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.builder.api.DataContainerNodeBuilder;
36 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
37 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
38 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
39 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
42 final class ChoiceModificationStrategy extends AbstractNodeContainerModificationStrategy {
43 private final Map<PathArgument, ModificationApplyOperation> childNodes;
44 // FIXME: enforce leaves not coming from two case statements at the same time
45 private final Map<CaseEnforcer, Collection<CaseEnforcer>> exclusions;
46 private final Map<PathArgument, CaseEnforcer> caseEnforcers;
48 ChoiceModificationStrategy(final ChoiceSchemaNode schemaNode, final DataTreeConfiguration treeConfig) {
49 super(ChoiceNode.class, treeConfig);
51 final Builder<PathArgument, ModificationApplyOperation> childBuilder = ImmutableMap.builder();
52 final Builder<PathArgument, CaseEnforcer> enforcerBuilder = ImmutableMap.builder();
53 for (final ChoiceCaseNode caze : schemaNode.getCases()) {
54 final CaseEnforcer enforcer = CaseEnforcer.forTree(caze, treeConfig);
55 if (enforcer != null) {
56 for (final Entry<NodeIdentifier, DataSchemaNode> e : enforcer.getChildEntries()) {
57 childBuilder.put(e.getKey(), SchemaAwareApplyOperation.from(e.getValue(), treeConfig));
58 enforcerBuilder.put(e.getKey(), enforcer);
60 for (final Entry<AugmentationIdentifier, AugmentationSchema> e : enforcer.getAugmentationEntries()) {
61 childBuilder.put(e.getKey(), new AugmentationModificationStrategy(e.getValue(), caze, treeConfig));
62 enforcerBuilder.put(e.getKey(), enforcer);
66 childNodes = childBuilder.build();
67 caseEnforcers = enforcerBuilder.build();
69 final Map<CaseEnforcer, Collection<CaseEnforcer>> exclusionsBuilder = new HashMap<>();
70 for (final CaseEnforcer e : caseEnforcers.values()) {
71 exclusionsBuilder.put(e, ImmutableList.copyOf(
72 Collections2.filter(caseEnforcers.values(), Predicates.not(Predicates.equalTo(e)))));
74 exclusions = ImmutableMap.copyOf(exclusionsBuilder);
78 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
79 return Optional.fromNullable(childNodes.get(child));
83 @SuppressWarnings("rawtypes")
84 protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
85 checkArgument(original instanceof ChoiceNode);
86 return ImmutableChoiceNodeBuilder.create((ChoiceNode) original);
90 void verifyStructure(final NormalizedNode<?, ?> writtenValue, final boolean verifyChildren) {
91 if (verifyChildrenStructure() && verifyChildren) {
92 enforceCases(writtenValue);
94 super.verifyStructure(writtenValue, verifyChildren);
97 private void enforceCases(final TreeNode tree) {
98 enforceCases(tree.getData());
101 private void enforceCases(final NormalizedNode<?, ?> normalizedNode) {
102 Verify.verify(normalizedNode instanceof ChoiceNode);
103 final Collection<DataContainerChild<?, ?>> children = ((ChoiceNode) normalizedNode).getValue();
104 if (!children.isEmpty()) {
105 final DataContainerChild<?, ?> firstChild = children.iterator().next();
106 final CaseEnforcer enforcer = caseEnforcers.get(firstChild.getIdentifier());
107 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());
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, id);
113 Preconditions.checkArgument(!maybeChild.isPresent(),
114 "Child %s (from case %s) implies non-presence of child %s (from case %s), which is %s",
115 firstChild.getIdentifier(), enforcer, id, other, maybeChild.orNull());
119 // Make sure all mandatory children are present
120 enforcer.enforceOnTreeNode(normalizedNode);
125 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
126 final TreeNode ret = super.applyMerge(modification, currentMeta, version);
132 protected TreeNode applyWrite(final ModifiedNode modification, final Optional<TreeNode> currentMeta,
133 final Version version) {
134 final TreeNode ret = super.applyWrite(modification, currentMeta, version);
140 protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
141 final TreeNode ret = super.applyTouch(modification, currentMeta, version);
147 protected NormalizedNode<?, ?> createEmptyValue(final NormalizedNode<?, ?> original) {
148 checkArgument(original instanceof ChoiceNode);
149 return ImmutableChoiceNodeBuilder.create().withNodeIdentifier(((ChoiceNode) original).getIdentifier()).build();