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.EmptyNodeWrapper;
11 import org.opendaylight.controller.sal.restconf.impl.SimpleNodeWrapper;
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;
21 public CompositeNodeWrapper read(InputStream entityStream) throws UnsupportedFormatException {
22 JsonParser parser = new JsonParser();
24 JsonElement rootElement = parser.parse(new InputStreamReader(entityStream));
25 if (!rootElement.isJsonObject()) {
26 throw new UnsupportedFormatException("Root element of Json has to be Object");
29 Set<Entry<String, JsonElement>> entrySetsOfRootJsonObject = rootElement.getAsJsonObject().entrySet();
30 if (entrySetsOfRootJsonObject.size() != 1) {
31 throw new UnsupportedFormatException("Json Object should contain one element");
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());
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());
45 throw new UnsupportedFormatException(
46 "Array as the first element in Json Object can have only Object element");
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.");
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);
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);
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)));
76 for (JsonElement childOfChildType : childType.getAsJsonArray()) {
77 addChildToParent(childName, childOfChildType, parent);
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));
87 private URI getNamespaceFrom(String jsonElementName) {
88 int indexOfDelimeter = jsonElementName.lastIndexOf(':');
89 if (indexOfDelimeter == -1) {
92 return URI.create(jsonElementName.substring(0, indexOfDelimeter));
95 private String getLocalNameFrom(String jsonElementName) {
96 int indexOfDelimeter = jsonElementName.lastIndexOf(':');
97 if (indexOfDelimeter == -1) {
98 return jsonElementName;
100 return jsonElementName.substring(indexOfDelimeter + 1, jsonElementName.length());