final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
Preconditions.checkArgument(m.isSealed(), "Attempted to verify unsealed modification %s", m);
- m.getStrategy().checkApplicable(PUBLIC_ROOT_PATH, m.getRootModification(), Optional.of(getTipRoot()));
+ m.getStrategy().checkApplicable(PUBLIC_ROOT_PATH, m.getRootModification(), Optional.of(getTipRoot()), m.getVersion());
}
@Override
package org.opendaylight.yangtools.yang.data.impl.schema.tree;
import static com.google.common.base.Preconditions.checkArgument;
-
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Verify;
@Override
protected void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification,
- final Optional<TreeNode> current) throws DataValidationFailedException {
+ final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
if (!modification.getOriginal().isPresent() && !current.isPresent()) {
throw new ModifiedNodeDoesNotExistException(path, String.format("Node %s does not exist. Cannot apply modification to its children.", path));
}
throw new ConflictingModificationAppliedException(path, "Node was deleted by other transaction.");
}
- checkChildPreconditions(path, modification, current.get());
+ checkChildPreconditions(path, modification, current.get(), version);
}
/**
* @param modification current modification
* @param current Current data tree node.
*/
- private void checkChildPreconditions(final YangInstanceIdentifier path, final NodeModification modification, final TreeNode current) throws DataValidationFailedException {
+ private void checkChildPreconditions(final YangInstanceIdentifier path, final NodeModification modification,
+ final TreeNode current, final Version version) throws DataValidationFailedException {
for (final NodeModification childMod : modification.getChildren()) {
final YangInstanceIdentifier.PathArgument childId = childMod.getIdentifier();
final Optional<TreeNode> childMeta = current.getChild(childId);
final YangInstanceIdentifier childPath = path.node(childId);
- resolveChildOperation(childId).checkApplicable(childPath, childMod, childMeta);
+ resolveChildOperation(childId).checkApplicable(childPath, childMod, childMeta, version);
}
}
@Override
protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification,
- final Optional<TreeNode> current) throws DataValidationFailedException {
+ final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
if (current.isPresent()) {
- checkChildPreconditions(path, modification, current.get());
+ checkChildPreconditions(path, modification, current.get(), version);
}
}
package org.opendaylight.yangtools.yang.data.impl.schema.tree;
import static com.google.common.base.Preconditions.checkArgument;
-
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@Override
protected final void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification,
- final Optional<TreeNode> current) throws IncorrectDataStructureException {
+ final Optional<TreeNode> current, final Version version) throws IncorrectDataStructureException {
throw new IncorrectDataStructureException(path, "Subtree modification is not allowed.");
}
}
@Override
- void checkApplicable(final YangInstanceIdentifier path,final NodeModification modification,
- final Optional<TreeNode> storeMetadata) {
+ void checkApplicable(final YangInstanceIdentifier path, final NodeModification modification,
+ final Optional<TreeNode> storeMetadata, final Version version) {
throw new IllegalStateException("Schema Context is not available.");
}
package org.opendaylight.yangtools.yang.data.impl.schema.tree;
-
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Verify;
}
- private static int findChildrenBefore(final Optional<TreeNode> current) {
- if (current.isPresent()) {
- return numOfChildrenFromValue(current.get().getData());
- } else {
- return 0;
+ private void validateMinMaxElements(final YangInstanceIdentifier path, final PathArgument id,
+ final NormalizedNode<?, ?> data) throws DataValidationFailedException {
+ final int children = numOfChildrenFromValue(data);
+ if (minElements != null && minElements > children) {
+ throw new DataValidationFailedException(path, String.format(
+ "%s does not have enough elements (%s), needs at least %s", id,
+ children, minElements));
}
- }
-
- private static int findChildrenAfter(final ModifiedNode modification) {
- if (modification.getWrittenValue() != null) {
- return numOfChildrenFromValue(modification.getWrittenValue());
- } else {
- return 0;
+ if (maxElements != null && maxElements < children) {
+ throw new DataValidationFailedException(path, String.format(
+ "%s has too many elements (%s), can have at most %s", id, children,
+ maxElements));
}
}
private void checkMinMaxElements(final YangInstanceIdentifier path, final NodeModification nodeMod,
- final Optional<TreeNode> current) throws DataValidationFailedException {
+ final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
if (!(nodeMod instanceof ModifiedNode)) {
LOG.debug("Could not validate {}, does not implement expected class {}", nodeMod, ModifiedNode.class);
return;
}
final ModifiedNode modification = (ModifiedNode) nodeMod;
- final int childrenBefore = (modification.getOperation() == LogicalOperation.WRITE) ? 0 : findChildrenBefore
- (current);
- Verify.verify(childrenBefore >= 0, "Child count before is %s (from %s)", childrenBefore, current);
- final int childrenAfter = findChildrenAfter(modification);
- Verify.verify(childrenAfter >= 0, "Child count after is %s (from %s)", childrenAfter, modification);
+ // We need to actually perform the operation to get deal with merge in a sane manner. We know the modification
+ // is immutable, so the result of validation will probably not change.
+ final Optional<TreeNode> maybeApplied = delegate.apply(modification, current, version);
+ Verify.verify(maybeApplied.isPresent());
- final int childrenModified = numOfChildrenFromChildMods(modification, current);
- LOG.debug("Modified child count is %s (from %s and %s)", childrenModified, modification, current);
+ final TreeNode applied = maybeApplied.get();
+ validateMinMaxElements(path, modification.getIdentifier(), applied.getData());
- final int childrenTotal = childrenBefore + childrenAfter + childrenModified;
- Verify.verify(childrenTotal >= 0, "Total child count is %s (from %s and %s)", childrenTotal, modification, current);
-
- if (minElements != null && minElements > childrenTotal) {
- throw new DataValidationFailedException(path, String.format(
- "%s does not have enough elements (%s), needs at least %s", modification.getIdentifier(),
- childrenTotal, minElements));
- }
- if (maxElements != null && maxElements < childrenTotal) {
- throw new DataValidationFailedException(path, String.format(
- "%s has too many elements (%s), can have at most %s", modification.getIdentifier(), childrenTotal,
- maxElements));
- }
+ // Everything passed. We now have a snapshot of the result node, it would be too bad if we just threw it out.
+ // We know what the result of an apply operation is going to be *if* the following are kept unchanged:
+ // - the 'current' node
+ // - the schemacontext (therefore, the fact this object is associated with the modification)
+ //
+ // So let's stash the result. We will pick it up during apply operation.
+ modification.setValidatedNode(this, current, applied);
}
private static int numOfChildrenFromValue(final NormalizedNode<?, ?> value) {
value.getClass()));
}
- private static int numOfChildrenFromChildMods(final ModifiedNode modification, final Optional<TreeNode> current) {
- int result = 0;
- for (final ModifiedNode modChild : modification.getChildren()) {
- switch (modChild.getOperation()) {
- case WRITE:
- if (!checkOriginalPresent(modChild)) {
- result++;
- }
- break;
- case MERGE:
- if (!checkOriginalPresent(modChild)) {
- result++;
- }
- break;
- case DELETE:
- if (checkOriginalPresent(modChild)) {
- result--;
- }
- break;
- case NONE:
- case TOUCH:
- // NOOP
- break;
- default:
- throw new IllegalArgumentException("Unsupported operation type: " + modChild.getOperation());
- }
- }
- return result;
- }
-
private static boolean checkOriginalPresent(ModifiedNode child) {
return child.getOriginal().isPresent();
}
@Override
protected void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification,
- final Optional<TreeNode> current) throws DataValidationFailedException {
- delegate.checkTouchApplicable(path, modification, current);
- checkMinMaxElements(path, modification, current);
+ final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
+ delegate.checkTouchApplicable(path, modification, current, version);
+ checkMinMaxElements(path, modification, current, version);
}
@Override
protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification,
- final Optional<TreeNode> current) throws DataValidationFailedException {
- delegate.checkMergeApplicable(path, modification, current);
- checkMinMaxElements(path, modification, current);
+ final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
+ delegate.checkMergeApplicable(path, modification, current, version);
+ checkMinMaxElements(path, modification, current, version);
}
@Override
protected void checkWriteApplicable(final YangInstanceIdentifier path, final NodeModification modification,
- final Optional<TreeNode> current) throws DataValidationFailedException {
- delegate.checkWriteApplicable(path, modification, current);
- checkMinMaxElements(path, modification, current);
+ final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
+ delegate.checkWriteApplicable(path, modification, current, version);
+ checkMinMaxElements(path, modification, current, version);
}
-
@Override
public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
return delegate.getChild(child);
@Override
protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
+ final TreeNode validated = modification.getValidatedNode(this, Optional.of(currentMeta));
+ if (validated != null) {
+ return validated;
+ }
+
+ // FIXME: the result moved, make sure we enforce again
return delegate.applyMerge(modification, currentMeta, version);
}
@Override
protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
+ final TreeNode validated = modification.getValidatedNode(this, Optional.of(currentMeta));
+ if (validated != null) {
+ return validated;
+ }
+
+ // FIXME: the result moved, make sure we enforce again
return delegate.applyTouch(modification, currentMeta, version);
}
@Override
protected TreeNode applyWrite(final ModifiedNode modification, final Optional<TreeNode> currentMeta,
final Version version) {
+ final TreeNode validated = modification.getValidatedNode(this, currentMeta);
+ if (validated != null) {
+ return validated;
+ }
+
+ // FIXME: the result moved, make sure we enforce again
return delegate.applyWrite(modification, currentMeta, version);
}
abstract Optional<TreeNode> apply(ModifiedNode modification, Optional<TreeNode> storeMeta, Version version);
/**
- *
- * Checks if provided node modification could be applied to current metadata node.
- *
- * @param modification Modification
- * @param current Metadata Node to which modification should be applied
- * @return true if modification is applicable
- * false if modification is no applicable
- * @throws DataValidationFailedException
- */
- abstract void checkApplicable(YangInstanceIdentifier path, NodeModification modification, Optional<TreeNode> current) throws DataValidationFailedException;
+ *
+ * Checks if provided node modification could be applied to current metadata node.
+ *
+ * @param modification Modification
+ * @param current Metadata Node to which modification should be applied
+ * @param version
+ * @return true if modification is applicable
+ * false if modification is no applicable
+ * @throws DataValidationFailedException
+ */
+ abstract void checkApplicable(YangInstanceIdentifier path, NodeModification modification, Optional<TreeNode> current, Version version) throws DataValidationFailedException;
/**
*
// Alternative history introduced in WRITE nodes. Instantiated when we touch any child underneath such a node.
private TreeNode writtenOriginal;
+ // Internal cache for TreeNodes created as part of validation
+ private SchemaAwareApplyOperation validatedOp;
+ private Optional<TreeNode> validatedCurrent;
+ private TreeNode validatedNode;
+
private ModifiedNode(final PathArgument identifier, final Optional<TreeNode> original, final ChildTrackingPolicy childPolicy) {
this.identifier = identifier;
this.original = original;
public static ModifiedNode createUnmodified(final TreeNode metadataTree, final ChildTrackingPolicy childPolicy) {
return new ModifiedNode(metadataTree.getIdentifier(), Optional.of(metadataTree), childPolicy);
}
+
+ void setValidatedNode(final SchemaAwareApplyOperation op, final Optional<TreeNode> current, final TreeNode node) {
+ this.validatedOp = Preconditions.checkNotNull(op);
+ this.validatedCurrent = Preconditions.checkNotNull(current);
+ this.validatedNode = Preconditions.checkNotNull(node);
+ }
+
+ TreeNode getValidatedNode(final SchemaAwareApplyOperation op, final Optional<TreeNode> current) {
+ return op.equals(validatedOp) && current.equals(validatedCurrent) ? validatedNode : null;
+ }
}
}
@Override
- final void checkApplicable(final YangInstanceIdentifier path, final NodeModification modification, final Optional<TreeNode> current)
- throws DataValidationFailedException {
- getDelegate().checkApplicable(path, modification, current);
+ final void checkApplicable(final YangInstanceIdentifier path, final NodeModification modification,
+ final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
+ getDelegate().checkApplicable(path, modification, current, version);
}
@Override
}
@Override
- final void checkApplicable(final YangInstanceIdentifier path,final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
+ final void checkApplicable(final YangInstanceIdentifier path,final NodeModification modification, final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
switch (modification.getOperation()) {
case DELETE:
checkDeleteApplicable(modification, current);
break;
case TOUCH:
- checkTouchApplicable(path, modification, current);
+ checkTouchApplicable(path, modification, current, version);
break;
case WRITE:
- checkWriteApplicable(path, modification, current);
+ checkWriteApplicable(path, modification, current, version);
break;
case MERGE:
- checkMergeApplicable(path, modification, current);
+ checkMergeApplicable(path, modification, current, version);
break;
case NONE:
break;
}
}
- protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
+ protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification,
+ final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
final Optional<TreeNode> original = modification.getOriginal();
if (original.isPresent() && current.isPresent()) {
/*
* @throws DataValidationFailedException
*/
protected void checkWriteApplicable(final YangInstanceIdentifier path, final NodeModification modification,
- final Optional<TreeNode> current) throws DataValidationFailedException {
+ final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
final Optional<TreeNode> original = modification.getOriginal();
if (original.isPresent() && current.isPresent()) {
checkNotConflicting(path, original.get(), current.get());
* @throws ConflictingModificationAppliedException If subtree was changed in conflicting way
* @throws IncorrectDataStructureException If subtree modification is not applicable (e.g. leaf node).
*/
- protected abstract void checkTouchApplicable(YangInstanceIdentifier path, final NodeModification modification,
- final Optional<TreeNode> current) throws DataValidationFailedException;
+ protected abstract void checkTouchApplicable(YangInstanceIdentifier path, NodeModification modification,
+ Optional<TreeNode> current, Version version) throws DataValidationFailedException;
/**
* Checks if supplied schema node belong to specified Data Tree type. All nodes belong to the operational tree,
}
@Override
- void checkApplicable(final YangInstanceIdentifier path, final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
+ void checkApplicable(final YangInstanceIdentifier path, final NodeModification modification,
+ final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
if (modification.getOperation() == LogicalOperation.TOUCH && !current.isPresent()) {
// Structural containers are created as needed, so we pretend this container is here
- delegate.checkApplicable(path, modification, fakeMeta(FAKE_VERSION));
+ delegate.checkApplicable(path, modification, fakeMeta(FAKE_VERSION), version);
} else {
- delegate.checkApplicable(path, modification, current);
+ delegate.checkApplicable(path, modification, current, version);
}
}
@Override
protected void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification,
- final Optional<TreeNode> current) throws IncorrectDataStructureException {
+ final Optional<TreeNode> current, final Version version) throws IncorrectDataStructureException {
throw new IncorrectDataStructureException(path, "Subtree modification is not allowed.");
}