X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-binding-broker%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fmd%2Fsal%2Fbinding%2Fimpl%2FLazyDataObjectModification.java;h=c7f850e04e9edcf77672b9de1096fde73cb90049;hb=8a01ebe93fac21b1ae80dcfcc81c21543ec1a687;hp=d12ec53c831b9f9f2921fe103148b18b92bb7684;hpb=03c13bd8a8bb89a729d739eb2fcd501a4dfa5439;p=controller.git diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/LazyDataObjectModification.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/LazyDataObjectModification.java index d12ec53c83..c7f850e04e 100644 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/LazyDataObjectModification.java +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/LazyDataObjectModification.java @@ -7,21 +7,29 @@ */ package org.opendaylight.controller.md.sal.binding.impl; -import com.google.common.base.Preconditions; +import static com.google.common.base.Verify.verify; +import static java.util.Objects.requireNonNull; +import static org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType.UNMODIFIED; + import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.opendaylight.controller.md.sal.binding.api.DataObjectModification; import org.opendaylight.mdsal.binding.dom.adapter.BindingStructuralType; import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode; +import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode; import org.opendaylight.yangtools.yang.binding.Augmentation; import org.opendaylight.yangtools.yang.binding.ChildOf; +import org.opendaylight.yangtools.yang.binding.ChoiceIn; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.Identifiable; import org.opendaylight.yangtools.yang.binding.Identifier; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.Item; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; @@ -38,62 +46,70 @@ import org.slf4j.LoggerFactory; * * @param Type of Binding Data Object */ +@Deprecated final class LazyDataObjectModification implements DataObjectModification { private static final Logger LOG = LoggerFactory.getLogger(LazyDataObjectModification.class); - private final BindingCodecTreeNode codec; + private final BindingDataObjectCodecTreeNode codec; private final DataTreeCandidateNode domData; private final PathArgument identifier; - private volatile Collection> childNodesCache; + private volatile Collection> childNodesCache; private volatile ModificationType modificationType; - private LazyDataObjectModification(final BindingCodecTreeNode codec, final DataTreeCandidateNode domData) { - this.codec = Preconditions.checkNotNull(codec); - this.domData = Preconditions.checkNotNull(domData); + private LazyDataObjectModification(final BindingDataObjectCodecTreeNode codec, + final DataTreeCandidateNode domData) { + this.codec = requireNonNull(codec); + this.domData = requireNonNull(domData); this.identifier = codec.deserializePathArgument(domData.getIdentifier()); } - static DataObjectModification create(final BindingCodecTreeNode codec, + static LazyDataObjectModification create(final BindingDataObjectCodecTreeNode codec, final DataTreeCandidateNode domData) { - return new LazyDataObjectModification<>(codec,domData); + return new LazyDataObjectModification<>(codec, domData); } - private static Collection> from( - final BindingCodecTreeNode parentCodec, final Collection domChildNodes) { - final List> result = new ArrayList<>(domChildNodes.size()); + private static Collection> from( + final BindingDataObjectCodecTreeNode parentCodec, + final Collection domChildNodes) { + final List> result = new ArrayList<>(domChildNodes.size()); populateList(result, parentCodec, domChildNodes); return result; } - private static void populateList(final List> result, - final BindingCodecTreeNode parentCodec, final Collection domChildNodes) { + private static void populateList(final List> result, + final BindingDataObjectCodecTreeNode parentCodec, + final Collection domChildNodes) { for (final DataTreeCandidateNode domChildNode : domChildNodes) { - final BindingStructuralType type = BindingStructuralType.from(domChildNode); - if (type != BindingStructuralType.NOT_ADDRESSABLE) { - /* - * Even if type is UNKNOWN, from perspective of BindingStructuralType - * we try to load codec for it. We will use that type to further specify - * debug log. - */ - try { - final BindingCodecTreeNode childCodec = - parentCodec.yangPathArgumentChild(domChildNode.getIdentifier()); - populateList(result,type, childCodec, domChildNode); - } catch (final IllegalArgumentException e) { - if (type == BindingStructuralType.UNKNOWN) { - LOG.debug("Unable to deserialize unknown DOM node {}",domChildNode,e); - } else { - LOG.debug("Binding representation for DOM node {} was not found",domChildNode,e); + if (domChildNode.getModificationType() != UNMODIFIED) { + final BindingStructuralType type = BindingStructuralType.from(domChildNode); + if (type != BindingStructuralType.NOT_ADDRESSABLE) { + /* + * Even if type is UNKNOWN, from perspective of BindingStructuralType + * we try to load codec for it. We will use that type to further specify + * debug log. + */ + try { + final BindingCodecTreeNode childCodec = parentCodec.yangPathArgumentChild( + domChildNode.getIdentifier()); + verify(childCodec instanceof BindingDataObjectCodecTreeNode, "Unhandled codec %s for type %s", + childCodec, type); + populateList(result, type, (BindingDataObjectCodecTreeNode) childCodec, domChildNode); + } catch (final IllegalArgumentException e) { + if (type == BindingStructuralType.UNKNOWN) { + LOG.debug("Unable to deserialize unknown DOM node {}", domChildNode, e); + } else { + LOG.debug("Binding representation for DOM node {} was not found", domChildNode, e); + } } } } } } - private static void populateList(final List> result, - final BindingStructuralType type, final BindingCodecTreeNode childCodec, + private static void populateList(final List> result, + final BindingStructuralType type, final BindingDataObjectCodecTreeNode childCodec, final DataTreeCandidateNode domChildNode) { switch (type) { case INVISIBLE_LIST: @@ -112,10 +128,12 @@ final class LazyDataObjectModification implements DataObje } } - private static void populateListWithSingleCodec(final List> result, - final BindingCodecTreeNode codec, final Collection childNodes) { + private static void populateListWithSingleCodec(final List> result, + final BindingDataObjectCodecTreeNode codec, final Collection childNodes) { for (final DataTreeCandidateNode child : childNodes) { - result.add(create(codec, child)); + if (child.getModificationType() != UNMODIFIED) { + result.add(create(codec, child)); + } } } @@ -196,55 +214,80 @@ final class LazyDataObjectModification implements DataObje } @Override - public Collection> getModifiedChildren() { - Collection> local = childNodesCache; + public Collection> getModifiedChildren() { + Collection> local = childNodesCache; if (local == null) { childNodesCache = local = from(codec, domData.getChildNodes()); } return local; } + @Override + public & DataObject, C extends ChildOf> + Collection> getModifiedChildren(final Class caseType, + final Class childType) { + return streamModifiedChildren(childType) + .filter(child -> caseType.equals(child.identifier.getCaseType().orElse(null))) + .collect(Collectors.toList()); + } + + @SuppressWarnings("unchecked") + private Stream> streamModifiedChildren( + final Class childType) { + return getModifiedChildren().stream() + .filter(child -> childType.isAssignableFrom(child.getDataType())) + .map(child -> (LazyDataObjectModification) child); + } + @Override public DataObjectModification getModifiedChild(final PathArgument arg) { final List domArgumentList = new ArrayList<>(); - final BindingCodecTreeNode childCodec = codec.bindingPathArgumentChild(arg, domArgumentList); + final BindingDataObjectCodecTreeNode childCodec = codec.bindingPathArgumentChild(arg, domArgumentList); final Iterator toEnter = domArgumentList.iterator(); DataTreeCandidateNode current = domData; while (toEnter.hasNext() && current != null) { - current = current.getModifiedChild(toEnter.next()); - } - if (current != null) { - return create(childCodec, current); + current = current.getModifiedChild(toEnter.next()).orElse(null); } - return null; + return current != null && current.getModificationType() != UNMODIFIED ? create(childCodec, current) : null; } @Override @SuppressWarnings("unchecked") public & ChildOf, K extends Identifier> DataObjectModification getModifiedChildListItem(final Class listItem, final K listKey) { - return (DataObjectModification) getModifiedChild(new InstanceIdentifier.IdentifiableItem<>( - listItem, listKey)); + return (DataObjectModification) getModifiedChild(IdentifiableItem.of(listItem, listKey)); } @Override @SuppressWarnings("unchecked") - public > DataObjectModification getModifiedChildContainer(final Class arg) { - return (DataObjectModification) getModifiedChild(new InstanceIdentifier.Item<>(arg)); + public & DataObject, C extends Identifiable & ChildOf, + K extends Identifier> DataObjectModification getModifiedChildListItem(final Class caseType, + final Class listItem, final K listKey) { + return (DataObjectModification) getModifiedChild(IdentifiableItem.of(caseType, listItem, listKey)); + } + + @Override + @SuppressWarnings("unchecked") + public > DataObjectModification getModifiedChildContainer(final Class child) { + return (DataObjectModification) getModifiedChild(Item.of(child)); + } + + @Override + @SuppressWarnings("unchecked") + public & DataObject, C extends ChildOf> DataObjectModification + getModifiedChildContainer(final Class caseType, final Class child) { + return (DataObjectModification) getModifiedChild(Item.of(caseType, child)); } @Override @SuppressWarnings("unchecked") public & DataObject> DataObjectModification getModifiedAugmentation( final Class augmentation) { - return (DataObjectModification) getModifiedChild(new InstanceIdentifier.Item<>(augmentation)); + return (DataObjectModification) getModifiedChild(Item.of(augmentation)); } private T deserialize(final Optional> dataAfter) { - if (dataAfter.isPresent()) { - return codec.deserialize(dataAfter.get()); - } - return null; + return dataAfter.map(codec::deserialize).orElse(null); } @Override