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("Array as the first element in Json Object can have only Object element");
45                 }
46             }
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.");
48         }
49     }
50     
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);
56         }
57         return firstNode;
58     }
59     
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);
67             }
68         } else if (childType.isJsonArray()) {
69             for (JsonElement childOfChildType : childType.getAsJsonArray()) {
70                 addChildToParent(childName, childOfChildType, parent);
71             }
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);
78             } else {
79                 child = new SimpleNodeWrapper(getNamespaceFrom(childName), getLocalNameFrom(childName), value);
80             }
81             parent.addValue(child);
82         }
83     }
84
85     private URI getNamespaceFrom(String jsonElementName) {
86         int indexOfDelimeter = jsonElementName.lastIndexOf(':');
87         if (indexOfDelimeter == -1) {
88             return null;
89         }
90         return URI.create(jsonElementName.substring(0, indexOfDelimeter));
91     }
92
93     private String getLocalNameFrom(String jsonElementName) {
94         int indexOfDelimeter = jsonElementName.lastIndexOf(':');
95         if (indexOfDelimeter == -1) {
96             return jsonElementName;
97         }
98         return jsonElementName.substring(indexOfDelimeter + 1, jsonElementName.length());
99     }
100
101 }