Drop unneeded generic type specifiers
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / SchemaUtils.java
index 83a45cab27803018779bfb1979970c466a1da6fb..6869b57d619d61f283c5367a3a06a6619454f707 100644 (file)
@@ -10,8 +10,10 @@ package org.opendaylight.yangtools.yang.data.impl.schema;
 import com.google.common.base.Function;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
+import com.google.common.base.Predicate;
 import com.google.common.collect.Collections2;
 import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 import java.util.Collection;
@@ -31,6 +33,10 @@ import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
 public final class SchemaUtils {
 
@@ -168,6 +174,7 @@ public final class SchemaUtils {
     /**
      * Recursively find all child nodes that come from choices.
      *
+     * @param schema schema
      * @return Map with all child nodes, to their most top augmentation
      */
     public static Map<QName, ChoiceSchemaNode> mapChildElementsFromChoices(final DataNodeContainer schema) {
@@ -213,6 +220,7 @@ public final class SchemaUtils {
     /**
      * Recursively find all child nodes that come from augmentations.
      *
+     * @param schema schema
      * @return Map with all child nodes, to their most top augmentation
      */
     public static Map<QName, AugmentationSchema> mapChildElementsFromAugments(final AugmentationTarget schema) {
@@ -276,6 +284,9 @@ public final class SchemaUtils {
      * Recursively list all child nodes.
      *
      * In case of choice, augment and cases, step in.
+     *
+     * @param nodeContainer node container
+     * @return set of QNames
      */
     public static Set<QName> getChildNodesRecursive(final DataNodeContainer nodeContainer) {
         Set<QName> allChildNodes = Sets.newHashSet();
@@ -302,6 +313,9 @@ public final class SchemaUtils {
      * Schema of the same child node from augment, and directly from target is not the same.
      * Schema of child node from augment is incomplete, therefore its useless for XML/NormalizedNode translation.
      *
+     * @param targetSchema target schema
+     * @param augmentSchema augment schema
+     * @return set of nodes
      */
     public static Set<DataSchemaNode> getRealSchemasForAugment(final AugmentationTarget targetSchema, final AugmentationSchema augmentSchema) {
         if (!(targetSchema.getAvailableAugmentations().contains(augmentSchema))) {
@@ -367,6 +381,10 @@ public final class SchemaUtils {
     /**
      * Tries to find in {@code parent} which is dealed as augmentation target node with QName as {@code child}. If such
      * node is found then it is returned, else null.
+     *
+     * @param parent parent node
+     * @param child child node
+     * @return augmentation schema
      */
     public static AugmentationSchema findCorrespondingAugment(final DataSchemaNode parent, final DataSchemaNode child) {
         if (parent instanceof AugmentationTarget && !(parent instanceof ChoiceSchemaNode)) {
@@ -385,4 +403,72 @@ public final class SchemaUtils {
         return new AugmentationIdentifier(ImmutableSet.copyOf(qnames));
     }
 
+    /**
+     * Finds schema node for given path in schema context.
+     * @param schemaContext schema context
+     * @param path path
+     * @return schema node on path
+     */
+    public static SchemaNode findParentSchemaOnPath(final SchemaContext schemaContext, final SchemaPath path) {
+        SchemaNode current = Preconditions.checkNotNull(schemaContext);
+        for (final QName qname : path.getPathFromRoot()) {
+            SchemaNode child;
+            if(current instanceof DataNodeContainer) {
+                child = ((DataNodeContainer) current).getDataChildByName(qname);
+
+                if (child == null && current instanceof SchemaContext) {
+                    child = tryFindGroupings((SchemaContext) current, qname).orNull();
+                }
+
+                if(child == null && current instanceof SchemaContext) {
+                    child = tryFindNotification((SchemaContext) current, qname)
+                            .or(tryFindRpc(((SchemaContext) current), qname)).orNull();
+                }
+            } else if (current instanceof ChoiceSchemaNode) {
+                child = ((ChoiceSchemaNode) current).getCaseNodeByName(qname);
+            } else if (current instanceof RpcDefinition) {
+                switch (qname.getLocalName()) {
+                    case "input":
+                        child = ((RpcDefinition) current).getInput();
+                        break;
+                    case "output":
+                        child = ((RpcDefinition) current).getOutput();
+                        break;
+                    default:
+                        child = null;
+                        break;
+                }
+            } else {
+                throw new IllegalArgumentException(String.format("Schema node %s does not allow children.", current));
+            }
+            current = child;
+        }
+        return current;
+    }
+
+    private static Optional<SchemaNode> tryFindGroupings(final SchemaContext ctx, final QName qname) {
+        return Optional.fromNullable(Iterables.find(ctx.getGroupings(), new SchemaNodePredicate(qname), null));
+    }
+
+    private static Optional<SchemaNode> tryFindRpc(final SchemaContext ctx, final QName qname) {
+        return Optional.fromNullable(Iterables.find(ctx.getOperations(), new SchemaNodePredicate(qname), null));
+    }
+
+    private static Optional<SchemaNode> tryFindNotification(final SchemaContext ctx, final QName qname) {
+        return Optional.fromNullable(Iterables.find(ctx.getNotifications(), new SchemaNodePredicate(qname), null));
+    }
+
+    private static final class SchemaNodePredicate implements Predicate<SchemaNode> {
+        private final QName qname;
+
+        public SchemaNodePredicate(final QName qname) {
+            this.qname = qname;
+        }
+
+        @Override
+        public boolean apply(final SchemaNode input) {
+            return input.getQName().equals(qname);
+        }
+    }
+
 }