Merge "BUG-1486: Pick Javassist version from odlparent"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / codec / SchemaTracker.java
index a75fd813ed03e8a37d02f14546d40e5b45cf0b79..16018df8c3ad177dad6cb26166b860b628eee89f 100644 (file)
@@ -13,6 +13,7 @@ import com.google.common.base.Preconditions;
 import java.io.IOException;
 import java.util.ArrayDeque;
 import java.util.Deque;
+import java.util.HashSet;
 
 import javax.xml.stream.XMLStreamWriter;
 
@@ -22,9 +23,11 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdent
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils;
+import org.opendaylight.yangtools.yang.data.impl.schema.transform.base.AugmentationSchemaProxy;
 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
+import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
@@ -49,14 +52,20 @@ public final class SchemaTracker {
     private final DataNodeContainer root;
 
     private SchemaTracker(final SchemaContext context, final SchemaPath path) {
-        DataNodeContainer current = Preconditions.checkNotNull(context);
+        DataSchemaNode current = Preconditions.checkNotNull(context);
         for (QName qname : path.getPathFromRoot()) {
-            final DataSchemaNode child = current.getDataChildByName(qname);
-            Preconditions.checkArgument(child instanceof DataNodeContainer);
-            current = (DataNodeContainer) child;
+            final DataSchemaNode child;
+            if(current instanceof DataNodeContainer) {
+                child = ((DataNodeContainer) current).getDataChildByName(qname);
+            } else if (current instanceof ChoiceNode) {
+                child = ((ChoiceNode) current).getCaseNodeByName(qname);
+            } else {
+                throw new IllegalArgumentException(String.format("Schema node %s does not allow children.",current));
+            }
+            current = child;
         }
-
-        this.root = current;
+        Preconditions.checkArgument(current instanceof DataNodeContainer,"Schema path must point to container or list. Supplied path %s pointed to: %s",path,current);
+        this.root = (DataNodeContainer) current;
     }
 
     /**
@@ -91,15 +100,27 @@ public final class SchemaTracker {
 
     private SchemaNode getSchema(final PathArgument name) {
         final Object parent = getParent();
-        Preconditions.checkState(parent instanceof DataNodeContainer);
-
+        SchemaNode schema = null;
         final QName qname = name.getNodeType();
-        final SchemaNode schema = ((DataNodeContainer)parent).getDataChildByName(qname);
+        if(parent instanceof DataNodeContainer) {
+            schema = ((DataNodeContainer)parent).getDataChildByName(qname);
+
+        } else if(parent instanceof ChoiceNode) {
+            for(ChoiceCaseNode caze : ((ChoiceNode) parent).getCases()) {
+                DataSchemaNode potential = caze.getDataChildByName(qname);
+                if(potential != null) {
+                    schema = potential;
+                    break;
+                }
+            }
+        } else {
+            throw new IllegalStateException("Unsupported schema type "+ parent.getClass() +" on stack.");
+        }
         Preconditions.checkArgument(schema != null, "Could not find schema for node %s in %s", qname, parent);
         return schema;
     }
 
-    public void startList(final NodeIdentifier name) {
+    public void startList(final PathArgument name) {
         final SchemaNode schema = getSchema(name);
         Preconditions.checkArgument(schema instanceof ListSchemaNode, "Node %s is not a list", schema.getPath());
         schemaStack.push(schema);
@@ -118,12 +139,12 @@ public final class SchemaTracker {
         return (LeafSchemaNode) schema;
     }
 
-    public SchemaNode startLeafSet(final NodeIdentifier name) {
+    public LeafListSchemaNode startLeafSet(final NodeIdentifier name) {
         final SchemaNode schema = getSchema(name);
 
         Preconditions.checkArgument(schema instanceof LeafListSchemaNode, "Node %s is not a leaf-list", schema.getPath());
         schemaStack.push(schema);
-        return schema;
+        return (LeafListSchemaNode)schema;
     }
 
     public LeafListSchemaNode leafSetEntryNode() {
@@ -133,22 +154,22 @@ public final class SchemaTracker {
         return (LeafListSchemaNode) parent;
     }
 
-    public SchemaNode startChoiceNode(final NodeIdentifier name) {
+    public ChoiceNode startChoiceNode(final NodeIdentifier name) {
         LOG.debug("Enter choice {}", name);
         final SchemaNode schema = getSchema(name);
 
         Preconditions.checkArgument(schema instanceof ChoiceNode, "Node %s is not a choice", schema.getPath());
         schemaStack.push(schema);
-        return schema;
+        return (ChoiceNode)schema;
     }
 
-    public SchemaNode startContainerNode(final NodeIdentifier name) {
+    public ContainerSchemaNode startContainerNode(final NodeIdentifier name) {
         LOG.debug("Enter container {}", name);
         final SchemaNode schema = getSchema(name);
 
         Preconditions.checkArgument(schema instanceof ContainerSchemaNode, "Node %s is not a container", schema.getPath());
         schemaStack.push(schema);
-        return schema;
+        return (ContainerSchemaNode)schema;
     }
 
     public AugmentationSchema startAugmentationNode(final AugmentationIdentifier identifier) {
@@ -156,9 +177,15 @@ public final class SchemaTracker {
         final Object parent = getParent();
 
         Preconditions.checkArgument(parent instanceof AugmentationTarget, "Augmentation not allowed under %s", parent);
+        Preconditions.checkArgument(parent instanceof DataNodeContainer, "Augmentation allowed only in DataNodeContainer",parent);
         final AugmentationSchema schema = SchemaUtils.findSchemaForAugment((AugmentationTarget) parent, identifier.getPossibleChildNames());
-        schemaStack.push(schema);
-        return schema;
+        HashSet<DataSchemaNode> realChildSchemas = new HashSet<>();
+        for(DataSchemaNode child : schema.getChildNodes()) {
+            realChildSchemas.add(((DataNodeContainer) parent).getDataChildByName(child.getQName()));
+        }
+        AugmentationSchema resolvedSchema = new AugmentationSchemaProxy(schema, realChildSchemas);
+        schemaStack.push(resolvedSchema);
+        return resolvedSchema;
     }
 
     public AnyXmlSchemaNode anyxmlNode(final NodeIdentifier name) {