1 package org.opendaylight.controller.sal.rest.impl;
3 import java.io.InputStream;
4 import java.io.InputStreamReader;
6 import java.util.Map.Entry;
9 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
10 import org.opendaylight.controller.sal.restconf.impl.SimpleNodeWrapper;
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;
20 public CompositeNodeWrapper read(InputStream entityStream) throws UnsupportedFormatException {
21 JsonParser parser = new JsonParser();
23 JsonElement rootElement = parser.parse(new InputStreamReader(entityStream));
24 if (!rootElement.isJsonObject()) {
25 throw new UnsupportedFormatException("Root element of Json has to be Object");
28 Set<Entry<String, JsonElement>> entrySetsOfRootJsonObject = rootElement.getAsJsonObject().entrySet();
29 if (entrySetsOfRootJsonObject.size() != 1) {
30 throw new UnsupportedFormatException("Json Object should contain one element");
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());
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());
44 throw new UnsupportedFormatException(
45 "Array as the first element in Json Object can have only Object element");
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.");
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);
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);
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));
75 for (JsonElement childOfChildType : childType.getAsJsonArray()) {
76 addChildToParent(childName, childOfChildType, parent);
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));
86 private URI getNamespaceFrom(String jsonElementName) {
87 int indexOfDelimeter = jsonElementName.lastIndexOf(':');
88 if (indexOfDelimeter == -1) {
91 return URI.create(jsonElementName.substring(0, indexOfDelimeter));
94 private String getLocalNameFrom(String jsonElementName) {
95 int indexOfDelimeter = jsonElementName.lastIndexOf(':');
96 if (indexOfDelimeter == -1) {
97 return jsonElementName;
99 return jsonElementName.substring(indexOfDelimeter + 1, jsonElementName.length());