Merge "Fix missing format placeholder"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / SchemaUtils.java
index 365148cc609f96b58648357009082e25448c41d6..a5c5148548418596d13c1ef26543aa89c527aff4 100644 (file)
@@ -18,10 +18,12 @@ import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
+import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
@@ -34,26 +36,63 @@ public final class SchemaUtils {
     private SchemaUtils() {
     }
 
+    /**
+     * @param qname - schema node to find
+     * @param dataSchemaNode - iterable of schemaNodes to look through
+     * @return - schema node with newest revision or absent if no schema node with matching qname is found
+     */
     public static final Optional<DataSchemaNode> findFirstSchema(final QName qname, final Iterable<DataSchemaNode> dataSchemaNode) {
+        DataSchemaNode sNode = null;
         if (dataSchemaNode != null && qname != null) {
             for (DataSchemaNode dsn : dataSchemaNode) {
                 if (qname.isEqualWithoutRevision(dsn.getQName())) {
-                    return Optional.<DataSchemaNode> of(dsn);
+                    if (sNode == null || sNode.getQName().getRevision().compareTo(dsn.getQName().getRevision()) < 0) {
+                        sNode = dsn;
+                    }
                 } else if (dsn instanceof ChoiceSchemaNode) {
                     for (ChoiceCaseNode choiceCase : ((ChoiceSchemaNode) dsn).getCases()) {
+
+                        final DataSchemaNode dataChildByName = choiceCase.getDataChildByName(qname);
+                        if (dataChildByName != null) {
+                            return Optional.of(dataChildByName);
+                        }
                         Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
-                        if (foundDsn != null && foundDsn.isPresent()) {
+                        if (foundDsn.isPresent()) {
                             return foundDsn;
                         }
                     }
                 }
             }
         }
-        return Optional.absent();
+        return Optional.fromNullable(sNode);
     }
 
+    /**
+     *
+     * Find child schema node identified by its QName within a provided schema node
+     *
+     * @param schema schema for parent node - search root
+     * @param qname qname(with or without a revision) of a child node to be found in the parent schema
+     * @return found schema node
+     * @throws java.lang.IllegalStateException if the child was not found in parent schema node
+     */
     public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname) {
-        return findSchemaForChild(schema, qname, schema.getChildNodes());
+        // Try to find child schema node directly, but use a fallback that compares QNames without revisions and auto-expands choices
+        final DataSchemaNode dataChildByName = schema.getDataChildByName(qname);
+        return dataChildByName == null ? findSchemaForChild(schema, qname, schema.getChildNodes()) : dataChildByName;
+    }
+
+    @Nullable
+    public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname, final boolean strictMode) {
+        if (strictMode) {
+            return findSchemaForChild(schema, qname);
+        }
+
+        Optional<DataSchemaNode> childSchemaOptional = findFirstSchema(qname, schema.getChildNodes());
+        if (!childSchemaOptional.isPresent()) {
+            return null;
+        }
+        return childSchemaOptional.get();
     }
 
     public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname, final Iterable<DataSchemaNode> childNodes) {