import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
+import java.util.List;
import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
import org.opendaylight.yangtools.yang.data.api.schema.tree.IncorrectDataStructureException;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
-import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
+import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.util.List;
-
abstract class SchemaAwareApplyOperation implements ModificationApplyOperation {
private static final Logger LOG = LoggerFactory.getLogger(SchemaAwareApplyOperation.class);
return new DataNodeContainerModificationStrategy.ContainerModificationStrategy((ContainerSchemaNode) schemaNode);
} else if (schemaNode instanceof ListSchemaNode) {
return fromListSchemaNode((ListSchemaNode) schemaNode);
- } else if (schemaNode instanceof ChoiceNode) {
- return new NormalizedNodeContainerModificationStrategy.ChoiceModificationStrategy((ChoiceNode) schemaNode);
+ } else if (schemaNode instanceof ChoiceSchemaNode) {
+ return new NormalizedNodeContainerModificationStrategy.ChoiceModificationStrategy((ChoiceSchemaNode) schemaNode);
} else if (schemaNode instanceof LeafListSchemaNode) {
return fromLeafListSchemaNode((LeafListSchemaNode) schemaNode);
} else if (schemaNode instanceof LeafSchemaNode) {
return null;
}
- public static boolean checkConflicting(final InstanceIdentifier path, final boolean condition, final String message) throws ConflictingModificationAppliedException {
+ public static boolean checkConflicting(final YangInstanceIdentifier path, final boolean condition, final String message) throws ConflictingModificationAppliedException {
if(!condition) {
throw new ConflictingModificationAppliedException(path, message);
}
}
}
- private static final void checkNotConflicting(final InstanceIdentifier path, final TreeNode original, final TreeNode current) throws ConflictingModificationAppliedException {
+ private static final void checkNotConflicting(final YangInstanceIdentifier path, final TreeNode original, final TreeNode current) throws ConflictingModificationAppliedException {
checkConflicting(path, original.getVersion().equals(current.getVersion()),
"Node was replaced by other transaction.");
checkConflicting(path, original.getSubtreeVersion().equals(current.getSubtreeVersion()),
@Override
public void verifyStructure(final ModifiedNode modification) throws IllegalArgumentException {
- if (modification.getType() == ModificationType.WRITE) {
+ if (modification.getOperation() == LogicalOperation.WRITE) {
verifyWrittenStructure(modification.getWrittenValue());
}
}
@Override
- public final void checkApplicable(final InstanceIdentifier path,final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
- switch (modification.getType()) {
+ public final void checkApplicable(final YangInstanceIdentifier path,final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
+ switch (modification.getOperation()) {
case DELETE:
checkDeleteApplicable(modification, current);
- case SUBTREE_MODIFIED:
+ case TOUCH:
checkSubtreeModificationApplicable(path, modification, current);
return;
case WRITE:
case MERGE:
checkMergeApplicable(path, modification, current);
return;
- case UNMODIFIED:
+ case NONE:
return;
default:
- throw new UnsupportedOperationException("Suplied modification type "+ modification.getType()+ "is not supported.");
+ throw new UnsupportedOperationException("Suplied modification type "+ modification.getOperation()+ "is not supported.");
}
}
- protected void checkMergeApplicable(final InstanceIdentifier path, final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
+ protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
Optional<TreeNode> original = modification.getOriginal();
if (original.isPresent() && current.isPresent()) {
/*
}
}
- protected void checkWriteApplicable(final InstanceIdentifier path, final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
+ /**
+ * Checks if write operation can be applied to current TreeNode.
+ * The operation checks if original tree node to which the modification is going to be applied exists and if
+ * current node in TreeNode structure exists.
+ *
+ * @param path Path from current node in TreeNode
+ * @param modification modification to apply
+ * @param current current node in TreeNode for modification to apply
+ * @throws DataValidationFailedException
+ */
+ protected void checkWriteApplicable(final YangInstanceIdentifier path, final NodeModification modification,
+ final Optional<TreeNode> current) throws DataValidationFailedException {
Optional<TreeNode> original = modification.getOriginal();
if (original.isPresent() && current.isPresent()) {
checkNotConflicting(path, original.get(), current.get());
} else if(original.isPresent()) {
throw new ConflictingModificationAppliedException(path,"Node was deleted by other transaction.");
+ } else if(current.isPresent()) {
+ throw new ConflictingModificationAppliedException(path,"Node was created by other transaction.");
}
}
}
}
+ boolean isOrdered() {
+ return false;
+ }
+
@Override
public final Optional<TreeNode> apply(final ModifiedNode modification,
final Optional<TreeNode> currentMeta, final Version version) {
- switch (modification.getType()) {
+ switch (modification.getOperation()) {
case DELETE:
- return modification.storeSnapshot(Optional.<TreeNode> absent());
- case SUBTREE_MODIFIED:
+ return modification.setSnapshot(Optional.<TreeNode> absent());
+ case TOUCH:
Preconditions.checkArgument(currentMeta.isPresent(), "Metadata not available for modification",
modification);
- return modification.storeSnapshot(Optional.of(applySubtreeChange(modification, currentMeta.get(),
+ return modification.setSnapshot(Optional.of(applySubtreeChange(modification, currentMeta.get(),
version)));
case MERGE:
- if(currentMeta.isPresent()) {
- return modification.storeSnapshot(Optional.of(applyMerge(modification,currentMeta.get(), version)));
- } // Fallback to write is intentional - if node is not preexisting merge is same as write
+ final TreeNode result;
+
+ // This is a slight optimization: a merge on a non-existing node equals to a write
+ if (currentMeta.isPresent()) {
+ result = applyMerge(modification,currentMeta.get(), version);
+ } else {
+ result = applyWrite(modification, currentMeta, version);
+ }
+
+ return modification.setSnapshot(Optional.of(result));
case WRITE:
- return modification.storeSnapshot(Optional.of(applyWrite(modification, currentMeta, version)));
- case UNMODIFIED:
+ return modification.setSnapshot(Optional.of(applyWrite(modification, currentMeta, version)));
+ case NONE:
return currentMeta;
default:
throw new IllegalArgumentException("Provided modification type is not supported.");
* @throws ConflictingModificationAppliedException If subtree was changed in conflicting way
* @throws IncorrectDataStructureException If subtree modification is not applicable (e.g. leaf node).
*/
- protected abstract void checkSubtreeModificationApplicable(InstanceIdentifier path, final NodeModification modification,
+ protected abstract void checkSubtreeModificationApplicable(YangInstanceIdentifier path, final NodeModification modification,
final Optional<TreeNode> current) throws DataValidationFailedException;
protected abstract void verifyWrittenStructure(NormalizedNode<?, ?> writtenValue);
-
- public static class UnkeyedListModificationStrategy extends SchemaAwareApplyOperation {
-
- private final Optional<ModificationApplyOperation> entryStrategy;
-
- protected UnkeyedListModificationStrategy(final ListSchemaNode schema) {
- entryStrategy = Optional.<ModificationApplyOperation> of(new DataNodeContainerModificationStrategy.UnkeyedListItemModificationStrategy(schema));
- }
-
- @Override
- protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
- final Version version) {
- return applyWrite(modification, Optional.of(currentMeta), version);
- }
-
- @Override
- protected TreeNode applySubtreeChange(final ModifiedNode modification,
- final TreeNode currentMeta, final Version version) {
- throw new UnsupportedOperationException("UnkeyedList does not support subtree change.");
- }
-
- @Override
- protected TreeNode applyWrite(final ModifiedNode modification,
- final Optional<TreeNode> currentMeta, final Version version) {
- return TreeNodeFactory.createTreeNode(modification.getWrittenValue(), version);
- }
-
- @Override
- public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
- if (child instanceof NodeIdentifier) {
- return entryStrategy;
- }
- return Optional.absent();
- }
-
- @Override
- protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
-
- }
-
- @Override
- protected void checkSubtreeModificationApplicable(final InstanceIdentifier path, final NodeModification modification,
- final Optional<TreeNode> current) throws IncorrectDataStructureException {
- throw new IncorrectDataStructureException(path, "Subtree modification is not allowed.");
- }
- }
}