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;
22 import java.util.Map.Entry;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
25 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
34 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
35 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
38 final class ChoiceModificationStrategy extends AbstractNodeContainerModificationStrategy {
39 private final Map<PathArgument, ModificationApplyOperation> childNodes;
40 // FIXME: enforce leaves not coming from two case statements at the same time
41 private final Map<CaseEnforcer, Collection<CaseEnforcer>> exclusions;
42 private final Map<PathArgument, CaseEnforcer> caseEnforcers;
44 ChoiceModificationStrategy(final ChoiceSchemaNode schemaNode, final TreeType treeType) {
45 super(ChoiceNode.class, treeType);
47 final Builder<PathArgument, ModificationApplyOperation> childBuilder = ImmutableMap.builder();
48 final Builder<PathArgument, CaseEnforcer> enforcerBuilder = ImmutableMap.builder();
49 for (final ChoiceCaseNode caze : schemaNode.getCases()) {
50 final CaseEnforcer enforcer = CaseEnforcer.forTree(caze, treeType);
51 if (enforcer != null) {
52 for (final Entry<NodeIdentifier, DataSchemaNode> e : enforcer.getChildEntries()) {
53 childBuilder.put(e.getKey(), SchemaAwareApplyOperation.from(e.getValue(), treeType));
54 enforcerBuilder.put(e.getKey(), enforcer);
58 childNodes = childBuilder.build();
59 caseEnforcers = enforcerBuilder.build();
61 final Builder<CaseEnforcer, Collection<CaseEnforcer>> exclusionsBuilder = ImmutableMap.builder();
62 for (CaseEnforcer e : caseEnforcers.values()) {
63 exclusionsBuilder.put(e, ImmutableList.copyOf(
64 Collections2.filter(caseEnforcers.values(), Predicates.not(Predicates.equalTo(e)))));
66 exclusions = exclusionsBuilder.build();
70 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
71 return Optional.fromNullable(childNodes.get(child));
75 @SuppressWarnings("rawtypes")
76 protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
77 checkArgument(original instanceof ChoiceNode);
78 return ImmutableChoiceNodeBuilder.create((ChoiceNode) original);
82 void verifyStructure(final NormalizedNode<?, ?> writtenValue, final boolean verifyChildren) {
83 if(verifyChildrenStructure() && verifyChildren) {
84 enforceCases(writtenValue);
86 super.verifyStructure(writtenValue, verifyChildren);
89 private void enforceCases(final TreeNode tree) {
90 enforceCases(tree.getData());
93 private void enforceCases(final NormalizedNode<?, ?> normalizedNode) {
94 Verify.verify(normalizedNode instanceof ChoiceNode);
95 final Collection<DataContainerChild<?, ?>> children = ((ChoiceNode) normalizedNode).getValue();
96 if (!children.isEmpty()) {
97 final DataContainerChild<?, ?> firstChild = children.iterator().next();
98 final CaseEnforcer enforcer = caseEnforcers.get(firstChild.getIdentifier());
99 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 // Make sure no leaves from other cases are present
102 for (CaseEnforcer other : exclusions.get(enforcer)) {
103 for (NodeIdentifier id : other.getChildIdentifiers()) {
104 final Optional<NormalizedNode<?, ?>> maybeChild = NormalizedNodes.getDirectChild(normalizedNode, id);
105 Preconditions.checkArgument(!maybeChild.isPresent(),
106 "Child %s (from case %s) implies non-presence of child %s (from case %s), which is %s",
107 firstChild.getIdentifier(), enforcer, id, other, maybeChild.orNull());
114 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
115 final TreeNode ret = super.applyMerge(modification, currentMeta, version);
121 protected TreeNode applyWrite(final ModifiedNode modification, final Optional<TreeNode> currentMeta,
122 final Version version) {
123 final TreeNode ret = super.applyWrite(modification, currentMeta, version);
129 protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
130 final TreeNode ret = super.applyTouch(modification, currentMeta, version);