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()) {
61 if(current instanceof DataNodeContainer) {
62 child = ((DataNodeContainer) current).getDataChildByName(qname);
64 if (child == null && current instanceof SchemaContext) {
65 child = tryFindGroupings((SchemaContext) current, qname).orNull();
68 if(child == null && current instanceof SchemaContext) {
69 child = tryFindNotification((SchemaContext) current, qname)
70 .or(tryFindRpc(((SchemaContext) current), qname)).orNull();
72 } else if (current instanceof ChoiceSchemaNode) {
73 child = ((ChoiceSchemaNode) current).getCaseNodeByName(qname);
74 } else if (current instanceof RpcDefinition) {
75 switch (qname.getLocalName()) {
77 child = ((RpcDefinition) current).getInput();
80 child = ((RpcDefinition) current).getOutput();
87 throw new IllegalArgumentException(String.format("Schema node %s does not allow children.", current));
91 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);
92 root = (DataNodeContainer) current;
95 private static Optional<SchemaNode> tryFindGroupings(final SchemaContext ctx, final QName qname) {
96 return Optional.<SchemaNode> fromNullable(Iterables.find(ctx.getGroupings(), new SchemaNodePredicate(qname), null));
99 private static Optional<SchemaNode> tryFindRpc(final SchemaContext ctx, final QName qname) {
100 return Optional.<SchemaNode>fromNullable(Iterables.find(ctx.getOperations(), new SchemaNodePredicate(qname), null));
103 private static Optional<SchemaNode> tryFindNotification(final SchemaContext ctx, final QName qname) {
104 return Optional.<SchemaNode>fromNullable(Iterables.find(ctx.getNotifications(), new SchemaNodePredicate(qname), null));
108 * Create a new writer with the specified context as its root.
110 * @param context Associated {@link SchemaContext}.
111 * @return A new {@link NormalizedNodeStreamWriter}
113 public static SchemaTracker create(final SchemaContext context) {
114 return create(context, SchemaPath.ROOT);
118 * Create a new writer with the specified context and rooted in the specified schema path
120 * @param context Associated {@link SchemaContext}
121 * @param path schema path
123 * @return A new {@link NormalizedNodeStreamWriter}
125 public static SchemaTracker create(final SchemaContext context, final SchemaPath path) {
126 return new SchemaTracker(context, path);
129 public Object getParent() {
130 if (schemaStack.isEmpty()) {
133 return schemaStack.peek();
136 private SchemaNode getSchema(final PathArgument name) {
137 final Object parent = getParent();
138 SchemaNode schema = null;
139 final QName qname = name.getNodeType();
140 if(parent instanceof DataNodeContainer) {
141 schema = ((DataNodeContainer)parent).getDataChildByName(qname);
143 if(schema == null && parent instanceof GroupingDefinition) {
144 schema = ((GroupingDefinition) parent);
147 if(schema == null && parent instanceof NotificationDefinition) {
148 schema = ((NotificationDefinition) parent);
150 } else if(parent instanceof ChoiceSchemaNode) {
151 schema = findChildInCases((ChoiceSchemaNode) parent, qname);
153 throw new IllegalStateException("Unsupported schema type "+ parent.getClass() +" on stack.");
155 Preconditions.checkArgument(schema != null, "Could not find schema for node %s in %s", qname, parent);
159 private static SchemaNode findChildInCases(final ChoiceSchemaNode parent, final QName qname) {
160 DataSchemaNode schema = null;
161 for(final ChoiceCaseNode caze : parent.getCases()) {
162 final DataSchemaNode potential = caze.getDataChildByName(qname);
163 if(potential != null) {
171 private static SchemaNode findCaseByChild(final ChoiceSchemaNode parent, final QName qname) {
172 DataSchemaNode schema = null;
173 for(final ChoiceCaseNode caze : parent.getCases()) {
174 final DataSchemaNode potential = caze.getDataChildByName(qname);
175 if(potential != null) {
183 public void startList(final PathArgument name) {
184 final SchemaNode schema = getSchema(name);
185 Preconditions.checkArgument(schema instanceof ListSchemaNode, "Node %s is not a list", schema.getPath());
186 schemaStack.push(schema);
189 public void startListItem(final PathArgument name) throws IOException {
190 final Object schema = getParent();
191 Preconditions.checkArgument(schema instanceof ListSchemaNode, "List item is not appropriate");
192 schemaStack.push(schema);
195 public LeafSchemaNode leafNode(final NodeIdentifier name) throws IOException {
196 final SchemaNode schema = getSchema(name);
198 Preconditions.checkArgument(schema instanceof LeafSchemaNode, "Node %s is not a leaf", schema.getPath());
199 return (LeafSchemaNode) schema;
202 public LeafListSchemaNode startLeafSet(final NodeIdentifier name) {
203 final SchemaNode schema = getSchema(name);
205 Preconditions.checkArgument(schema instanceof LeafListSchemaNode, "Node %s is not a leaf-list", schema.getPath());
206 schemaStack.push(schema);
207 return (LeafListSchemaNode)schema;
210 public LeafListSchemaNode leafSetEntryNode() {
211 final Object parent = getParent();
213 Preconditions.checkArgument(parent instanceof LeafListSchemaNode, "Not currently in a leaf-list");
214 return (LeafListSchemaNode) parent;
217 public ChoiceSchemaNode startChoiceNode(final NodeIdentifier name) {
218 LOG.debug("Enter choice {}", name);
219 final SchemaNode schema = getSchema(name);
221 Preconditions.checkArgument(schema instanceof ChoiceSchemaNode, "Node %s is not a choice", schema.getPath());
222 schemaStack.push(schema);
223 return (ChoiceSchemaNode)schema;
226 public SchemaNode startContainerNode(final NodeIdentifier name) {
227 LOG.debug("Enter container {}", name);
228 final SchemaNode schema = getSchema(name);
230 boolean isAllowed = schema instanceof ContainerSchemaNode;
231 isAllowed |= schema instanceof NotificationDefinition;
233 Preconditions.checkArgument(isAllowed, "Node %s is not a container nor a notification", schema.getPath());
234 schemaStack.push(schema);
239 public SchemaNode startYangModeledAnyXmlNode(final NodeIdentifier name) {
240 LOG.debug("Enter yang modeled anyXml {}", name);
241 final SchemaNode schema = getSchema(name);
243 Preconditions.checkArgument(schema instanceof YangModeledAnyXmlSchemaNode,
244 "Node %s is not an yang modeled anyXml.", schema.getPath());
246 schemaStack.push(((YangModeledAnyXmlSchemaNode) schema).getSchemaOfAnyXmlData());
251 public AugmentationSchema startAugmentationNode(final AugmentationIdentifier identifier) {
252 LOG.debug("Enter augmentation {}", identifier);
253 Object parent = getParent();
255 Preconditions.checkArgument(parent instanceof AugmentationTarget, "Augmentation not allowed under %s", parent);
256 if(parent instanceof ChoiceSchemaNode) {
257 final QName name = Iterables.get(identifier.getPossibleChildNames(), 0);
258 parent = findCaseByChild((ChoiceSchemaNode) parent, name);
260 Preconditions.checkArgument(parent instanceof DataNodeContainer, "Augmentation allowed only in DataNodeContainer",parent);
261 final AugmentationSchema schema = SchemaUtils.findSchemaForAugment((AugmentationTarget) parent, identifier.getPossibleChildNames());
262 final HashSet<DataSchemaNode> realChildSchemas = new HashSet<>();
263 for(final DataSchemaNode child : schema.getChildNodes()) {
264 realChildSchemas.add(((DataNodeContainer) parent).getDataChildByName(child.getQName()));
266 final AugmentationSchema resolvedSchema = new EffectiveAugmentationSchema(schema, realChildSchemas);
267 schemaStack.push(resolvedSchema);
268 return resolvedSchema;
271 public AnyXmlSchemaNode anyxmlNode(final NodeIdentifier name) {
272 final SchemaNode schema = getSchema(name);
274 Preconditions.checkArgument(schema instanceof AnyXmlSchemaNode, "Node %s is not anyxml", schema.getPath());
275 return (AnyXmlSchemaNode)schema;
278 public Object endNode() {
279 return schemaStack.pop();
282 private static final class SchemaNodePredicate implements Predicate<SchemaNode> {
283 private final QName qname;
285 public SchemaNodePredicate(final QName qname) {
290 public boolean apply(final SchemaNode input) {
291 return input.getQName().equals(qname);