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.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Predicate;
14 import com.google.common.collect.Iterables;
15 import java.io.IOException;
16 import java.util.ArrayDeque;
17 import java.util.Deque;
18 import java.util.HashSet;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
24 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils;
25 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
27 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
28 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
29 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
32 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
34 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
38 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
42 import org.opendaylight.yangtools.yang.model.api.YangModeledAnyXmlSchemaNode;
43 import org.opendaylight.yangtools.yang.model.util.EffectiveAugmentationSchema;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * Utility class for tracking the underlying state of the underlying
52 public final class SchemaTracker {
53 private static final Logger LOG = LoggerFactory.getLogger(SchemaTracker.class);
54 private final Deque<Object> schemaStack = new ArrayDeque<>();
55 private final DataNodeContainer root;
57 private SchemaTracker(final SchemaContext context, final SchemaPath path) {
58 SchemaNode current = SchemaUtils.findParentSchemaOnPath(context, path);
59 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);
60 root = (DataNodeContainer) current;
64 * Create a new writer with the specified context as its root.
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 context Associated {@link SchemaContext}
77 * @param path schema path
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 if(schema == null && parent instanceof GroupingDefinition) {
100 schema = ((GroupingDefinition) parent);
103 if(schema == null && parent instanceof NotificationDefinition) {
104 schema = ((NotificationDefinition) parent);
106 } else if(parent instanceof ChoiceSchemaNode) {
107 schema = findChildInCases((ChoiceSchemaNode) parent, qname);
109 throw new IllegalStateException("Unsupported schema type "+ parent.getClass() +" on stack.");
111 Preconditions.checkArgument(schema != null, "Could not find schema for node %s in %s", qname, parent);
115 private static SchemaNode findChildInCases(final ChoiceSchemaNode parent, final QName qname) {
116 DataSchemaNode schema = null;
117 for(final ChoiceCaseNode caze : parent.getCases()) {
118 final DataSchemaNode potential = caze.getDataChildByName(qname);
119 if(potential != null) {
127 private static SchemaNode findCaseByChild(final ChoiceSchemaNode parent, final QName qname) {
128 DataSchemaNode schema = null;
129 for(final ChoiceCaseNode caze : parent.getCases()) {
130 final DataSchemaNode potential = caze.getDataChildByName(qname);
131 if(potential != null) {
139 public void startList(final PathArgument name) {
140 final SchemaNode schema = getSchema(name);
141 Preconditions.checkArgument(schema instanceof ListSchemaNode, "Node %s is not a list", schema.getPath());
142 schemaStack.push(schema);
145 public void startListItem(final PathArgument name) throws IOException {
146 final Object schema = getParent();
147 Preconditions.checkArgument(schema instanceof ListSchemaNode, "List item is not appropriate");
148 schemaStack.push(schema);
151 public LeafSchemaNode leafNode(final NodeIdentifier name) throws IOException {
152 final SchemaNode schema = getSchema(name);
154 Preconditions.checkArgument(schema instanceof LeafSchemaNode, "Node %s is not a leaf", schema.getPath());
155 return (LeafSchemaNode) schema;
158 public LeafListSchemaNode startLeafSet(final NodeIdentifier name) {
159 final SchemaNode schema = getSchema(name);
161 Preconditions.checkArgument(schema instanceof LeafListSchemaNode, "Node %s is not a leaf-list", schema.getPath());
162 schemaStack.push(schema);
163 return (LeafListSchemaNode)schema;
166 public LeafListSchemaNode leafSetEntryNode() {
167 final Object parent = getParent();
169 Preconditions.checkArgument(parent instanceof LeafListSchemaNode, "Not currently in a leaf-list");
170 return (LeafListSchemaNode) parent;
173 public ChoiceSchemaNode startChoiceNode(final NodeIdentifier name) {
174 LOG.debug("Enter choice {}", name);
175 final SchemaNode schema = getSchema(name);
177 Preconditions.checkArgument(schema instanceof ChoiceSchemaNode, "Node %s is not a choice", schema.getPath());
178 schemaStack.push(schema);
179 return (ChoiceSchemaNode)schema;
182 public SchemaNode startContainerNode(final NodeIdentifier name) {
183 LOG.debug("Enter container {}", name);
184 final SchemaNode schema = getSchema(name);
186 boolean isAllowed = schema instanceof ContainerSchemaNode;
187 isAllowed |= schema instanceof NotificationDefinition;
189 Preconditions.checkArgument(isAllowed, "Node %s is not a container nor a notification", schema.getPath());
190 schemaStack.push(schema);
195 public SchemaNode startYangModeledAnyXmlNode(final NodeIdentifier name) {
196 LOG.debug("Enter yang modeled anyXml {}", name);
197 final SchemaNode schema = getSchema(name);
199 Preconditions.checkArgument(schema instanceof YangModeledAnyXmlSchemaNode,
200 "Node %s is not an yang modeled anyXml.", schema.getPath());
202 schemaStack.push(((YangModeledAnyXmlSchemaNode) schema).getSchemaOfAnyXmlData());
207 public AugmentationSchema startAugmentationNode(final AugmentationIdentifier identifier) {
208 LOG.debug("Enter augmentation {}", identifier);
209 Object parent = getParent();
211 Preconditions.checkArgument(parent instanceof AugmentationTarget, "Augmentation not allowed under %s", parent);
212 if(parent instanceof ChoiceSchemaNode) {
213 final QName name = Iterables.get(identifier.getPossibleChildNames(), 0);
214 parent = findCaseByChild((ChoiceSchemaNode) parent, name);
216 Preconditions.checkArgument(parent instanceof DataNodeContainer, "Augmentation allowed only in DataNodeContainer",parent);
217 final AugmentationSchema schema = SchemaUtils.findSchemaForAugment((AugmentationTarget) parent, identifier.getPossibleChildNames());
218 final HashSet<DataSchemaNode> realChildSchemas = new HashSet<>();
219 for(final DataSchemaNode child : schema.getChildNodes()) {
220 realChildSchemas.add(((DataNodeContainer) parent).getDataChildByName(child.getQName()));
222 final AugmentationSchema resolvedSchema = new EffectiveAugmentationSchema(schema, realChildSchemas);
223 schemaStack.push(resolvedSchema);
224 return resolvedSchema;
227 public AnyXmlSchemaNode anyxmlNode(final NodeIdentifier name) {
228 final SchemaNode schema = getSchema(name);
230 Preconditions.checkArgument(schema instanceof AnyXmlSchemaNode, "Node %s is not anyxml", schema.getPath());
231 return (AnyXmlSchemaNode)schema;
234 public Object endNode() {
235 return schemaStack.pop();