X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-data-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fdata%2Fimpl%2Fschema%2Ftree%2FAbstractNodeContainerModificationStrategy.java;h=4821b404a06e1688faa79071aec5d7ec018aeca4;hb=641fc551dba4beb342e811681914f5e5db46bb6f;hp=c0ddf6c4aa5f332d4c6c4cff3be79d2c97b34324;hpb=c55eefb2dcc799601c6ed51409cd5f670fb7fe73;p=yangtools.git diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/AbstractNodeContainerModificationStrategy.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/AbstractNodeContainerModificationStrategy.java index c0ddf6c4aa..4821b404a0 100644 --- a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/AbstractNodeContainerModificationStrategy.java +++ b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/AbstractNodeContainerModificationStrategy.java @@ -8,59 +8,159 @@ 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 static java.util.Objects.requireNonNull; + +import com.google.common.base.MoreObjects; +import com.google.common.base.MoreObjects.ToStringHelper; +import com.google.common.base.Verify; import java.util.Collection; +import java.util.Optional; +import org.eclipse.jdt.annotation.NonNull; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +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.NormalizedNodeContainer; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException; import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType; import org.opendaylight.yangtools.yang.data.api.schema.tree.ModifiedNodeDoesNotExistException; +import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType; import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.MutableTreeNode; 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.data.impl.schema.builder.api.NormalizedNodeContainerBuilder; +import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus; + +abstract class AbstractNodeContainerModificationStrategy + extends SchemaAwareApplyOperation { + abstract static class Invisible extends AbstractNodeContainerModificationStrategy { + private final @NonNull SchemaAwareApplyOperation entryStrategy; -abstract class AbstractNodeContainerModificationStrategy extends SchemaAwareApplyOperation { + Invisible(final NormalizedNodeContainerSupport support, final DataTreeConfiguration treeConfig, + final SchemaAwareApplyOperation entryStrategy) { + super(support, treeConfig); + this.entryStrategy = requireNonNull(entryStrategy); + } + + @Override + final T getSchema() { + return entryStrategy.getSchema(); + } - private final Class> nodeClass; + final Optional entryStrategy() { + return Optional.of(entryStrategy); + } - protected AbstractNodeContainerModificationStrategy(final Class> nodeClass) { - this.nodeClass = Preconditions.checkNotNull(nodeClass); + @Override + ToStringHelper addToStringAttributes(final ToStringHelper helper) { + return super.addToStringAttributes(helper).add("entry", entryStrategy); + } } - @Override - void verifyStructure(final ModifiedNode modification) throws IllegalArgumentException { - for (ModifiedNode childModification : modification.getChildren()) { - resolveChildOperation(childModification.getIdentifier()).verifyStructure(childModification); + abstract static class Visible extends AbstractNodeContainerModificationStrategy { + private final @NonNull T schema; + + Visible(final NormalizedNodeContainerSupport support, final DataTreeConfiguration treeConfig, + final T schema) { + super(support, treeConfig); + this.schema = requireNonNull(schema); } + + @Override + final T getSchema() { + return schema; + } + + @Override + ToStringHelper addToStringAttributes(final ToStringHelper helper) { + return super.addToStringAttributes(helper).add("schema", schema); + } + } + + private final NormalizedNodeContainerSupport support; + private final boolean verifyChildrenStructure; + + AbstractNodeContainerModificationStrategy(final NormalizedNodeContainerSupport support, + final DataTreeConfiguration treeConfig) { + this.support = requireNonNull(support); + this.verifyChildrenStructure = treeConfig.getTreeType() == TreeType.CONFIGURATION; + } + + @Override + protected final ChildTrackingPolicy getChildPolicy() { + return support.childPolicy; } - @SuppressWarnings("rawtypes") @Override - protected void verifyWrittenStructure(final NormalizedNode writtenValue) { + final void verifyValue(final NormalizedNode writtenValue) { + final Class nodeClass = support.requiredClass; checkArgument(nodeClass.isInstance(writtenValue), "Node %s is not of type %s", writtenValue, nodeClass); checkArgument(writtenValue instanceof NormalizedNodeContainer); + } + + @Override + final void verifyValueChildren(final NormalizedNode writtenValue) { + if (verifyChildrenStructure) { + final NormalizedNodeContainer container = (NormalizedNodeContainer) writtenValue; + for (final Object child : container.getValue()) { + checkArgument(child instanceof NormalizedNode); + final NormalizedNode castedChild = (NormalizedNode) child; + final Optional childOp = getChild(castedChild.getIdentifier()); + if (childOp.isPresent()) { + childOp.get().fullVerifyStructure(castedChild); + } else { + throw new SchemaValidationFailedException(String.format( + "Node %s is not a valid child of %s according to the schema.", + castedChild.getIdentifier(), container.getIdentifier())); + } + } + + optionalVerifyValueChildren(writtenValue); + } + mandatoryVerifyValueChildren(writtenValue); + } - NormalizedNodeContainer container = (NormalizedNodeContainer) writtenValue; - for (Object child : container.getValue()) { + /** + * Perform additional verification on written value's child structure, like presence of mandatory children and + * exclusion. The default implementation does nothing and is not invoked for non-CONFIG data trees. + * + * @param writtenValue Effective written value + */ + void optionalVerifyValueChildren(final NormalizedNode writtenValue) { + // Defaults to no-op + } + + /** + * Perform additional verification on written value's child structure, like presence of mandatory children. + * The default implementation does nothing. + * + * @param writtenValue Effective written value + */ + void mandatoryVerifyValueChildren(final NormalizedNode writtenValue) { + // Defaults to no-op + } + + @Override + protected final void recursivelyVerifyStructure(final NormalizedNode value) { + final NormalizedNodeContainer container = (NormalizedNodeContainer) value; + for (final Object child : container.getValue()) { checkArgument(child instanceof NormalizedNode); + final NormalizedNode castedChild = (NormalizedNode) child; + final Optional childOp = getChild(castedChild.getIdentifier()); + if (!childOp.isPresent()) { + throw new SchemaValidationFailedException( + String.format("Node %s is not a valid child of %s according to the schema.", + castedChild.getIdentifier(), container.getIdentifier())); + } - /* - * FIXME: fail-fast semantics: - * - * We can validate the data structure here, aborting the commit - * before it ever progresses to being committed. - */ + childOp.get().recursivelyVerifyStructure(castedChild); } } @Override - protected TreeNode applyWrite(final ModifiedNode modification, + protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode newValue, final Optional currentMeta, final Version version) { - final NormalizedNode newValue = modification.getWrittenValue(); final TreeNode newValueMeta = TreeNodeFactory.createTreeNode(newValue, version); if (modification.getChildren().isEmpty()) { @@ -87,9 +187,13 @@ abstract class AbstractNodeContainerModificationStrategy extends SchemaAwareAppl mutable.setSubtreeVersion(version); @SuppressWarnings("rawtypes") - final NormalizedNodeContainerBuilder dataBuilder = createBuilder(newValue); + final NormalizedNodeContainerBuilder dataBuilder = support.createBuilder(newValue); + final TreeNode result = mutateChildren(mutable, dataBuilder, version, modification.getChildren()); - return mutateChildren(mutable, dataBuilder, version, modification.getChildren()); + // We are good to go except one detail: this is a single logical write, but + // we have a result TreeNode which has been forced to materialized, e.g. it + // is larger than it needs to be. Create a new TreeNode to host the data. + return TreeNodeFactory.createTreeNode(result.getData(), version); } /** @@ -106,11 +210,11 @@ abstract class AbstractNodeContainerModificationStrategy extends SchemaAwareAppl private TreeNode mutateChildren(final MutableTreeNode meta, final NormalizedNodeContainerBuilder data, final Version nodeVersion, final Iterable modifications) { - for (ModifiedNode mod : modifications) { - final YangInstanceIdentifier.PathArgument id = mod.getIdentifier(); + for (final ModifiedNode mod : modifications) { + final PathArgument id = mod.getIdentifier(); final Optional cm = meta.getChild(id); - Optional result = resolveChildOperation(id).apply(mod, cm, nodeVersion); + final Optional result = resolveChildOperation(id).apply(mod, cm, nodeVersion); if (result.isPresent()) { final TreeNode tn = result.get(); meta.addChild(tn); @@ -126,77 +230,178 @@ abstract class AbstractNodeContainerModificationStrategy extends SchemaAwareAppl } @Override - protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, - final Version version) { - // For Node Containers - merge is same as subtree change - we only replace children. + protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) { + /* + * The node which we are merging exists. We now need to expand any child operations implied by the value. Once + * we do that, ModifiedNode children will look like this node were a TOUCH and we will let applyTouch() do the + * heavy lifting of applying the children recursively (either through here or through applyWrite(). + */ + final NormalizedNode value = modification.getWrittenValue(); + + Verify.verify(value instanceof NormalizedNodeContainer, "Attempted to merge non-container %s", value); + @SuppressWarnings({"unchecked", "rawtypes"}) + final Collection> children = ((NormalizedNodeContainer) value).getValue(); + for (final NormalizedNode c : children) { + final PathArgument id = c.getIdentifier(); + modification.modifyChild(id, resolveChildOperation(id), version); + } return applyTouch(modification, currentMeta, version); } + private void mergeChildrenIntoModification(final ModifiedNode modification, + final Collection> children, final Version version) { + for (final NormalizedNode c : children) { + final ModificationApplyOperation childOp = resolveChildOperation(c.getIdentifier()); + final ModifiedNode childNode = modification.modifyChild(c.getIdentifier(), childOp, version); + childOp.mergeIntoModifiedNode(childNode, c, version); + } + } + @Override - public TreeNode applyTouch(final ModifiedNode modification, - final TreeNode currentMeta, final Version version) { - final MutableTreeNode newMeta = currentMeta.mutable(); - newMeta.setSubtreeVersion(version); + final void mergeIntoModifiedNode(final ModifiedNode modification, final NormalizedNode value, + final Version version) { + @SuppressWarnings({ "unchecked", "rawtypes" }) + final Collection> children = ((NormalizedNodeContainer)value).getValue(); + + switch (modification.getOperation()) { + case NONE: + // Fresh node, just record a MERGE with a value + recursivelyVerifyStructure(value); + modification.updateValue(LogicalOperation.MERGE, value); + return; + case TOUCH: + mergeChildrenIntoModification(modification, children, version); + // We record empty merge value, since real children merges + // are already expanded. This is needed to satisfy non-null for merge + // original merge value can not be used since it mean different + // order of operation - parent changes are always resolved before + // children ones, and having node in TOUCH means children was modified + // before. + modification.updateValue(LogicalOperation.MERGE, support.createEmptyValue(value)); + return; + case MERGE: + // Merging into an existing node. Merge data children modifications (maybe recursively) and mark + // as MERGE, invalidating cached snapshot + mergeChildrenIntoModification(modification, children, version); + modification.updateOperationType(LogicalOperation.MERGE); + return; + case DELETE: + // Delete performs a data dependency check on existence of the node. Performing a merge on DELETE means + // we are really performing a write. One thing that ruins that are any child modifications. If there + // are any, we will perform a read() to get the current state of affairs, turn this into into a WRITE + // and then append any child entries. + if (!modification.getChildren().isEmpty()) { + // Version does not matter here as we'll throw it out + final Optional current = apply(modification, modification.getOriginal(), + Version.initial()); + if (current.isPresent()) { + modification.updateValue(LogicalOperation.WRITE, current.get().getData()); + mergeChildrenIntoModification(modification, children, version); + return; + } + } + + modification.updateValue(LogicalOperation.WRITE, value); + return; + case WRITE: + // We are augmenting a previous write. We'll just walk value's children, get the corresponding + // ModifiedNode and run recursively on it + mergeChildrenIntoModification(modification, children, version); + modification.updateOperationType(LogicalOperation.WRITE); + return; + default: + throw new IllegalArgumentException("Unsupported operation " + modification.getOperation()); + } + } + + @Override + protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) { /* - * The user has issued an empty merge operation. In this case we do not perform - * a data tree mutation, do not pass GO, and do not collect useless garbage. + * The user may have issued an empty merge operation. In this case we do not perform + * a data tree mutation, do not pass GO, and do not collect useless garbage. It + * also means the ModificationType is UNMODIFIED. */ final Collection children = modification.getChildren(); - if (children.isEmpty()) { - modification.resolveModificationType(ModificationType.UNMODIFIED); - newMeta.setData(currentMeta.getData()); - return newMeta.seal(); - } + if (!children.isEmpty()) { + @SuppressWarnings("rawtypes") + final NormalizedNodeContainerBuilder dataBuilder = support.createBuilder(currentMeta.getData()); + final MutableTreeNode newMeta = currentMeta.mutable(); + newMeta.setSubtreeVersion(version); + final TreeNode ret = mutateChildren(newMeta, dataBuilder, version, children); - @SuppressWarnings("rawtypes") - NormalizedNodeContainerBuilder dataBuilder = createBuilder(currentMeta.getData()); + /* + * It is possible that the only modifications under this node were empty merges, + * which were turned into UNMODIFIED. If that is the case, we can turn this operation + * into UNMODIFIED, too, potentially cascading it up to root. This has the benefit + * of speeding up any users, who can skip processing child nodes. + * + * In order to do that, though, we have to check all child operations are UNMODIFIED. + * Let's do precisely that, stopping as soon we find a different result. + */ + for (final ModifiedNode child : children) { + if (child.getModificationType() != ModificationType.UNMODIFIED) { + modification.resolveModificationType(ModificationType.SUBTREE_MODIFIED); + return ret; + } + } + } - /* - * TODO: this is not entirely accurate. If there is only an empty merge operation - * among the children, its effect is ModificationType.UNMODIFIED. That would - * mean this operation can be turned into UNMODIFIED, cascading that further - * up the root -- potentially turning the entire transaction into a no-op - * from the perspective of physical replication. - * - * In order to do that, though, we either have to walk the children ourselves - * (looking for a non-UNMODIFIED child), or have mutateChildren() pass that - * information back to us. - */ - modification.resolveModificationType(ModificationType.SUBTREE_MODIFIED); - return mutateChildren(newMeta, dataBuilder, version, children); + // The merge operation did not have any children, or all of them turned out to be UNMODIFIED, hence do not + // replace the metadata node. + modification.resolveModificationType(ModificationType.UNMODIFIED); + return currentMeta; } @Override - protected void checkTouchApplicable(final YangInstanceIdentifier path, final NodeModification modification, - final Optional current) throws DataValidationFailedException { + protected final void checkTouchApplicable(final ModificationPath path, final NodeModification modification, + final Optional 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)); + final YangInstanceIdentifier id = path.toInstanceIdentifier(); + throw new ModifiedNodeDoesNotExistException(id, + String.format("Node %s does not exist. Cannot apply modification to its children.", id)); } - SchemaAwareApplyOperation.checkConflicting(path, current.isPresent(), "Node was deleted by other transaction."); - checkChildPreconditions(path, modification, current); + checkConflicting(path, current.isPresent(), "Node was deleted by other transaction."); + checkChildPreconditions(path, modification, current.get(), version); } - private void checkChildPreconditions(final YangInstanceIdentifier path, final NodeModification modification, final Optional current) throws DataValidationFailedException { - final TreeNode currentMeta = current.get(); - for (NodeModification childMod : modification.getChildren()) { - final YangInstanceIdentifier.PathArgument childId = childMod.getIdentifier(); - final Optional childMeta = currentMeta.getChild(childId); + @Override + protected final void checkMergeApplicable(final ModificationPath path, final NodeModification modification, + final Optional current, final Version version) throws DataValidationFailedException { + if (current.isPresent()) { + checkChildPreconditions(path, modification, current.get(), version); + } + } - YangInstanceIdentifier childPath = path.node(childId); - resolveChildOperation(childId).checkApplicable(childPath, childMod, childMeta); + /** + * Recursively check child preconditions. + * + * @param path current node path + * @param modification current modification + * @param current Current data tree node. + */ + private void checkChildPreconditions(final ModificationPath path, final NodeModification modification, + final TreeNode current, final Version version) throws DataValidationFailedException { + for (final NodeModification childMod : modification.getChildren()) { + final PathArgument childId = childMod.getIdentifier(); + final Optional childMeta = current.getChild(childId); + + path.push(childId); + try { + resolveChildOperation(childId).checkApplicable(path, childMod, childMeta, version); + } finally { + path.pop(); + } } } @Override - protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification, - final Optional current) throws DataValidationFailedException { - if(current.isPresent()) { - checkChildPreconditions(path, modification,current); - } + public final String toString() { + return addToStringAttributes(MoreObjects.toStringHelper(this)).toString(); } - @SuppressWarnings("rawtypes") - protected abstract NormalizedNodeContainerBuilder createBuilder(NormalizedNode original); + ToStringHelper addToStringAttributes(final ToStringHelper helper) { + return helper.add("support", support).add("verifyChildren", verifyChildrenStructure); + } }