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, final Class<? extends NormalizedNode<?, ?>> nodeClass) {
29 this.nodeClass = Preconditions.checkNotNull(nodeClass);
34 protected final void verifyStructure(final NormalizedNode<?, ?> writtenValue, final boolean verifyChildren) {
35 checkArgument(nodeClass.isInstance(writtenValue), "Node should must be of type %s", nodeClass);
39 public final Optional<ModificationApplyOperation> getChild(final PathArgument child) {
40 throw new UnsupportedOperationException("Node " + schema.getPath()
41 + "is leaf type node. Child nodes not allowed");
45 protected final ChildTrackingPolicy getChildPolicy() {
46 return ChildTrackingPolicy.NONE;
50 protected final TreeNode applyTouch(final ModifiedNode modification,
51 final TreeNode currentMeta, final Version version) {
52 throw new UnsupportedOperationException("Node " + schema.getPath()
53 + "is leaf type node. Subtree change is not allowed.");
57 protected final TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
58 final Version version) {
59 // Just overwrite whatever was there, but be sure to run validation
60 verifyStructure(modification.getWrittenValue(), true);
61 modification.resolveModificationType(ModificationType.WRITE);
62 return applyWrite(modification, null, version);
66 protected final TreeNode applyWrite(final ModifiedNode modification,
67 final Optional<TreeNode> currentMeta, final Version version) {
68 return TreeNodeFactory.createTreeNode(modification.getWrittenValue(), version);
72 protected final void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification,
73 final Optional<TreeNode> current, final Version version) throws IncorrectDataStructureException {
74 throw new IncorrectDataStructureException(path, "Subtree modification is not allowed.");
78 void mergeIntoModifiedNode(final ModifiedNode node, final NormalizedNode<?, ?> value, final Version version) {
80 switch (node.getOperation()) {
81 // Delete performs a data dependency check on existence of the node. Performing a merge
83 // are really performing a write.
89 node.updateValue(LogicalOperation.MERGE, value);
94 void recursivelyVerifyStructure(final NormalizedNode<?, ?> value) {
95 verifyStructure(value, false);