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