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 com.google.common.collect.Iterables;
13 import java.io.IOException;
14 import java.util.ArrayDeque;
15 import java.util.Deque;
16 import java.util.HashSet;
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.model.api.AnyXmlSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
25 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
26 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
27 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
30 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
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.NotificationDefinition;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
39 import org.opendaylight.yangtools.yang.model.api.YangModeledAnyXmlSchemaNode;
40 import org.opendaylight.yangtools.yang.model.util.EffectiveAugmentationSchema;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * Utility class for tracking the underlying state of the underlying
49 public final class SchemaTracker {
50 private static final Logger LOG = LoggerFactory.getLogger(SchemaTracker.class);
51 private final Deque<Object> schemaStack = new ArrayDeque<>();
52 private final DataNodeContainer root;
54 private SchemaTracker(final SchemaContext context, final SchemaPath path) {
55 SchemaNode current = SchemaUtils.findParentSchemaOnPath(context, path);
56 Preconditions.checkArgument(current instanceof DataNodeContainer,"Schema path must point to container or list or an rpc input/output. Supplied path %s pointed to: %s",path,current);
57 root = (DataNodeContainer) current;
61 * Create a new writer with the specified context as its root.
63 * @param context Associated {@link SchemaContext}.
64 * @return A new {@link NormalizedNodeStreamWriter}
66 public static SchemaTracker create(final SchemaContext context) {
67 return create(context, SchemaPath.ROOT);
71 * Create a new writer with the specified context and rooted in the specified schema path
73 * @param context Associated {@link SchemaContext}
74 * @param path schema path
76 * @return A new {@link NormalizedNodeStreamWriter}
78 public static SchemaTracker create(final SchemaContext context, final SchemaPath path) {
79 return new SchemaTracker(context, path);
82 public Object getParent() {
83 if (schemaStack.isEmpty()) {
86 return schemaStack.peek();
89 private SchemaNode getSchema(final PathArgument name) {
90 final Object parent = getParent();
91 SchemaNode schema = null;
92 final QName qname = name.getNodeType();
93 if(parent instanceof DataNodeContainer) {
94 schema = ((DataNodeContainer)parent).getDataChildByName(qname);
96 if(schema == null && parent instanceof GroupingDefinition) {
97 schema = ((GroupingDefinition) parent);
100 if(schema == null && parent instanceof NotificationDefinition) {
101 schema = ((NotificationDefinition) parent);
103 } else if(parent instanceof ChoiceSchemaNode) {
104 schema = findChildInCases((ChoiceSchemaNode) parent, qname);
106 throw new IllegalStateException("Unsupported schema type "+ parent.getClass() +" on stack.");
108 Preconditions.checkArgument(schema != null, "Could not find schema for node %s in %s", qname, parent);
112 private static SchemaNode findChildInCases(final ChoiceSchemaNode parent, final QName qname) {
113 DataSchemaNode schema = null;
114 for(final ChoiceCaseNode caze : parent.getCases()) {
115 final DataSchemaNode potential = caze.getDataChildByName(qname);
116 if(potential != null) {
124 private static SchemaNode findCaseByChild(final ChoiceSchemaNode parent, final QName qname) {
125 DataSchemaNode schema = null;
126 for(final ChoiceCaseNode caze : parent.getCases()) {
127 final DataSchemaNode potential = caze.getDataChildByName(qname);
128 if(potential != null) {
136 public void startList(final PathArgument name) {
137 final SchemaNode schema = getSchema(name);
138 Preconditions.checkArgument(schema instanceof ListSchemaNode, "Node %s is not a list", schema.getPath());
139 schemaStack.push(schema);
142 public void startListItem(final PathArgument name) throws IOException {
143 final Object schema = getParent();
144 Preconditions.checkArgument(schema instanceof ListSchemaNode, "List item is not appropriate");
145 schemaStack.push(schema);
148 public LeafSchemaNode leafNode(final NodeIdentifier name) throws IOException {
149 final SchemaNode schema = getSchema(name);
151 Preconditions.checkArgument(schema instanceof LeafSchemaNode, "Node %s is not a leaf", schema.getPath());
152 return (LeafSchemaNode) schema;
155 public LeafListSchemaNode startLeafSet(final NodeIdentifier name) {
156 final SchemaNode schema = getSchema(name);
158 Preconditions.checkArgument(schema instanceof LeafListSchemaNode, "Node %s is not a leaf-list", schema.getPath());
159 schemaStack.push(schema);
160 return (LeafListSchemaNode)schema;
163 public LeafListSchemaNode leafSetEntryNode() {
164 final Object parent = getParent();
166 Preconditions.checkArgument(parent instanceof LeafListSchemaNode, "Not currently in a leaf-list");
167 return (LeafListSchemaNode) parent;
170 public ChoiceSchemaNode startChoiceNode(final NodeIdentifier name) {
171 LOG.debug("Enter choice {}", name);
172 final SchemaNode schema = getSchema(name);
174 Preconditions.checkArgument(schema instanceof ChoiceSchemaNode, "Node %s is not a choice", schema.getPath());
175 schemaStack.push(schema);
176 return (ChoiceSchemaNode)schema;
179 public SchemaNode startContainerNode(final NodeIdentifier name) {
180 LOG.debug("Enter container {}", name);
181 final SchemaNode schema = getSchema(name);
183 boolean isAllowed = schema instanceof ContainerSchemaNode;
184 isAllowed |= schema instanceof NotificationDefinition;
186 Preconditions.checkArgument(isAllowed, "Node %s is not a container nor a notification", schema.getPath());
187 schemaStack.push(schema);
192 public SchemaNode startYangModeledAnyXmlNode(final NodeIdentifier name) {
193 LOG.debug("Enter yang modeled anyXml {}", name);
194 final SchemaNode schema = getSchema(name);
196 Preconditions.checkArgument(schema instanceof YangModeledAnyXmlSchemaNode,
197 "Node %s is not an yang modeled anyXml.", schema.getPath());
199 schemaStack.push(((YangModeledAnyXmlSchemaNode) schema).getSchemaOfAnyXmlData());
204 public AugmentationSchema startAugmentationNode(final AugmentationIdentifier identifier) {
205 LOG.debug("Enter augmentation {}", identifier);
206 Object parent = getParent();
208 Preconditions.checkArgument(parent instanceof AugmentationTarget, "Augmentation not allowed under %s", parent);
209 if(parent instanceof ChoiceSchemaNode) {
210 final QName name = Iterables.get(identifier.getPossibleChildNames(), 0);
211 parent = findCaseByChild((ChoiceSchemaNode) parent, name);
213 Preconditions.checkArgument(parent instanceof DataNodeContainer, "Augmentation allowed only in DataNodeContainer",parent);
214 final AugmentationSchema schema = SchemaUtils.findSchemaForAugment((AugmentationTarget) parent, identifier.getPossibleChildNames());
215 final HashSet<DataSchemaNode> realChildSchemas = new HashSet<>();
216 for(final DataSchemaNode child : schema.getChildNodes()) {
217 realChildSchemas.add(((DataNodeContainer) parent).getDataChildByName(child.getQName()));
219 final AugmentationSchema resolvedSchema = new EffectiveAugmentationSchema(schema, realChildSchemas);
220 schemaStack.push(resolvedSchema);
221 return resolvedSchema;
224 public AnyXmlSchemaNode anyxmlNode(final NodeIdentifier name) {
225 final SchemaNode schema = getSchema(name);
227 Preconditions.checkArgument(schema instanceof AnyXmlSchemaNode, "Node %s is not anyxml", schema.getPath());
228 return (AnyXmlSchemaNode)schema;
231 public Object endNode() {
232 return schemaStack.pop();