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.controller.md.sal.dom.store.impl;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import java.util.HashSet;
13 import java.util.List;
16 import java.util.concurrent.ExecutionException;
18 import org.opendaylight.controller.md.sal.dom.store.impl.tree.ModificationType;
19 import org.opendaylight.controller.md.sal.dom.store.impl.tree.NodeModification;
20 import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreMetadataNode;
21 import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreNodeCompositeBuilder;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
27 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue;
28 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
29 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
38 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableAugmentationNodeBuilder;
44 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
45 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
46 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
47 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
48 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
49 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedLeafSetNodeBuilder;
50 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
51 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
52 import org.opendaylight.yangtools.yang.data.impl.schema.transform.base.AugmentationSchemaProxy;
53 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
54 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
55 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
56 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
57 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
58 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
59 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
60 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
61 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
62 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
64 import com.google.common.base.Function;
65 import com.google.common.base.Optional;
66 import com.google.common.base.Preconditions;
67 import com.google.common.cache.CacheBuilder;
68 import com.google.common.cache.CacheLoader;
69 import com.google.common.cache.LoadingCache;
70 import com.google.common.collect.ImmutableMap;
71 import com.google.common.primitives.UnsignedLong;
73 public abstract class SchemaAwareApplyOperation implements ModificationApplyOperation {
75 public static SchemaAwareApplyOperation from(final DataSchemaNode schemaNode) {
76 if (schemaNode instanceof ContainerSchemaNode) {
77 return new ContainerModificationStrategy((ContainerSchemaNode) schemaNode);
78 } else if (schemaNode instanceof ListSchemaNode) {
79 return fromListSchemaNode((ListSchemaNode) schemaNode);
80 } else if (schemaNode instanceof ChoiceNode) {
81 return new ChoiceModificationStrategy((ChoiceNode) schemaNode);
82 } else if (schemaNode instanceof LeafListSchemaNode) {
83 return fromLeafListSchemaNode((LeafListSchemaNode) schemaNode);
84 } else if (schemaNode instanceof LeafSchemaNode) {
85 return new LeafModificationStrategy((LeafSchemaNode) schemaNode);
87 throw new IllegalArgumentException("Not supported schema node type for " + schemaNode.getClass());
90 private static SchemaAwareApplyOperation fromListSchemaNode(final ListSchemaNode schemaNode) {
91 List<QName> keyDefinition = schemaNode.getKeyDefinition();
92 if (keyDefinition == null || keyDefinition.isEmpty()) {
93 return new UnkeyedListModificationStrategy(schemaNode);
95 if (schemaNode.isUserOrdered()) {
96 return new OrderedMapModificationStrategy(schemaNode);
99 return new UnorderedMapModificationStrategy(schemaNode);
102 private static SchemaAwareApplyOperation fromLeafListSchemaNode(final LeafListSchemaNode schemaNode) {
103 if(schemaNode.isUserOrdered()) {
104 return new OrderedLeafSetModificationStrategy(schemaNode);
106 return new UnorderedLeafSetModificationStrategy(schemaNode);
111 public static SchemaAwareApplyOperation from(final DataNodeContainer resolvedTree,
112 final AugmentationTarget augSchemas, final AugmentationIdentifier identifier) {
113 AugmentationSchema augSchema = null;
114 allAugments: for (AugmentationSchema potential : augSchemas.getAvailableAugmentations()) {
115 boolean containsAll = true;
116 for (DataSchemaNode child : potential.getChildNodes()) {
117 if (identifier.getPossibleChildNames().contains(child.getQName())) {
118 augSchema = potential;
123 if (augSchema != null) {
124 return new AugmentationModificationStrategy(augSchema, resolvedTree);
129 protected final ModificationApplyOperation resolveChildOperation(final PathArgument child) {
130 Optional<ModificationApplyOperation> potential = getChild(child);
131 checkArgument(potential.isPresent(), "Operation for child %s is not defined.", child);
132 return potential.get();
136 public void verifyStructure(final NodeModification modification) throws IllegalArgumentException {
137 if (modification.getModificationType() == ModificationType.WRITE) {
138 verifyWritenStructure(modification.getWritenValue());
142 protected abstract void verifyWritenStructure(NormalizedNode<?, ?> writenValue);
145 public void checkApplicable(final InstanceIdentifier path,final NodeModification modification, final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
146 switch (modification.getModificationType()) {
148 checkDeleteApplicable(modification, current);
149 case SUBTREE_MODIFIED:
150 checkSubtreeModificationApplicable(path,modification, current);
153 checkWriteApplicable(path,modification, current);
156 checkMergeApplicable(path,modification,current);
161 throw new UnsupportedOperationException("Suplied modification type "+modification.getModificationType()+ "is not supported.");
166 protected void checkMergeApplicable(final InstanceIdentifier path,final NodeModification modification, final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
167 Optional<StoreMetadataNode> original = modification.getOriginal();
168 if (original.isPresent() && current.isPresent()) {
170 * We need to do conflict detection only and only if the value of leaf changed
171 * before two transactions. If value of leaf is unchanged between two transactions
172 * it should not cause transaction to fail, since result of this merge
173 * leads to same data.
175 if(!original.get().getData().equals(current.get().getData())) {
177 checkNotConflicting(path,original.get(), current.get());
182 protected void checkWriteApplicable(final InstanceIdentifier path,final NodeModification modification, final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
183 Optional<StoreMetadataNode> original = modification.getOriginal();
184 if (original.isPresent() && current.isPresent()) {
185 checkNotConflicting(path,original.get(), current.get());
186 } else if(original.isPresent()) {
187 throw new DataPreconditionFailedException(path,"Node was deleted by other transaction.");
191 protected static final void checkNotConflicting(final InstanceIdentifier path,final StoreMetadataNode original, final StoreMetadataNode current) throws DataPreconditionFailedException {
192 checkDataPrecondition(path, original.getNodeVersion().equals(current.getNodeVersion()),"Node was replaced by other transaction.");
193 checkDataPrecondition(path,original.getSubtreeVersion().equals(current.getSubtreeVersion()), "Node children was modified by other transaction");
196 protected abstract void checkSubtreeModificationApplicable(InstanceIdentifier path,final NodeModification modification,
197 final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException;
199 private void checkDeleteApplicable(final NodeModification modification, final Optional<StoreMetadataNode> current) {
203 public final Optional<StoreMetadataNode> apply(final NodeModification modification,
204 final Optional<StoreMetadataNode> currentMeta, final UnsignedLong subtreeVersion) {
206 switch (modification.getModificationType()) {
208 return modification.storeSnapshot(Optional.<StoreMetadataNode> absent());
209 case SUBTREE_MODIFIED:
210 Preconditions.checkArgument(currentMeta.isPresent(), "Metadata not available for modification",
212 return modification.storeSnapshot(Optional.of(applySubtreeChange(modification, currentMeta.get(),
215 if(currentMeta.isPresent()) {
216 return modification.storeSnapshot(Optional.of(applyMerge(modification,currentMeta.get(),subtreeVersion)));
217 } // Fallback to write is intentional - if node is not preexisting merge is same as write
219 return modification.storeSnapshot(Optional.of(applyWrite(modification, currentMeta, subtreeVersion)));
223 throw new IllegalArgumentException("Provided modification type is not supported.");
227 protected abstract StoreMetadataNode applyMerge(NodeModification modification,
228 StoreMetadataNode currentMeta, UnsignedLong subtreeVersion);
230 protected abstract StoreMetadataNode applyWrite(NodeModification modification,
231 Optional<StoreMetadataNode> currentMeta, UnsignedLong subtreeVersion);
233 protected abstract StoreMetadataNode applySubtreeChange(NodeModification modification,
234 StoreMetadataNode currentMeta, UnsignedLong subtreeVersion);
236 public static abstract class ValueNodeModificationStrategy<T extends DataSchemaNode> extends
237 SchemaAwareApplyOperation {
239 private final T schema;
240 private final Class<? extends NormalizedNode<?, ?>> nodeClass;
242 protected ValueNodeModificationStrategy(final T schema, final Class<? extends NormalizedNode<?, ?>> nodeClass) {
244 this.schema = schema;
245 this.nodeClass = nodeClass;
249 protected void verifyWritenStructure(final NormalizedNode<?, ?> writenValue) {
250 checkArgument(nodeClass.isInstance(writenValue), "Node should must be of type %s", nodeClass);
254 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
255 throw new UnsupportedOperationException("Node " + schema.getPath()
256 + "is leaf type node. Child nodes not allowed");
260 protected StoreMetadataNode applySubtreeChange(final NodeModification modification,
261 final StoreMetadataNode currentMeta, final UnsignedLong subtreeVersion) {
262 throw new UnsupportedOperationException("Node " + schema.getPath()
263 + "is leaf type node. Subtree change is not allowed.");
267 protected StoreMetadataNode applyMerge(final NodeModification modification, final StoreMetadataNode currentMeta,
268 final UnsignedLong subtreeVersion) {
269 return applyWrite(modification, Optional.of(currentMeta), subtreeVersion);
273 protected StoreMetadataNode applyWrite(final NodeModification modification,
274 final Optional<StoreMetadataNode> currentMeta, final UnsignedLong subtreeVersion) {
275 UnsignedLong nodeVersion = subtreeVersion;
276 return StoreMetadataNode.builder().setNodeVersion(nodeVersion).setSubtreeVersion(subtreeVersion)
277 .setData(modification.getWritenValue()).build();
281 protected void checkSubtreeModificationApplicable(final InstanceIdentifier path,final NodeModification modification,
282 final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
283 throw new DataPreconditionFailedException(path, "Subtree modification is not allowed.");
288 public static class LeafSetEntryModificationStrategy extends ValueNodeModificationStrategy<LeafListSchemaNode> {
290 @SuppressWarnings({ "unchecked", "rawtypes" })
291 protected LeafSetEntryModificationStrategy(final LeafListSchemaNode schema) {
292 super(schema, (Class) LeafSetEntryNode.class);
296 public static class LeafModificationStrategy extends ValueNodeModificationStrategy<LeafSchemaNode> {
298 @SuppressWarnings({ "unchecked", "rawtypes" })
299 protected LeafModificationStrategy(final LeafSchemaNode schema) {
300 super(schema, (Class) LeafNode.class);
304 public static abstract class NormalizedNodeContainerModificationStrategy extends SchemaAwareApplyOperation {
306 private final Class<? extends NormalizedNode<?, ?>> nodeClass;
308 protected NormalizedNodeContainerModificationStrategy(final Class<? extends NormalizedNode<?, ?>> nodeClass) {
309 this.nodeClass = nodeClass;
313 public void verifyStructure(final NodeModification modification) throws IllegalArgumentException {
314 if (modification.getModificationType() == ModificationType.WRITE) {
317 for (NodeModification childModification : modification.getModifications()) {
318 resolveChildOperation(childModification.getIdentifier()).verifyStructure(childModification);
323 protected void checkWriteApplicable(final InstanceIdentifier path, final NodeModification modification,
324 final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
325 // FIXME: Implement proper write check for replacement of node container
326 // prerequisite is to have transaction chain available for clients
327 // otherwise this will break chained writes to same node.
330 @SuppressWarnings("rawtypes")
332 protected void verifyWritenStructure(final NormalizedNode<?, ?> writenValue) {
333 checkArgument(nodeClass.isInstance(writenValue), "Node should must be of type %s", nodeClass);
334 checkArgument(writenValue instanceof NormalizedNodeContainer);
335 NormalizedNodeContainer writenCont = (NormalizedNodeContainer) writenValue;
336 for (Object child : writenCont.getValue()) {
337 checkArgument(child instanceof NormalizedNode);
338 NormalizedNode childNode = (NormalizedNode) child;
343 protected StoreMetadataNode applyWrite(final NodeModification modification,
344 final Optional<StoreMetadataNode> currentMeta, final UnsignedLong subtreeVersion) {
346 NormalizedNode<?, ?> newValue = modification.getWritenValue();
348 final UnsignedLong nodeVersion;
349 if (currentMeta.isPresent()) {
350 nodeVersion = StoreUtils.increase(currentMeta.get().getNodeVersion());
352 nodeVersion = subtreeVersion;
355 final StoreMetadataNode newValueMeta = StoreMetadataNode.createRecursively(newValue, nodeVersion, nodeVersion);
356 if (!modification.hasAdditionalModifications()) {
360 @SuppressWarnings("rawtypes")
361 NormalizedNodeContainerBuilder dataBuilder = createBuilder(newValue);
362 StoreNodeCompositeBuilder builder = StoreNodeCompositeBuilder.from(dataBuilder) //
363 .setNodeVersion(nodeVersion) //
364 .setSubtreeVersion(subtreeVersion);
366 return mutateChildren(modification.getModifications(), newValueMeta, builder, nodeVersion);
370 protected StoreMetadataNode applyMerge(final NodeModification modification, final StoreMetadataNode currentMeta,
371 final UnsignedLong subtreeVersion) {
372 // For Node Containers - merge is same as subtree change - we only replace children.
373 return applySubtreeChange(modification, currentMeta, subtreeVersion);
377 public StoreMetadataNode applySubtreeChange(final NodeModification modification,
378 final StoreMetadataNode currentMeta, final UnsignedLong subtreeVersion) {
379 // Bump subtree version to its new target
380 final UnsignedLong updatedSubtreeVersion = StoreUtils.increase(currentMeta.getSubtreeVersion());
382 @SuppressWarnings("rawtypes")
383 NormalizedNodeContainerBuilder dataBuilder = createBuilder(currentMeta.getData());
384 StoreNodeCompositeBuilder builder = StoreNodeCompositeBuilder.from(dataBuilder, currentMeta)
385 .setIdentifier(modification.getIdentifier()).setNodeVersion(currentMeta.getNodeVersion())
386 .setSubtreeVersion(updatedSubtreeVersion);
388 return mutateChildren(modification.getModifications(), currentMeta, builder, updatedSubtreeVersion);
391 private StoreMetadataNode mutateChildren(final Iterable<NodeModification> modifications, final StoreMetadataNode meta,
392 final StoreNodeCompositeBuilder builder, final UnsignedLong nodeVersion) {
394 for (NodeModification mod : modifications) {
395 final PathArgument id = mod.getIdentifier();
396 final Optional<StoreMetadataNode> cm = meta.getChild(id);
398 Optional<StoreMetadataNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
399 if (result.isPresent()) {
400 builder.add(result.get());
406 return builder.build();
410 protected void checkSubtreeModificationApplicable(final InstanceIdentifier path,final NodeModification modification,
411 final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
412 checkDataPrecondition(path, current.isPresent(), "Node was deleted by other transaction.");
413 checkChildPreconditions(path,modification,current);
417 private void checkChildPreconditions(final InstanceIdentifier path, final NodeModification modification, final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
418 StoreMetadataNode currentMeta = current.get();
419 for (NodeModification childMod : modification.getModifications()) {
420 PathArgument childId = childMod.getIdentifier();
421 Optional<StoreMetadataNode> childMeta = currentMeta.getChild(childId);
422 InstanceIdentifier childPath = StoreUtils.append(path, childId);
423 resolveChildOperation(childId).checkApplicable(childPath,childMod, childMeta);
428 protected void checkMergeApplicable(final InstanceIdentifier path, final NodeModification modification,
429 final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
430 if(current.isPresent()) {
431 checkChildPreconditions(path,modification,current);
435 @SuppressWarnings("rawtypes")
436 protected abstract NormalizedNodeContainerBuilder createBuilder(NormalizedNode<?, ?> original);
439 public static abstract class DataNodeContainerModificationStrategy<T extends DataNodeContainer> extends
440 NormalizedNodeContainerModificationStrategy {
442 private final T schema;
443 private final LoadingCache<PathArgument, ModificationApplyOperation> childCache = CacheBuilder.newBuilder()
444 .build(CacheLoader.from(new Function<PathArgument, ModificationApplyOperation>() {
447 public ModificationApplyOperation apply(final PathArgument identifier) {
448 if (identifier instanceof AugmentationIdentifier && schema instanceof AugmentationTarget) {
449 return from(schema, (AugmentationTarget) schema, (AugmentationIdentifier) identifier);
452 DataSchemaNode child = schema.getDataChildByName(identifier.getNodeType());
460 protected DataNodeContainerModificationStrategy(final T schema,
461 final Class<? extends NormalizedNode<?, ?>> nodeClass) {
463 this.schema = schema;
466 protected T getSchema() {
471 public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
473 return Optional.<ModificationApplyOperation> fromNullable(childCache.get(identifier));
474 } catch (ExecutionException e) {
475 return Optional.absent();
480 @SuppressWarnings("rawtypes")
481 protected abstract DataContainerNodeBuilder createBuilder(NormalizedNode<?, ?> original);
484 public String toString() {
485 return getClass().getSimpleName() + " [" + schema + "]";
490 public static class ContainerModificationStrategy extends
491 DataNodeContainerModificationStrategy<ContainerSchemaNode> {
493 public ContainerModificationStrategy(final ContainerSchemaNode schemaNode) {
494 super(schemaNode, ContainerNode.class);
498 @SuppressWarnings("rawtypes")
499 protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
500 checkArgument(original instanceof ContainerNode);
501 return ImmutableContainerNodeBuilder.create((ContainerNode) original);
505 public static class UnkeyedListItemModificationStrategy extends
506 DataNodeContainerModificationStrategy<ListSchemaNode> {
508 public UnkeyedListItemModificationStrategy(final ListSchemaNode schemaNode) {
509 super(schemaNode, UnkeyedListEntryNode.class);
513 @SuppressWarnings("rawtypes")
514 protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
515 checkArgument(original instanceof UnkeyedListEntryNode);
516 return ImmutableUnkeyedListEntryNodeBuilder.create((UnkeyedListEntryNode) original);
520 public static class AugmentationModificationStrategy extends
521 DataNodeContainerModificationStrategy<AugmentationSchema> {
523 protected AugmentationModificationStrategy(final AugmentationSchema schema, final DataNodeContainer resolved) {
524 super(createAugmentProxy(schema,resolved), AugmentationNode.class);
528 @SuppressWarnings("rawtypes")
529 protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
530 checkArgument(original instanceof AugmentationNode);
531 return ImmutableAugmentationNodeBuilder.create((AugmentationNode) original);
535 public static class ChoiceModificationStrategy extends NormalizedNodeContainerModificationStrategy {
537 private final ChoiceNode schema;
538 private final Map<PathArgument, ModificationApplyOperation> childNodes;
540 public ChoiceModificationStrategy(final ChoiceNode schemaNode) {
541 super(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode.class);
542 this.schema = schemaNode;
543 ImmutableMap.Builder<PathArgument, ModificationApplyOperation> child = ImmutableMap.builder();
545 for (ChoiceCaseNode caze : schemaNode.getCases()) {
546 for (DataSchemaNode cazeChild : caze.getChildNodes()) {
547 SchemaAwareApplyOperation childNode = from(cazeChild);
548 child.put(new NodeIdentifier(cazeChild.getQName()), childNode);
551 childNodes = child.build();
555 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
556 return Optional.fromNullable(childNodes.get(child));
560 @SuppressWarnings("rawtypes")
561 protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
562 checkArgument(original instanceof org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode);
563 return ImmutableChoiceNodeBuilder.create((org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode) original);
567 public static class ListEntryModificationStrategy extends DataNodeContainerModificationStrategy<ListSchemaNode> {
569 protected ListEntryModificationStrategy(final ListSchemaNode schema) {
570 super(schema, MapEntryNode.class);
574 @SuppressWarnings("rawtypes")
575 protected final DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
576 checkArgument(original instanceof MapEntryNode);
577 return ImmutableMapEntryNodeBuilder.create((MapEntryNode) original);
581 public static class UnorderedLeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy {
583 private final Optional<ModificationApplyOperation> entryStrategy;
585 @SuppressWarnings({ "unchecked", "rawtypes" })
586 protected UnorderedLeafSetModificationStrategy(final LeafListSchemaNode schema) {
587 super((Class) LeafSetNode.class);
588 entryStrategy = Optional.<ModificationApplyOperation> of(new LeafSetEntryModificationStrategy(schema));
591 @SuppressWarnings("rawtypes")
593 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
594 checkArgument(original instanceof LeafSetNode<?>);
595 return ImmutableLeafSetNodeBuilder.create((LeafSetNode<?>) original);
599 public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
600 if (identifier instanceof NodeWithValue) {
601 return entryStrategy;
603 return Optional.absent();
607 public static class OrderedLeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy {
609 private final Optional<ModificationApplyOperation> entryStrategy;
611 @SuppressWarnings({ "unchecked", "rawtypes" })
612 protected OrderedLeafSetModificationStrategy(final LeafListSchemaNode schema) {
613 super((Class) LeafSetNode.class);
614 entryStrategy = Optional.<ModificationApplyOperation> of(new LeafSetEntryModificationStrategy(schema));
617 @SuppressWarnings("rawtypes")
619 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
620 checkArgument(original instanceof OrderedLeafSetNode<?>);
621 return ImmutableOrderedLeafSetNodeBuilder.create((OrderedLeafSetNode<?>) original);
625 public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
626 if (identifier instanceof NodeWithValue) {
627 return entryStrategy;
629 return Optional.absent();
633 public static class UnkeyedListModificationStrategy extends SchemaAwareApplyOperation {
635 private final Optional<ModificationApplyOperation> entryStrategy;
637 protected UnkeyedListModificationStrategy(final ListSchemaNode schema) {
638 entryStrategy = Optional.<ModificationApplyOperation> of(new UnkeyedListItemModificationStrategy(schema));
642 protected StoreMetadataNode applyMerge(final NodeModification modification, final StoreMetadataNode currentMeta,
643 final UnsignedLong subtreeVersion) {
644 return applyWrite(modification, Optional.of(currentMeta), subtreeVersion);
648 protected StoreMetadataNode applySubtreeChange(final NodeModification modification,
649 final StoreMetadataNode currentMeta, final UnsignedLong subtreeVersion) {
650 throw new UnsupportedOperationException("UnkeyedList does not support subtree change.");
654 protected StoreMetadataNode applyWrite(final NodeModification modification,
655 final Optional<StoreMetadataNode> currentMeta, final UnsignedLong subtreeVersion) {
656 return StoreMetadataNode.createRecursively(modification.getWritenValue(), subtreeVersion);
660 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
661 if (child instanceof NodeIdentifier) {
662 return entryStrategy;
664 return Optional.absent();
668 protected void verifyWritenStructure(final NormalizedNode<?, ?> writenValue) {
673 protected void checkSubtreeModificationApplicable(final InstanceIdentifier path,final NodeModification modification,
674 final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
675 throw new DataPreconditionFailedException(path, "Subtree modification is not allowed.");
680 public static class UnorderedMapModificationStrategy extends NormalizedNodeContainerModificationStrategy {
682 private final Optional<ModificationApplyOperation> entryStrategy;
684 protected UnorderedMapModificationStrategy(final ListSchemaNode schema) {
685 super(MapNode.class);
686 entryStrategy = Optional.<ModificationApplyOperation> of(new ListEntryModificationStrategy(schema));
689 @SuppressWarnings("rawtypes")
691 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
692 checkArgument(original instanceof MapNode);
693 return ImmutableMapNodeBuilder.create((MapNode) original);
697 public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
698 if (identifier instanceof NodeIdentifierWithPredicates) {
699 return entryStrategy;
701 return Optional.absent();
705 public String toString() {
706 return "UnorderedMapModificationStrategy [entry=" + entryStrategy + "]";
710 public static class OrderedMapModificationStrategy extends NormalizedNodeContainerModificationStrategy {
712 private final Optional<ModificationApplyOperation> entryStrategy;
714 protected OrderedMapModificationStrategy(final ListSchemaNode schema) {
715 super(OrderedMapNode.class);
716 entryStrategy = Optional.<ModificationApplyOperation> of(new ListEntryModificationStrategy(schema));
719 @SuppressWarnings("rawtypes")
721 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
722 checkArgument(original instanceof OrderedMapNode);
723 return ImmutableOrderedMapNodeBuilder.create((OrderedMapNode) original);
727 public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
728 if (identifier instanceof NodeIdentifierWithPredicates) {
729 return entryStrategy;
731 return Optional.absent();
735 public String toString() {
736 return "OrderedMapModificationStrategy [entry=" + entryStrategy + "]";
740 public void verifyIdentifier(final PathArgument identifier) {
744 public static AugmentationSchema createAugmentProxy(final AugmentationSchema schema, final DataNodeContainer resolved) {
745 Set<DataSchemaNode> realChildSchemas = new HashSet<>();
746 for(DataSchemaNode augChild : schema.getChildNodes()) {
747 realChildSchemas.add(resolved.getDataChildByName(augChild.getQName()));
749 return new AugmentationSchemaProxy(schema, realChildSchemas);
752 public static boolean checkDataPrecondition(final InstanceIdentifier path, final boolean condition, final String message) throws DataPreconditionFailedException {
754 throw new DataPreconditionFailedException(path, message);