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 static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import com.google.common.collect.Iterables;
15 import java.io.IOException;
16 import java.util.ArrayDeque;
17 import java.util.Collection;
18 import java.util.Deque;
19 import java.util.Optional;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.yangtools.odlext.model.api.YangModeledAnyXmlSchemaNode;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
27 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
28 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils;
29 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
32 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
36 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
38 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
39 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
46 import org.opendaylight.yangtools.yang.model.util.EffectiveAugmentationSchema;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 * Utility class for tracking the underlying state of the underlying
55 public final class SchemaTracker {
56 private static final Logger LOG = LoggerFactory.getLogger(SchemaTracker.class);
57 private final Deque<WithStatus> schemaStack = new ArrayDeque<>();
58 private final DataNodeContainer root;
60 private SchemaTracker(final DataNodeContainer root) {
61 this.root = requireNonNull(root);
65 * Create a new writer with the specified node as its root.
67 * @param root Root node
68 * @return A new {@link NormalizedNodeStreamWriter}
70 public static @NonNull SchemaTracker create(final DataNodeContainer root) {
71 return new SchemaTracker(root);
75 * Create a new writer with the specified context and rooted in the specified schema path.
77 * @param context Associated {@link SchemaContext}
78 * @param path schema path
79 * @return A new {@link NormalizedNodeStreamWriter}
81 public static @NonNull SchemaTracker create(final SchemaContext context, final SchemaPath path) {
82 final Collection<SchemaNode> schemaNodes = SchemaUtils.findParentSchemaNodesOnPath(context, path);
83 checkArgument(!schemaNodes.isEmpty(), "Unable to find schema node for supplied schema path: %s", path);
84 if (schemaNodes.size() > 1) {
85 LOG.warn("More possible schema nodes {} for supplied schema path {}", schemaNodes, path);
87 final Optional<DataNodeContainer> current = schemaNodes.stream()
88 .filter(node -> node instanceof DataNodeContainer).map(DataNodeContainer.class::cast)
90 checkArgument(current.isPresent(),
91 "Schema path must point to container or list or an rpc input/output. Supplied path %s pointed to: %s",
93 return new SchemaTracker(current.get());
96 public Object getParent() {
97 if (schemaStack.isEmpty()) {
100 return schemaStack.peek();
103 private SchemaNode getSchema(final PathArgument name) {
104 final Object parent = getParent();
105 SchemaNode schema = null;
106 final QName qname = name.getNodeType();
107 if (parent instanceof DataNodeContainer) {
108 schema = ((DataNodeContainer)parent).getDataChildByName(qname);
109 if (schema == null) {
110 if (parent instanceof GroupingDefinition) {
111 schema = (GroupingDefinition) parent;
112 } else if (parent instanceof NotificationDefinition) {
113 schema = (NotificationDefinition) parent;
116 } else if (parent instanceof ChoiceSchemaNode) {
117 schema = findChildInCases((ChoiceSchemaNode) parent, qname);
119 throw new IllegalStateException("Unsupported schema type " + parent.getClass() + " on stack.");
122 checkArgument(schema != null, "Could not find schema for node %s in %s", qname, parent);
126 private static SchemaNode findChildInCases(final ChoiceSchemaNode parent, final QName qname) {
127 DataSchemaNode schema = null;
128 for (final CaseSchemaNode caze : parent.getCases().values()) {
129 final DataSchemaNode potential = caze.getDataChildByName(qname);
130 if (potential != null) {
138 private static SchemaNode findCaseByChild(final ChoiceSchemaNode parent, final QName qname) {
139 DataSchemaNode schema = null;
140 for (final CaseSchemaNode caze : parent.getCases().values()) {
141 final DataSchemaNode potential = caze.getDataChildByName(qname);
142 if (potential != null) {
150 public void startList(final PathArgument name) {
151 final SchemaNode schema = getSchema(name);
152 checkArgument(schema instanceof ListSchemaNode, "Node %s is not a list", schema.getPath());
153 schemaStack.push(schema);
156 public void startListItem(final PathArgument name) throws IOException {
157 final Object schema = getParent();
158 checkArgument(schema instanceof ListSchemaNode, "List item is not appropriate");
159 schemaStack.push((ListSchemaNode) schema);
162 public LeafSchemaNode leafNode(final NodeIdentifier name) throws IOException {
163 final SchemaNode schema = getSchema(name);
165 checkArgument(schema instanceof LeafSchemaNode, "Node %s is not a leaf", schema.getPath());
166 return (LeafSchemaNode) schema;
169 public void startLeafNode(final NodeIdentifier name) throws IOException {
170 schemaStack.push(leafNode(name));
173 public LeafListSchemaNode startLeafSet(final NodeIdentifier name) {
174 final SchemaNode schema = getSchema(name);
176 checkArgument(schema instanceof LeafListSchemaNode, "Node %s is not a leaf-list", schema.getPath());
177 schemaStack.push(schema);
178 return (LeafListSchemaNode)schema;
181 public LeafListSchemaNode leafSetEntryNode(final QName qname) {
182 final Object parent = getParent();
183 if (parent instanceof LeafListSchemaNode) {
184 return (LeafListSchemaNode) parent;
187 final SchemaNode child = SchemaUtils.findDataChildSchemaByQName((SchemaNode) parent, qname);
188 checkArgument(child instanceof LeafListSchemaNode,
189 "Node %s is neither a leaf-list nor currently in a leaf-list", child.getPath());
190 return (LeafListSchemaNode) child;
193 public void startLeafSetEntryNode(final NodeWithValue<?> name) {
194 schemaStack.push(leafSetEntryNode(name.getNodeType()));
197 public ChoiceSchemaNode startChoiceNode(final NodeIdentifier name) {
198 LOG.debug("Enter choice {}", name);
199 final SchemaNode schema = getSchema(name);
201 checkArgument(schema instanceof ChoiceSchemaNode, "Node %s is not a choice", schema.getPath());
202 schemaStack.push(schema);
203 return (ChoiceSchemaNode)schema;
206 public SchemaNode startContainerNode(final NodeIdentifier name) {
207 LOG.debug("Enter container {}", name);
208 final SchemaNode schema = getSchema(name);
210 boolean isAllowed = schema instanceof ContainerSchemaNode;
211 isAllowed |= schema instanceof NotificationDefinition;
213 checkArgument(isAllowed, "Node %s is not a container nor a notification", schema.getPath());
214 schemaStack.push(schema);
219 public SchemaNode startYangModeledAnyXmlNode(final NodeIdentifier name) {
220 LOG.debug("Enter yang modeled anyXml {}", name);
221 final SchemaNode schema = getSchema(name);
223 checkArgument(schema instanceof YangModeledAnyXmlSchemaNode, "Node %s is not an yang modeled anyXml.",
226 schemaStack.push(((YangModeledAnyXmlSchemaNode) schema).getSchemaOfAnyXmlData());
231 public AugmentationSchemaNode startAugmentationNode(final AugmentationIdentifier identifier) {
232 LOG.debug("Enter augmentation {}", identifier);
233 Object parent = getParent();
235 checkArgument(parent instanceof AugmentationTarget, "Augmentation not allowed under %s", parent);
236 if (parent instanceof ChoiceSchemaNode) {
237 final QName name = Iterables.get(identifier.getPossibleChildNames(), 0);
238 parent = findCaseByChild((ChoiceSchemaNode) parent, name);
240 checkArgument(parent instanceof DataNodeContainer, "Augmentation allowed only in DataNodeContainer", parent);
241 final AugmentationSchemaNode schema = SchemaUtils.findSchemaForAugment((AugmentationTarget) parent,
242 identifier.getPossibleChildNames());
243 final AugmentationSchemaNode resolvedSchema = EffectiveAugmentationSchema.create(schema,
244 (DataNodeContainer) parent);
245 schemaStack.push(resolvedSchema);
246 return resolvedSchema;
249 public AnyXmlSchemaNode anyxmlNode(final NodeIdentifier name) {
250 final SchemaNode schema = getSchema(name);
251 checkArgument(schema instanceof AnyXmlSchemaNode, "Node %s is not anyxml", schema.getPath());
252 return (AnyXmlSchemaNode)schema;
255 public void startAnyxmlNode(final NodeIdentifier name) {
256 schemaStack.push(anyxmlNode(name));
259 public Object endNode() {
260 return schemaStack.pop();