Merge "BUG-2829: make static QName field hold a cached reference"
[yangtools.git] / yang / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / JsonParserStream.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.codec.gson;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import com.google.gson.JsonIOException;
13 import com.google.gson.JsonParseException;
14 import com.google.gson.JsonSyntaxException;
15 import com.google.gson.stream.JsonReader;
16 import com.google.gson.stream.MalformedJsonException;
17 import java.io.Closeable;
18 import java.io.EOFException;
19 import java.io.Flushable;
20 import java.io.IOException;
21 import java.net.URI;
22 import java.util.ArrayDeque;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.Deque;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Set;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
32 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
34 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
36 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.Module;
40 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
44
45 /**
46  * This class parses JSON elements from a GSON JsonReader. It disallows multiple elements of the same name unlike the
47  * default GSON JsonParser.
48  */
49 @Beta
50 public final class JsonParserStream implements Closeable, Flushable {
51     private final Deque<URI> namespaces = new ArrayDeque<>();
52     private final NormalizedNodeStreamWriter writer;
53     private final JSONCodecFactory codecs;
54     private final SchemaContext schema;
55     private final DataSchemaNode parentNode;
56
57     private JsonParserStream(final NormalizedNodeStreamWriter writer, final SchemaContext schemaContext, final DataSchemaNode parentNode) {
58         this.schema = Preconditions.checkNotNull(schemaContext);
59         this.writer = Preconditions.checkNotNull(writer);
60         this.codecs = JSONCodecFactory.create(schemaContext);
61         this.parentNode = parentNode;
62     }
63
64     public static JsonParserStream create(final NormalizedNodeStreamWriter writer, final SchemaContext schemaContext, final SchemaNode parentNode ) {
65         if(parentNode instanceof RpcDefinition) {
66             return new JsonParserStream(writer, schemaContext, new RpcAsContainer((RpcDefinition) parentNode));
67         }
68         Preconditions.checkArgument(parentNode instanceof DataSchemaNode, "Instance of DataSchemaNode class awaited.");
69         return new JsonParserStream(writer, schemaContext, (DataSchemaNode) parentNode);
70     }
71
72     public static JsonParserStream create(final NormalizedNodeStreamWriter writer, final SchemaContext schemaContext) {
73         return new JsonParserStream(writer, schemaContext, schemaContext);
74     }
75
76     public JsonParserStream parse(final JsonReader reader) throws JsonIOException, JsonSyntaxException {
77         // code copied from gson's JsonParser and Stream classes
78
79         final boolean lenient = reader.isLenient();
80         reader.setLenient(true);
81         boolean isEmpty = true;
82         try {
83             reader.peek();
84             isEmpty = false;
85             final CompositeNodeDataWithSchema compositeNodeDataWithSchema = new CompositeNodeDataWithSchema(parentNode);
86             read(reader, compositeNodeDataWithSchema);
87             compositeNodeDataWithSchema.write(writer);
88
89             return this;
90             // return read(reader);
91         } catch (final EOFException e) {
92             if (isEmpty) {
93                 return this;
94                 // return JsonNull.INSTANCE;
95             }
96             // The stream ended prematurely so it is likely a syntax error.
97             throw new JsonSyntaxException(e);
98         } catch (final MalformedJsonException e) {
99             throw new JsonSyntaxException(e);
100         } catch (final IOException e) {
101             throw new JsonIOException(e);
102         } catch (final NumberFormatException e) {
103             throw new JsonSyntaxException(e);
104         } catch (StackOverflowError | OutOfMemoryError e) {
105             throw new JsonParseException("Failed parsing JSON source: " + reader + " to Json", e);
106         } finally {
107             reader.setLenient(lenient);
108         }
109     }
110
111     private final void setValue(final AbstractNodeDataWithSchema parent, final String value) {
112         Preconditions.checkArgument(parent instanceof SimpleNodeDataWithSchema, "Node %s is not a simple type", parent);
113
114         final Object translatedValue = translateValueByType(value, parent.getSchema());
115         ((SimpleNodeDataWithSchema) parent).setValue(translatedValue);
116     }
117
118     public void read(final JsonReader in, AbstractNodeDataWithSchema parent) throws IOException {
119         switch (in.peek()) {
120         case STRING:
121         case NUMBER:
122             setValue(parent, in.nextString());
123             break;
124         case BOOLEAN:
125             setValue(parent, Boolean.toString(in.nextBoolean()));
126             break;
127         case NULL:
128             in.nextNull();
129             setValue(parent, null);
130             break;
131         case BEGIN_ARRAY:
132             in.beginArray();
133             while (in.hasNext()) {
134                 final AbstractNodeDataWithSchema newChild = newArrayEntry(parent);
135                 read(in, newChild);
136             }
137             in.endArray();
138             return;
139         case BEGIN_OBJECT:
140             final Set<String> namesakes = new HashSet<>();
141             in.beginObject();
142             /*
143              * This allows parsing of incorrectly /as showcased/
144              * in testconf nesting of list items - eg.
145              * lists with one value are sometimes serialized
146              * without wrapping array.
147              *
148              */
149             if(isArray(parent)) {
150                 parent = newArrayEntry(parent);
151             }
152             while (in.hasNext()) {
153                 final String jsonElementName = in.nextName();
154                 final NamespaceAndName namespaceAndName = resolveNamespace(jsonElementName, parent.getSchema());
155                 final String localName = namespaceAndName.getName();
156                 addNamespace(namespaceAndName.getUri());
157                 if (namesakes.contains(jsonElementName)) {
158                     throw new JsonSyntaxException("Duplicate name " + jsonElementName + " in JSON input.");
159                 }
160                 namesakes.add(jsonElementName);
161                 final Deque<DataSchemaNode> childDataSchemaNodes = findSchemaNodeByNameAndNamespace(parent.getSchema(),
162                         localName, getCurrentNamespace());
163                 if (childDataSchemaNodes.isEmpty()) {
164                     throw new IllegalStateException("Schema for node with name " + localName + " and namespace "
165                             + getCurrentNamespace() + " doesn't exist.");
166                 }
167
168                 final AbstractNodeDataWithSchema newChild = ((CompositeNodeDataWithSchema) parent).addChild(childDataSchemaNodes);
169                 /*
170                  * FIXME:anyxml data shouldn't be skipped but should be loaded somehow.
171                  * will be able to load anyxml which conforms to YANG data using these
172                  * parser, for other anyxml will be harder.
173                  */
174                 if (newChild instanceof AnyXmlNodeDataWithSchema) {
175                     in.skipValue();
176                 } else {
177                     read(in, newChild);
178                 }
179                 removeNamespace();
180             }
181             in.endObject();
182             return;
183         case END_DOCUMENT:
184         case NAME:
185         case END_OBJECT:
186         case END_ARRAY:
187             break;
188         }
189     }
190
191     private boolean isArray(final AbstractNodeDataWithSchema parent) {
192         return parent instanceof ListNodeDataWithSchema || parent instanceof ListNodeDataWithSchema;
193     }
194
195     private AbstractNodeDataWithSchema newArrayEntry(final AbstractNodeDataWithSchema parent) {
196         AbstractNodeDataWithSchema newChild;
197         if (parent instanceof ListNodeDataWithSchema) {
198             newChild = new ListEntryNodeDataWithSchema(parent.getSchema());
199         } else if (parent instanceof LeafListNodeDataWithSchema) {
200             newChild = new LeafListEntryNodeDataWithSchema(parent.getSchema());
201         } else {
202             throw new IllegalStateException("Incorrec nesting caused by parser.");
203         }
204         ((CompositeNodeDataWithSchema) parent).addChild(newChild);
205         return newChild;
206     }
207
208     private Object translateValueByType(final String value, final DataSchemaNode node) {
209         final TypeDefinition<? extends Object> typeDefinition = typeDefinition(node);
210         if (typeDefinition == null) {
211             return value;
212         }
213
214         return codecs.codecFor(typeDefinition).deserialize(value);
215     }
216
217     private static TypeDefinition<? extends Object> typeDefinition(final DataSchemaNode node) {
218         TypeDefinition<?> baseType = null;
219         if (node instanceof LeafListSchemaNode) {
220             baseType = ((LeafListSchemaNode) node).getType();
221         } else if (node instanceof LeafSchemaNode) {
222             baseType = ((LeafSchemaNode) node).getType();
223         } else if (node instanceof AnyXmlSchemaNode) {
224             return null;
225         } else {
226             throw new IllegalArgumentException("Unhandled parameter types: " + Arrays.<Object> asList(node).toString());
227         }
228
229         if (baseType != null) {
230             while (baseType.getBaseType() != null) {
231                 baseType = baseType.getBaseType();
232             }
233         }
234         return baseType;
235     }
236
237     private void removeNamespace() {
238         namespaces.pop();
239     }
240
241     private void addNamespace(final URI namespace) {
242         namespaces.push(namespace);
243     }
244
245     private NamespaceAndName resolveNamespace(final String childName, final DataSchemaNode dataSchemaNode) {
246         final int lastIndexOfColon = childName.lastIndexOf(':');
247         String moduleNamePart = null;
248         String nodeNamePart = null;
249         URI namespace = null;
250         if (lastIndexOfColon != -1) {
251             moduleNamePart = childName.substring(0, lastIndexOfColon);
252             nodeNamePart = childName.substring(lastIndexOfColon + 1);
253
254             final Module m = schema.findModuleByName(moduleNamePart, null);
255             namespace = m == null ? null : m.getNamespace();
256         } else {
257             nodeNamePart = childName;
258         }
259
260         if (namespace == null) {
261             Set<URI> potentialUris = Collections.emptySet();
262             potentialUris = resolveAllPotentialNamespaces(nodeNamePart, dataSchemaNode);
263             if (potentialUris.contains(getCurrentNamespace())) {
264                 namespace = getCurrentNamespace();
265             } else if (potentialUris.size() == 1) {
266                 namespace = potentialUris.iterator().next();
267             } else if (potentialUris.size() > 1) {
268                 throw new IllegalStateException("Choose suitable module name for element "+nodeNamePart+":"+toModuleNames(potentialUris));
269             } else if (potentialUris.isEmpty()) {
270                 throw new IllegalStateException("Schema node with name "+nodeNamePart+" wasn't found.");
271             }
272         }
273
274         return new NamespaceAndName(nodeNamePart, namespace);
275     }
276
277     private String toModuleNames(final Set<URI> potentialUris) {
278         final StringBuilder builder = new StringBuilder();
279         for (final URI potentialUri : potentialUris) {
280             builder.append("\n");
281             //FIXME how to get information about revision from JSON input? currently first available is used.
282             builder.append(schema.findModuleByNamespace(potentialUri).iterator().next().getName());
283         }
284         return builder.toString();
285     }
286
287     private Set<URI> resolveAllPotentialNamespaces(final String elementName, final DataSchemaNode dataSchemaNode) {
288         final Set<URI> potentialUris = new HashSet<>();
289         final Set<ChoiceSchemaNode> choices = new HashSet<>();
290         if (dataSchemaNode instanceof DataNodeContainer) {
291             for (final DataSchemaNode childSchemaNode : ((DataNodeContainer) dataSchemaNode).getChildNodes()) {
292                 if (childSchemaNode instanceof ChoiceSchemaNode) {
293                     choices.add((ChoiceSchemaNode)childSchemaNode);
294                 } else if (childSchemaNode.getQName().getLocalName().equals(elementName)) {
295                     potentialUris.add(childSchemaNode.getQName().getNamespace());
296                 }
297             }
298
299             for (final ChoiceSchemaNode choiceNode : choices) {
300                 for (final ChoiceCaseNode concreteCase : choiceNode.getCases()) {
301                     potentialUris.addAll(resolveAllPotentialNamespaces(elementName, concreteCase));
302                 }
303             }
304         }
305         return potentialUris;
306     }
307
308     private URI getCurrentNamespace() {
309         return namespaces.peek();
310     }
311
312     /**
313      * Returns stack of schema nodes via which it was necessary to pass to get schema node with specified
314      * {@code childName} and {@code namespace}
315      *
316      * @param dataSchemaNode
317      * @param childName
318      * @param namespace
319      * @return stack of schema nodes via which it was passed through. If found schema node is direct child then stack
320      *         contains only one node. If it is found under choice and case then stack should contains 2*n+1 element
321      *         (where n is number of choices through it was passed)
322      */
323     private Deque<DataSchemaNode> findSchemaNodeByNameAndNamespace(final DataSchemaNode dataSchemaNode,
324             final String childName, final URI namespace) {
325         final Deque<DataSchemaNode> result = new ArrayDeque<>();
326         final List<ChoiceSchemaNode> childChoices = new ArrayList<>();
327         if (dataSchemaNode instanceof DataNodeContainer) {
328             for (final DataSchemaNode childNode : ((DataNodeContainer) dataSchemaNode).getChildNodes()) {
329                 if (childNode instanceof ChoiceSchemaNode) {
330                     childChoices.add((ChoiceSchemaNode) childNode);
331                 } else {
332                     final QName childQName = childNode.getQName();
333                     if (childQName.getLocalName().equals(childName) && childQName.getNamespace().equals(namespace)) {
334                         result.push(childNode);
335                         return result;
336                     }
337                 }
338             }
339         }
340         // try to find data schema node in choice (looking for first match)
341         for (final ChoiceSchemaNode choiceNode : childChoices) {
342             for (final ChoiceCaseNode concreteCase : choiceNode.getCases()) {
343                 final Deque<DataSchemaNode> resultFromRecursion = findSchemaNodeByNameAndNamespace(concreteCase, childName,
344                         namespace);
345                 if (!resultFromRecursion.isEmpty()) {
346                     resultFromRecursion.push(concreteCase);
347                     resultFromRecursion.push(choiceNode);
348                     return resultFromRecursion;
349                 }
350             }
351         }
352         return result;
353     }
354
355     private static class NamespaceAndName {
356         private final URI uri;
357         private final String name;
358
359         public NamespaceAndName(final String name, final URI uri) {
360             this.name = name;
361             this.uri = uri;
362         }
363
364         public String getName() {
365             return name;
366         }
367
368         public URI getUri() {
369             return uri;
370         }
371     }
372
373     @Override
374     public void flush() throws IOException {
375         writer.flush();
376     }
377
378     @Override
379     public void close() throws IOException {
380         writer.flush();
381         writer.close();
382     }
383 }