Merge "MD-SAL Statistics Manager -Added group-id to group-statistics API"
[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.net.URI;
6 import java.util.Map.Entry;
7 import java.util.Set;
8
9 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
10 import org.opendaylight.controller.sal.restconf.impl.SimpleNodeWrapper;
11
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;
17
18 class JsonReader {
19
20     public CompositeNodeWrapper read(InputStream entityStream) throws UnsupportedFormatException {
21         JsonParser parser = new JsonParser();
22
23         JsonElement rootElement = parser.parse(new InputStreamReader(entityStream));
24         if (!rootElement.isJsonObject()) {
25             throw new UnsupportedFormatException("Root element of Json has to be Object");
26         }
27
28         Set<Entry<String, JsonElement>> entrySetsOfRootJsonObject = rootElement.getAsJsonObject().entrySet();
29         if (entrySetsOfRootJsonObject.size() != 1) {
30             throw new UnsupportedFormatException("Json Object should contain one element");
31         } else {
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());
37             }
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());
43                     }
44                     throw new UnsupportedFormatException(
45                             "Array as the first element in Json Object can have only Object element");
46                 }
47             }
48             throw new UnsupportedFormatException(
49                     "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet.");
50         }
51     }
52
53     private CompositeNodeWrapper createStructureWithRoot(String rootObjectName, JsonObject rootObject) {
54         CompositeNodeWrapper firstNode = new CompositeNodeWrapper(getNamespaceFrom(rootObjectName),
55                 getLocalNameFrom(rootObjectName));
56         for (Entry<String, JsonElement> childOfFirstNode : rootObject.entrySet()) {
57             addChildToParent(childOfFirstNode.getKey(), childOfFirstNode.getValue(), firstNode);
58         }
59         return firstNode;
60     }
61
62     private void addChildToParent(String childName, JsonElement childType, CompositeNodeWrapper parent) {
63         if (childType.isJsonObject()) {
64             CompositeNodeWrapper child = new CompositeNodeWrapper(getNamespaceFrom(childName),
65                     getLocalNameFrom(childName));
66             parent.addValue(child);
67             for (Entry<String, JsonElement> childOfChild : childType.getAsJsonObject().entrySet()) {
68                 addChildToParent(childOfChild.getKey(), childOfChild.getValue(), child);
69             }
70         } else if (childType.isJsonArray()) {
71             if (childType.getAsJsonArray().size() == 1 && childType.getAsJsonArray().get(0).isJsonNull()) {
72                 parent.addValue(new SimpleNodeWrapper(getNamespaceFrom(childName), getLocalNameFrom(childName), null));
73
74             } else {
75                 for (JsonElement childOfChildType : childType.getAsJsonArray()) {
76                     addChildToParent(childName, childOfChildType, parent);
77                 }
78             }
79         } else if (childType.isJsonPrimitive()) {
80             JsonPrimitive childPrimitive = childType.getAsJsonPrimitive();
81             String value = childPrimitive.getAsString();
82             parent.addValue(new SimpleNodeWrapper(getNamespaceFrom(childName), getLocalNameFrom(childName), value));
83         }
84     }
85
86     private URI getNamespaceFrom(String jsonElementName) {
87         int indexOfDelimeter = jsonElementName.lastIndexOf(':');
88         if (indexOfDelimeter == -1) {
89             return null;
90         }
91         return URI.create(jsonElementName.substring(0, indexOfDelimeter));
92     }
93
94     private String getLocalNameFrom(String jsonElementName) {
95         int indexOfDelimeter = jsonElementName.lastIndexOf(':');
96         if (indexOfDelimeter == -1) {
97             return jsonElementName;
98         }
99         return jsonElementName.substring(indexOfDelimeter + 1, jsonElementName.length());
100     }
101
102 }