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