Allow DataObjectModification to address grouping nodes
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / LazyDataObjectModification.java
index eb2aed7..c3aff15 100644 (file)
@@ -13,11 +13,14 @@ 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;
@@ -47,7 +50,7 @@ final class LazyDataObjectModification<T extends DataObject> implements DataObje
     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) {
@@ -56,19 +59,19 @@ final class LazyDataObjectModification<T extends DataObject> implements DataObje
         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);
     }
 
-    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);
@@ -93,7 +96,7 @@ final class LazyDataObjectModification<T extends DataObject> implements DataObje
         }
     }
 
-    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) {
@@ -113,7 +116,7 @@ final class LazyDataObjectModification<T extends DataObject> implements DataObje
         }
     }
 
-    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));
@@ -197,14 +200,31 @@ final class LazyDataObjectModification<T extends DataObject> implements DataObje
     }
 
     @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<>();
@@ -229,8 +249,23 @@ final class LazyDataObjectModification<T extends DataObject> implements DataObje
 
     @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
@@ -241,10 +276,7 @@ final class LazyDataObjectModification<T extends DataObject> implements DataObje
     }
 
     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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.