1 package org.opendaylight.controller.sal.rest.impl;
3 import java.io.InputStream;
4 import java.io.InputStreamReader;
6 import java.util.Map.Entry;
9 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
10 import org.opendaylight.controller.sal.restconf.impl.SimpleNodeWrapper;
12 import com.google.common.collect.Lists;
13 import com.google.gson.JsonElement;
14 import com.google.gson.JsonObject;
15 import com.google.gson.JsonParser;
16 import com.google.gson.JsonPrimitive;
20 public CompositeNodeWrapper read(InputStream entityStream) throws UnsupportedFormatException {
21 JsonParser parser = new JsonParser();
23 JsonElement rootElement = parser.parse(new InputStreamReader(entityStream));
24 if (!rootElement.isJsonObject()) {
25 throw new UnsupportedFormatException("Root element of Json has to be Object");
28 Set<Entry<String, JsonElement>> entrySetsOfRootJsonObject = rootElement.getAsJsonObject().entrySet();
29 if (entrySetsOfRootJsonObject.size() != 1) {
30 throw new UnsupportedFormatException("Json Object should contain one element");
32 Entry<String, JsonElement> childEntry = Lists.newArrayList(entrySetsOfRootJsonObject).get(0);
33 String firstElementName = childEntry.getKey();
34 JsonElement firstElementType = childEntry.getValue();
35 if (firstElementType.isJsonObject()) { // container in yang
36 return createStructureWithRoot(firstElementName, firstElementType.getAsJsonObject());
38 if (firstElementType.isJsonArray()) { // list in yang
39 if (firstElementType.getAsJsonArray().size() == 1) {
40 JsonElement firstElementInArray = firstElementType.getAsJsonArray().get(0);
41 if (firstElementInArray.isJsonObject()) {
42 return createStructureWithRoot(firstElementName, firstElementInArray.getAsJsonObject());
44 throw new UnsupportedFormatException("Array as the first element in Json Object can have only Object element");
47 throw new UnsupportedFormatException("First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet.");
51 private CompositeNodeWrapper createStructureWithRoot(String rootObjectName, JsonObject rootObject) {
52 CompositeNodeWrapper firstNode = new CompositeNodeWrapper(getNamespaceFrom(rootObjectName),
53 getLocalNameFrom(rootObjectName));
54 for (Entry<String, JsonElement> childOfFirstNode : rootObject.entrySet()) {
55 addChildToParent(childOfFirstNode.getKey(), childOfFirstNode.getValue(), firstNode);
60 private void addChildToParent(String childName, JsonElement childType, CompositeNodeWrapper parent) {
61 if (childType.isJsonObject()) {
62 CompositeNodeWrapper child = new CompositeNodeWrapper(getNamespaceFrom(childName),
63 getLocalNameFrom(childName));
64 parent.addValue(child);
65 for (Entry<String, JsonElement> childOfChild : childType.getAsJsonObject().entrySet()) {
66 addChildToParent(childOfChild.getKey(), childOfChild.getValue(), child);
68 } else if (childType.isJsonArray()) {
69 for (JsonElement childOfChildType : childType.getAsJsonArray()) {
70 addChildToParent(childName, childOfChildType, parent);
72 } else if (childType.isJsonPrimitive()) {
73 JsonPrimitive childPrimitive = childType.getAsJsonPrimitive();
74 String value = childPrimitive.getAsString();
75 SimpleNodeWrapper child = null;
76 if (value.equals("[null]")) {
77 child = new SimpleNodeWrapper(getNamespaceFrom(childName), getLocalNameFrom(childName), null);
79 child = new SimpleNodeWrapper(getNamespaceFrom(childName), getLocalNameFrom(childName), value);
81 parent.addValue(child);
85 private URI getNamespaceFrom(String jsonElementName) {
86 int indexOfDelimeter = jsonElementName.lastIndexOf(':');
87 if (indexOfDelimeter == -1) {
90 return URI.create(jsonElementName.substring(0, indexOfDelimeter));
93 private String getLocalNameFrom(String jsonElementName) {
94 int indexOfDelimeter = jsonElementName.lastIndexOf(':');
95 if (indexOfDelimeter == -1) {
96 return jsonElementName;
98 return jsonElementName.substring(indexOfDelimeter + 1, jsonElementName.length());