import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Deque;
+import java.util.HashSet;
import javax.xml.stream.XMLStreamWriter;
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;
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;
}
/**
* Create a new writer with the specified context as its root.
*
- * @param writer Output {@link XMLStreamWriter}
* @param context Associated {@link SchemaContext}.
* @return A new {@link NormalizedNodeStreamWriter}
*/
/**
* Create a new writer with the specified context and rooted in the specified schema path
*
- * @param writer Output {@link XMLStreamWriter}
- * @param context Associated {@link SchemaContext}.
+ * @param context Associated {@link SchemaContext}
+ * @param path schema path
*
* @return A new {@link NormalizedNodeStreamWriter}
*/
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);
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() {
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) {
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) {