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%2FAbstractModifiedNodeBasedCandidateNode.java;h=02d961eed3c05d1fb0dd1dfd802623b01dfa4252;hb=refs%2Fchanges%2F89%2F56089%2F1;hp=daea495326fdf1ef20e38f1712a82de380ae4571;hpb=4c05c29ad991d49a50c3993e4c2e8146bab8242a;p=yangtools.git diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/AbstractModifiedNodeBasedCandidateNode.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/AbstractModifiedNodeBasedCandidateNode.java index daea495326..02d961eed3 100644 --- a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/AbstractModifiedNodeBasedCandidateNode.java +++ b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/AbstractModifiedNodeBasedCandidateNode.java @@ -1,33 +1,33 @@ /* - * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2015, 2016 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.yangtools.yang.data.impl.schema.tree; -import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.google.common.base.Verify; import com.google.common.collect.Collections2; +import com.google.common.collect.ImmutableList; import java.util.Collection; import javax.annotation.Nonnull; import javax.annotation.Nullable; 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.DataTreeCandidateNode; import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType; import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode; - abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandidateNode { private final ModifiedNode mod; private final TreeNode newMeta; private final TreeNode oldMeta; - protected AbstractModifiedNodeBasedCandidateNode(final ModifiedNode mod, - final TreeNode oldMeta, final TreeNode newMeta) { + protected AbstractModifiedNodeBasedCandidateNode(final ModifiedNode mod, final TreeNode oldMeta, + final TreeNode newMeta) { this.newMeta = newMeta; this.oldMeta = oldMeta; this.mod = Preconditions.checkNotNull(mod); @@ -45,73 +45,126 @@ abstract class AbstractModifiedNodeBasedCandidateNode implements DataTreeCandida return oldMeta; } - private static final TreeNode childMeta(final TreeNode parent, final PathArgument id) { - if (parent != null) { - return parent.getChild(id).orNull(); - } else { - return null; + private static TreeNode childMeta(final TreeNode parent, final PathArgument id) { + return parent == null ? null : parent.getChild(id).orNull(); + } + + private static boolean canHaveChildren(@Nullable final TreeNode oldMeta, @Nullable final TreeNode newMeta) { + if (oldMeta != null) { + return oldMeta.getData() instanceof NormalizedNodeContainer; } + if (newMeta != null) { + return newMeta.getData() instanceof NormalizedNodeContainer; + } + return false; } - private DataTreeCandidateNode childNode(final ModifiedNode input) { - final PathArgument id = input.getIdentifier(); - return createChildNode(input, childMeta(oldMeta, id), childMeta(newMeta, id)); + @SuppressWarnings("unchecked") + private static NormalizedNodeContainer> getContainer(@Nullable final TreeNode meta) { + return (meta == null ? null : (NormalizedNodeContainer>)meta.getData()); } - private static DataTreeCandidateNode createChildNode(@Nonnull final ModifiedNode input, @Nullable final TreeNode oldMeta, @Nullable final TreeNode newMeta) { - return new ChildNode(input, oldMeta, newMeta); + private ChildNode childNode(final ModifiedNode childMod) { + final PathArgument id = childMod.getIdentifier(); + return new ChildNode(childMod, childMeta(oldMeta, id), childMeta(newMeta, id)); } @Override + @Nonnull public Collection getChildNodes() { - return Collections2.transform(mod.getChildren(), new Function() { - @Override - public DataTreeCandidateNode apply(final ModifiedNode input) { - return childNode(input); + switch (mod.getModificationType()) { + case APPEARED: + case DISAPPEARED: + case SUBTREE_MODIFIED: + return Collections2.transform(mod.getChildren(), this::childNode); + case UNMODIFIED: + // Unmodified node, but we still need to resolve potential children. canHaveChildren returns + // false if both arguments are null. + if (!canHaveChildren(oldMeta, newMeta)) { + return ImmutableList.of(); + } + + return Collections2.transform(getContainer(newMeta != null ? newMeta : oldMeta).getValue(), + AbstractRecursiveCandidateNode::unmodifiedNode); + case DELETE: + case WRITE: + // This is unusual, the user is requesting we follow into an otherwise-terminal node. + // We need to fudge things based on before/after data to correctly fake the expectations. + if (!canHaveChildren(oldMeta, newMeta)) { + return ImmutableList.of(); } - }); + return AbstractDataTreeCandidateNode.deltaChildren(getContainer(oldMeta), getContainer(newMeta)); + default: + throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType()); + } } @Override + @Nonnull public ModificationType getModificationType() { return Verify.verifyNotNull(mod.getModificationType(), "Node %s does not have resolved modification type", mod); } - private Optional> optionalData(final TreeNode meta) { - if (meta != null) { - return Optional.>of(meta.getData()); - } else { - return Optional.absent(); - } + private static Optional> optionalData(final TreeNode meta) { + return meta == null ? Optional.absent() : Optional.of(meta.getData()); } @Override - public Optional> getDataAfter() { + @Nonnull + public final Optional> getDataAfter() { return optionalData(newMeta); } @Override - public Optional> getDataBefore() { + @Nonnull + public final Optional> getDataBefore() { return optionalData(oldMeta); } @Override - public DataTreeCandidateNode getModifiedChild(final PathArgument identifier) { - final Optional childMod = mod.getChild(identifier); - if (childMod.isPresent()) { - return childNode(childMod.get()); + public final DataTreeCandidateNode getModifiedChild(final PathArgument identifier) { + switch (mod.getModificationType()) { + case APPEARED: + case DISAPPEARED: + case SUBTREE_MODIFIED: + final Optional childMod = mod.getChild(identifier); + if (childMod.isPresent()) { + return childNode(childMod.get()); + } + return null; + case UNMODIFIED: + if (!canHaveChildren(oldMeta, newMeta)) { + return null; + } + final Optional> maybeChild = getContainer(newMeta != null ? newMeta : oldMeta) + .getChild(identifier); + return maybeChild.isPresent() ? AbstractRecursiveCandidateNode.unmodifiedNode(maybeChild.get()) : null; + case DELETE: + case WRITE: + if (!canHaveChildren(oldMeta, newMeta)) { + return null; + } + return AbstractDataTreeCandidateNode.deltaChild(getContainer(oldMeta), getContainer(newMeta), identifier); + default: + throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType()); } - return null; } - static final class ChildNode extends AbstractModifiedNodeBasedCandidateNode { - public ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) { + private static final class ChildNode extends AbstractModifiedNodeBasedCandidateNode { + ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) { super(mod, oldMeta, newMeta); } @Override + @Nonnull public PathArgument getIdentifier() { return getMod().getIdentifier(); } } -} \ No newline at end of file + + @Override + public String toString() { + return this.getClass().getSimpleName() + "{mod = " + this.mod + ", oldMeta = " + this.oldMeta + ", newMeta = " + + this.newMeta + "}"; + } +}