1 package org.opendaylight.controller.sal.rest.impl;
3 import java.io.InputStream;
4 import java.io.InputStreamReader;
5 import java.util.Map.Entry;
8 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
9 import org.opendaylight.controller.sal.restconf.impl.SimpleNodeWrapper;
11 import com.google.common.collect.Lists;
12 import com.google.gson.JsonElement;
13 import com.google.gson.JsonObject;
14 import com.google.gson.JsonParser;
15 import com.google.gson.JsonPrimitive;
19 public CompositeNodeWrapper read(InputStream entityStream) throws UnsupportedJsonFormatException {
20 JsonParser parser = new JsonParser();
22 JsonElement rootElement = parser.parse(new InputStreamReader(entityStream));
23 if (!rootElement.isJsonObject()) {
24 throw new UnsupportedJsonFormatException("Root element of Json has to be Object");
27 Set<Entry<String, JsonElement>> entrySetsOfRootJsonObject = rootElement.getAsJsonObject().entrySet();
28 if (entrySetsOfRootJsonObject.size() != 1) {
29 throw new UnsupportedJsonFormatException("Json Object should contain one element");
31 Entry<String, JsonElement> childEntry = Lists.newArrayList(entrySetsOfRootJsonObject).get(0);
32 String firstElementName = childEntry.getKey();
33 JsonElement firstElementType = childEntry.getValue();
34 if (firstElementType.isJsonObject()) { // container in yang
35 return createStructureWithRoot(firstElementName, firstElementType.getAsJsonObject());
37 if (firstElementType.isJsonArray()) { // list in yang
38 if (firstElementType.getAsJsonArray().size() == 1) {
39 JsonElement firstElementInArray = firstElementType.getAsJsonArray().get(0);
40 if (firstElementInArray.isJsonObject()) {
41 return createStructureWithRoot(firstElementName, firstElementInArray.getAsJsonObject());
43 throw new UnsupportedJsonFormatException("Array as the first element in Json Object can have only Object element");
46 throw new UnsupportedJsonFormatException("First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet.");
50 private CompositeNodeWrapper createStructureWithRoot(String rootObjectName, JsonObject rootObject) {
51 CompositeNodeWrapper firstNode = new CompositeNodeWrapper(rootObjectName);
52 for (Entry<String, JsonElement> childOfFirstNode : rootObject.entrySet()) {
53 addChildToParent(childOfFirstNode.getKey(), childOfFirstNode.getValue(), firstNode);
58 private void addChildToParent(String childName, JsonElement childType, CompositeNodeWrapper parent) {
59 if (childType.isJsonObject()) {
60 CompositeNodeWrapper child = new CompositeNodeWrapper(childName);
61 parent.addValue(child);
62 for (Entry<String, JsonElement> childOfChild : childType.getAsJsonObject().entrySet()) {
63 addChildToParent(childOfChild.getKey(), childOfChild.getValue(), child);
65 } else if (childType.isJsonArray()) {
66 for (JsonElement childOfChildType : childType.getAsJsonArray()) {
67 addChildToParent(childName, childOfChildType, parent);
69 } else if (childType.isJsonPrimitive()) {
70 JsonPrimitive childPrimitive = childType.getAsJsonPrimitive();
71 String value = childPrimitive.getAsString();
72 SimpleNodeWrapper child = null;
73 if (value.equals("[null]")) {
74 child = new SimpleNodeWrapper(childName, null);
76 child = new SimpleNodeWrapper(childName, value);
78 parent.addValue(child);