*/
package org.opendaylight.yangtools.yang.data.codec.gson;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
+import static java.util.Objects.requireNonNull;
+
import com.google.common.annotations.Beta;
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
import com.google.gson.JsonIOException;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.MalformedJsonException;
-
import java.io.Closeable;
import java.io.EOFException;
import java.io.Flushable;
import java.io.IOException;
import java.net.URI;
+import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.ArrayDeque;
-import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Deque;
import java.util.HashSet;
-import java.util.List;
+import java.util.Iterator;
+import java.util.Map.Entry;
import java.util.Set;
-
-import org.opendaylight.yangtools.yang.common.QName;
+import javax.xml.transform.dom.DOMSource;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.odlext.model.api.YangModeledAnyxmlSchemaNode;
+import org.opendaylight.yangtools.util.xml.UntrustedXML;
import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
-import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
-import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
+import org.opendaylight.yangtools.yang.data.util.AbstractNodeDataWithSchema;
+import org.opendaylight.yangtools.yang.data.util.AnyXmlNodeDataWithSchema;
+import org.opendaylight.yangtools.yang.data.util.CompositeNodeDataWithSchema;
+import org.opendaylight.yangtools.yang.data.util.LeafListEntryNodeDataWithSchema;
+import org.opendaylight.yangtools.yang.data.util.LeafListNodeDataWithSchema;
+import org.opendaylight.yangtools.yang.data.util.LeafNodeDataWithSchema;
+import org.opendaylight.yangtools.yang.data.util.ListEntryNodeDataWithSchema;
+import org.opendaylight.yangtools.yang.data.util.ListNodeDataWithSchema;
+import org.opendaylight.yangtools.yang.data.util.OperationAsContainer;
+import org.opendaylight.yangtools.yang.data.util.ParserStreamUtils;
+import org.opendaylight.yangtools.yang.data.util.SimpleNodeDataWithSchema;
+import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.OperationDefinition;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
+import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Text;
/**
* This class parses JSON elements from a GSON JsonReader. It disallows multiple elements of the same name unlike the
*/
@Beta
public final class JsonParserStream implements Closeable, Flushable {
+ static final String ANYXML_ARRAY_ELEMENT_ID = "array-element";
+
private final Deque<URI> namespaces = new ArrayDeque<>();
private final NormalizedNodeStreamWriter writer;
- private final CodecFactory codecs;
- private final SchemaContext schema;
+ private final JSONCodecFactory codecs;
+ private final DataSchemaNode parentNode;
- private JsonParserStream(final NormalizedNodeStreamWriter writer, final SchemaContext schemaContext) {
- this.schema = Preconditions.checkNotNull(schemaContext);
- this.writer = Preconditions.checkNotNull(writer);
- this.codecs = CodecFactory.create(schemaContext);
+ private JsonParserStream(final NormalizedNodeStreamWriter writer, final JSONCodecFactory codecs,
+ final DataSchemaNode parentNode) {
+ this.writer = requireNonNull(writer);
+ this.codecs = requireNonNull(codecs);
+ this.parentNode = parentNode;
+ }
+
+ /**
+ * Create a new {@link JsonParserStream} backed by specified {@link NormalizedNodeStreamWriter}
+ * and {@link JSONCodecFactory}. The stream will be logically rooted at the top of the SchemaContext associated
+ * with the specified codec factory.
+ *
+ * @param writer NormalizedNodeStreamWriter to use for instantiation of normalized nodes
+ * @param codecFactory {@link JSONCodecFactory} to use for parsing leaves
+ * @return A new {@link JsonParserStream}
+ * @throws NullPointerException if any of the arguments are null
+ */
+ public static @NonNull JsonParserStream create(final @NonNull NormalizedNodeStreamWriter writer,
+ final @NonNull JSONCodecFactory codecFactory) {
+ return new JsonParserStream(writer, codecFactory, codecFactory.getSchemaContext());
}
- public static JsonParserStream create(final NormalizedNodeStreamWriter writer, final SchemaContext schemaContext) {
- return new JsonParserStream(writer, schemaContext);
+ /**
+ * Create a new {@link JsonParserStream} backed by specified {@link NormalizedNodeStreamWriter}
+ * and {@link JSONCodecFactory}. The stream will be logically rooted at the specified parent node.
+ *
+ * @param writer NormalizedNodeStreamWriter to use for instantiation of normalized nodes
+ * @param codecFactory {@link JSONCodecFactory} to use for parsing leaves
+ * @param parentNode Logical root node
+ * @return A new {@link JsonParserStream}
+ * @throws NullPointerException if any of the arguments are null
+ */
+ public static @NonNull JsonParserStream create(final @NonNull NormalizedNodeStreamWriter writer,
+ final @NonNull JSONCodecFactory codecFactory, final @NonNull SchemaNode parentNode) {
+ final DataSchemaNode parent;
+ if (parentNode instanceof DataSchemaNode) {
+ parent = (DataSchemaNode) parentNode;
+ } else if (parentNode instanceof OperationDefinition) {
+ parent = OperationAsContainer.of((OperationDefinition) parentNode);
+ } else {
+ throw new IllegalArgumentException("Illegal parent node " + requireNonNull(parentNode));
+ }
+ return new JsonParserStream(writer, codecFactory, parent);
}
- public JsonParserStream parse(final JsonReader reader) throws JsonIOException, JsonSyntaxException {
+ public JsonParserStream parse(final JsonReader reader) {
// code copied from gson's JsonParser and Stream classes
- boolean lenient = reader.isLenient();
+ final boolean lenient = reader.isLenient();
reader.setLenient(true);
boolean isEmpty = true;
try {
reader.peek();
isEmpty = false;
- CompositeNodeDataWithSchema compositeNodeDataWithSchema = new CompositeNodeDataWithSchema(schema);
+ final CompositeNodeDataWithSchema<?> compositeNodeDataWithSchema =
+ new CompositeNodeDataWithSchema<>(parentNode);
read(reader, compositeNodeDataWithSchema);
compositeNodeDataWithSchema.write(writer);
return this;
- // return read(reader);
- } catch (EOFException e) {
+ } catch (final EOFException e) {
if (isEmpty) {
return this;
- // return JsonNull.INSTANCE;
}
// The stream ended prematurely so it is likely a syntax error.
throw new JsonSyntaxException(e);
- } catch (MalformedJsonException e) {
+ } catch (final MalformedJsonException | NumberFormatException e) {
throw new JsonSyntaxException(e);
- } catch (IOException e) {
+ } catch (final IOException e) {
throw new JsonIOException(e);
- } catch (NumberFormatException e) {
- throw new JsonSyntaxException(e);
} catch (StackOverflowError | OutOfMemoryError e) {
throw new JsonParseException("Failed parsing JSON source: " + reader + " to Json", e);
} finally {
}
}
- private final void setValue(final AbstractNodeDataWithSchema parent, final String value) {
- Preconditions.checkArgument(parent instanceof SimpleNodeDataWithSchema, "Node %s is not a simple type", parent);
+ private void traverseAnyXmlValue(final JsonReader in, final Document doc, final Element parentElement)
+ throws IOException {
+ switch (in.peek()) {
+ case STRING:
+ case NUMBER:
+ Text textNode = doc.createTextNode(in.nextString());
+ parentElement.appendChild(textNode);
+ break;
+ case BOOLEAN:
+ textNode = doc.createTextNode(Boolean.toString(in.nextBoolean()));
+ parentElement.appendChild(textNode);
+ break;
+ case NULL:
+ in.nextNull();
+ textNode = doc.createTextNode("null");
+ parentElement.appendChild(textNode);
+ break;
+ case BEGIN_ARRAY:
+ in.beginArray();
+ while (in.hasNext()) {
+ final Element childElement = doc.createElement(ANYXML_ARRAY_ELEMENT_ID);
+ parentElement.appendChild(childElement);
+ traverseAnyXmlValue(in, doc, childElement);
+ }
+ in.endArray();
+ break;
+ case BEGIN_OBJECT:
+ in.beginObject();
+ while (in.hasNext()) {
+ final Element childElement = doc.createElement(in.nextName());
+ parentElement.appendChild(childElement);
+ traverseAnyXmlValue(in, doc, childElement);
+ }
+ in.endObject();
+ break;
+ default:
+ break;
+ }
+ }
+
+ private void readAnyXmlValue(final JsonReader in, final AnyXmlNodeDataWithSchema parent,
+ final String anyXmlObjectName) throws IOException {
+ final String anyXmlObjectNS = getCurrentNamespace().toString();
+ final Document doc = UntrustedXML.newDocumentBuilder().newDocument();
+ final Element rootElement = doc.createElementNS(anyXmlObjectNS, anyXmlObjectName);
+ doc.appendChild(rootElement);
+ traverseAnyXmlValue(in, doc, rootElement);
- final Object translatedValue = translateValueByType(value, parent.getSchema());
- ((SimpleNodeDataWithSchema) parent).setValue(translatedValue);
+ final DOMSource domSource = new DOMSource(doc.getDocumentElement());
+ parent.setValue(domSource);
}
- public void read(final JsonReader in, final AbstractNodeDataWithSchema parent) throws IOException {
+ public void read(final JsonReader in, AbstractNodeDataWithSchema<?> parent) throws IOException {
switch (in.peek()) {
- case STRING:
- case NUMBER:
- setValue(parent, in.nextString());
- break;
- case BOOLEAN:
- setValue(parent, Boolean.toString(in.nextBoolean()));
- break;
- case NULL:
- in.nextNull();
- setValue(parent, null);
- break;
- case BEGIN_ARRAY:
- in.beginArray();
- while (in.hasNext()) {
- AbstractNodeDataWithSchema newChild = null;
- if (parent instanceof ListNodeDataWithSchema) {
- newChild = new ListEntryNodeDataWithSchema(parent.getSchema());
- ((CompositeNodeDataWithSchema) parent).addChild(newChild);
- } else if (parent instanceof LeafListNodeDataWithSchema) {
- newChild = new LeafListEntryNodeDataWithSchema(parent.getSchema());
- ((CompositeNodeDataWithSchema) parent).addChild(newChild);
- }
- read(in, newChild);
- }
- in.endArray();
- return;
- case BEGIN_OBJECT:
- Set<String> namesakes = new HashSet<>();
- in.beginObject();
- while (in.hasNext()) {
- final String jsonElementName = in.nextName();
- final NamespaceAndName namespaceAndName = resolveNamespace(jsonElementName);
- final String localName = namespaceAndName.getName();
- addNamespace(namespaceAndName.getUri());
- if (namesakes.contains(jsonElementName)) {
- throw new JsonSyntaxException("Duplicate name " + jsonElementName + " in JSON input.");
+ case STRING:
+ case NUMBER:
+ setValue(parent, in.nextString());
+ break;
+ case BOOLEAN:
+ setValue(parent, Boolean.toString(in.nextBoolean()));
+ break;
+ case NULL:
+ in.nextNull();
+ setValue(parent, null);
+ break;
+ case BEGIN_ARRAY:
+ in.beginArray();
+ while (in.hasNext()) {
+ if (parent instanceof LeafNodeDataWithSchema) {
+ read(in, parent);
+ } else {
+ final AbstractNodeDataWithSchema<?> newChild = newArrayEntry(parent);
+ read(in, newChild);
+ }
}
- namesakes.add(jsonElementName);
- final Deque<DataSchemaNode> childDataSchemaNodes = findSchemaNodeByNameAndNamespace(parent.getSchema(),
- localName, getCurrentNamespace());
- if (childDataSchemaNodes.isEmpty()) {
- throw new IllegalStateException("Schema for node with name " + localName + " and namespace "
- + getCurrentNamespace() + " doesn't exist.");
+ in.endArray();
+ return;
+ case BEGIN_OBJECT:
+ final Set<String> namesakes = new HashSet<>();
+ in.beginObject();
+ /*
+ * This allows parsing of incorrectly /as showcased/
+ * in testconf nesting of list items - eg.
+ * lists with one value are sometimes serialized
+ * without wrapping array.
+ *
+ */
+ if (isArray(parent)) {
+ parent = newArrayEntry(parent);
}
+ while (in.hasNext()) {
+ final String jsonElementName = in.nextName();
+ DataSchemaNode parentSchema = parent.getSchema();
+ if (parentSchema instanceof YangModeledAnyxmlSchemaNode) {
+ parentSchema = ((YangModeledAnyxmlSchemaNode) parentSchema).getSchemaOfAnyXmlData();
+ }
+ final Entry<String, URI> namespaceAndName = resolveNamespace(jsonElementName, parentSchema);
+ final String localName = namespaceAndName.getKey();
+ addNamespace(namespaceAndName.getValue());
+ if (!namesakes.add(jsonElementName)) {
+ throw new JsonSyntaxException("Duplicate name " + jsonElementName + " in JSON input.");
+ }
+
+ final Deque<DataSchemaNode> childDataSchemaNodes =
+ ParserStreamUtils.findSchemaNodeByNameAndNamespace(parentSchema, localName,
+ getCurrentNamespace());
+ checkState(!childDataSchemaNodes.isEmpty(),
+ "Schema for node with name %s and namespace %s does not exist at %s",
+ localName, getCurrentNamespace(), parentSchema.getPath());
+
- AbstractNodeDataWithSchema newChild;
- newChild = ((CompositeNodeDataWithSchema) parent).addChild(childDataSchemaNodes);
-// FIXME:anyxml data shouldn't be skipped but should be loaded somehow. will be specified after 17AUG2014
- if (newChild instanceof AnyXmlNodeDataWithSchema) {
- in.skipValue();
- } else {
- read(in, newChild);
+ final AbstractNodeDataWithSchema<?> newChild = ((CompositeNodeDataWithSchema<?>) parent)
+ .addChild(childDataSchemaNodes);
+ if (newChild instanceof AnyXmlNodeDataWithSchema) {
+ readAnyXmlValue(in, (AnyXmlNodeDataWithSchema) newChild, jsonElementName);
+ } else {
+ read(in, newChild);
+ }
+ removeNamespace();
}
- removeNamespace();
- }
- in.endObject();
- return;
- case END_DOCUMENT:
- case NAME:
- case END_OBJECT:
- case END_ARRAY:
- break;
+ in.endObject();
+ return;
+ default:
+ break;
}
}
- private Object translateValueByType(final String value, final DataSchemaNode node) {
- final TypeDefinition<? extends Object> typeDefinition = typeDefinition(node);
- if (typeDefinition == null) {
- return value;
- }
-
- return codecs.codecFor(typeDefinition).deserialize(value);
+ private static boolean isArray(final AbstractNodeDataWithSchema<?> parent) {
+ return parent instanceof ListNodeDataWithSchema || parent instanceof LeafListNodeDataWithSchema;
}
- private static TypeDefinition<? extends Object> typeDefinition(final DataSchemaNode node) {
- TypeDefinition<?> baseType = null;
- if (node instanceof LeafListSchemaNode) {
- baseType = ((LeafListSchemaNode) node).getType();
- } else if (node instanceof LeafSchemaNode) {
- baseType = ((LeafSchemaNode) node).getType();
- } else if (node instanceof AnyXmlSchemaNode) {
- return null;
+ private static AbstractNodeDataWithSchema<?> newArrayEntry(final AbstractNodeDataWithSchema<?> parent) {
+ AbstractNodeDataWithSchema<?> newChild;
+ if (parent instanceof ListNodeDataWithSchema) {
+ newChild = ListEntryNodeDataWithSchema.forSchema(((ListNodeDataWithSchema) parent).getSchema());
+ } else if (parent instanceof LeafListNodeDataWithSchema) {
+ newChild = new LeafListEntryNodeDataWithSchema(((LeafListNodeDataWithSchema) parent).getSchema());
} else {
- throw new IllegalArgumentException("Unhandled parameter types: " + Arrays.<Object> asList(node).toString());
+ throw new IllegalStateException("Found an unexpected array nested under " + parent.getSchema().getQName());
}
+ ((CompositeNodeDataWithSchema<?>) parent).addChild(newChild);
+ return newChild;
+ }
- if (baseType != null) {
- while (baseType.getBaseType() != null) {
- baseType = baseType.getBaseType();
- }
- }
- return baseType;
+ private void setValue(final AbstractNodeDataWithSchema<?> parent, final String value) {
+ checkArgument(parent instanceof SimpleNodeDataWithSchema, "Node %s is not a simple type",
+ parent.getSchema().getQName());
+ final SimpleNodeDataWithSchema<?> parentSimpleNode = (SimpleNodeDataWithSchema<?>) parent;
+ checkArgument(parentSimpleNode.getValue() == null, "Node '%s' has already set its value to '%s'",
+ parentSimpleNode.getSchema().getQName(), parentSimpleNode.getValue());
+
+ final Object translatedValue = translateValueByType(value, parentSimpleNode.getSchema());
+ parentSimpleNode.setValue(translatedValue);
+ }
+
+ private Object translateValueByType(final String value, final DataSchemaNode node) {
+ checkArgument(node instanceof TypedDataSchemaNode);
+ return codecs.codecFor((TypedDataSchemaNode) node).parseValue(null, value);
}
private void removeNamespace() {
namespaces.pop();
}
- private void addNamespace(final Optional<URI> namespace) {
- if (!namespace.isPresent()) {
- if (namespaces.isEmpty()) {
- throw new IllegalStateException("Namespace has to be specified at top level.");
- } else {
- namespaces.push(namespaces.peek());
- }
- } else {
- namespaces.push(namespace.get());
- }
+ private void addNamespace(final URI namespace) {
+ namespaces.push(namespace);
}
- private NamespaceAndName resolveNamespace(final String childName) {
- int lastIndexOfColon = childName.lastIndexOf(':');
+ private Entry<String, URI> resolveNamespace(final String childName, final DataSchemaNode dataSchemaNode) {
+ final int lastIndexOfColon = childName.lastIndexOf(':');
String moduleNamePart = null;
String nodeNamePart = null;
URI namespace = null;
moduleNamePart = childName.substring(0, lastIndexOfColon);
nodeNamePart = childName.substring(lastIndexOfColon + 1);
- final Module m = schema.findModuleByName(moduleNamePart, null);
- namespace = m == null ? null : m.getNamespace();
+ final Iterator<Module> m = codecs.getSchemaContext().findModules(moduleNamePart).iterator();
+ namespace = m.hasNext() ? m.next().getNamespace() : null;
} else {
nodeNamePart = childName;
}
- Optional<URI> namespaceOpt = namespace == null ? Optional.<URI> absent() : Optional.of(namespace);
- return new NamespaceAndName(nodeNamePart, namespaceOpt);
+ if (namespace == null) {
+ final Set<URI> potentialUris = resolveAllPotentialNamespaces(nodeNamePart, dataSchemaNode);
+ if (potentialUris.contains(getCurrentNamespace())) {
+ namespace = getCurrentNamespace();
+ } else if (potentialUris.size() == 1) {
+ namespace = potentialUris.iterator().next();
+ } else if (potentialUris.size() > 1) {
+ throw new IllegalStateException("Choose suitable module name for element " + nodeNamePart + ":"
+ + toModuleNames(potentialUris));
+ } else if (potentialUris.isEmpty()) {
+ throw new IllegalStateException("Schema node with name " + nodeNamePart + " was not found under "
+ + dataSchemaNode.getQName() + ".");
+ }
+ }
+
+ return new SimpleImmutableEntry<>(nodeNamePart, namespace);
}
- private URI getCurrentNamespace() {
- return namespaces.peek();
+ private String toModuleNames(final Set<URI> potentialUris) {
+ final StringBuilder builder = new StringBuilder();
+ for (final URI potentialUri : potentialUris) {
+ builder.append('\n');
+ //FIXME how to get information about revision from JSON input? currently first available is used.
+ builder.append(codecs.getSchemaContext().findModules(potentialUri).iterator().next().getName());
+ }
+ return builder.toString();
}
- /**
- * Returns stack of schema nodes via which it was necessary to pass to get schema node with specified
- * {@code childName} and {@code namespace}
- *
- * @param dataSchemaNode
- * @param childName
- * @param namespace
- * @return stack of schema nodes via which it was passed through. If found schema node is direct child then stack
- * contains only one node. If it is found under choice and case then stack should contains 2*n+1 element
- * (where n is number of choices through it was passed)
- */
- private Deque<DataSchemaNode> findSchemaNodeByNameAndNamespace(final DataSchemaNode dataSchemaNode,
- final String childName, final URI namespace) {
- final Deque<DataSchemaNode> result = new ArrayDeque<>();
- List<ChoiceNode> childChoices = new ArrayList<>();
+ private Set<URI> resolveAllPotentialNamespaces(final String elementName, final DataSchemaNode dataSchemaNode) {
+ final Set<URI> potentialUris = new HashSet<>();
+ final Set<ChoiceSchemaNode> choices = new HashSet<>();
if (dataSchemaNode instanceof DataNodeContainer) {
- for (DataSchemaNode childNode : ((DataNodeContainer) dataSchemaNode).getChildNodes()) {
- if (childNode instanceof ChoiceNode) {
- childChoices.add((ChoiceNode) childNode);
- } else {
- final QName childQName = childNode.getQName();
- if (childQName.getLocalName().equals(childName) && childQName.getNamespace().equals(namespace)) {
- result.push(childNode);
- return result;
- }
+ for (final DataSchemaNode childSchemaNode : ((DataNodeContainer) dataSchemaNode).getChildNodes()) {
+ if (childSchemaNode instanceof ChoiceSchemaNode) {
+ choices.add((ChoiceSchemaNode)childSchemaNode);
+ } else if (childSchemaNode.getQName().getLocalName().equals(elementName)) {
+ potentialUris.add(childSchemaNode.getQName().getNamespace());
}
}
- }
- // try to find data schema node in choice (looking for first match)
- for (ChoiceNode choiceNode : childChoices) {
- for (ChoiceCaseNode concreteCase : choiceNode.getCases()) {
- Deque<DataSchemaNode> resultFromRecursion = findSchemaNodeByNameAndNamespace(concreteCase, childName,
- namespace);
- if (!resultFromRecursion.isEmpty()) {
- resultFromRecursion.push(concreteCase);
- resultFromRecursion.push(choiceNode);
- return resultFromRecursion;
+
+ for (final ChoiceSchemaNode choiceNode : choices) {
+ for (final CaseSchemaNode concreteCase : choiceNode.getCases().values()) {
+ potentialUris.addAll(resolveAllPotentialNamespaces(elementName, concreteCase));
}
}
}
- return result;
+ return potentialUris;
}
- private static class NamespaceAndName {
- private final Optional<URI> uri;
- private final String name;
-
- public NamespaceAndName(final String name, final Optional<URI> uri) {
- this.name = name;
- this.uri = uri;
- }
-
- public String getName() {
- return name;
- }
-
- public Optional<URI> getUri() {
- return uri;
- }
+ private URI getCurrentNamespace() {
+ return namespaces.peek();
}
@Override