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=342745b735999cb1266d5b9b07b22f7601d89d59;hb=bf009ab944ceb87cbd348ee5b1e23eaefe2577da;hp=5ea52b1686cf3aca334ac8d1961125cac15ad78d;hpb=68a49d923b1398043d5453c2732661957835cc29;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 5ea52b1686..342745b735 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,15 +8,19 @@ package org.opendaylight.yangtools.yang.data.impl.schema.tree; import static com.google.common.base.Preconditions.checkArgument; +import static java.util.Objects.requireNonNull; -import com.google.common.base.Preconditions; +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.eclipse.jdt.annotation.Nullable; 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.ConflictingModificationAppliedException; 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; @@ -27,60 +31,140 @@ 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 class AbstractNodeContainerModificationStrategy + extends SchemaAwareApplyOperation { + abstract static class Invisible extends AbstractNodeContainerModificationStrategy { + private final @NonNull SchemaAwareApplyOperation entryStrategy; - private final Class> nodeClass; + Invisible(final NormalizedNodeContainerSupport support, final DataTreeConfiguration treeConfig, + final SchemaAwareApplyOperation entryStrategy) { + super(support, treeConfig); + this.entryStrategy = requireNonNull(entryStrategy); + } + + @Override + final T getSchema() { + return entryStrategy.getSchema(); + } + + final Optional entryStrategy() { + return Optional.of(entryStrategy); + } + + @Override + ToStringHelper addToStringAttributes(final ToStringHelper helper) { + return super.addToStringAttributes(helper).add("entry", entryStrategy); + } + } + + 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); + } + } + + /** + * Fake TreeNode version used in + * {@link #checkTouchApplicable(ModificationPath, NodeModification, Optional, Version)} + * It is okay to use a global constant, as the delegate will ignore it anyway. + */ + private static final Version FAKE_VERSION = Version.initial(); + + private final NormalizedNodeContainerSupport support; private final boolean verifyChildrenStructure; - protected AbstractNodeContainerModificationStrategy(final Class> nodeClass, + AbstractNodeContainerModificationStrategy(final NormalizedNodeContainerSupport support, final DataTreeConfiguration treeConfig) { - this.nodeClass = Preconditions.checkNotNull(nodeClass , "nodeClass"); + this.support = requireNonNull(support); this.verifyChildrenStructure = treeConfig.getTreeType() == TreeType.CONFIGURATION; } @Override - void verifyStructure(final NormalizedNode writtenValue, final boolean verifyChildren) { + protected final ChildTrackingPolicy getChildPolicy() { + return support.childPolicy; + } + + @Override + 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); - if (verifyChildrenStructure && verifyChildren) { + } + + @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()); + for (final NormalizedNode child : container.getValue()) { + final Optional childOp = getChild(child.getIdentifier()); if (childOp.isPresent()) { - childOp.get().verifyStructure(castedChild, verifyChildren); + childOp.get().fullVerifyStructure(child); } else { throw new SchemaValidationFailedException(String.format( "Node %s is not a valid child of %s according to the schema.", - castedChild.getIdentifier(), container.getIdentifier())); + child.getIdentifier(), container.getIdentifier())); } } + + optionalVerifyValueChildren(writtenValue); } + mandatoryVerifyValueChildren(writtenValue); + } + + /** + * 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 void recursivelyVerifyStructure(final NormalizedNode value) { + 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()) { - childOp.get().recursivelyVerifyStructure(castedChild); - } else { + for (final NormalizedNode child : container.getValue()) { + final Optional childOp = getChild(child.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())); + child.getIdentifier(), container.getIdentifier())); } + + childOp.get().recursivelyVerifyStructure(child); } } @Override - protected TreeNode applyWrite(final ModifiedNode modification, - final Optional currentMeta, final Version version) { - final NormalizedNode newValue = modification.getWrittenValue(); + protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode newValue, + final Optional currentMeta, final Version version) { final TreeNode newValueMeta = TreeNodeFactory.createTreeNode(newValue, version); if (modification.getChildren().isEmpty()) { @@ -107,7 +191,7 @@ 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()); // We are good to go except one detail: this is a single logical write, but @@ -132,9 +216,9 @@ abstract class AbstractNodeContainerModificationStrategy extends SchemaAwareAppl for (final ModifiedNode mod : modifications) { final PathArgument id = mod.getIdentifier(); - final Optional cm = meta.getChild(id); + final Optional cm = meta.getChild(id); - final 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); @@ -159,9 +243,7 @@ abstract class AbstractNodeContainerModificationStrategy extends SchemaAwareAppl 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) { + for (final NormalizedNode c : ((NormalizedNodeContainer) value).getValue()) { final PathArgument id = c.getIdentifier(); modification.modifyChild(id, resolveChildOperation(id), version); } @@ -169,7 +251,7 @@ abstract class AbstractNodeContainerModificationStrategy extends SchemaAwareAppl } private void mergeChildrenIntoModification(final ModifiedNode modification, - final Collection> children, final Version version) { + 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); @@ -180,8 +262,8 @@ abstract class AbstractNodeContainerModificationStrategy extends SchemaAwareAppl @Override final void mergeIntoModifiedNode(final ModifiedNode modification, final NormalizedNode value, final Version version) { - @SuppressWarnings({ "unchecked", "rawtypes" }) - final Collection> children = ((NormalizedNodeContainer)value).getValue(); + final Collection> children = + ((NormalizedNodeContainer)value).getValue(); switch (modification.getOperation()) { case NONE: @@ -198,7 +280,7 @@ abstract class AbstractNodeContainerModificationStrategy extends SchemaAwareAppl // 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, createEmptyValue(value)); + modification.updateValue(LogicalOperation.MERGE, support.createEmptyValue(value)); return; case MERGE: // Merging into an existing node. Merge data children modifications (maybe recursively) and mark @@ -213,7 +295,7 @@ abstract class AbstractNodeContainerModificationStrategy extends SchemaAwareAppl // 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(), + final Optional current = apply(modification, modification.getOriginal(), Version.initial()); if (current.isPresent()) { modification.updateValue(LogicalOperation.WRITE, current.get().getData()); @@ -245,7 +327,7 @@ abstract class AbstractNodeContainerModificationStrategy extends SchemaAwareAppl final Collection children = modification.getChildren(); if (!children.isEmpty()) { @SuppressWarnings("rawtypes") - final NormalizedNodeContainerBuilder dataBuilder = createBuilder(currentMeta.getData()); + final NormalizedNodeContainerBuilder dataBuilder = support.createBuilder(currentMeta.getData()); final MutableTreeNode newMeta = currentMeta.mutable(); newMeta.setSubtreeVersion(version); final TreeNode ret = mutateChildren(newMeta, dataBuilder, version, children); @@ -274,16 +356,49 @@ abstract class AbstractNodeContainerModificationStrategy extends SchemaAwareAppl } @Override - protected void checkTouchApplicable(final ModificationPath path, final NodeModification modification, - final Optional current, final Version version) throws DataValidationFailedException { - if (!modification.getOriginal().isPresent() && !current.isPresent()) { - final YangInstanceIdentifier id = path.toInstanceIdentifier(); - throw new ModifiedNodeDoesNotExistException(id, - String.format("Node %s does not exist. Cannot apply modification to its children.", id)); + protected final void checkTouchApplicable(final ModificationPath path, final NodeModification modification, + final Optional current, final Version version) throws DataValidationFailedException { + final TreeNode currentNode; + if (!current.isPresent()) { + currentNode = defaultTreeNode(); + if (currentNode == null) { + if (!modification.getOriginal().isPresent()) { + final YangInstanceIdentifier id = path.toInstanceIdentifier(); + throw new ModifiedNodeDoesNotExistException(id, + String.format("Node %s does not exist. Cannot apply modification to its children.", id)); + } + + throw new ConflictingModificationAppliedException(path.toInstanceIdentifier(), + "Node was deleted by other transaction."); + } + } else { + currentNode = current.get(); } - checkConflicting(path, current.isPresent(), "Node was deleted by other transaction."); - checkChildPreconditions(path, modification, current.get(), version); + checkChildPreconditions(path, modification, currentNode, version); + } + + /** + * Return the default tree node. Default implementation does nothing, but can be overridden to call + * {@link #defaultTreeNode(NormalizedNode)}. + * + * @return Default empty tree node, or null if no default is available + */ + @Nullable TreeNode defaultTreeNode() { + // Defaults to no recovery + return null; + } + + static final TreeNode defaultTreeNode(final NormalizedNode emptyNode) { + return TreeNodeFactory.createTreeNode(emptyNode, FAKE_VERSION); + } + + @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); + } } /** @@ -297,7 +412,7 @@ abstract class AbstractNodeContainerModificationStrategy extends SchemaAwareAppl 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); + final Optional childMeta = current.getChild(childId); path.push(childId); try { @@ -309,19 +424,7 @@ abstract class AbstractNodeContainerModificationStrategy extends SchemaAwareAppl } @Override - protected 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); - } + ToStringHelper addToStringAttributes(final ToStringHelper helper) { + return helper.add("support", support).add("verifyChildren", verifyChildrenStructure); } - - protected boolean verifyChildrenStructure() { - return verifyChildrenStructure; - } - - @SuppressWarnings("rawtypes") - protected abstract NormalizedNodeContainerBuilder createBuilder(NormalizedNode original); - - protected abstract NormalizedNode createEmptyValue(NormalizedNode original); }