Propagate EffectiveModelContext to more places
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / DataSchemaContextNode.java
index a83373457e9bca4df43f653b9418076c03a9b0c3..4ba7406b78119876de53f43f8541b5ae70395b98 100644 (file)
@@ -7,20 +7,21 @@
  */
 package org.opendaylight.yangtools.yang.data.util;
 
-import com.google.common.collect.FluentIterable;
 import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
 import java.util.List;
 import java.util.Optional;
 import java.util.Set;
 import java.util.stream.Collectors;
-import javax.annotation.Nullable;
 import org.eclipse.jdt.annotation.NonNull;
-import org.opendaylight.yangtools.concepts.Identifiable;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.yangtools.concepts.AbstractIdentifiable;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
-import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
@@ -28,12 +29,11 @@ import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
 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
@@ -42,12 +42,11 @@ import org.opendaylight.yangtools.yang.model.util.EffectiveAugmentationSchema;
  *
  * @param <T> Path Argument type
  */
-public abstract class DataSchemaContextNode<T extends PathArgument> implements Identifiable<T> {
+public abstract class DataSchemaContextNode<T extends PathArgument> extends AbstractIdentifiable<T> {
     private final DataSchemaNode dataSchemaNode;
-    private final T identifier;
 
     protected DataSchemaContextNode(final T identifier, final SchemaNode schema) {
-        this.identifier = identifier;
+        super(identifier);
         if (schema instanceof DataSchemaNode) {
             this.dataSchemaNode = (DataSchemaNode) schema;
         } else {
@@ -55,11 +54,6 @@ public abstract class DataSchemaContextNode<T extends PathArgument> implements I
         }
     }
 
-    @Override
-    public T getIdentifier() {
-        return identifier;
-    }
-
     public boolean isMixin() {
         return false;
     }
@@ -71,7 +65,7 @@ public abstract class DataSchemaContextNode<T extends PathArgument> implements I
     public abstract boolean isLeaf();
 
     protected Set<QName> getQNameIdentifiers() {
-        return ImmutableSet.of(identifier.getNodeType());
+        return ImmutableSet.of(getIdentifier().getNodeType());
     }
 
     /**
@@ -80,11 +74,11 @@ public abstract class DataSchemaContextNode<T extends PathArgument> implements I
      * @param child Child path argument
      * @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 +102,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);
@@ -130,7 +120,7 @@ public abstract class DataSchemaContextNode<T extends PathArgument> implements I
     private static ChoiceSchemaNode findChoice(final Iterable<ChoiceSchemaNode> choices, final QName child) {
         for (ChoiceSchemaNode choice : choices) {
             // FIXME: this looks weird: what are we looking for again?
-            for (CaseSchemaNode caze : choice.getCases().values()) {
+            for (CaseSchemaNode caze : choice.getCases()) {
                 if (findChildSchemaNode(caze, child) != null) {
                     return choice;
                 }
@@ -151,21 +141,6 @@ public abstract class DataSchemaContextNode<T extends PathArgument> implements I
             .collect(Collectors.toSet()));
     }
 
-    /**
-     * Returns an AugmentationSchemaNode as effective in a parent node.
-     *
-     * @param schema Augmentation schema
-     * @param parent Parent schema
-     * @return Adjusted Augmentation schema
-     * @throws NullPointerException if any of the arguments is null
-     * @deprecated Use {@link EffectiveAugmentationSchema#create(AugmentationSchemaNode, DataNodeContainer)} instead.
-     */
-    @Deprecated
-    public static AugmentationSchemaNode augmentationProxy(final AugmentationSchemaNode schema,
-            final DataNodeContainer parent) {
-        return EffectiveAugmentationSchema.create(schema, parent);
-    }
-
     /**
      * Returns a DataContextNodeOperation for provided child node
      *
@@ -175,23 +150,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) {
@@ -202,8 +171,10 @@ public abstract class DataSchemaContextNode<T extends PathArgument> implements I
             return new ChoiceNodeContextNode((ChoiceSchemaNode) potential);
         } else if (potential instanceof LeafListSchemaNode) {
             return fromLeafListSchemaNode((LeafListSchemaNode) potential);
-        } else if (potential instanceof AnyXmlSchemaNode) {
-            return new AnyXmlContextNode((AnyXmlSchemaNode) potential);
+        } else if (potential instanceof AnydataSchemaNode) {
+            return new AnydataContextNode((AnydataSchemaNode) potential);
+        } else if (potential instanceof AnyxmlSchemaNode) {
+            return new AnyXmlContextNode((AnyxmlSchemaNode) potential);
         }
         return null;
     }
@@ -226,7 +197,7 @@ public abstract class DataSchemaContextNode<T extends PathArgument> implements I
         return new UnorderedLeafListMixinContextNode(potential);
     }
 
-    public static DataSchemaContextNode<?> from(final SchemaContext ctx) {
+    public static DataSchemaContextNode<?> from(final EffectiveModelContext ctx) {
         return new ContainerContextNode(ctx);
     }
 }