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.tree.data;
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.DataPreconditionFailedException;
19 import org.opendaylight.controller.md.sal.dom.store.impl.tree.ModificationType;
20 import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreUtils;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
26 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue;
27 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
28 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
37 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
40 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableAugmentationNodeBuilder;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
44 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
45 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
46 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
47 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
48 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedLeafSetNodeBuilder;
49 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
50 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
51 import org.opendaylight.yangtools.yang.data.impl.schema.transform.base.AugmentationSchemaProxy;
52 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
53 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
54 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
55 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
56 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
57 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
58 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
59 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
60 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
61 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
63 import com.google.common.base.Function;
64 import com.google.common.base.Optional;
65 import com.google.common.base.Preconditions;
66 import com.google.common.cache.CacheBuilder;
67 import com.google.common.cache.CacheLoader;
68 import com.google.common.cache.LoadingCache;
69 import com.google.common.collect.ImmutableMap;
70 import com.google.common.primitives.UnsignedLong;
72 abstract class SchemaAwareApplyOperation implements ModificationApplyOperation {
74 public static SchemaAwareApplyOperation from(final DataSchemaNode schemaNode) {
75 if (schemaNode instanceof ContainerSchemaNode) {
76 return new ContainerModificationStrategy((ContainerSchemaNode) schemaNode);
77 } else if (schemaNode instanceof ListSchemaNode) {
78 return fromListSchemaNode((ListSchemaNode) schemaNode);
79 } else if (schemaNode instanceof ChoiceNode) {
80 return new ChoiceModificationStrategy((ChoiceNode) schemaNode);
81 } else if (schemaNode instanceof LeafListSchemaNode) {
82 return fromLeafListSchemaNode((LeafListSchemaNode) schemaNode);
83 } else if (schemaNode instanceof LeafSchemaNode) {
84 return new LeafModificationStrategy((LeafSchemaNode) schemaNode);
86 throw new IllegalArgumentException("Not supported schema node type for " + schemaNode.getClass());
89 private static SchemaAwareApplyOperation fromListSchemaNode(final ListSchemaNode schemaNode) {
90 List<QName> keyDefinition = schemaNode.getKeyDefinition();
91 if (keyDefinition == null || keyDefinition.isEmpty()) {
92 return new UnkeyedListModificationStrategy(schemaNode);
94 if (schemaNode.isUserOrdered()) {
95 return new OrderedMapModificationStrategy(schemaNode);
98 return new UnorderedMapModificationStrategy(schemaNode);
101 private static SchemaAwareApplyOperation fromLeafListSchemaNode(final LeafListSchemaNode schemaNode) {
102 if(schemaNode.isUserOrdered()) {
103 return new OrderedLeafSetModificationStrategy(schemaNode);
105 return new UnorderedLeafSetModificationStrategy(schemaNode);
110 public static SchemaAwareApplyOperation from(final DataNodeContainer resolvedTree,
111 final AugmentationTarget augSchemas, final AugmentationIdentifier identifier) {
112 AugmentationSchema augSchema = null;
115 for (AugmentationSchema potential : augSchemas.getAvailableAugmentations()) {
116 for (DataSchemaNode child : potential.getChildNodes()) {
117 if (identifier.getPossibleChildNames().contains(child.getQName())) {
118 augSchema = potential;
124 if (augSchema != null) {
125 return new AugmentationModificationStrategy(augSchema, resolvedTree);
130 protected final ModificationApplyOperation resolveChildOperation(final PathArgument child) {
131 Optional<ModificationApplyOperation> potential = getChild(child);
132 checkArgument(potential.isPresent(), "Operation for child %s is not defined.", child);
133 return potential.get();
137 public void verifyStructure(final NodeModification modification) throws IllegalArgumentException {
138 if (modification.getModificationType() == ModificationType.WRITE) {
139 verifyWrittenStructure(modification.getWrittenValue());
143 protected abstract void verifyWrittenStructure(NormalizedNode<?, ?> writtenValue);
146 public void checkApplicable(final InstanceIdentifier path,final NodeModification modification, final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
147 switch (modification.getModificationType()) {
149 checkDeleteApplicable(modification, current);
150 case SUBTREE_MODIFIED:
151 checkSubtreeModificationApplicable(path,modification, current);
154 checkWriteApplicable(path,modification, current);
157 checkMergeApplicable(path,modification,current);
162 throw new UnsupportedOperationException("Suplied modification type "+modification.getModificationType()+ "is not supported.");
167 protected void checkMergeApplicable(final InstanceIdentifier path,final NodeModification modification, final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
168 Optional<StoreMetadataNode> original = modification.getOriginal();
169 if (original.isPresent() && current.isPresent()) {
171 * We need to do conflict detection only and only if the value of leaf changed
172 * before two transactions. If value of leaf is unchanged between two transactions
173 * it should not cause transaction to fail, since result of this merge
174 * leads to same data.
176 if(!original.get().getData().equals(current.get().getData())) {
178 checkNotConflicting(path,original.get(), current.get());
183 protected void checkWriteApplicable(final InstanceIdentifier path,final NodeModification modification, final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
184 Optional<StoreMetadataNode> original = modification.getOriginal();
185 if (original.isPresent() && current.isPresent()) {
186 checkNotConflicting(path,original.get(), current.get());
187 } else if(original.isPresent()) {
188 throw new DataPreconditionFailedException(path,"Node was deleted by other transaction.");
192 protected static final void checkNotConflicting(final InstanceIdentifier path,final StoreMetadataNode original, final StoreMetadataNode current) throws DataPreconditionFailedException {
193 checkDataPrecondition(path, original.getNodeVersion().equals(current.getNodeVersion()),"Node was replaced by other transaction.");
194 checkDataPrecondition(path,original.getSubtreeVersion().equals(current.getSubtreeVersion()), "Node children was modified by other transaction");
197 protected abstract void checkSubtreeModificationApplicable(InstanceIdentifier path,final NodeModification modification,
198 final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException;
200 private void checkDeleteApplicable(final NodeModification modification, final Optional<StoreMetadataNode> current) {
204 public final Optional<StoreMetadataNode> apply(final NodeModification modification,
205 final Optional<StoreMetadataNode> currentMeta, final UnsignedLong subtreeVersion) {
207 switch (modification.getModificationType()) {
209 return modification.storeSnapshot(Optional.<StoreMetadataNode> absent());
210 case SUBTREE_MODIFIED:
211 Preconditions.checkArgument(currentMeta.isPresent(), "Metadata not available for modification",
213 return modification.storeSnapshot(Optional.of(applySubtreeChange(modification, currentMeta.get(),
216 if(currentMeta.isPresent()) {
217 return modification.storeSnapshot(Optional.of(applyMerge(modification,currentMeta.get(),subtreeVersion)));
218 } // Fallback to write is intentional - if node is not preexisting merge is same as write
220 return modification.storeSnapshot(Optional.of(applyWrite(modification, currentMeta, subtreeVersion)));
224 throw new IllegalArgumentException("Provided modification type is not supported.");
228 protected abstract StoreMetadataNode applyMerge(NodeModification modification,
229 StoreMetadataNode currentMeta, UnsignedLong subtreeVersion);
231 protected abstract StoreMetadataNode applyWrite(NodeModification modification,
232 Optional<StoreMetadataNode> currentMeta, UnsignedLong subtreeVersion);
234 protected abstract StoreMetadataNode applySubtreeChange(NodeModification modification,
235 StoreMetadataNode currentMeta, UnsignedLong subtreeVersion);
237 public static abstract class ValueNodeModificationStrategy<T extends DataSchemaNode> extends 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 verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
250 checkArgument(nodeClass.isInstance(writtenValue), "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.getWrittenValue()).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 verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
333 checkArgument(nodeClass.isInstance(writtenValue), "Node should must be of type %s", nodeClass);
334 checkArgument(writtenValue instanceof NormalizedNodeContainer);
336 NormalizedNodeContainer container = (NormalizedNodeContainer) writtenValue;
337 for (Object child : container.getValue()) {
338 checkArgument(child instanceof NormalizedNode);
341 * FIXME: fail-fast semantics:
343 * We can validate the data structure here, aborting the commit
344 * before it ever progresses to being committed.
350 protected StoreMetadataNode applyWrite(final NodeModification modification,
351 final Optional<StoreMetadataNode> currentMeta, final UnsignedLong subtreeVersion) {
353 NormalizedNode<?, ?> newValue = modification.getWrittenValue();
355 final UnsignedLong nodeVersion;
356 if (currentMeta.isPresent()) {
357 nodeVersion = StoreUtils.increase(currentMeta.get().getNodeVersion());
359 nodeVersion = subtreeVersion;
362 final StoreMetadataNode newValueMeta = StoreMetadataNode.createRecursively(newValue, nodeVersion, nodeVersion);
363 if (!modification.hasAdditionalModifications()) {
367 @SuppressWarnings("rawtypes")
368 NormalizedNodeContainerBuilder dataBuilder = createBuilder(newValue);
369 StoreNodeCompositeBuilder builder = StoreNodeCompositeBuilder.from(dataBuilder) //
370 .setNodeVersion(nodeVersion) //
371 .setSubtreeVersion(subtreeVersion);
373 return mutateChildren(modification.getModifications(), newValueMeta, builder, nodeVersion);
377 protected StoreMetadataNode applyMerge(final NodeModification modification, final StoreMetadataNode currentMeta,
378 final UnsignedLong subtreeVersion) {
379 // For Node Containers - merge is same as subtree change - we only replace children.
380 return applySubtreeChange(modification, currentMeta, subtreeVersion);
384 public StoreMetadataNode applySubtreeChange(final NodeModification modification,
385 final StoreMetadataNode currentMeta, final UnsignedLong subtreeVersion) {
386 // Bump subtree version to its new target
387 final UnsignedLong updatedSubtreeVersion = StoreUtils.increase(currentMeta.getSubtreeVersion());
389 @SuppressWarnings("rawtypes")
390 NormalizedNodeContainerBuilder dataBuilder = createBuilder(currentMeta.getData());
391 StoreNodeCompositeBuilder builder = StoreNodeCompositeBuilder.from(dataBuilder, currentMeta)
392 .setIdentifier(modification.getIdentifier()).setNodeVersion(currentMeta.getNodeVersion())
393 .setSubtreeVersion(updatedSubtreeVersion);
395 return mutateChildren(modification.getModifications(), currentMeta, builder, updatedSubtreeVersion);
398 private StoreMetadataNode mutateChildren(final Iterable<NodeModification> modifications, final StoreMetadataNode meta,
399 final StoreNodeCompositeBuilder builder, final UnsignedLong nodeVersion) {
401 for (NodeModification mod : modifications) {
402 final PathArgument id = mod.getIdentifier();
403 final Optional<StoreMetadataNode> cm = meta.getChild(id);
405 Optional<StoreMetadataNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
406 if (result.isPresent()) {
407 builder.add(result.get());
413 return builder.build();
417 protected void checkSubtreeModificationApplicable(final InstanceIdentifier path,final NodeModification modification,
418 final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
419 checkDataPrecondition(path, current.isPresent(), "Node was deleted by other transaction.");
420 checkChildPreconditions(path,modification,current);
424 private void checkChildPreconditions(final InstanceIdentifier path, final NodeModification modification, final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
425 StoreMetadataNode currentMeta = current.get();
426 for (NodeModification childMod : modification.getModifications()) {
427 PathArgument childId = childMod.getIdentifier();
428 Optional<StoreMetadataNode> childMeta = currentMeta.getChild(childId);
429 InstanceIdentifier childPath = StoreUtils.append(path, childId);
430 resolveChildOperation(childId).checkApplicable(childPath,childMod, childMeta);
435 protected void checkMergeApplicable(final InstanceIdentifier path, final NodeModification modification,
436 final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
437 if(current.isPresent()) {
438 checkChildPreconditions(path,modification,current);
442 @SuppressWarnings("rawtypes")
443 protected abstract NormalizedNodeContainerBuilder createBuilder(NormalizedNode<?, ?> original);
446 public static abstract class DataNodeContainerModificationStrategy<T extends DataNodeContainer> extends NormalizedNodeContainerModificationStrategy {
448 private final T schema;
449 private final LoadingCache<PathArgument, ModificationApplyOperation> childCache = CacheBuilder.newBuilder()
450 .build(CacheLoader.from(new Function<PathArgument, ModificationApplyOperation>() {
453 public ModificationApplyOperation apply(final PathArgument identifier) {
454 if (identifier instanceof AugmentationIdentifier && schema instanceof AugmentationTarget) {
455 return from(schema, (AugmentationTarget) schema, (AugmentationIdentifier) identifier);
458 DataSchemaNode child = schema.getDataChildByName(identifier.getNodeType());
466 protected DataNodeContainerModificationStrategy(final T schema,
467 final Class<? extends NormalizedNode<?, ?>> nodeClass) {
469 this.schema = schema;
472 protected T getSchema() {
477 public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
479 return Optional.<ModificationApplyOperation> fromNullable(childCache.get(identifier));
480 } catch (ExecutionException e) {
481 return Optional.absent();
486 @SuppressWarnings("rawtypes")
487 protected abstract DataContainerNodeBuilder createBuilder(NormalizedNode<?, ?> original);
490 public String toString() {
491 return getClass().getSimpleName() + " [" + schema + "]";
496 public static class ContainerModificationStrategy extends DataNodeContainerModificationStrategy<ContainerSchemaNode> {
498 public ContainerModificationStrategy(final ContainerSchemaNode schemaNode) {
499 super(schemaNode, ContainerNode.class);
503 @SuppressWarnings("rawtypes")
504 protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
505 checkArgument(original instanceof ContainerNode);
506 return ImmutableContainerNodeBuilder.create((ContainerNode) original);
510 public static class UnkeyedListItemModificationStrategy extends DataNodeContainerModificationStrategy<ListSchemaNode> {
512 public UnkeyedListItemModificationStrategy(final ListSchemaNode schemaNode) {
513 super(schemaNode, UnkeyedListEntryNode.class);
517 @SuppressWarnings("rawtypes")
518 protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
519 checkArgument(original instanceof UnkeyedListEntryNode);
520 return ImmutableUnkeyedListEntryNodeBuilder.create((UnkeyedListEntryNode) original);
524 public static class AugmentationModificationStrategy extends DataNodeContainerModificationStrategy<AugmentationSchema> {
526 protected AugmentationModificationStrategy(final AugmentationSchema schema, final DataNodeContainer resolved) {
527 super(createAugmentProxy(schema,resolved), AugmentationNode.class);
531 @SuppressWarnings("rawtypes")
532 protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
533 checkArgument(original instanceof AugmentationNode);
534 return ImmutableAugmentationNodeBuilder.create((AugmentationNode) original);
538 public static class ChoiceModificationStrategy extends NormalizedNodeContainerModificationStrategy {
540 private final Map<PathArgument, ModificationApplyOperation> childNodes;
542 public ChoiceModificationStrategy(final ChoiceNode schemaNode) {
543 super(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode.class);
544 ImmutableMap.Builder<PathArgument, ModificationApplyOperation> child = ImmutableMap.builder();
546 for (ChoiceCaseNode caze : schemaNode.getCases()) {
547 for (DataSchemaNode cazeChild : caze.getChildNodes()) {
548 SchemaAwareApplyOperation childNode = from(cazeChild);
549 child.put(new NodeIdentifier(cazeChild.getQName()), childNode);
552 childNodes = child.build();
556 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
557 return Optional.fromNullable(childNodes.get(child));
561 @SuppressWarnings("rawtypes")
562 protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
563 checkArgument(original instanceof org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode);
564 return ImmutableChoiceNodeBuilder.create((org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode) original);
568 public static class ListEntryModificationStrategy extends DataNodeContainerModificationStrategy<ListSchemaNode> {
570 protected ListEntryModificationStrategy(final ListSchemaNode schema) {
571 super(schema, MapEntryNode.class);
575 @SuppressWarnings("rawtypes")
576 protected final DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
577 checkArgument(original instanceof MapEntryNode);
578 return ImmutableMapEntryNodeBuilder.create((MapEntryNode) original);
582 public static class UnorderedLeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy {
584 private final Optional<ModificationApplyOperation> entryStrategy;
586 @SuppressWarnings({ "unchecked", "rawtypes" })
587 protected UnorderedLeafSetModificationStrategy(final LeafListSchemaNode schema) {
588 super((Class) LeafSetNode.class);
589 entryStrategy = Optional.<ModificationApplyOperation> of(new LeafSetEntryModificationStrategy(schema));
592 @SuppressWarnings("rawtypes")
594 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
595 checkArgument(original instanceof LeafSetNode<?>);
596 return ImmutableLeafSetNodeBuilder.create((LeafSetNode<?>) original);
600 public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
601 if (identifier instanceof NodeWithValue) {
602 return entryStrategy;
604 return Optional.absent();
608 public static class OrderedLeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy {
610 private final Optional<ModificationApplyOperation> entryStrategy;
612 @SuppressWarnings({ "unchecked", "rawtypes" })
613 protected OrderedLeafSetModificationStrategy(final LeafListSchemaNode schema) {
614 super((Class) LeafSetNode.class);
615 entryStrategy = Optional.<ModificationApplyOperation> of(new LeafSetEntryModificationStrategy(schema));
618 @SuppressWarnings("rawtypes")
620 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
621 checkArgument(original instanceof OrderedLeafSetNode<?>);
622 return ImmutableOrderedLeafSetNodeBuilder.create((OrderedLeafSetNode<?>) original);
626 public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
627 if (identifier instanceof NodeWithValue) {
628 return entryStrategy;
630 return Optional.absent();
634 public static class UnkeyedListModificationStrategy extends SchemaAwareApplyOperation {
636 private final Optional<ModificationApplyOperation> entryStrategy;
638 protected UnkeyedListModificationStrategy(final ListSchemaNode schema) {
639 entryStrategy = Optional.<ModificationApplyOperation> of(new UnkeyedListItemModificationStrategy(schema));
643 protected StoreMetadataNode applyMerge(final NodeModification modification, final StoreMetadataNode currentMeta,
644 final UnsignedLong subtreeVersion) {
645 return applyWrite(modification, Optional.of(currentMeta), subtreeVersion);
649 protected StoreMetadataNode applySubtreeChange(final NodeModification modification,
650 final StoreMetadataNode currentMeta, final UnsignedLong subtreeVersion) {
651 throw new UnsupportedOperationException("UnkeyedList does not support subtree change.");
655 protected StoreMetadataNode applyWrite(final NodeModification modification,
656 final Optional<StoreMetadataNode> currentMeta, final UnsignedLong subtreeVersion) {
657 return StoreMetadataNode.createRecursively(modification.getWrittenValue(), subtreeVersion);
661 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
662 if (child instanceof NodeIdentifier) {
663 return entryStrategy;
665 return Optional.absent();
669 protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
674 protected void checkSubtreeModificationApplicable(final InstanceIdentifier path,final NodeModification modification,
675 final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
676 throw new DataPreconditionFailedException(path, "Subtree modification is not allowed.");
681 public static class UnorderedMapModificationStrategy extends NormalizedNodeContainerModificationStrategy {
683 private final Optional<ModificationApplyOperation> entryStrategy;
685 protected UnorderedMapModificationStrategy(final ListSchemaNode schema) {
686 super(MapNode.class);
687 entryStrategy = Optional.<ModificationApplyOperation> of(new ListEntryModificationStrategy(schema));
690 @SuppressWarnings("rawtypes")
692 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
693 checkArgument(original instanceof MapNode);
694 return ImmutableMapNodeBuilder.create((MapNode) original);
698 public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
699 if (identifier instanceof NodeIdentifierWithPredicates) {
700 return entryStrategy;
702 return Optional.absent();
706 public String toString() {
707 return "UnorderedMapModificationStrategy [entry=" + entryStrategy + "]";
711 public static class OrderedMapModificationStrategy extends NormalizedNodeContainerModificationStrategy {
713 private final Optional<ModificationApplyOperation> entryStrategy;
715 protected OrderedMapModificationStrategy(final ListSchemaNode schema) {
716 super(OrderedMapNode.class);
717 entryStrategy = Optional.<ModificationApplyOperation> of(new ListEntryModificationStrategy(schema));
720 @SuppressWarnings("rawtypes")
722 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
723 checkArgument(original instanceof OrderedMapNode);
724 return ImmutableOrderedMapNodeBuilder.create((OrderedMapNode) original);
728 public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
729 if (identifier instanceof NodeIdentifierWithPredicates) {
730 return entryStrategy;
732 return Optional.absent();
736 public String toString() {
737 return "OrderedMapModificationStrategy [entry=" + entryStrategy + "]";
741 public static AugmentationSchema createAugmentProxy(final AugmentationSchema schema, final DataNodeContainer resolved) {
742 Set<DataSchemaNode> realChildSchemas = new HashSet<>();
743 for(DataSchemaNode augChild : schema.getChildNodes()) {
744 realChildSchemas.add(resolved.getDataChildByName(augChild.getQName()));
746 return new AugmentationSchemaProxy(schema, realChildSchemas);
749 public static boolean checkDataPrecondition(final InstanceIdentifier path, final boolean condition, final String message) throws DataPreconditionFailedException {
751 throw new DataPreconditionFailedException(path, message);