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;
11 import static java.util.Objects.requireNonNull;
13 import java.util.Optional;
14 import org.eclipse.jdt.annotation.NonNull;
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.ValueNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.IncorrectDataStructureException;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
25 final class ValueNodeModificationStrategy<T extends DataSchemaNode> extends SchemaAwareApplyOperation<T> {
26 private final Class<? extends ValueNode> nodeClass;
27 private final @NonNull T schema;
29 ValueNodeModificationStrategy(final Class<? extends ValueNode> nodeClass, final T schema) {
30 this.nodeClass = requireNonNull(nodeClass);
31 this.schema = requireNonNull(schema);
40 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
41 throw new UnsupportedOperationException("Node " + schema.getPath()
42 + " is leaf type node. Child nodes not allowed");
46 protected ChildTrackingPolicy getChildPolicy() {
47 return ChildTrackingPolicy.NONE;
51 protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta,
52 final Version version) {
53 throw new UnsupportedOperationException("Node " + schema.getPath()
54 + " is leaf type node. Subtree change is not allowed.");
58 protected 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 final NormalizedNode<?, ?> newValue = modification.getWrittenValue();
62 verifyWrittenValue(newValue);
63 modification.resolveModificationType(ModificationType.WRITE);
64 return applyWrite(modification, newValue, null, version);
68 protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode<?, ?> newValue,
69 final Optional<TreeNode> currentMeta, final Version version) {
70 return TreeNodeFactory.createTreeNode(newValue, version);
74 protected void checkTouchApplicable(final ModificationPath path, final NodeModification modification,
75 final Optional<TreeNode> current, final Version version) throws IncorrectDataStructureException {
76 throw new IncorrectDataStructureException(path.toInstanceIdentifier(), "Subtree modification is not allowed.");
80 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 verifyValue(final NormalizedNode<?, ?> writtenValue) {
96 verifyWrittenValue(writtenValue);
100 void recursivelyVerifyStructure(final NormalizedNode<?, ?> value) {
101 verifyWrittenValue(value);
104 private void verifyWrittenValue(final NormalizedNode<?, ?> value) {
105 checkArgument(nodeClass.isInstance(value), "Expected an instance of %s, have %s", nodeClass, value);