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;
12 import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO;
14 import com.google.common.collect.Lists;
15 import com.google.gson.JsonElement;
16 import com.google.gson.JsonObject;
17 import com.google.gson.JsonParser;
18 import com.google.gson.JsonPrimitive;
22 public CompositeNodeWrapper read(InputStream entityStream) throws UnsupportedFormatException {
23 JsonParser parser = new JsonParser();
25 JsonElement rootElement = parser.parse(new InputStreamReader(entityStream));
26 if (!rootElement.isJsonObject()) {
27 throw new UnsupportedFormatException("Root element of Json has to be Object");
30 Set<Entry<String, JsonElement>> entrySetsOfRootJsonObject = rootElement.getAsJsonObject().entrySet();
31 if (entrySetsOfRootJsonObject.size() != 1) {
32 throw new UnsupportedFormatException("Json Object should contain one element");
34 Entry<String, JsonElement> childEntry = Lists.newArrayList(entrySetsOfRootJsonObject).get(0);
35 String firstElementName = childEntry.getKey();
36 JsonElement firstElementType = childEntry.getValue();
37 if (firstElementType.isJsonObject()) { // container in yang
38 return createStructureWithRoot(firstElementName, firstElementType.getAsJsonObject());
40 if (firstElementType.isJsonArray()) { // list in yang
41 if (firstElementType.getAsJsonArray().size() == 1) {
42 JsonElement firstElementInArray = firstElementType.getAsJsonArray().get(0);
43 if (firstElementInArray.isJsonObject()) {
44 return createStructureWithRoot(firstElementName, firstElementInArray.getAsJsonObject());
46 throw new UnsupportedFormatException(
47 "Array as the first element in Json Object can have only Object element");
50 throw new UnsupportedFormatException(
51 "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet.");
55 private CompositeNodeWrapper createStructureWithRoot(String rootObjectName, JsonObject rootObject) {
56 CompositeNodeWrapper firstNode = new CompositeNodeWrapper(getNamespaceFor(rootObjectName),
57 getLocalNameFor(rootObjectName));
58 for (Entry<String, JsonElement> childOfFirstNode : rootObject.entrySet()) {
59 addChildToParent(childOfFirstNode.getKey(), childOfFirstNode.getValue(), firstNode);
64 private void addChildToParent(String childName, JsonElement childType, CompositeNodeWrapper parent) {
65 if (childType.isJsonObject()) {
66 CompositeNodeWrapper child = new CompositeNodeWrapper(getNamespaceFor(childName),
67 getLocalNameFor(childName));
68 parent.addValue(child);
69 for (Entry<String, JsonElement> childOfChild : childType.getAsJsonObject().entrySet()) {
70 addChildToParent(childOfChild.getKey(), childOfChild.getValue(), child);
72 } else if (childType.isJsonArray()) {
73 if (childType.getAsJsonArray().size() == 1 && childType.getAsJsonArray().get(0).isJsonNull()) {
74 parent.addValue(new EmptyNodeWrapper(getNamespaceFor(childName), getLocalNameFor(childName)));
77 for (JsonElement childOfChildType : childType.getAsJsonArray()) {
78 addChildToParent(childName, childOfChildType, parent);
81 } else if (childType.isJsonPrimitive()) {
82 JsonPrimitive childPrimitive = childType.getAsJsonPrimitive();
83 String value = childPrimitive.getAsString();
84 parent.addValue(new SimpleNodeWrapper(getNamespaceFor(childName), getLocalNameFor(childName),
85 resolveValueOfElement(value)));
89 private URI getNamespaceFor(String jsonElementName) {
90 String[] moduleNameAndLocalName = jsonElementName.split(":");
91 if (moduleNameAndLocalName.length != 2) { // it is not "moduleName:localName"
94 return URI.create(moduleNameAndLocalName[0]);
97 private String getLocalNameFor(String jsonElementName) {
98 String[] moduleNameAndLocalName = jsonElementName.split(":");
99 if (moduleNameAndLocalName.length != 2) { // it is not "moduleName:localName"
100 return jsonElementName;
102 return moduleNameAndLocalName[1];
107 * value of json element
108 * @return if value is "moduleName:localName" then {@link IdentityValuesDTO} else
109 * the same string as parameter "value"
111 private Object resolveValueOfElement(String value) {
112 URI namespace = getNamespaceFor(value);
113 return namespace == null ? value : new IdentityValuesDTO(namespace.toString(), getLocalNameFor(value), null);