2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.impl.codec;
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import java.io.IOException;
13 import java.util.ArrayDeque;
14 import java.util.Deque;
15 import java.util.HashSet;
16 import javax.xml.stream.XMLStreamWriter;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
22 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils;
23 import org.opendaylight.yangtools.yang.data.impl.schema.transform.base.AugmentationSchemaProxy;
24 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
26 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
27 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
28 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
29 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * Utility class for tracking the underlying state of the underlying
46 public final class SchemaTracker {
47 private static final Logger LOG = LoggerFactory.getLogger(SchemaTracker.class);
48 private final Deque<Object> schemaStack = new ArrayDeque<>();
49 private final DataNodeContainer root;
51 private SchemaTracker(final SchemaContext context, final SchemaPath path) {
52 DataSchemaNode current = Preconditions.checkNotNull(context);
53 for (QName qname : path.getPathFromRoot()) {
54 final DataSchemaNode child;
55 if(current instanceof DataNodeContainer) {
56 child = ((DataNodeContainer) current).getDataChildByName(qname);
57 } else if (current instanceof ChoiceNode) {
58 child = ((ChoiceNode) current).getCaseNodeByName(qname);
60 throw new IllegalArgumentException(String.format("Schema node %s does not allow children.",current));
64 Preconditions.checkArgument(current instanceof DataNodeContainer,"Schema path must point to container or list. Supplied path %s pointed to: %s",path,current);
65 this.root = (DataNodeContainer) current;
69 * Create a new writer with the specified context as its root.
71 * @param writer Output {@link XMLStreamWriter}
72 * @param context Associated {@link SchemaContext}.
73 * @return A new {@link NormalizedNodeStreamWriter}
75 public static SchemaTracker create(final SchemaContext context) {
76 return create(context, SchemaPath.ROOT);
80 * Create a new writer with the specified context and rooted in the specified schema path
82 * @param writer Output {@link XMLStreamWriter}
83 * @param context Associated {@link SchemaContext}.
85 * @return A new {@link NormalizedNodeStreamWriter}
87 public static SchemaTracker create(final SchemaContext context, final SchemaPath path) {
88 return new SchemaTracker(context, path);
91 public Object getParent() {
92 if (schemaStack.isEmpty()) {
95 return schemaStack.peek();
98 private SchemaNode getSchema(final PathArgument name) {
99 final Object parent = getParent();
100 SchemaNode schema = null;
101 final QName qname = name.getNodeType();
102 if(parent instanceof DataNodeContainer) {
103 schema = ((DataNodeContainer)parent).getDataChildByName(qname);
105 } else if(parent instanceof ChoiceNode) {
106 for(ChoiceCaseNode caze : ((ChoiceNode) parent).getCases()) {
107 DataSchemaNode potential = caze.getDataChildByName(qname);
108 if(potential != null) {
114 throw new IllegalStateException("Unsupported schema type "+ parent.getClass() +" on stack.");
116 Preconditions.checkArgument(schema != null, "Could not find schema for node %s in %s", qname, parent);
120 public void startList(final NodeIdentifier name) {
121 final SchemaNode schema = getSchema(name);
122 Preconditions.checkArgument(schema instanceof ListSchemaNode, "Node %s is not a list", schema.getPath());
123 schemaStack.push(schema);
126 public void startListItem(final PathArgument name) throws IOException {
127 final Object schema = getParent();
128 Preconditions.checkArgument(schema instanceof ListSchemaNode, "List item is not appropriate");
129 schemaStack.push(schema);
132 public LeafSchemaNode leafNode(final NodeIdentifier name) throws IOException {
133 final SchemaNode schema = getSchema(name);
135 Preconditions.checkArgument(schema instanceof LeafSchemaNode, "Node %s is not a leaf", schema.getPath());
136 return (LeafSchemaNode) schema;
139 public SchemaNode startLeafSet(final NodeIdentifier name) {
140 final SchemaNode schema = getSchema(name);
142 Preconditions.checkArgument(schema instanceof LeafListSchemaNode, "Node %s is not a leaf-list", schema.getPath());
143 schemaStack.push(schema);
147 public LeafListSchemaNode leafSetEntryNode() {
148 final Object parent = getParent();
150 Preconditions.checkArgument(parent instanceof LeafListSchemaNode, "Not currently in a leaf-list");
151 return (LeafListSchemaNode) parent;
154 public SchemaNode startChoiceNode(final NodeIdentifier name) {
155 LOG.debug("Enter choice {}", name);
156 final SchemaNode schema = getSchema(name);
158 Preconditions.checkArgument(schema instanceof ChoiceNode, "Node %s is not a choice", schema.getPath());
159 schemaStack.push(schema);
163 public SchemaNode startContainerNode(final NodeIdentifier name) {
164 LOG.debug("Enter container {}", name);
165 final SchemaNode schema = getSchema(name);
167 Preconditions.checkArgument(schema instanceof ContainerSchemaNode, "Node %s is not a container", schema.getPath());
168 schemaStack.push(schema);
172 public AugmentationSchema startAugmentationNode(final AugmentationIdentifier identifier) {
173 LOG.debug("Enter augmentation {}", identifier);
174 final Object parent = getParent();
176 Preconditions.checkArgument(parent instanceof AugmentationTarget, "Augmentation not allowed under %s", parent);
177 Preconditions.checkArgument(parent instanceof DataNodeContainer, "Augmentation allowed only in DataNodeContainer",parent);
178 final AugmentationSchema schema = SchemaUtils.findSchemaForAugment((AugmentationTarget) parent, identifier.getPossibleChildNames());
179 HashSet<DataSchemaNode> realChildSchemas = new HashSet<>();
180 for(DataSchemaNode child : schema.getChildNodes()) {
181 realChildSchemas.add(((DataNodeContainer) parent).getDataChildByName(child.getQName()));
183 AugmentationSchema resolvedSchema = new AugmentationSchemaProxy(schema, realChildSchemas);
184 schemaStack.push(resolvedSchema);
185 return resolvedSchema;
188 public AnyXmlSchemaNode anyxmlNode(final NodeIdentifier name) {
189 final SchemaNode schema = getSchema(name);
191 Preconditions.checkArgument(schema instanceof AnyXmlSchemaNode, "Node %s is not anyxml", schema.getPath());
192 return (AnyXmlSchemaNode)schema;
195 public Object endNode() {
196 return schemaStack.pop();