X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-dom-broker%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fmd%2Fsal%2Fdom%2Fstore%2Fimpl%2FSchemaAwareApplyOperation.java;h=85dc7118f4badad53ecdf8f568ff7ac6e916508b;hb=9d269c372a5d565101629cea6dad14a35a9e0591;hp=6308b6f63ebb14dd33191f50d013d012cc8bca58;hpb=c4940d6fa1f6928b7189afb2dc6964fb2f2cbae2;p=controller.git diff --git a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/SchemaAwareApplyOperation.java b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/SchemaAwareApplyOperation.java index 6308b6f63e..85dc7118f4 100644 --- a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/SchemaAwareApplyOperation.java +++ b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/SchemaAwareApplyOperation.java @@ -2,16 +2,22 @@ package org.opendaylight.controller.md.sal.dom.store.impl; import static com.google.common.base.Preconditions.checkArgument; +import java.util.List; +import java.util.Map; import java.util.Set; +import java.util.concurrent.ExecutionException; import org.opendaylight.controller.md.sal.dom.store.impl.tree.ModificationType; import org.opendaylight.controller.md.sal.dom.store.impl.tree.NodeModification; import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreMetadataNode; import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreNodeCompositeBuilder; +import org.opendaylight.yangtools.yang.common.QName; +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.NodeIdentifierWithPredicates; import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue; import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument; +import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode; import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; import org.opendaylight.yangtools.yang.data.api.schema.LeafNode; import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode; @@ -20,12 +26,22 @@ import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; import org.opendaylight.yangtools.yang.data.api.schema.MapNode; 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.OrderedMapNode; +import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode; +import org.opendaylight.yangtools.yang.data.impl.schema.Builders; import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder; import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder; +import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder; import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder; import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder; import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder; import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder; +import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedLeafSetNodeBuilder; +import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder; +import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder; +import org.opendaylight.yangtools.yang.model.api.AugmentationSchema; +import org.opendaylight.yangtools.yang.model.api.AugmentationTarget; +import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode; import org.opendaylight.yangtools.yang.model.api.ChoiceNode; import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; @@ -36,9 +52,11 @@ import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; import com.google.common.base.Function; import com.google.common.base.Optional; -import com.google.common.cache.Cache; +import com.google.common.base.Preconditions; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet.Builder; import com.google.common.primitives.UnsignedLong; @@ -49,20 +67,54 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper if (schemaNode instanceof ContainerSchemaNode) { return new ContainerModificationStrategy((ContainerSchemaNode) schemaNode); } else if (schemaNode instanceof ListSchemaNode) { - return new ListMapModificationStrategy((ListSchemaNode) schemaNode); + return fromListSchemaNode((ListSchemaNode) schemaNode); } else if (schemaNode instanceof ChoiceNode) { return new ChoiceModificationStrategy((ChoiceNode) schemaNode); } else if (schemaNode instanceof LeafListSchemaNode) { - return new LeafSetEntryModificationStrategy((LeafListSchemaNode) schemaNode); + return fromLeafListSchemaNode((LeafListSchemaNode) schemaNode); } else if (schemaNode instanceof LeafSchemaNode) { return new LeafModificationStrategy((LeafSchemaNode) schemaNode); } throw new IllegalArgumentException("Not supported schema node type for " + schemaNode.getClass()); } - @Override - public Optional getChild(final PathArgument child) { - throw new IllegalArgumentException(); + private static SchemaAwareApplyOperation fromListSchemaNode(final ListSchemaNode schemaNode) { + List keyDefinition = schemaNode.getKeyDefinition(); + if (keyDefinition == null || keyDefinition.isEmpty()) { + return new UnkeyedListModificationStrategy(schemaNode); + } + if (schemaNode.isUserOrdered()) { + return new OrderedMapModificationStrategy(schemaNode); + } + + return new UnorderedMapModificationStrategy(schemaNode); + } + + private static SchemaAwareApplyOperation fromLeafListSchemaNode(final LeafListSchemaNode schemaNode) { + if(schemaNode.isUserOrdered()) { + return new OrderedLeafSetModificationStrategy(schemaNode); + } else { + return new UnorderedLeafSetModificationStrategy(schemaNode); + } + } + + + public static SchemaAwareApplyOperation from(final DataNodeContainer resolvedTree, + final AugmentationTarget augSchemas, final AugmentationIdentifier identifier) { + AugmentationSchema augSchema = null; + allAugments: for (AugmentationSchema potential : augSchemas.getAvailableAugmentations()) { + boolean containsAll = true; + for (DataSchemaNode child : potential.getChildNodes()) { + if (identifier.getPossibleChildNames().contains(child.getQName())) { + augSchema = potential; + break allAugments; + } + } + } + if (augSchema != null) { + return new AugmentationModificationStrategy(augSchema, resolvedTree); + } + return null; } protected final ModificationApplyOperation resolveChildOperation(final PathArgument child) { @@ -89,6 +141,8 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper return isSubtreeModificationApplicable(modification, current); case WRITE: return isWriteApplicable(modification, current); + case MERGE: + return isMergeApplicable(modification,current); case UNMODIFIED: return true; default: @@ -96,6 +150,16 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper } } + private boolean isMergeApplicable(final NodeModification modification, final Optional current) { + Optional original = modification.getOriginal(); + if (original.isPresent() && current.isPresent()) { + return isNotConflicting(original.get(), current.get()); + } else if (current.isPresent()) { + return true; + } + return true; + } + protected boolean isWriteApplicable(final NodeModification modification, final Optional current) { Optional original = modification.getOriginal(); if (original.isPresent() && current.isPresent()) { @@ -123,13 +187,21 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper @Override public final Optional apply(final NodeModification modification, final Optional currentMeta, final UnsignedLong subtreeVersion) { + switch (modification.getModificationType()) { case DELETE: - return Optional.absent(); + return modification.storeSnapshot(Optional. absent()); case SUBTREE_MODIFIED: - return Optional.of(applySubtreeChange(modification, currentMeta.get(), subtreeVersion)); + Preconditions.checkArgument(currentMeta.isPresent(), "Metadata not available for modification", + modification); + return modification.storeSnapshot(Optional.of(applySubtreeChange(modification, currentMeta.get(), + subtreeVersion))); + case MERGE: + if(currentMeta.isPresent()) { + return modification.storeSnapshot(Optional.of(applyMerge(modification,currentMeta.get(),subtreeVersion))); + } // Fallback to write is intentional - if node is not preexisting merge is same as write case WRITE: - return Optional.of(applyWrite(modification, currentMeta, subtreeVersion)); + return modification.storeSnapshot(Optional.of(applyWrite(modification, currentMeta, subtreeVersion))); case UNMODIFIED: return currentMeta; default: @@ -137,6 +209,9 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper } } + protected abstract StoreMetadataNode applyMerge(NodeModification modification, + StoreMetadataNode currentMeta, UnsignedLong subtreeVersion); + protected abstract StoreMetadataNode applyWrite(NodeModification modification, Optional currentMeta, UnsignedLong subtreeVersion); @@ -173,14 +248,16 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper + "is leaf type node. Subtree change is not allowed."); } + @Override + protected StoreMetadataNode applyMerge(final NodeModification modification, final StoreMetadataNode currentMeta, + final UnsignedLong subtreeVersion) { + return applyWrite(modification, Optional.of(currentMeta), subtreeVersion); + } + @Override protected StoreMetadataNode applyWrite(final NodeModification modification, final Optional currentMeta, final UnsignedLong subtreeVersion) { UnsignedLong nodeVersion = subtreeVersion; - if (currentMeta.isPresent()) { - nodeVersion = StoreUtils.increase(currentMeta.get().getNodeVersion()); - } - return StoreMetadataNode.builder().setNodeVersion(nodeVersion).setSubtreeVersion(subtreeVersion) .setData(modification.getWritenValue()).build(); } @@ -217,13 +294,12 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper this.nodeClass = nodeClass; } - @Override public void verifyStructure(final NodeModification modification) throws IllegalArgumentException { - if(modification.getModificationType() == ModificationType.WRITE) { + if (modification.getModificationType() == ModificationType.WRITE) { } - for(NodeModification childModification : modification.getModifications()) { + for (NodeModification childModification : modification.getModifications()) { resolveChildOperation(childModification.getIdentifier()).verifyStructure(childModification); } } @@ -234,7 +310,7 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper checkArgument(nodeClass.isInstance(writenValue), "Node should must be of type %s", nodeClass); checkArgument(writenValue instanceof NormalizedNodeContainer); NormalizedNodeContainer writenCont = (NormalizedNodeContainer) writenValue; - for(Object child : writenCont.getValue()) { + for (Object child : writenCont.getValue()) { checkArgument(child instanceof NormalizedNode); NormalizedNode childNode = (NormalizedNode) child; } @@ -250,7 +326,7 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper if (currentMeta.isPresent()) { nodeVersion = StoreUtils.increase(currentMeta.get().getNodeVersion()); } - StoreMetadataNode newValueMeta = StoreMetadataNode.createRecursivelly(newValue, nodeVersion, nodeVersion); + StoreMetadataNode newValueMeta = StoreMetadataNode.createRecursively(newValue, nodeVersion, nodeVersion); if (!modification.hasAdditionalModifications()) { return newValueMeta; @@ -269,6 +345,13 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper } + @Override + protected StoreMetadataNode applyMerge(final NodeModification modification, final StoreMetadataNode currentMeta, + final UnsignedLong subtreeVersion) { + // For Node Containers - merge is same as subtree change - we only replace children. + return applySubtreeChange(modification, currentMeta, subtreeVersion); + } + @Override public StoreMetadataNode applySubtreeChange(final NodeModification modification, final StoreMetadataNode currentMeta, final UnsignedLong subtreeVersion) { @@ -277,7 +360,6 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper @SuppressWarnings("rawtypes") NormalizedNodeContainerBuilder dataBuilder = createBuilder(modification.getIdentifier()); StoreNodeCompositeBuilder builder = StoreNodeCompositeBuilder.from(dataBuilder) - // .setIdentifier(modification.getIdentifier()).setNodeVersion(currentMeta.getNodeVersion()) .setSubtreeVersion(updatedSubtreeVersion); // We process preexisting nodes @@ -348,17 +430,21 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper NormalizedNodeContainerModificationStrategy { private final T schema; - private final Cache childCache = CacheBuilder.newBuilder() + private final LoadingCache childCache = CacheBuilder.newBuilder() .build(CacheLoader.from(new Function() { - @Override - public ModificationApplyOperation apply(final PathArgument identifier) { - DataSchemaNode child = schema.getDataChildByName(identifier.getNodeType()); - if (child == null || child.isAugmenting()) { - return null; + @Override + public ModificationApplyOperation apply(final PathArgument identifier) { + if (identifier instanceof AugmentationIdentifier && schema instanceof AugmentationTarget) { + return from(schema, (AugmentationTarget) schema, (AugmentationIdentifier) identifier); + } + + DataSchemaNode child = schema.getDataChildByName(identifier.getNodeType()); + if (child == null) { + return null; + } + return from(child); } - return from(child); - } })); protected DataNodeContainerModificationStrategy(final T schema, @@ -373,11 +459,11 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper @Override public Optional getChild(final PathArgument identifier) { - DataSchemaNode child = schema.getDataChildByName(identifier.getNodeType()); - if (child == null || child.isAugmenting()) { + try { + return Optional. fromNullable(childCache.get(identifier)); + } catch (ExecutionException e) { return Optional.absent(); } - return Optional. of(from(child)); } @Override @@ -408,20 +494,67 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper } + public static class UnkeyedListItemModificationStrategy extends + DataNodeContainerModificationStrategy { + + public UnkeyedListItemModificationStrategy(final ListSchemaNode schemaNode) { + super(schemaNode, UnkeyedListEntryNode.class); + } + + @Override + @SuppressWarnings("rawtypes") + protected DataContainerNodeBuilder createBuilder(final PathArgument identifier) { + checkArgument(identifier instanceof NodeIdentifier); + return ImmutableUnkeyedListEntryNodeBuilder.create().withNodeIdentifier((NodeIdentifier) identifier); + } + + } + + public static class AugmentationModificationStrategy extends + DataNodeContainerModificationStrategy { + + protected AugmentationModificationStrategy(final AugmentationSchema schema, final DataNodeContainer resolved) { + super(schema, AugmentationNode.class); + // FIXME: Use resolved children instead of unresolved. + + } + + @Override + protected DataContainerNodeBuilder createBuilder(final PathArgument identifier) { + return Builders.augmentationBuilder().withNodeIdentifier((AugmentationIdentifier) identifier); + } + + } + public static class ChoiceModificationStrategy extends NormalizedNodeContainerModificationStrategy { private final ChoiceNode schema; + private final Map childNodes; public ChoiceModificationStrategy(final ChoiceNode schemaNode) { super(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode.class); this.schema = schemaNode; + ImmutableMap.Builder child = ImmutableMap.builder(); + + for (ChoiceCaseNode caze : schemaNode.getCases()) { + for (DataSchemaNode cazeChild : caze.getChildNodes()) { + SchemaAwareApplyOperation childNode = from(cazeChild); + child.put(new NodeIdentifier(cazeChild.getQName()), childNode); + } + } + childNodes = child.build(); + } + + @Override + public Optional getChild(final PathArgument child) { + return Optional.fromNullable(childNodes.get(child)); } @Override @SuppressWarnings("rawtypes") protected DataContainerNodeBuilder createBuilder(final PathArgument identifier) { checkArgument(identifier instanceof NodeIdentifier); - return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) identifier); + return ImmutableChoiceNodeBuilder.create().withNodeIdentifier((NodeIdentifier) identifier); } } @@ -440,12 +573,12 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper } - public static class LeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy { + public static class UnorderedLeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy { private final Optional entryStrategy; @SuppressWarnings({ "unchecked", "rawtypes" }) - protected LeafSetModificationStrategy(final LeafListSchemaNode schema) { + protected UnorderedLeafSetModificationStrategy(final LeafListSchemaNode schema) { super((Class) LeafSetNode.class); entryStrategy = Optional. of(new LeafSetEntryModificationStrategy(schema)); } @@ -466,11 +599,84 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper } - public static class ListMapModificationStrategy extends NormalizedNodeContainerModificationStrategy { + public static class OrderedLeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy { private final Optional entryStrategy; - protected ListMapModificationStrategy(final ListSchemaNode schema) { + @SuppressWarnings({ "unchecked", "rawtypes" }) + protected OrderedLeafSetModificationStrategy(final LeafListSchemaNode schema) { + super((Class) LeafSetNode.class); + entryStrategy = Optional. of(new LeafSetEntryModificationStrategy(schema)); + } + + @SuppressWarnings("rawtypes") + @Override + protected NormalizedNodeContainerBuilder createBuilder(final PathArgument identifier) { + return ImmutableOrderedLeafSetNodeBuilder.create().withNodeIdentifier((NodeIdentifier) identifier); + } + + @Override + public Optional getChild(final PathArgument identifier) { + if (identifier instanceof NodeWithValue) { + return entryStrategy; + } + return Optional.absent(); + } + + } + + public static class UnkeyedListModificationStrategy extends SchemaAwareApplyOperation { + + private final Optional entryStrategy; + + protected UnkeyedListModificationStrategy(final ListSchemaNode schema) { + entryStrategy = Optional. of(new UnkeyedListItemModificationStrategy(schema)); + } + + @Override + protected StoreMetadataNode applyMerge(final NodeModification modification, final StoreMetadataNode currentMeta, + final UnsignedLong subtreeVersion) { + return applyWrite(modification, Optional.of(currentMeta), subtreeVersion); + } + + @Override + protected StoreMetadataNode applySubtreeChange(final NodeModification modification, + final StoreMetadataNode currentMeta, final UnsignedLong subtreeVersion) { + throw new UnsupportedOperationException("UnkeyedList does not support subtree change."); + } + + @Override + protected StoreMetadataNode applyWrite(final NodeModification modification, + final Optional currentMeta, final UnsignedLong subtreeVersion) { + return StoreMetadataNode.createRecursively(modification.getWritenValue(), subtreeVersion); + } + + @Override + public Optional getChild(final PathArgument child) { + if (child instanceof NodeIdentifier) { + return entryStrategy; + } + return Optional.absent(); + } + + @Override + protected void verifyWritenStructure(final NormalizedNode writenValue) { + + } + + @Override + protected boolean isSubtreeModificationApplicable(final NodeModification modification, + final Optional current) { + return false; + } + + } + + public static class UnorderedMapModificationStrategy extends NormalizedNodeContainerModificationStrategy { + + private final Optional entryStrategy; + + protected UnorderedMapModificationStrategy(final ListSchemaNode schema) { super(MapNode.class); entryStrategy = Optional. of(new ListEntryModificationStrategy(schema)); } @@ -491,7 +697,36 @@ public abstract class SchemaAwareApplyOperation implements ModificationApplyOper @Override public String toString() { - return "ListMapModificationStrategy [entry=" + entryStrategy + "]"; + return "UnorderedMapModificationStrategy [entry=" + entryStrategy + "]"; + } + } + + public static class OrderedMapModificationStrategy extends NormalizedNodeContainerModificationStrategy { + + private final Optional entryStrategy; + + protected OrderedMapModificationStrategy(final ListSchemaNode schema) { + super(OrderedMapNode.class); + entryStrategy = Optional. of(new ListEntryModificationStrategy(schema)); + } + + @SuppressWarnings("rawtypes") + @Override + protected NormalizedNodeContainerBuilder createBuilder(final PathArgument identifier) { + return ImmutableOrderedMapNodeBuilder.create().withNodeIdentifier((NodeIdentifier) identifier); + } + + @Override + public Optional getChild(final PathArgument identifier) { + if (identifier instanceof NodeIdentifierWithPredicates) { + return entryStrategy; + } + return Optional.absent(); + } + + @Override + public String toString() { + return "OrderedMapModificationStrategy [entry=" + entryStrategy + "]"; } }