Merge "Bug 1442: Fixed SchemaTracker resolving case children."
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / codec / SchemaTracker.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.codec;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import java.io.IOException;
13 import java.util.ArrayDeque;
14 import java.util.Deque;
15 import java.util.HashSet;
16 import javax.xml.stream.XMLStreamWriter;
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.data.impl.schema.transform.base.AugmentationSchemaProxy;
24 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
26 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
27 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
28 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
29 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
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.SchemaContext;
36 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * Utility class for tracking the underlying state of the underlying
43  * schema node.
44  */
45 @Beta
46 public final class SchemaTracker {
47     private static final Logger LOG = LoggerFactory.getLogger(SchemaTracker.class);
48     private final Deque<Object> schemaStack = new ArrayDeque<>();
49     private final DataNodeContainer root;
50
51     private SchemaTracker(final SchemaContext context, final SchemaPath path) {
52         DataSchemaNode current = Preconditions.checkNotNull(context);
53         for (QName qname : path.getPathFromRoot()) {
54             final DataSchemaNode child;
55             if(current instanceof DataNodeContainer) {
56                 child = ((DataNodeContainer) current).getDataChildByName(qname);
57             } else if (current instanceof ChoiceNode) {
58                 child = ((ChoiceNode) current).getCaseNodeByName(qname);
59             } else {
60                 throw new IllegalArgumentException(String.format("Schema node %s does not allow children.",current));
61             }
62             current = child;
63         }
64         Preconditions.checkArgument(current instanceof DataNodeContainer,"Schema path must point to container or list. Supplied path %s pointed to: %s",path,current);
65         this.root = (DataNodeContainer) current;
66     }
67
68     /**
69      * Create a new writer with the specified context as its root.
70      *
71      * @param writer Output {@link XMLStreamWriter}
72      * @param context Associated {@link SchemaContext}.
73      * @return A new {@link NormalizedNodeStreamWriter}
74      */
75     public static SchemaTracker create(final SchemaContext context) {
76         return create(context, SchemaPath.ROOT);
77     }
78
79     /**
80      * Create a new writer with the specified context and rooted in the specified schema path
81      *
82      * @param writer Output {@link XMLStreamWriter}
83      * @param context Associated {@link SchemaContext}.
84      *
85      * @return A new {@link NormalizedNodeStreamWriter}
86      */
87     public static SchemaTracker create(final SchemaContext context, final SchemaPath path) {
88         return new SchemaTracker(context, path);
89     }
90
91     public Object getParent() {
92         if (schemaStack.isEmpty()) {
93             return root;
94         }
95         return schemaStack.peek();
96     }
97
98     private SchemaNode getSchema(final PathArgument name) {
99         final Object parent = getParent();
100         SchemaNode schema = null;
101         final QName qname = name.getNodeType();
102         if(parent instanceof DataNodeContainer) {
103             schema = ((DataNodeContainer)parent).getDataChildByName(qname);
104
105         } else if(parent instanceof ChoiceNode) {
106             for(ChoiceCaseNode caze : ((ChoiceNode) parent).getCases()) {
107                 DataSchemaNode potential = caze.getDataChildByName(qname);
108                 if(potential != null) {
109                     schema = potential;
110                     break;
111                 }
112             }
113         } else {
114             throw new IllegalStateException("Unsupported schema type "+ parent.getClass() +" on stack.");
115         }
116         Preconditions.checkArgument(schema != null, "Could not find schema for node %s in %s", qname, parent);
117         return schema;
118     }
119
120     public void startList(final NodeIdentifier name) {
121         final SchemaNode schema = getSchema(name);
122         Preconditions.checkArgument(schema instanceof ListSchemaNode, "Node %s is not a list", schema.getPath());
123         schemaStack.push(schema);
124     }
125
126     public void startListItem(final PathArgument name) throws IOException {
127         final Object schema = getParent();
128         Preconditions.checkArgument(schema instanceof ListSchemaNode, "List item is not appropriate");
129         schemaStack.push(schema);
130     }
131
132     public LeafSchemaNode leafNode(final NodeIdentifier name) throws IOException {
133         final SchemaNode schema = getSchema(name);
134
135         Preconditions.checkArgument(schema instanceof LeafSchemaNode, "Node %s is not a leaf", schema.getPath());
136         return (LeafSchemaNode) schema;
137     }
138
139     public SchemaNode startLeafSet(final NodeIdentifier name) {
140         final SchemaNode schema = getSchema(name);
141
142         Preconditions.checkArgument(schema instanceof LeafListSchemaNode, "Node %s is not a leaf-list", schema.getPath());
143         schemaStack.push(schema);
144         return schema;
145     }
146
147     public LeafListSchemaNode leafSetEntryNode() {
148         final Object parent = getParent();
149
150         Preconditions.checkArgument(parent instanceof LeafListSchemaNode, "Not currently in a leaf-list");
151         return (LeafListSchemaNode) parent;
152     }
153
154     public SchemaNode startChoiceNode(final NodeIdentifier name) {
155         LOG.debug("Enter choice {}", name);
156         final SchemaNode schema = getSchema(name);
157
158         Preconditions.checkArgument(schema instanceof ChoiceNode, "Node %s is not a choice", schema.getPath());
159         schemaStack.push(schema);
160         return schema;
161     }
162
163     public SchemaNode startContainerNode(final NodeIdentifier name) {
164         LOG.debug("Enter container {}", name);
165         final SchemaNode schema = getSchema(name);
166
167         Preconditions.checkArgument(schema instanceof ContainerSchemaNode, "Node %s is not a container", schema.getPath());
168         schemaStack.push(schema);
169         return schema;
170     }
171
172     public AugmentationSchema startAugmentationNode(final AugmentationIdentifier identifier) {
173         LOG.debug("Enter augmentation {}", identifier);
174         final Object parent = getParent();
175
176         Preconditions.checkArgument(parent instanceof AugmentationTarget, "Augmentation not allowed under %s", parent);
177         Preconditions.checkArgument(parent instanceof DataNodeContainer, "Augmentation allowed only in DataNodeContainer",parent);
178         final AugmentationSchema schema = SchemaUtils.findSchemaForAugment((AugmentationTarget) parent, identifier.getPossibleChildNames());
179         HashSet<DataSchemaNode> realChildSchemas = new HashSet<>();
180         for(DataSchemaNode child : schema.getChildNodes()) {
181             realChildSchemas.add(((DataNodeContainer) parent).getDataChildByName(child.getQName()));
182         }
183         AugmentationSchema resolvedSchema = new AugmentationSchemaProxy(schema, realChildSchemas);
184         schemaStack.push(resolvedSchema);
185         return resolvedSchema;
186     }
187
188     public AnyXmlSchemaNode anyxmlNode(final NodeIdentifier name) {
189         final SchemaNode schema = getSchema(name);
190
191         Preconditions.checkArgument(schema instanceof AnyXmlSchemaNode, "Node %s is not anyxml", schema.getPath());
192         return (AnyXmlSchemaNode)schema;
193     }
194
195     public Object endNode() {
196         return schemaStack.pop();
197     }
198 }