Create unit tests for anydata implemetnation.
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / DataSchemaContextNode.java
index 22b93d8621aca0727d02a63c8da448a1593fcff2..53e5dbc63547af443404408990f49e8eae4bf2be 100644 (file)
@@ -7,14 +7,14 @@
  */
 package org.opendaylight.yangtools.yang.data.util;
 
-import com.google.common.collect.FluentIterable;
 import com.google.common.collect.ImmutableSet;
-import java.util.HashSet;
+import com.google.common.collect.Iterables;
 import java.util.List;
 import java.util.Optional;
 import java.util.Set;
-import javax.annotation.Nullable;
+import java.util.stream.Collectors;
 import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.concepts.Identifiable;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -33,7 +33,6 @@ import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
-import org.opendaylight.yangtools.yang.model.util.EffectiveAugmentationSchema;
 
 /**
  * Schema derived data providing necessary information for mapping between
@@ -78,13 +77,13 @@ public abstract class DataSchemaContextNode<T extends PathArgument> implements I
      * Find a child node identifier by its {@link PathArgument}.
      *
      * @param child Child path argument
-     * @return
+     * @return A child node, or null if not found
      */
-    @Nullable public abstract DataSchemaContextNode<?> getChild(PathArgument child);
+    public abstract @Nullable DataSchemaContextNode<?> getChild(PathArgument child);
 
-    @Nullable public abstract DataSchemaContextNode<?> getChild(QName child);
+    public abstract @Nullable DataSchemaContextNode<?> getChild(QName child);
 
-    @Nullable public DataSchemaNode getDataSchemaNode() {
+    public @Nullable DataSchemaNode getDataSchemaNode() {
         return dataSchemaNode;
     }
 
@@ -108,17 +107,13 @@ public abstract class DataSchemaContextNode<T extends PathArgument> implements I
     }
 
     static DataSchemaNode findChildSchemaNode(final DataNodeContainer parent, final QName child) {
-        DataSchemaNode potential = parent.getDataChildByName(child);
-        if (potential == null) {
-            Iterable<ChoiceSchemaNode> choices = FluentIterable.from(
-                    parent.getChildNodes()).filter(ChoiceSchemaNode.class);
-            potential = findChoice(choices, child);
-        }
-        return potential;
+        final DataSchemaNode potential = parent.getDataChildByName(child);
+        return potential == null ? findChoice(Iterables.filter(parent.getChildNodes(), ChoiceSchemaNode.class), child)
+                : potential;
     }
 
     static DataSchemaContextNode<?> fromSchemaAndQNameChecked(final DataNodeContainer schema, final QName child) {
-        DataSchemaNode result = findChildSchemaNode(schema, child);
+        final DataSchemaNode result = findChildSchemaNode(schema, child);
         // We try to look up if this node was added by augmentation
         if (result != null && schema instanceof DataSchemaNode && result.isAugmenting()) {
             return fromAugmentation(schema, (AugmentationTarget) schema, result);
@@ -139,21 +134,16 @@ public abstract class DataSchemaContextNode<T extends PathArgument> implements I
         return null;
     }
 
-    public static AugmentationIdentifier augmentationIdentifierFrom(final AugmentationSchemaNode augmentation) {
-        ImmutableSet.Builder<QName> potentialChildren = ImmutableSet.builder();
-        for (DataSchemaNode child : augmentation.getChildNodes()) {
-            potentialChildren.add(child.getQName());
-        }
-        return new AugmentationIdentifier(potentialChildren.build());
-    }
-
-    static DataNodeContainer augmentationProxy(final AugmentationSchemaNode augmentation,
-            final DataNodeContainer schema) {
-        Set<DataSchemaNode> children = new HashSet<>();
-        for (DataSchemaNode augNode : augmentation.getChildNodes()) {
-            children.add(schema.getDataChildByName(augNode.getQName()));
-        }
-        return new EffectiveAugmentationSchema(augmentation, children);
+    /**
+     * Create AugmentationIdentifier from an AugmentationSchemaNode.
+     *
+     * @param schema Augmentation schema
+     * @return AugmentationIdentifier for the schema
+     * @throws NullPointerException if {@code schema} is null
+     */
+    public static AugmentationIdentifier augmentationIdentifierFrom(final AugmentationSchemaNode schema) {
+        return new AugmentationIdentifier(schema.getChildNodes().stream().map(DataSchemaNode::getQName)
+            .collect(Collectors.toSet()));
     }
 
     /**
@@ -165,23 +155,17 @@ public abstract class DataSchemaContextNode<T extends PathArgument> implements I
      * DataContextNodeOperation for child as call for
      * {@link #fromDataSchemaNode(DataSchemaNode)}.
      */
-    @Nullable static DataSchemaContextNode<?> fromAugmentation(final DataNodeContainer parent,
+    static @Nullable DataSchemaContextNode<?> fromAugmentation(final DataNodeContainer parent,
             final AugmentationTarget parentAug, final DataSchemaNode child) {
-        AugmentationSchemaNode augmentation = null;
         for (AugmentationSchemaNode aug : parentAug.getAvailableAugmentations()) {
-            DataSchemaNode potential = aug.getDataChildByName(child.getQName());
-            if (potential != null) {
-                augmentation = aug;
-                break;
+            if (aug.findDataChildByName(child.getQName()).isPresent()) {
+                return new AugmentationContextNode(aug, parent);
             }
         }
-        if (augmentation != null) {
-            return new AugmentationContextNode(augmentation, parent);
-        }
         return fromDataSchemaNode(child);
     }
 
-    @Nullable public static DataSchemaContextNode<?> fromDataSchemaNode(final DataSchemaNode potential) {
+    public static @Nullable DataSchemaContextNode<?> fromDataSchemaNode(final DataSchemaNode potential) {
         if (potential instanceof ContainerSchemaNode) {
             return new ContainerContextNode((ContainerSchemaNode) potential);
         } else if (potential instanceof ListSchemaNode) {