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 org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.IncorrectDataStructureException;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
22 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 abstract class AbstractValueNodeModificationStrategy<T extends DataSchemaNode> extends SchemaAwareApplyOperation {
25 private final Class<? extends NormalizedNode<?, ?>> nodeClass;
26 private final T schema;
28 protected AbstractValueNodeModificationStrategy(final T schema,
29 final Class<? extends NormalizedNode<?, ?>> nodeClass) {
30 this.nodeClass = Preconditions.checkNotNull(nodeClass);
35 protected final void verifyStructure(final NormalizedNode<?, ?> writtenValue, final boolean verifyChildren) {
36 checkArgument(nodeClass.isInstance(writtenValue), "Node should must be of type %s", nodeClass);
40 public final Optional<ModificationApplyOperation> getChild(final PathArgument child) {
41 throw new UnsupportedOperationException("Node " + schema.getPath()
42 + "is leaf type node. Child nodes not allowed");
46 protected final ChildTrackingPolicy getChildPolicy() {
47 return ChildTrackingPolicy.NONE;
51 protected final TreeNode applyTouch(final ModifiedNode modification,
52 final TreeNode currentMeta, final Version version) {
53 throw new UnsupportedOperationException("Node " + schema.getPath()
54 + "is leaf type node. Subtree change is not allowed.");
58 protected final TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
59 final Version version) {
60 // Just overwrite whatever was there, but be sure to run validation
61 verifyStructure(modification.getWrittenValue(), true);
62 modification.resolveModificationType(ModificationType.WRITE);
63 return applyWrite(modification, null, version);
67 protected final TreeNode applyWrite(final ModifiedNode modification,
68 final Optional<TreeNode> currentMeta, final Version version) {
69 return TreeNodeFactory.createTreeNode(modification.getWrittenValue(), version);
73 protected final void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification,
74 final Optional<TreeNode> current, final Version version) throws IncorrectDataStructureException {
75 throw new IncorrectDataStructureException(path, "Subtree modification is not allowed.");
79 void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode<?, ?> value, final Version version) {
81 switch (node.getOperation()) {
82 // Delete performs a data dependency check on existence of the node. Performing a merge
84 // are really performing a write.
90 node.updateValue(LogicalOperation.MERGE, value);
95 void recursivelyVerifyStructure(final NormalizedNode<?, ?> value) {
96 verifyStructure(value, false);