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