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 = Preconditions.checkNotNull(context);
59 for (final QName qname : path.getPathFromRoot()) {
60 current = getChild(current, qname);
62 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);
63 root = (DataNodeContainer) current;
66 private SchemaNode getChild(final SchemaNode node, final QName qname) {
67 SchemaNode child = null;
69 if (node instanceof DataNodeContainer) {
70 child = ((DataNodeContainer) node).getDataChildByName(qname);
72 if (child == null && node instanceof SchemaContext) {
73 child = tryFindGroupings((SchemaContext) node, qname).orNull();
76 if(child == null && node instanceof SchemaContext) {
77 child = tryFindNotification((SchemaContext) node, qname)
78 .or(tryFindRpc(((SchemaContext) node), qname)).orNull();
80 } else if (node instanceof ChoiceSchemaNode) {
81 child = ((ChoiceSchemaNode) node).getCaseNodeByName(qname);
82 } else if (node instanceof RpcDefinition) {
83 switch (qname.getLocalName()) {
85 child = ((RpcDefinition) node).getInput();
88 child = ((RpcDefinition) node).getOutput();
95 throw new IllegalArgumentException(String.format("Schema node %s does not allow children.", node));
101 private static Optional<SchemaNode> tryFindGroupings(final SchemaContext ctx, final QName qname) {
102 return Optional.<SchemaNode> fromNullable(Iterables.find(ctx.getGroupings(), new SchemaNodePredicate(qname), null));
105 private static Optional<SchemaNode> tryFindRpc(final SchemaContext ctx, final QName qname) {
106 return Optional.<SchemaNode>fromNullable(Iterables.find(ctx.getOperations(), new SchemaNodePredicate(qname), null));
109 private static Optional<SchemaNode> tryFindNotification(final SchemaContext ctx, final QName qname) {
110 return Optional.<SchemaNode>fromNullable(Iterables.find(ctx.getNotifications(), new SchemaNodePredicate(qname), null));
114 * Create a new writer with the specified context as its root.
116 * @param context Associated {@link SchemaContext}.
117 * @return A new {@link NormalizedNodeStreamWriter}
119 public static SchemaTracker create(final SchemaContext context) {
120 return create(context, SchemaPath.ROOT);
124 * Create a new writer with the specified context and rooted in the specified schema path
126 * @param context Associated {@link SchemaContext}
127 * @param path schema path
129 * @return A new {@link NormalizedNodeStreamWriter}
131 public static SchemaTracker create(final SchemaContext context, final SchemaPath path) {
132 return new SchemaTracker(context, path);
135 public Object getParent() {
136 if (schemaStack.isEmpty()) {
139 return schemaStack.peek();
142 private SchemaNode getSchema(final PathArgument name) {
143 final Object parent = getParent();
144 SchemaNode schema = null;
145 final QName qname = name.getNodeType();
146 if(parent instanceof DataNodeContainer) {
147 schema = ((DataNodeContainer)parent).getDataChildByName(qname);
149 if(schema == null && parent instanceof GroupingDefinition) {
150 schema = ((GroupingDefinition) parent);
153 if(schema == null && parent instanceof NotificationDefinition) {
154 schema = ((NotificationDefinition) parent);
156 } else if(parent instanceof ChoiceSchemaNode) {
157 schema = findChildInCases((ChoiceSchemaNode) parent, qname);
159 throw new IllegalStateException("Unsupported schema type "+ parent.getClass() +" on stack.");
161 Preconditions.checkArgument(schema != null, "Could not find schema for node %s in %s", qname, parent);
165 private static SchemaNode findChildInCases(final ChoiceSchemaNode parent, final QName qname) {
166 DataSchemaNode schema = null;
167 for(final ChoiceCaseNode caze : parent.getCases()) {
168 final DataSchemaNode potential = caze.getDataChildByName(qname);
169 if(potential != null) {
177 private static SchemaNode findCaseByChild(final ChoiceSchemaNode parent, final QName qname) {
178 DataSchemaNode schema = null;
179 for(final ChoiceCaseNode caze : parent.getCases()) {
180 final DataSchemaNode potential = caze.getDataChildByName(qname);
181 if(potential != null) {
189 public void startList(final PathArgument name) {
190 final SchemaNode schema = getSchema(name);
191 Preconditions.checkArgument(schema instanceof ListSchemaNode, "Node %s is not a list", schema.getPath());
192 schemaStack.push(schema);
195 public void startListItem(final PathArgument name) throws IOException {
196 final Object schema = getParent();
197 Preconditions.checkArgument(schema instanceof ListSchemaNode, "List item is not appropriate");
198 schemaStack.push(schema);
201 public LeafSchemaNode leafNode(final NodeIdentifier name) throws IOException {
202 final SchemaNode schema = getSchema(name);
204 Preconditions.checkArgument(schema instanceof LeafSchemaNode, "Node %s is not a leaf", schema.getPath());
205 return (LeafSchemaNode) schema;
208 public LeafListSchemaNode startLeafSet(final NodeIdentifier name) {
209 final SchemaNode schema = getSchema(name);
211 Preconditions.checkArgument(schema instanceof LeafListSchemaNode, "Node %s is not a leaf-list", schema.getPath());
212 schemaStack.push(schema);
213 return (LeafListSchemaNode)schema;
217 * FIXME: This method is kept for API compatability in stable/beryllium and should be removed in boron.
218 * It has been marked Deprecated to dissuade continued use in the stable/beryllium branch.
221 public LeafListSchemaNode leafSetEntryNode() {
222 final Object parent = getParent();
224 Preconditions.checkArgument(parent instanceof LeafListSchemaNode, "Not currently in a leaf-list");
225 return (LeafListSchemaNode) parent;
228 public LeafListSchemaNode leafSetEntryNode(final QName qname) {
229 final Object parent = getParent();
230 if (parent instanceof LeafListSchemaNode) {
231 return (LeafListSchemaNode) parent;
233 final SchemaNode child = getChild((SchemaNode) parent, qname);
234 Preconditions.checkArgument(child instanceof LeafListSchemaNode,
235 "Node %s is neither a leaf-list nor currently in a leaf-list", child.getPath());
236 return (LeafListSchemaNode) child;
240 public ChoiceSchemaNode startChoiceNode(final NodeIdentifier name) {
241 LOG.debug("Enter choice {}", name);
242 final SchemaNode schema = getSchema(name);
244 Preconditions.checkArgument(schema instanceof ChoiceSchemaNode, "Node %s is not a choice", schema.getPath());
245 schemaStack.push(schema);
246 return (ChoiceSchemaNode)schema;
249 public SchemaNode startContainerNode(final NodeIdentifier name) {
250 LOG.debug("Enter container {}", name);
251 final SchemaNode schema = getSchema(name);
253 boolean isAllowed = schema instanceof ContainerSchemaNode;
254 isAllowed |= schema instanceof NotificationDefinition;
256 Preconditions.checkArgument(isAllowed, "Node %s is not a container nor a notification", schema.getPath());
257 schemaStack.push(schema);
262 public SchemaNode startYangModeledAnyXmlNode(final NodeIdentifier name) {
263 LOG.debug("Enter yang modeled anyXml {}", name);
264 final SchemaNode schema = getSchema(name);
266 Preconditions.checkArgument(schema instanceof YangModeledAnyXmlSchemaNode,
267 "Node %s is not an yang modeled anyXml.", schema.getPath());
269 schemaStack.push(((YangModeledAnyXmlSchemaNode) schema).getSchemaOfAnyXmlData());
274 public AugmentationSchema startAugmentationNode(final AugmentationIdentifier identifier) {
275 LOG.debug("Enter augmentation {}", identifier);
276 Object parent = getParent();
278 Preconditions.checkArgument(parent instanceof AugmentationTarget, "Augmentation not allowed under %s", parent);
279 if(parent instanceof ChoiceSchemaNode) {
280 final QName name = Iterables.get(identifier.getPossibleChildNames(), 0);
281 parent = findCaseByChild((ChoiceSchemaNode) parent, name);
283 Preconditions.checkArgument(parent instanceof DataNodeContainer, "Augmentation allowed only in DataNodeContainer",parent);
284 final AugmentationSchema schema = SchemaUtils.findSchemaForAugment((AugmentationTarget) parent, identifier.getPossibleChildNames());
285 final HashSet<DataSchemaNode> realChildSchemas = new HashSet<>();
286 for(final DataSchemaNode child : schema.getChildNodes()) {
287 realChildSchemas.add(((DataNodeContainer) parent).getDataChildByName(child.getQName()));
289 final AugmentationSchema resolvedSchema = new EffectiveAugmentationSchema(schema, realChildSchemas);
290 schemaStack.push(resolvedSchema);
291 return resolvedSchema;
294 public AnyXmlSchemaNode anyxmlNode(final NodeIdentifier name) {
295 final SchemaNode schema = getSchema(name);
297 Preconditions.checkArgument(schema instanceof AnyXmlSchemaNode, "Node %s is not anyxml", schema.getPath());
298 return (AnyXmlSchemaNode)schema;
301 public Object endNode() {
302 return schemaStack.pop();
305 private static final class SchemaNodePredicate implements Predicate<SchemaNode> {
306 private final QName qname;
308 public SchemaNodePredicate(final QName qname) {
313 public boolean apply(final SchemaNode input) {
314 return input.getQName().equals(qname);