Fix schema-ref traversal 75/83675/4
authorAnna Bencurova <Anna.Bencurova@pantheon.tech>
Thu, 15 Aug 2019 12:17:34 +0000 (14:17 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 16 Aug 2019 10:28:58 +0000 (12:28 +0200)
When traversing schema-mount definitions, we need to account for
schema-ref being a choice and hence its children are not directly
reachable.

JIRA: YANGTOOLS-1015
Change-Id: Iaa9ddbf33c172c433cb323166e5b1bcea5eea649
Signed-off-by: Anna Bencurova <Anna.Bencurova@pantheon.tech>
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/rfc8528-data-util/src/main/java/org/opendaylight/yangtools/rcf8528/data/util/AbstractMountPointContextFactory.java

index 9009eb862c05503589bc4618efb68939c9bfaf28..a10195b42ecc2b2e4d27ce6db7b5a6999210b1b6 100644 (file)
@@ -26,6 +26,7 @@ import org.opendaylight.yangtools.rfc8528.model.api.SchemaMountConstants;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
@@ -84,6 +85,8 @@ public abstract class AbstractMountPointContextFactory extends AbstractDynamicMo
         QName.create(SchemaMountConstants.RFC8528_MODULE, "module").intern());
     private static final NodeIdentifier LABEL = NodeIdentifier.create(
         QName.create(SchemaMountConstants.RFC8528_MODULE, "label").intern());
+    private static final NodeIdentifier SCHEMA_REF = NodeIdentifier.create(
+        QName.create(SchemaMountConstants.RFC8528_MODULE, "schema-ref").intern());
     private static final NodeIdentifier INLINE = NodeIdentifier.create(
         QName.create(SchemaMountConstants.RFC8528_MODULE, "inline").intern());
     private static final NodeIdentifier SHARED_SCHEMA = NodeIdentifier.create(
@@ -133,20 +136,27 @@ public abstract class AbstractMountPointContextFactory extends AbstractDynamicMo
                     checkArgument(value instanceof Boolean, "Unexpected config leaf value %s", cfg);
                     return (Boolean) value;
                 }).orElse(Boolean.TRUE),
-                entry.getChild(SHARED_SCHEMA).map(sharedSchema -> {
-                    checkArgument(sharedSchema instanceof ContainerNode, "Unexpected shared-schema container %s",
-                        sharedSchema);
-                    return ((ContainerNode) sharedSchema).getChild(PARENT_REFERENCE).map(parentRef -> {
-                        // FIXME: decode
-                        return ImmutableSet.<String>of();
-                    }).orElseGet(ImmutableSet::of);
-                }).orElseGet(() -> {
-                    checkArgument(entry.getChild(INLINE).isPresent(), "Unhandled schema-ref type in %s", entry);
-                    return ImmutableSet.of();
-                }));
+                getSchema(entry.getChild(SCHEMA_REF)
+                    .orElseThrow(() -> new IllegalArgumentException("Missing schema-ref choice in " + entry))));
         }).collect(Collectors.toList()), this::createContextFactory);
     }
 
+    private static ImmutableSet<String> getSchema(final DataContainerChild<?, ?> child) {
+        checkArgument(child instanceof ChoiceNode, "Unexpected schema-ref choice %s", child);
+        final ChoiceNode schemaRef = (ChoiceNode) child;
+
+        return schemaRef.getChild(SHARED_SCHEMA).map(sharedSchema -> {
+            checkArgument(sharedSchema instanceof ContainerNode, "Unexpected shared-schema container %s", sharedSchema);
+            return ((ContainerNode) sharedSchema).getChild(PARENT_REFERENCE).map(parentRef -> {
+                // FIXME: decode
+                return ImmutableSet.<String>of();
+            }).orElseGet(ImmutableSet::of);
+        }).orElseGet(() -> {
+            checkArgument(schemaRef.getChild(INLINE).isPresent(), "Unhandled schema-ref type in %s", schemaRef);
+            return ImmutableSet.of();
+        });
+    }
+
     /**
      * Create a fresh {@link MountPointContextFactory} for a nested {@link MountPointDefinition}.
      *