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
60 modification.resolveModificationType(ModificationType.WRITE);
61 return applyWrite(modification, null, version);
65 protected final TreeNode applyWrite(final ModifiedNode modification,
66 final Optional<TreeNode> currentMeta, final Version version) {
67 return TreeNodeFactory.createTreeNode(modification.getWrittenValue(), version);
71 protected final void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification,
72 final Optional<TreeNode> current) throws IncorrectDataStructureException {
73 throw new IncorrectDataStructureException(path, "Subtree modification is not allowed.");