Merge "Fix to feature files to include ietf-yang-types-20130715"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / ValueNodeModificationStrategy.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
9
10 import com.google.common.base.Optional;
11 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
13 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
14 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.IncorrectDataStructureException;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23
24 import static com.google.common.base.Preconditions.checkArgument;
25
26 abstract class ValueNodeModificationStrategy<T extends DataSchemaNode> extends SchemaAwareApplyOperation {
27
28     private final T schema;
29     private final Class<? extends NormalizedNode<?, ?>> nodeClass;
30
31     protected ValueNodeModificationStrategy(final T schema, final Class<? extends NormalizedNode<?, ?>> nodeClass) {
32         super();
33         this.schema = schema;
34         this.nodeClass = nodeClass;
35     }
36
37     @Override
38     protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
39         checkArgument(nodeClass.isInstance(writtenValue), "Node should must be of type %s", nodeClass);
40     }
41
42     @Override
43     public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
44         throw new UnsupportedOperationException("Node " + schema.getPath()
45                 + "is leaf type node. Child nodes not allowed");
46     }
47
48     @Override
49     protected TreeNode applySubtreeChange(final ModifiedNode modification,
50             final TreeNode currentMeta, final Version version) {
51         throw new UnsupportedOperationException("Node " + schema.getPath()
52                 + "is leaf type node. Subtree change is not allowed.");
53     }
54
55     @Override
56     protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
57             final Version version) {
58         // Just overwrite whatever was there
59         return applyWrite(modification, null, version);
60     }
61
62     @Override
63     protected TreeNode applyWrite(final ModifiedNode modification,
64             final Optional<TreeNode> currentMeta, final Version version) {
65         return TreeNodeFactory.createTreeNodeRecursively(modification.getWrittenValue(), version);
66     }
67
68     @Override
69     protected void checkSubtreeModificationApplicable(final YangInstanceIdentifier path, final NodeModification modification,
70             final Optional<TreeNode> current) throws IncorrectDataStructureException {
71         throw new IncorrectDataStructureException(path, "Subtree modification is not allowed.");
72     }
73
74     public static class LeafSetEntryModificationStrategy extends ValueNodeModificationStrategy<LeafListSchemaNode> {
75         @SuppressWarnings({ "unchecked", "rawtypes" })
76         protected LeafSetEntryModificationStrategy(final LeafListSchemaNode schema) {
77             super(schema, (Class) LeafSetEntryNode.class);
78         }
79     }
80
81     public static class LeafModificationStrategy extends ValueNodeModificationStrategy<LeafSchemaNode> {
82         @SuppressWarnings({ "unchecked", "rawtypes" })
83         protected LeafModificationStrategy(final LeafSchemaNode schema) {
84             super(schema, (Class) LeafNode.class);
85         }
86     }
87 }