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 DataNodeContainer current = Preconditions.checkNotNull(context);
53 for (QName qname : path.getPathFromRoot()) {
54 final DataSchemaNode child = current.getDataChildByName(qname);
55 Preconditions.checkArgument(child instanceof DataNodeContainer);
56 current = (DataNodeContainer) child;
63 * Create a new writer with the specified context as its root.
65 * @param writer Output {@link XMLStreamWriter}
66 * @param context Associated {@link SchemaContext}.
67 * @return A new {@link NormalizedNodeStreamWriter}
69 public static SchemaTracker create(final SchemaContext context) {
70 return create(context, SchemaPath.ROOT);
74 * Create a new writer with the specified context and rooted in the specified schema path
76 * @param writer Output {@link XMLStreamWriter}
77 * @param context Associated {@link SchemaContext}.
79 * @return A new {@link NormalizedNodeStreamWriter}
81 public static SchemaTracker create(final SchemaContext context, final SchemaPath path) {
82 return new SchemaTracker(context, path);
85 public Object getParent() {
86 if (schemaStack.isEmpty()) {
89 return schemaStack.peek();
92 private SchemaNode getSchema(final PathArgument name) {
93 final Object parent = getParent();
94 SchemaNode schema = null;
95 final QName qname = name.getNodeType();
96 if(parent instanceof DataNodeContainer) {
97 schema = ((DataNodeContainer)parent).getDataChildByName(qname);
99 } else if(parent instanceof ChoiceNode) {
100 for(ChoiceCaseNode caze : ((ChoiceNode) parent).getCases()) {
101 DataSchemaNode potential = caze.getDataChildByName(qname);
102 if(potential != null) {
108 throw new IllegalStateException("Unsupported schema type "+ parent.getClass() +" on stack.");
110 Preconditions.checkArgument(schema != null, "Could not find schema for node %s in %s", qname, parent);
114 public void startList(final NodeIdentifier name) {
115 final SchemaNode schema = getSchema(name);
116 Preconditions.checkArgument(schema instanceof ListSchemaNode, "Node %s is not a list", schema.getPath());
117 schemaStack.push(schema);
120 public void startListItem(final PathArgument name) throws IOException {
121 final Object schema = getParent();
122 Preconditions.checkArgument(schema instanceof ListSchemaNode, "List item is not appropriate");
123 schemaStack.push(schema);
126 public LeafSchemaNode leafNode(final NodeIdentifier name) throws IOException {
127 final SchemaNode schema = getSchema(name);
129 Preconditions.checkArgument(schema instanceof LeafSchemaNode, "Node %s is not a leaf", schema.getPath());
130 return (LeafSchemaNode) schema;
133 public SchemaNode startLeafSet(final NodeIdentifier name) {
134 final SchemaNode schema = getSchema(name);
136 Preconditions.checkArgument(schema instanceof LeafListSchemaNode, "Node %s is not a leaf-list", schema.getPath());
137 schemaStack.push(schema);
141 public LeafListSchemaNode leafSetEntryNode() {
142 final Object parent = getParent();
144 Preconditions.checkArgument(parent instanceof LeafListSchemaNode, "Not currently in a leaf-list");
145 return (LeafListSchemaNode) parent;
148 public SchemaNode startChoiceNode(final NodeIdentifier name) {
149 LOG.debug("Enter choice {}", name);
150 final SchemaNode schema = getSchema(name);
152 Preconditions.checkArgument(schema instanceof ChoiceNode, "Node %s is not a choice", schema.getPath());
153 schemaStack.push(schema);
157 public SchemaNode startContainerNode(final NodeIdentifier name) {
158 LOG.debug("Enter container {}", name);
159 final SchemaNode schema = getSchema(name);
161 Preconditions.checkArgument(schema instanceof ContainerSchemaNode, "Node %s is not a container", schema.getPath());
162 schemaStack.push(schema);
166 public AugmentationSchema startAugmentationNode(final AugmentationIdentifier identifier) {
167 LOG.debug("Enter augmentation {}", identifier);
168 final Object parent = getParent();
170 Preconditions.checkArgument(parent instanceof AugmentationTarget, "Augmentation not allowed under %s", parent);
171 Preconditions.checkArgument(parent instanceof DataNodeContainer, "Augmentation allowed only in DataNodeContainer",parent);
172 final AugmentationSchema schema = SchemaUtils.findSchemaForAugment((AugmentationTarget) parent, identifier.getPossibleChildNames());
173 HashSet<DataSchemaNode> realChildSchemas = new HashSet<>();
174 for(DataSchemaNode child : schema.getChildNodes()) {
175 realChildSchemas.add(((DataNodeContainer) parent).getDataChildByName(child.getQName()));
177 AugmentationSchema resolvedSchema = new AugmentationSchemaProxy(schema, realChildSchemas);
178 schemaStack.push(resolvedSchema);
179 return resolvedSchema;
182 public AnyXmlSchemaNode anyxmlNode(final NodeIdentifier name) {
183 final SchemaNode schema = getSchema(name);
185 Preconditions.checkArgument(schema instanceof AnyXmlSchemaNode, "Node %s is not anyxml", schema.getPath());
186 return (AnyXmlSchemaNode)schema;
189 public Object endNode() {
190 return schemaStack.pop();