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.codec.gson;
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;
22 import java.util.ArrayDeque;
23 import java.util.Collections;
24 import java.util.Deque;
25 import java.util.HashSet;
27 import org.opendaylight.yangtools.yang.data.api.schema.stream.DataSchemaNodeAwareAdaptor;
28 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
29 import org.opendaylight.yangtools.yang.data.api.schema.stream.SchemaAwareNormalizedNodeStreamWriter;
30 import org.opendaylight.yangtools.yang.data.util.AbstractNodeDataWithSchema;
31 import org.opendaylight.yangtools.yang.data.util.AnyXmlNodeDataWithSchema;
32 import org.opendaylight.yangtools.yang.data.util.CompositeNodeDataWithSchema;
33 import org.opendaylight.yangtools.yang.data.util.LeafListEntryNodeDataWithSchema;
34 import org.opendaylight.yangtools.yang.data.util.LeafListNodeDataWithSchema;
35 import org.opendaylight.yangtools.yang.data.util.LeafNodeDataWithSchema;
36 import org.opendaylight.yangtools.yang.data.util.ListEntryNodeDataWithSchema;
37 import org.opendaylight.yangtools.yang.data.util.ListNodeDataWithSchema;
38 import org.opendaylight.yangtools.yang.data.util.ParserStreamUtils;
39 import org.opendaylight.yangtools.yang.data.util.RpcAsContainer;
40 import org.opendaylight.yangtools.yang.data.util.SimpleNodeDataWithSchema;
41 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
43 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
45 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
46 import org.opendaylight.yangtools.yang.model.api.Module;
47 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
48 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
49 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
50 import org.opendaylight.yangtools.yang.model.api.YangModeledAnyXmlSchemaNode;
53 * This class parses JSON elements from a GSON JsonReader. It disallows multiple elements of the same name unlike the
54 * default GSON JsonParser.
57 public final class JsonParserStream implements Closeable, Flushable {
58 private final Deque<URI> namespaces = new ArrayDeque<>();
59 private final SchemaAwareNormalizedNodeStreamWriter writer;
60 private final JSONCodecFactory codecs;
61 private final SchemaContext schema;
62 private final DataSchemaNode parentNode;
64 private JsonParserStream(final NormalizedNodeStreamWriter writer, final SchemaContext schemaContext,
65 final DataSchemaNode parentNode) {
66 this.schema = Preconditions.checkNotNull(schemaContext);
67 this.writer = DataSchemaNodeAwareAdaptor.forWriter(writer);
68 this.codecs = JSONCodecFactory.create(schemaContext);
69 this.parentNode = parentNode;
72 public static JsonParserStream create(final NormalizedNodeStreamWriter writer, final SchemaContext schemaContext,
73 final SchemaNode parentNode ) {
74 if (parentNode instanceof RpcDefinition) {
75 return new JsonParserStream(writer, schemaContext, new RpcAsContainer((RpcDefinition) parentNode));
77 Preconditions.checkArgument(parentNode instanceof DataSchemaNode, "Instance of DataSchemaNode class awaited.");
78 return new JsonParserStream(writer, schemaContext, (DataSchemaNode) parentNode);
81 public static JsonParserStream create(final NormalizedNodeStreamWriter writer, final SchemaContext schemaContext) {
82 return new JsonParserStream(writer, schemaContext, schemaContext);
85 public JsonParserStream parse(final JsonReader reader) {
86 // code copied from gson's JsonParser and Stream classes
88 final boolean lenient = reader.isLenient();
89 reader.setLenient(true);
90 boolean isEmpty = true;
94 final CompositeNodeDataWithSchema compositeNodeDataWithSchema = new CompositeNodeDataWithSchema(parentNode);
95 read(reader, compositeNodeDataWithSchema);
96 compositeNodeDataWithSchema.write(writer);
99 } catch (final EOFException e) {
103 // The stream ended prematurely so it is likely a syntax error.
104 throw new JsonSyntaxException(e);
105 } catch (final MalformedJsonException | NumberFormatException e) {
106 throw new JsonSyntaxException(e);
107 } catch (final IOException e) {
108 throw new JsonIOException(e);
109 } catch (StackOverflowError | OutOfMemoryError e) {
110 throw new JsonParseException("Failed parsing JSON source: " + reader + " to Json", e);
112 reader.setLenient(lenient);
116 private void setValue(final AbstractNodeDataWithSchema parent, final String value) {
117 Preconditions.checkArgument(parent instanceof SimpleNodeDataWithSchema, "Node %s is not a simple type",
118 parent.getSchema().getQName());
119 final SimpleNodeDataWithSchema parentSimpleNode = (SimpleNodeDataWithSchema) parent;
120 Preconditions.checkArgument(parentSimpleNode.getValue() == null, "Node '%s' has already set its value to '%s'",
121 parentSimpleNode.getSchema().getQName(), parentSimpleNode.getValue());
123 final Object translatedValue = translateValueByType(value, parentSimpleNode.getSchema());
124 parentSimpleNode.setValue(translatedValue);
127 public void read(final JsonReader in, AbstractNodeDataWithSchema parent) throws IOException {
131 setValue(parent, in.nextString());
134 setValue(parent, Boolean.toString(in.nextBoolean()));
138 setValue(parent, null);
142 while (in.hasNext()) {
143 if (parent instanceof LeafNodeDataWithSchema) {
146 final AbstractNodeDataWithSchema newChild = newArrayEntry(parent);
153 final Set<String> namesakes = new HashSet<>();
156 * This allows parsing of incorrectly /as showcased/
157 * in testconf nesting of list items - eg.
158 * lists with one value are sometimes serialized
159 * without wrapping array.
162 if (isArray(parent)) {
163 parent = newArrayEntry(parent);
165 while (in.hasNext()) {
166 final String jsonElementName = in.nextName();
167 DataSchemaNode parentSchema = parent.getSchema();
168 if (parentSchema instanceof YangModeledAnyXmlSchemaNode) {
169 parentSchema = ((YangModeledAnyXmlSchemaNode) parentSchema).getSchemaOfAnyXmlData();
171 final NamespaceAndName namespaceAndName = resolveNamespace(jsonElementName, parentSchema);
172 final String localName = namespaceAndName.getName();
173 addNamespace(namespaceAndName.getUri());
174 if (namesakes.contains(jsonElementName)) {
175 throw new JsonSyntaxException("Duplicate name " + jsonElementName + " in JSON input.");
177 namesakes.add(jsonElementName);
179 final Deque<DataSchemaNode> childDataSchemaNodes =
180 ParserStreamUtils.findSchemaNodeByNameAndNamespace(parentSchema, localName, getCurrentNamespace());
181 if (childDataSchemaNodes.isEmpty()) {
182 throw new IllegalStateException("Schema for node with name " + localName + " and namespace "
183 + getCurrentNamespace() + " doesn't exist.");
186 final AbstractNodeDataWithSchema newChild = ((CompositeNodeDataWithSchema) parent).addChild(childDataSchemaNodes);
188 * FIXME:anyxml data shouldn't be skipped but should be loaded somehow.
189 * will be able to load anyxml which conforms to YANG data using these
190 * parser, for other anyxml will be harder.
192 if (newChild instanceof AnyXmlNodeDataWithSchema) {
209 private static boolean isArray(final AbstractNodeDataWithSchema parent) {
210 return parent instanceof ListNodeDataWithSchema || parent instanceof LeafListNodeDataWithSchema;
213 private static AbstractNodeDataWithSchema newArrayEntry(final AbstractNodeDataWithSchema parent) {
214 AbstractNodeDataWithSchema newChild;
215 if (parent instanceof ListNodeDataWithSchema) {
216 newChild = new ListEntryNodeDataWithSchema(parent.getSchema());
217 } else if (parent instanceof LeafListNodeDataWithSchema) {
218 newChild = new LeafListEntryNodeDataWithSchema(parent.getSchema());
220 throw new IllegalStateException("Found an unexpected array nested under "+ parent.getSchema().getQName());
222 ((CompositeNodeDataWithSchema) parent).addChild(newChild);
226 private Object translateValueByType(final String value, final DataSchemaNode node) {
227 if (node instanceof AnyXmlSchemaNode) {
229 * FIXME: Figure out some YANG extension dispatch, which will
230 * reuse JSON parsing or XML parsing - anyxml is not well-defined in
235 return codecs.codecFor(node).deserialize(value);
238 private void removeNamespace() {
242 private void addNamespace(final URI namespace) {
243 namespaces.push(namespace);
246 private NamespaceAndName resolveNamespace(final String childName, final DataSchemaNode dataSchemaNode) {
247 final int lastIndexOfColon = childName.lastIndexOf(':');
248 String moduleNamePart = null;
249 String nodeNamePart = null;
250 URI namespace = null;
251 if (lastIndexOfColon != -1) {
252 moduleNamePart = childName.substring(0, lastIndexOfColon);
253 nodeNamePart = childName.substring(lastIndexOfColon + 1);
255 final Module m = schema.findModuleByName(moduleNamePart, null);
256 namespace = m == null ? null : m.getNamespace();
258 nodeNamePart = childName;
261 if (namespace == null) {
262 Set<URI> potentialUris = Collections.emptySet();
263 potentialUris = resolveAllPotentialNamespaces(nodeNamePart, dataSchemaNode);
264 if (potentialUris.contains(getCurrentNamespace())) {
265 namespace = getCurrentNamespace();
266 } else if (potentialUris.size() == 1) {
267 namespace = potentialUris.iterator().next();
268 } else if (potentialUris.size() > 1) {
269 throw new IllegalStateException("Choose suitable module name for element "+nodeNamePart+":"+toModuleNames(potentialUris));
270 } else if (potentialUris.isEmpty()) {
271 throw new IllegalStateException("Schema node with name "+nodeNamePart+" wasn't found under "+dataSchemaNode.getQName()+".");
275 return new NamespaceAndName(nodeNamePart, namespace);
278 private String toModuleNames(final Set<URI> potentialUris) {
279 final StringBuilder builder = new StringBuilder();
280 for (final URI potentialUri : potentialUris) {
281 builder.append("\n");
282 //FIXME how to get information about revision from JSON input? currently first available is used.
283 builder.append(schema.findModuleByNamespace(potentialUri).iterator().next().getName());
285 return builder.toString();
288 private Set<URI> resolveAllPotentialNamespaces(final String elementName, final DataSchemaNode dataSchemaNode) {
289 final Set<URI> potentialUris = new HashSet<>();
290 final Set<ChoiceSchemaNode> choices = new HashSet<>();
291 if (dataSchemaNode instanceof DataNodeContainer) {
292 for (final DataSchemaNode childSchemaNode : ((DataNodeContainer) dataSchemaNode).getChildNodes()) {
293 if (childSchemaNode instanceof ChoiceSchemaNode) {
294 choices.add((ChoiceSchemaNode)childSchemaNode);
295 } else if (childSchemaNode.getQName().getLocalName().equals(elementName)) {
296 potentialUris.add(childSchemaNode.getQName().getNamespace());
300 for (final ChoiceSchemaNode choiceNode : choices) {
301 for (final ChoiceCaseNode concreteCase : choiceNode.getCases()) {
302 potentialUris.addAll(resolveAllPotentialNamespaces(elementName, concreteCase));
306 return potentialUris;
309 private URI getCurrentNamespace() {
310 return namespaces.peek();
315 private static class NamespaceAndName {
316 private final URI uri;
317 private final String name;
319 public NamespaceAndName(final String name, final URI uri) {
324 public String getName() {
328 public URI getUri() {
334 public void flush() throws IOException {
339 public void close() throws IOException {