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 return StoreMetadataNode.builder(subtreeVersion).setSubtreeVersion(subtreeVersion)
276 .setData(modification.getWrittenValue()).build();
280 protected void checkSubtreeModificationApplicable(final InstanceIdentifier path,final NodeModification modification,
281 final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
282 throw new DataPreconditionFailedException(path, "Subtree modification is not allowed.");
287 public static class LeafSetEntryModificationStrategy extends ValueNodeModificationStrategy<LeafListSchemaNode> {
289 @SuppressWarnings({ "unchecked", "rawtypes" })
290 protected LeafSetEntryModificationStrategy(final LeafListSchemaNode schema) {
291 super(schema, (Class) LeafSetEntryNode.class);
295 public static class LeafModificationStrategy extends ValueNodeModificationStrategy<LeafSchemaNode> {
297 @SuppressWarnings({ "unchecked", "rawtypes" })
298 protected LeafModificationStrategy(final LeafSchemaNode schema) {
299 super(schema, (Class) LeafNode.class);
303 public static abstract class NormalizedNodeContainerModificationStrategy extends SchemaAwareApplyOperation {
305 private final Class<? extends NormalizedNode<?, ?>> nodeClass;
307 protected NormalizedNodeContainerModificationStrategy(final Class<? extends NormalizedNode<?, ?>> nodeClass) {
308 this.nodeClass = nodeClass;
312 public void verifyStructure(final NodeModification modification) throws IllegalArgumentException {
313 if (modification.getModificationType() == ModificationType.WRITE) {
316 for (NodeModification childModification : modification.getModifications()) {
317 resolveChildOperation(childModification.getIdentifier()).verifyStructure(childModification);
322 protected void checkWriteApplicable(final InstanceIdentifier path, final NodeModification modification,
323 final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
324 // FIXME: Implement proper write check for replacement of node container
325 // prerequisite is to have transaction chain available for clients
326 // otherwise this will break chained writes to same node.
329 @SuppressWarnings("rawtypes")
331 protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
332 checkArgument(nodeClass.isInstance(writtenValue), "Node should must be of type %s", nodeClass);
333 checkArgument(writtenValue instanceof NormalizedNodeContainer);
335 NormalizedNodeContainer container = (NormalizedNodeContainer) writtenValue;
336 for (Object child : container.getValue()) {
337 checkArgument(child instanceof NormalizedNode);
340 * FIXME: fail-fast semantics:
342 * We can validate the data structure here, aborting the commit
343 * before it ever progresses to being committed.
349 protected StoreMetadataNode applyWrite(final NodeModification modification,
350 final Optional<StoreMetadataNode> currentMeta, final UnsignedLong subtreeVersion) {
352 NormalizedNode<?, ?> newValue = modification.getWrittenValue();
354 final UnsignedLong nodeVersion;
355 if (currentMeta.isPresent()) {
356 nodeVersion = StoreUtils.increase(currentMeta.get().getNodeVersion());
358 nodeVersion = subtreeVersion;
361 final StoreMetadataNode newValueMeta = StoreMetadataNode.createRecursively(newValue, nodeVersion, nodeVersion);
362 if (!modification.hasAdditionalModifications()) {
366 @SuppressWarnings("rawtypes")
367 NormalizedNodeContainerBuilder dataBuilder = createBuilder(newValue);
368 StoreNodeCompositeBuilder builder = StoreNodeCompositeBuilder.create(nodeVersion, dataBuilder) //
369 .setSubtreeVersion(subtreeVersion);
371 return mutateChildren(modification.getModifications(), newValueMeta, builder, nodeVersion);
375 protected StoreMetadataNode applyMerge(final NodeModification modification, final StoreMetadataNode currentMeta,
376 final UnsignedLong subtreeVersion) {
377 // For Node Containers - merge is same as subtree change - we only replace children.
378 return applySubtreeChange(modification, currentMeta, subtreeVersion);
382 public StoreMetadataNode applySubtreeChange(final NodeModification modification,
383 final StoreMetadataNode currentMeta, final UnsignedLong subtreeVersion) {
384 // Bump subtree version to its new target
385 final UnsignedLong updatedSubtreeVersion = StoreUtils.increase(currentMeta.getSubtreeVersion());
387 @SuppressWarnings("rawtypes")
388 NormalizedNodeContainerBuilder dataBuilder = createBuilder(currentMeta.getData());
389 StoreNodeCompositeBuilder builder = StoreNodeCompositeBuilder.create(dataBuilder, currentMeta)
390 .setIdentifier(modification.getIdentifier())
391 .setSubtreeVersion(updatedSubtreeVersion);
393 return mutateChildren(modification.getModifications(), currentMeta, builder, updatedSubtreeVersion);
396 private StoreMetadataNode mutateChildren(final Iterable<NodeModification> modifications, final StoreMetadataNode meta,
397 final StoreNodeCompositeBuilder builder, final UnsignedLong nodeVersion) {
399 for (NodeModification mod : modifications) {
400 final PathArgument id = mod.getIdentifier();
401 final Optional<StoreMetadataNode> cm = meta.getChild(id);
403 Optional<StoreMetadataNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
404 if (result.isPresent()) {
405 builder.add(result.get());
411 return builder.build();
415 protected void checkSubtreeModificationApplicable(final InstanceIdentifier path,final NodeModification modification,
416 final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
417 checkDataPrecondition(path, current.isPresent(), "Node was deleted by other transaction.");
418 checkChildPreconditions(path,modification,current);
422 private void checkChildPreconditions(final InstanceIdentifier path, final NodeModification modification, final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
423 StoreMetadataNode currentMeta = current.get();
424 for (NodeModification childMod : modification.getModifications()) {
425 PathArgument childId = childMod.getIdentifier();
426 Optional<StoreMetadataNode> childMeta = currentMeta.getChild(childId);
427 InstanceIdentifier childPath = StoreUtils.append(path, childId);
428 resolveChildOperation(childId).checkApplicable(childPath,childMod, childMeta);
433 protected void checkMergeApplicable(final InstanceIdentifier path, final NodeModification modification,
434 final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
435 if(current.isPresent()) {
436 checkChildPreconditions(path,modification,current);
440 @SuppressWarnings("rawtypes")
441 protected abstract NormalizedNodeContainerBuilder createBuilder(NormalizedNode<?, ?> original);
444 public static abstract class DataNodeContainerModificationStrategy<T extends DataNodeContainer> extends NormalizedNodeContainerModificationStrategy {
446 private final T schema;
447 private final LoadingCache<PathArgument, ModificationApplyOperation> childCache = CacheBuilder.newBuilder()
448 .build(CacheLoader.from(new Function<PathArgument, ModificationApplyOperation>() {
451 public ModificationApplyOperation apply(final PathArgument identifier) {
452 if (identifier instanceof AugmentationIdentifier && schema instanceof AugmentationTarget) {
453 return from(schema, (AugmentationTarget) schema, (AugmentationIdentifier) identifier);
456 DataSchemaNode child = schema.getDataChildByName(identifier.getNodeType());
464 protected DataNodeContainerModificationStrategy(final T schema,
465 final Class<? extends NormalizedNode<?, ?>> nodeClass) {
467 this.schema = schema;
470 protected T getSchema() {
475 public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
477 return Optional.<ModificationApplyOperation> fromNullable(childCache.get(identifier));
478 } catch (ExecutionException e) {
479 return Optional.absent();
484 @SuppressWarnings("rawtypes")
485 protected abstract DataContainerNodeBuilder createBuilder(NormalizedNode<?, ?> original);
488 public String toString() {
489 return getClass().getSimpleName() + " [" + schema + "]";
494 public static class ContainerModificationStrategy extends DataNodeContainerModificationStrategy<ContainerSchemaNode> {
496 public ContainerModificationStrategy(final ContainerSchemaNode schemaNode) {
497 super(schemaNode, ContainerNode.class);
501 @SuppressWarnings("rawtypes")
502 protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
503 checkArgument(original instanceof ContainerNode);
504 return ImmutableContainerNodeBuilder.create((ContainerNode) original);
508 public static class UnkeyedListItemModificationStrategy extends DataNodeContainerModificationStrategy<ListSchemaNode> {
510 public UnkeyedListItemModificationStrategy(final ListSchemaNode schemaNode) {
511 super(schemaNode, UnkeyedListEntryNode.class);
515 @SuppressWarnings("rawtypes")
516 protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
517 checkArgument(original instanceof UnkeyedListEntryNode);
518 return ImmutableUnkeyedListEntryNodeBuilder.create((UnkeyedListEntryNode) original);
522 public static class AugmentationModificationStrategy extends DataNodeContainerModificationStrategy<AugmentationSchema> {
524 protected AugmentationModificationStrategy(final AugmentationSchema schema, final DataNodeContainer resolved) {
525 super(createAugmentProxy(schema,resolved), AugmentationNode.class);
529 @SuppressWarnings("rawtypes")
530 protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
531 checkArgument(original instanceof AugmentationNode);
532 return ImmutableAugmentationNodeBuilder.create((AugmentationNode) original);
536 public static class ChoiceModificationStrategy extends NormalizedNodeContainerModificationStrategy {
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 ImmutableMap.Builder<PathArgument, ModificationApplyOperation> child = ImmutableMap.builder();
544 for (ChoiceCaseNode caze : schemaNode.getCases()) {
545 for (DataSchemaNode cazeChild : caze.getChildNodes()) {
546 SchemaAwareApplyOperation childNode = from(cazeChild);
547 child.put(new NodeIdentifier(cazeChild.getQName()), childNode);
550 childNodes = child.build();
554 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
555 return Optional.fromNullable(childNodes.get(child));
559 @SuppressWarnings("rawtypes")
560 protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
561 checkArgument(original instanceof org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode);
562 return ImmutableChoiceNodeBuilder.create((org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode) original);
566 public static class ListEntryModificationStrategy extends DataNodeContainerModificationStrategy<ListSchemaNode> {
568 protected ListEntryModificationStrategy(final ListSchemaNode schema) {
569 super(schema, MapEntryNode.class);
573 @SuppressWarnings("rawtypes")
574 protected final DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
575 checkArgument(original instanceof MapEntryNode);
576 return ImmutableMapEntryNodeBuilder.create((MapEntryNode) original);
580 public static class UnorderedLeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy {
582 private final Optional<ModificationApplyOperation> entryStrategy;
584 @SuppressWarnings({ "unchecked", "rawtypes" })
585 protected UnorderedLeafSetModificationStrategy(final LeafListSchemaNode schema) {
586 super((Class) LeafSetNode.class);
587 entryStrategy = Optional.<ModificationApplyOperation> of(new LeafSetEntryModificationStrategy(schema));
590 @SuppressWarnings("rawtypes")
592 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
593 checkArgument(original instanceof LeafSetNode<?>);
594 return ImmutableLeafSetNodeBuilder.create((LeafSetNode<?>) original);
598 public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
599 if (identifier instanceof NodeWithValue) {
600 return entryStrategy;
602 return Optional.absent();
606 public static class OrderedLeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy {
608 private final Optional<ModificationApplyOperation> entryStrategy;
610 @SuppressWarnings({ "unchecked", "rawtypes" })
611 protected OrderedLeafSetModificationStrategy(final LeafListSchemaNode schema) {
612 super((Class) LeafSetNode.class);
613 entryStrategy = Optional.<ModificationApplyOperation> of(new LeafSetEntryModificationStrategy(schema));
616 @SuppressWarnings("rawtypes")
618 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
619 checkArgument(original instanceof OrderedLeafSetNode<?>);
620 return ImmutableOrderedLeafSetNodeBuilder.create((OrderedLeafSetNode<?>) original);
624 public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
625 if (identifier instanceof NodeWithValue) {
626 return entryStrategy;
628 return Optional.absent();
632 public static class UnkeyedListModificationStrategy extends SchemaAwareApplyOperation {
634 private final Optional<ModificationApplyOperation> entryStrategy;
636 protected UnkeyedListModificationStrategy(final ListSchemaNode schema) {
637 entryStrategy = Optional.<ModificationApplyOperation> of(new UnkeyedListItemModificationStrategy(schema));
641 protected StoreMetadataNode applyMerge(final NodeModification modification, final StoreMetadataNode currentMeta,
642 final UnsignedLong subtreeVersion) {
643 return applyWrite(modification, Optional.of(currentMeta), subtreeVersion);
647 protected StoreMetadataNode applySubtreeChange(final NodeModification modification,
648 final StoreMetadataNode currentMeta, final UnsignedLong subtreeVersion) {
649 throw new UnsupportedOperationException("UnkeyedList does not support subtree change.");
653 protected StoreMetadataNode applyWrite(final NodeModification modification,
654 final Optional<StoreMetadataNode> currentMeta, final UnsignedLong subtreeVersion) {
655 return StoreMetadataNode.createRecursively(modification.getWrittenValue(), subtreeVersion);
659 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
660 if (child instanceof NodeIdentifier) {
661 return entryStrategy;
663 return Optional.absent();
667 protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
672 protected void checkSubtreeModificationApplicable(final InstanceIdentifier path,final NodeModification modification,
673 final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
674 throw new DataPreconditionFailedException(path, "Subtree modification is not allowed.");
679 public static class UnorderedMapModificationStrategy extends NormalizedNodeContainerModificationStrategy {
681 private final Optional<ModificationApplyOperation> entryStrategy;
683 protected UnorderedMapModificationStrategy(final ListSchemaNode schema) {
684 super(MapNode.class);
685 entryStrategy = Optional.<ModificationApplyOperation> of(new ListEntryModificationStrategy(schema));
688 @SuppressWarnings("rawtypes")
690 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
691 checkArgument(original instanceof MapNode);
692 return ImmutableMapNodeBuilder.create((MapNode) original);
696 public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
697 if (identifier instanceof NodeIdentifierWithPredicates) {
698 return entryStrategy;
700 return Optional.absent();
704 public String toString() {
705 return "UnorderedMapModificationStrategy [entry=" + entryStrategy + "]";
709 public static class OrderedMapModificationStrategy extends NormalizedNodeContainerModificationStrategy {
711 private final Optional<ModificationApplyOperation> entryStrategy;
713 protected OrderedMapModificationStrategy(final ListSchemaNode schema) {
714 super(OrderedMapNode.class);
715 entryStrategy = Optional.<ModificationApplyOperation> of(new ListEntryModificationStrategy(schema));
718 @SuppressWarnings("rawtypes")
720 protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
721 checkArgument(original instanceof OrderedMapNode);
722 return ImmutableOrderedMapNodeBuilder.create((OrderedMapNode) original);
726 public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
727 if (identifier instanceof NodeIdentifierWithPredicates) {
728 return entryStrategy;
730 return Optional.absent();
734 public String toString() {
735 return "OrderedMapModificationStrategy [entry=" + entryStrategy + "]";
739 public static AugmentationSchema createAugmentProxy(final AugmentationSchema schema, final DataNodeContainer resolved) {
740 Set<DataSchemaNode> realChildSchemas = new HashSet<>();
741 for(DataSchemaNode augChild : schema.getChildNodes()) {
742 realChildSchemas.add(resolved.getDataChildByName(augChild.getQName()));
744 return new AugmentationSchemaProxy(schema, realChildSchemas);
747 public static boolean checkDataPrecondition(final InstanceIdentifier path, final boolean condition, final String message) throws DataPreconditionFailedException {
749 throw new DataPreconditionFailedException(path, message);