*/
package org.opendaylight.controller.md.sal.binding.impl;
-import com.google.common.base.Preconditions;
+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.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;
private final DataTreeCandidateNode domData;
private final PathArgument identifier;
- private volatile Collection<DataObjectModification<? extends DataObject>> childNodesCache;
+ private volatile Collection<LazyDataObjectModification<? extends DataObject>> childNodesCache;
private volatile ModificationType modificationType;
private LazyDataObjectModification(final BindingCodecTreeNode<T> codec, final DataTreeCandidateNode domData) {
- this.codec = Preconditions.checkNotNull(codec);
- this.domData = Preconditions.checkNotNull(domData);
+ this.codec = requireNonNull(codec);
+ this.domData = requireNonNull(domData);
this.identifier = codec.deserializePathArgument(domData.getIdentifier());
}
- static <T extends DataObject> DataObjectModification<T> create(final BindingCodecTreeNode<T> codec,
+ static <T extends DataObject> LazyDataObjectModification<T> create(final BindingCodecTreeNode<T> codec,
final DataTreeCandidateNode domData) {
- return new LazyDataObjectModification<>(codec,domData);
+ return new LazyDataObjectModification<>(codec, domData);
}
- private static Collection<DataObjectModification<? extends DataObject>> from(
+ private static Collection<LazyDataObjectModification<? extends DataObject>> from(
final BindingCodecTreeNode<?> parentCodec, final Collection<DataTreeCandidateNode> domChildNodes) {
- final List<DataObjectModification<? extends DataObject>> result = new ArrayList<>(domChildNodes.size());
+ final List<LazyDataObjectModification<? extends DataObject>> result = new ArrayList<>(domChildNodes.size());
populateList(result, parentCodec, domChildNodes);
return result;
}
- private static void populateList(final List<DataObjectModification<? extends DataObject>> result,
+ private static void populateList(final List<LazyDataObjectModification<? extends DataObject>> result,
final BindingCodecTreeNode<?> parentCodec, final Collection<DataTreeCandidateNode> 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());
+ 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);
+ }
}
}
}
}
}
- private static void populateList(final List<DataObjectModification<? extends DataObject>> result,
+ private static void populateList(final List<LazyDataObjectModification<? extends DataObject>> result,
final BindingStructuralType type, final BindingCodecTreeNode<?> childCodec,
final DataTreeCandidateNode domChildNode) {
switch (type) {
}
}
- private static void populateListWithSingleCodec(final List<DataObjectModification<? extends DataObject>> result,
+ private static void populateListWithSingleCodec(final List<LazyDataObjectModification<? extends DataObject>> result,
final BindingCodecTreeNode<?> codec, final Collection<DataTreeCandidateNode> childNodes) {
for (final DataTreeCandidateNode child : childNodes) {
- result.add(create(codec, child));
+ if (child.getModificationType() != UNMODIFIED) {
+ result.add(create(codec, child));
+ }
}
}
}
@Override
- public Collection<DataObjectModification<? extends DataObject>> getModifiedChildren() {
- Collection<DataObjectModification<? extends DataObject>> local = childNodesCache;
+ public Collection<LazyDataObjectModification<? extends DataObject>> getModifiedChildren() {
+ Collection<LazyDataObjectModification<? extends DataObject>> local = childNodesCache;
if (local == null) {
childNodesCache = local = from(codec, domData.getChildNodes());
}
return local;
}
+ @Override
+ public <H extends ChoiceIn<? super T> & DataObject, C extends ChildOf<? super H>>
+ Collection<DataObjectModification<C>> getModifiedChildren(final Class<H> caseType,
+ final Class<C> childType) {
+ return streamModifiedChildren(childType)
+ .filter(child -> caseType.equals(child.identifier.getCaseType().orElse(null)))
+ .collect(Collectors.toList());
+ }
+
+ @SuppressWarnings("unchecked")
+ private <C extends DataObject> Stream<LazyDataObjectModification<C>> streamModifiedChildren(
+ final Class<C> childType) {
+ return getModifiedChildren().stream()
+ .filter(child -> childType.isAssignableFrom(child.getDataType()))
+ .map(child -> (LazyDataObjectModification<C>) child);
+ }
+
@Override
public DataObjectModification<? extends DataObject> getModifiedChild(final PathArgument arg) {
final List<YangInstanceIdentifier.PathArgument> domArgumentList = new ArrayList<>();
while (toEnter.hasNext() && current != null) {
current = current.getModifiedChild(toEnter.next());
}
- if (current != null) {
- return create(childCodec, current);
- }
- return null;
+ return current != null && current.getModificationType() != UNMODIFIED ? create(childCodec, current) : null;
}
@Override
@Override
@SuppressWarnings("unchecked")
- public <C extends ChildOf<? super T>> DataObjectModification<C> getModifiedChildContainer(final Class<C> arg) {
- return (DataObjectModification<C>) getModifiedChild(Item.of(arg));
+ public <H extends ChoiceIn<? super T> & DataObject, C extends Identifiable<K> & ChildOf<? super H>,
+ K extends Identifier<C>> DataObjectModification<C> getModifiedChildListItem(final Class<H> caseType,
+ final Class<C> listItem, final K listKey) {
+ return (DataObjectModification<C>) getModifiedChild(IdentifiableItem.of(caseType, listItem, listKey));
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public <C extends ChildOf<? super T>> DataObjectModification<C> getModifiedChildContainer(final Class<C> child) {
+ return (DataObjectModification<C>) getModifiedChild(Item.of(child));
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public <H extends ChoiceIn<? super T> & DataObject, C extends ChildOf<? super H>> DataObjectModification<C>
+ getModifiedChildContainer(final Class<H> caseType, final Class<C> child) {
+ return (DataObjectModification<C>) getModifiedChild(Item.of(caseType, child));
}
@Override
}
private T deserialize(final Optional<NormalizedNode<?, ?>> dataAfter) {
- if (dataAfter.isPresent()) {
- return codec.deserialize(dataAfter.get());
- }
- return null;
+ return dataAfter.map(codec::deserialize).orElse(null);
}
@Override