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.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;
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.ChoiceNode;
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;
46 * This class parses JSON elements from a GSON JsonReader. It disallows multiple elements of the same name unlike the
47 * default GSON JsonParser.
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;
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;
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));
68 Preconditions.checkArgument(parentNode instanceof DataSchemaNode, "Instance of DataSchemaNode class awaited.");
69 return new JsonParserStream(writer, schemaContext, (DataSchemaNode) parentNode);
72 public static JsonParserStream create(final NormalizedNodeStreamWriter writer, final SchemaContext schemaContext) {
73 return new JsonParserStream(writer, schemaContext, schemaContext);
76 public JsonParserStream parse(final JsonReader reader) throws JsonIOException, JsonSyntaxException {
77 // code copied from gson's JsonParser and Stream classes
79 final boolean lenient = reader.isLenient();
80 reader.setLenient(true);
81 boolean isEmpty = true;
85 final CompositeNodeDataWithSchema compositeNodeDataWithSchema = new CompositeNodeDataWithSchema(parentNode);
86 read(reader, compositeNodeDataWithSchema);
87 compositeNodeDataWithSchema.write(writer);
90 // return read(reader);
91 } catch (final EOFException e) {
94 // return JsonNull.INSTANCE;
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);
107 reader.setLenient(lenient);
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);
114 final Object translatedValue = translateValueByType(value, parent.getSchema());
115 ((SimpleNodeDataWithSchema) parent).setValue(translatedValue);
118 public void read(final JsonReader in, final AbstractNodeDataWithSchema parent) throws IOException {
122 setValue(parent, in.nextString());
125 setValue(parent, Boolean.toString(in.nextBoolean()));
129 setValue(parent, null);
133 while (in.hasNext()) {
134 AbstractNodeDataWithSchema newChild = null;
135 if (parent instanceof ListNodeDataWithSchema) {
136 newChild = new ListEntryNodeDataWithSchema(parent.getSchema());
137 ((CompositeNodeDataWithSchema) parent).addChild(newChild);
138 } else if (parent instanceof LeafListNodeDataWithSchema) {
139 newChild = new LeafListEntryNodeDataWithSchema(parent.getSchema());
140 ((CompositeNodeDataWithSchema) parent).addChild(newChild);
147 final Set<String> namesakes = new HashSet<>();
149 while (in.hasNext()) {
150 final String jsonElementName = in.nextName();
151 final NamespaceAndName namespaceAndName = resolveNamespace(jsonElementName, parent.getSchema());
152 final String localName = namespaceAndName.getName();
153 addNamespace(namespaceAndName.getUri());
154 if (namesakes.contains(jsonElementName)) {
155 throw new JsonSyntaxException("Duplicate name " + jsonElementName + " in JSON input.");
157 namesakes.add(jsonElementName);
158 final Deque<DataSchemaNode> childDataSchemaNodes = findSchemaNodeByNameAndNamespace(parent.getSchema(),
159 localName, getCurrentNamespace());
160 if (childDataSchemaNodes.isEmpty()) {
161 throw new IllegalStateException("Schema for node with name " + localName + " and namespace "
162 + getCurrentNamespace() + " doesn't exist.");
165 AbstractNodeDataWithSchema newChild;
166 newChild = ((CompositeNodeDataWithSchema) parent).addChild(childDataSchemaNodes);
167 // FIXME:anyxml data shouldn't be skipped but should be loaded somehow. will be specified after 17AUG2014
168 if (newChild instanceof AnyXmlNodeDataWithSchema) {
185 private Object translateValueByType(final String value, final DataSchemaNode node) {
186 final TypeDefinition<? extends Object> typeDefinition = typeDefinition(node);
187 if (typeDefinition == null) {
191 return codecs.codecFor(typeDefinition).deserialize(value);
194 private static TypeDefinition<? extends Object> typeDefinition(final DataSchemaNode node) {
195 TypeDefinition<?> baseType = null;
196 if (node instanceof LeafListSchemaNode) {
197 baseType = ((LeafListSchemaNode) node).getType();
198 } else if (node instanceof LeafSchemaNode) {
199 baseType = ((LeafSchemaNode) node).getType();
200 } else if (node instanceof AnyXmlSchemaNode) {
203 throw new IllegalArgumentException("Unhandled parameter types: " + Arrays.<Object> asList(node).toString());
206 if (baseType != null) {
207 while (baseType.getBaseType() != null) {
208 baseType = baseType.getBaseType();
214 private void removeNamespace() {
218 private void addNamespace(final URI namespace) {
219 namespaces.push(namespace);
222 private NamespaceAndName resolveNamespace(final String childName, final DataSchemaNode dataSchemaNode) {
223 final int lastIndexOfColon = childName.lastIndexOf(':');
224 String moduleNamePart = null;
225 String nodeNamePart = null;
226 URI namespace = null;
227 if (lastIndexOfColon != -1) {
228 moduleNamePart = childName.substring(0, lastIndexOfColon);
229 nodeNamePart = childName.substring(lastIndexOfColon + 1);
231 final Module m = schema.findModuleByName(moduleNamePart, null);
232 namespace = m == null ? null : m.getNamespace();
234 nodeNamePart = childName;
237 if (namespace == null) {
238 Set<URI> potentialUris = Collections.emptySet();
239 potentialUris = resolveAllPotentialNamespaces(nodeNamePart, dataSchemaNode);
240 if (potentialUris.contains(getCurrentNamespace())) {
241 namespace = getCurrentNamespace();
242 } else if (potentialUris.size() == 1) {
243 namespace = potentialUris.iterator().next();
244 } else if (potentialUris.size() > 1) {
245 throw new IllegalStateException("Choose suitable module name for element "+nodeNamePart+":"+toModuleNames(potentialUris));
246 } else if (potentialUris.isEmpty()) {
247 throw new IllegalStateException("Schema node with name "+nodeNamePart+" wasn't found.");
251 return new NamespaceAndName(nodeNamePart, namespace);
254 private String toModuleNames(final Set<URI> potentialUris) {
255 final StringBuilder builder = new StringBuilder();
256 for (final URI potentialUri : potentialUris) {
257 builder.append("\n");
258 //FIXME how to get information about revision from JSON input? currently first available is used.
259 builder.append(schema.findModuleByNamespace(potentialUri).iterator().next().getName());
261 return builder.toString();
264 private Set<URI> resolveAllPotentialNamespaces(final String elementName, final DataSchemaNode dataSchemaNode) {
265 final Set<URI> potentialUris = new HashSet<>();
266 final Set<ChoiceNode> choices = new HashSet<>();
267 if (dataSchemaNode instanceof DataNodeContainer) {
268 for (final DataSchemaNode childSchemaNode : ((DataNodeContainer) dataSchemaNode).getChildNodes()) {
269 if (childSchemaNode instanceof ChoiceNode) {
270 choices.add((ChoiceNode)childSchemaNode);
271 } else if (childSchemaNode.getQName().getLocalName().equals(elementName)) {
272 potentialUris.add(childSchemaNode.getQName().getNamespace());
276 for (final ChoiceNode choiceNode : choices) {
277 for (final ChoiceCaseNode concreteCase : choiceNode.getCases()) {
278 potentialUris.addAll(resolveAllPotentialNamespaces(elementName, concreteCase));
282 return potentialUris;
285 private URI getCurrentNamespace() {
286 return namespaces.peek();
290 * Returns stack of schema nodes via which it was necessary to pass to get schema node with specified
291 * {@code childName} and {@code namespace}
293 * @param dataSchemaNode
296 * @return stack of schema nodes via which it was passed through. If found schema node is direct child then stack
297 * contains only one node. If it is found under choice and case then stack should contains 2*n+1 element
298 * (where n is number of choices through it was passed)
300 private Deque<DataSchemaNode> findSchemaNodeByNameAndNamespace(final DataSchemaNode dataSchemaNode,
301 final String childName, final URI namespace) {
302 final Deque<DataSchemaNode> result = new ArrayDeque<>();
303 final List<ChoiceNode> childChoices = new ArrayList<>();
304 if (dataSchemaNode instanceof DataNodeContainer) {
305 for (final DataSchemaNode childNode : ((DataNodeContainer) dataSchemaNode).getChildNodes()) {
306 if (childNode instanceof ChoiceNode) {
307 childChoices.add((ChoiceNode) childNode);
309 final QName childQName = childNode.getQName();
310 if (childQName.getLocalName().equals(childName) && childQName.getNamespace().equals(namespace)) {
311 result.push(childNode);
317 // try to find data schema node in choice (looking for first match)
318 for (final ChoiceNode choiceNode : childChoices) {
319 for (final ChoiceCaseNode concreteCase : choiceNode.getCases()) {
320 final Deque<DataSchemaNode> resultFromRecursion = findSchemaNodeByNameAndNamespace(concreteCase, childName,
322 if (!resultFromRecursion.isEmpty()) {
323 resultFromRecursion.push(concreteCase);
324 resultFromRecursion.push(choiceNode);
325 return resultFromRecursion;
332 private static class NamespaceAndName {
333 private final URI uri;
334 private final String name;
336 public NamespaceAndName(final String name, final URI uri) {
341 public String getName() {
345 public URI getUri() {
351 public void flush() throws IOException {
356 public void close() throws IOException {