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%2FContainerModificationStrategy.java;h=821625f9f6868a87a86ed8a5a0a5b8c336de7e90;hb=39d6912a0df959abf070bcec53d2157f938eb7fc;hp=8e28fdd86810222fd84dd49893c7dc163d04127b;hpb=f29f6b8445fae2505bce3feedfb3d005695adcdd;p=yangtools.git diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/ContainerModificationStrategy.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/ContainerModificationStrategy.java index 8e28fdd868..821625f9f6 100644 --- a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/ContainerModificationStrategy.java +++ b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/ContainerModificationStrategy.java @@ -7,36 +7,78 @@ */ 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 java.util.Optional; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; -import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType; -import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration; +import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode; +import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version; import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder; import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; /** - * General container modification strategy. Used by {@link PresenceContainerModificationStrategy} via subclassing - * and by {@link StructuralContainerModificationStrategy} as a delegate. + * General container modification strategy. This is used by {@link EnforcingMandatory} in case of presence containers + * with mandatory nodes, as it needs to tap into {@link SchemaAwareApplyOperation}'s operations, or subclassed + * for the purposes of {@link StructuralContainerModificationStrategy}'s automatic lifecycle. */ -class ContainerModificationStrategy extends AbstractDataNodeContainerModificationStrategy { - ContainerModificationStrategy(final ContainerSchemaNode schemaNode, final TreeType treeType) { - super(schemaNode, ContainerNode.class, treeType); +class ContainerModificationStrategy extends DataNodeContainerModificationStrategy { + private static final class EnforcingMandatory extends ContainerModificationStrategy { + private final MandatoryLeafEnforcer enforcer; + + EnforcingMandatory(final ContainerSchemaNode schemaNode, final DataTreeConfiguration treeConfig, + final MandatoryLeafEnforcer enforcer) { + super(schemaNode, treeConfig); + this.enforcer = requireNonNull(enforcer); + } + + @Override + void additionalVerifyValueChildren(final NormalizedNode writtenValue) { + enforcer.enforceOnData(writtenValue); + } + + @Override + protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, + final Version version) { + final TreeNode ret = super.applyMerge(modification, currentMeta, version); + enforcer.enforceOnTreeNode(ret); + return ret; + } + + @Override + protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode newValue, + final Optional currentMeta, final Version version) { + final TreeNode ret = super.applyWrite(modification, newValue, currentMeta, version); + enforcer.enforceOnTreeNode(ret); + return ret; + } + + @Override + protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, + final Version version) { + final TreeNode ret = super.applyTouch(modification, currentMeta, version); + enforcer.enforceOnTreeNode(ret); + return ret; + } } - @Override - @SuppressWarnings("rawtypes") - protected final DataContainerNodeBuilder createBuilder(final NormalizedNode original) { - checkArgument(original instanceof ContainerNode); - return ImmutableContainerNodeBuilder.create((ContainerNode) original); + private static final NormalizedNodeContainerSupport SUPPORT = + new NormalizedNodeContainerSupport<>(ContainerNode.class, ImmutableContainerNodeBuilder::create, + ImmutableContainerNodeBuilder::create); + + ContainerModificationStrategy(final ContainerSchemaNode schemaNode, final DataTreeConfiguration treeConfig) { + super(SUPPORT, schemaNode, treeConfig); } - @Override - protected NormalizedNode createEmptyValue(NormalizedNode original) { - Preconditions.checkArgument(original instanceof ContainerNode); - return ImmutableContainerNodeBuilder.create().withNodeIdentifier(((ContainerNode) original).getIdentifier()) - .build(); + static ModificationApplyOperation of(final ContainerSchemaNode schema, final DataTreeConfiguration treeConfig) { + if (schema.isPresenceContainer()) { + final Optional enforcer = MandatoryLeafEnforcer.forContainer(schema, treeConfig); + return enforcer.isPresent() ? new EnforcingMandatory(schema, treeConfig, enforcer.get()) + : new ContainerModificationStrategy(schema, treeConfig); + } + + return new StructuralContainerModificationStrategy(schema, treeConfig); } }