BUG-868: migrate InstanceIdentifer.builder() users
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / tree / data / 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  */package org.opendaylight.controller.md.sal.dom.store.impl.tree.data;
8
9  import static com.google.common.base.Preconditions.checkArgument;
10
11 import org.opendaylight.controller.md.sal.dom.store.impl.tree.DataPreconditionFailedException;
12  import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
13  import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
14  import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
15  import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
16  import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17  import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
18  import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
19  import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
20
21 import com.google.common.base.Optional;
22  import com.google.common.primitives.UnsignedLong;
23
24  abstract class ValueNodeModificationStrategy<T extends DataSchemaNode> extends SchemaAwareApplyOperation {
25
26      private final T schema;
27      private final Class<? extends NormalizedNode<?, ?>> nodeClass;
28
29      protected ValueNodeModificationStrategy(final T schema, final Class<? extends NormalizedNode<?, ?>> nodeClass) {
30          super();
31          this.schema = schema;
32          this.nodeClass = nodeClass;
33      }
34
35      @Override
36      protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
37          checkArgument(nodeClass.isInstance(writtenValue), "Node should must be of type %s", nodeClass);
38      }
39
40      @Override
41      public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
42          throw new UnsupportedOperationException("Node " + schema.getPath()
43                  + "is leaf type node. Child nodes not allowed");
44      }
45
46      @Override
47      protected StoreMetadataNode applySubtreeChange(final NodeModification modification,
48              final StoreMetadataNode currentMeta, final UnsignedLong subtreeVersion) {
49          throw new UnsupportedOperationException("Node " + schema.getPath()
50                  + "is leaf type node. Subtree change is not allowed.");
51      }
52
53      @Override
54      protected StoreMetadataNode applyMerge(final NodeModification modification, final StoreMetadataNode currentMeta,
55              final UnsignedLong subtreeVersion) {
56          return applyWrite(modification, Optional.of(currentMeta), subtreeVersion);
57      }
58
59      @Override
60      protected StoreMetadataNode applyWrite(final NodeModification modification,
61              final Optional<StoreMetadataNode> currentMeta, final UnsignedLong subtreeVersion) {
62          return StoreMetadataNode.builder(subtreeVersion).setSubtreeVersion(subtreeVersion)
63                  .setData(modification.getWrittenValue()).build();
64      }
65
66      @Override
67      protected void checkSubtreeModificationApplicable(final InstanceIdentifier path,final NodeModification modification,
68              final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
69          throw new DataPreconditionFailedException(path, "Subtree modification is not allowed.");
70      }
71
72      public static class LeafSetEntryModificationStrategy extends ValueNodeModificationStrategy<LeafListSchemaNode> {
73          @SuppressWarnings({ "unchecked", "rawtypes" })
74          protected LeafSetEntryModificationStrategy(final LeafListSchemaNode schema) {
75              super(schema, (Class) LeafSetEntryNode.class);
76          }
77      }
78
79      public static class LeafModificationStrategy extends ValueNodeModificationStrategy<LeafSchemaNode> {
80          @SuppressWarnings({ "unchecked", "rawtypes" })
81          protected LeafModificationStrategy(final LeafSchemaNode schema) {
82              super(schema, (Class) LeafNode.class);
83          }
84      }
85  }