Added Json to CompositeNode translation
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / rest / impl / JsonReader.java
1 package org.opendaylight.controller.sal.rest.impl;
2
3 import java.io.InputStream;
4 import java.io.InputStreamReader;
5 import java.util.Map.Entry;
6 import java.util.Set;
7
8 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
9 import org.opendaylight.controller.sal.restconf.impl.SimpleNodeWrapper;
10
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;
16
17 class JsonReader {
18
19     public CompositeNodeWrapper read(InputStream entityStream) throws UnsupportedJsonFormatException {
20         JsonParser parser = new JsonParser();
21         
22         JsonElement rootElement = parser.parse(new InputStreamReader(entityStream));
23         if (!rootElement.isJsonObject()) {
24             throw new UnsupportedJsonFormatException("Root element of Json has to be Object");
25         }
26         
27         Set<Entry<String, JsonElement>> entrySetsOfRootJsonObject = rootElement.getAsJsonObject().entrySet();
28         if (entrySetsOfRootJsonObject.size() != 1) {
29             throw new UnsupportedJsonFormatException("Json Object should contain one element");
30         } else {
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());
36             }
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());
42                     }
43                     throw new UnsupportedJsonFormatException("Array as the first element in Json Object can have only Object element");
44                 }
45             }
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.");
47         }
48     }
49     
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);
54         }
55         return firstNode;
56     }
57     
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);
64             }
65         } else if (childType.isJsonArray()) {
66             for (JsonElement childOfChildType : childType.getAsJsonArray()) {
67                 addChildToParent(childName, childOfChildType, parent);
68             }
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);
75             } else {
76                 child = new SimpleNodeWrapper(childName, value);
77             }
78             parent.addValue(child);
79         }
80     }
81 }