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.rest.impl.RestUtil.PrefixMapingFromJson;
10 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
11 import org.opendaylight.controller.sal.restconf.impl.EmptyNodeWrapper;
12 import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO;
13 import org.opendaylight.controller.sal.restconf.impl.SimpleNodeWrapper;
15 import com.google.common.collect.Lists;
16 import com.google.gson.JsonElement;
17 import com.google.gson.JsonObject;
18 import com.google.gson.JsonParser;
19 import com.google.gson.JsonPrimitive;
23 public CompositeNodeWrapper read(InputStream entityStream) throws UnsupportedFormatException {
24 JsonParser parser = new JsonParser();
26 JsonElement rootElement = parser.parse(new InputStreamReader(entityStream));
27 if (!rootElement.isJsonObject()) {
28 throw new UnsupportedFormatException("Root element of Json has to be Object");
31 Set<Entry<String, JsonElement>> entrySetsOfRootJsonObject = rootElement.getAsJsonObject().entrySet();
32 if (entrySetsOfRootJsonObject.size() != 1) {
33 throw new UnsupportedFormatException("Json Object should contain one element");
35 Entry<String, JsonElement> childEntry = Lists.newArrayList(entrySetsOfRootJsonObject).get(0);
36 String firstElementName = childEntry.getKey();
37 JsonElement firstElementType = childEntry.getValue();
38 if (firstElementType.isJsonObject()) { // container in yang
39 return createStructureWithRoot(firstElementName, firstElementType.getAsJsonObject());
41 if (firstElementType.isJsonArray()) { // list in yang
42 if (firstElementType.getAsJsonArray().size() == 1) {
43 JsonElement firstElementInArray = firstElementType.getAsJsonArray().get(0);
44 if (firstElementInArray.isJsonObject()) {
45 return createStructureWithRoot(firstElementName, firstElementInArray.getAsJsonObject());
47 throw new UnsupportedFormatException(
48 "Array as the first element in Json Object can have only Object element");
51 throw new UnsupportedFormatException(
52 "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet.");
56 private CompositeNodeWrapper createStructureWithRoot(String rootObjectName, JsonObject rootObject) {
57 CompositeNodeWrapper firstNode = new CompositeNodeWrapper(getNamespaceFor(rootObjectName),
58 getLocalNameFor(rootObjectName));
59 for (Entry<String, JsonElement> childOfFirstNode : rootObject.entrySet()) {
60 addChildToParent(childOfFirstNode.getKey(), childOfFirstNode.getValue(), firstNode);
65 private void addChildToParent(String childName, JsonElement childType, CompositeNodeWrapper parent) {
66 if (childType.isJsonObject()) {
67 CompositeNodeWrapper child = new CompositeNodeWrapper(getNamespaceFor(childName), 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 // it is not "moduleName:localName"
92 if (moduleNameAndLocalName.length != 2) {
95 return URI.create(moduleNameAndLocalName[0]);
98 private String getLocalNameFor(String jsonElementName) {
99 String[] moduleNameAndLocalName = jsonElementName.split(":");
100 // it is not "moduleName:localName"
101 if (moduleNameAndLocalName.length != 2) {
102 return jsonElementName;
104 return moduleNameAndLocalName[1];
107 private Object resolveValueOfElement(String value) {
108 // it could be instance-identifier Built-In Type
109 if (value.startsWith("/")) {
110 IdentityValuesDTO resolvedValue = RestUtil.asInstanceIdentifier(value, new PrefixMapingFromJson());
111 if (resolvedValue != null) {
112 return resolvedValue;
115 // it could be identityref Built-In Type
116 URI namespace = getNamespaceFor(value);
117 if (namespace != null) {
118 return new IdentityValuesDTO(namespace.toString(), getLocalNameFor(value), null);
120 // it is not "prefix:value" but just "value"