1f7b061e921cd057d13ece0002e998973fb66db2
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / rest / impl / JsonReader.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.sal.rest.impl;
9
10 import java.io.InputStream;
11 import java.io.InputStreamReader;
12 import java.net.URI;
13 import java.util.Map.Entry;
14 import java.util.Set;
15
16 import org.opendaylight.controller.sal.rest.impl.RestUtil.PrefixMapingFromJson;
17 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
18 import org.opendaylight.controller.sal.restconf.impl.EmptyNodeWrapper;
19 import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO;
20 import org.opendaylight.controller.sal.restconf.impl.SimpleNodeWrapper;
21
22 import com.google.common.collect.Lists;
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonObject;
25 import com.google.gson.JsonParser;
26 import com.google.gson.JsonPrimitive;
27
28 class JsonReader {
29
30     public CompositeNodeWrapper read(InputStream entityStream) throws UnsupportedFormatException {
31         JsonParser parser = new JsonParser();
32
33         JsonElement rootElement = parser.parse(new InputStreamReader(entityStream));
34         if( rootElement.isJsonNull() )
35         {
36             //no content, so return null to indicate no input
37             return null;
38         }
39
40         if (!rootElement.isJsonObject()) {
41             throw new UnsupportedFormatException("Root element of Json has to be Object");
42         }
43
44         Set<Entry<String, JsonElement>> entrySetsOfRootJsonObject = rootElement.getAsJsonObject().entrySet();
45         if (entrySetsOfRootJsonObject.size() != 1) {
46             throw new UnsupportedFormatException("Json Object should contain one element");
47         } else {
48             Entry<String, JsonElement> childEntry = Lists.newArrayList(entrySetsOfRootJsonObject).get(0);
49             String firstElementName = childEntry.getKey();
50             JsonElement firstElementType = childEntry.getValue();
51             if (firstElementType.isJsonObject()) { // container in yang
52                 return createStructureWithRoot(firstElementName, firstElementType.getAsJsonObject());
53             }
54             if (firstElementType.isJsonArray()) { // list in yang
55                 if (firstElementType.getAsJsonArray().size() == 1) {
56                     JsonElement firstElementInArray = firstElementType.getAsJsonArray().get(0);
57                     if (firstElementInArray.isJsonObject()) {
58                         return createStructureWithRoot(firstElementName, firstElementInArray.getAsJsonObject());
59                     }
60                     throw new UnsupportedFormatException(
61                             "Array as the first element in Json Object can have only Object element");
62                 }
63             }
64             throw new UnsupportedFormatException(
65                     "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet.");
66         }
67     }
68
69     private CompositeNodeWrapper createStructureWithRoot(String rootObjectName, JsonObject rootObject) {
70         CompositeNodeWrapper firstNode = new CompositeNodeWrapper(getNamespaceFor(rootObjectName),
71                 getLocalNameFor(rootObjectName));
72         for (Entry<String, JsonElement> childOfFirstNode : rootObject.entrySet()) {
73             addChildToParent(childOfFirstNode.getKey(), childOfFirstNode.getValue(), firstNode);
74         }
75         return firstNode;
76     }
77
78     private void addChildToParent(String childName, JsonElement childType, CompositeNodeWrapper parent) {
79         if (childType.isJsonObject()) {
80             CompositeNodeWrapper child = new CompositeNodeWrapper(getNamespaceFor(childName), getLocalNameFor(childName));
81             parent.addValue(child);
82             for (Entry<String, JsonElement> childOfChild : childType.getAsJsonObject().entrySet()) {
83                 addChildToParent(childOfChild.getKey(), childOfChild.getValue(), child);
84             }
85         } else if (childType.isJsonArray()) {
86             if (childType.getAsJsonArray().size() == 1 && childType.getAsJsonArray().get(0).isJsonNull()) {
87                 parent.addValue(new EmptyNodeWrapper(getNamespaceFor(childName), getLocalNameFor(childName)));
88
89             } else {
90                 for (JsonElement childOfChildType : childType.getAsJsonArray()) {
91                     addChildToParent(childName, childOfChildType, parent);
92                 }
93             }
94         } else if (childType.isJsonPrimitive()) {
95             JsonPrimitive childPrimitive = childType.getAsJsonPrimitive();
96             String value = childPrimitive.getAsString().trim();
97             parent.addValue(new SimpleNodeWrapper(getNamespaceFor(childName), getLocalNameFor(childName),
98                     resolveValueOfElement(value)));
99         }
100     }
101
102     private URI getNamespaceFor(String jsonElementName) {
103         String[] moduleNameAndLocalName = jsonElementName.split(":");
104         // it is not "moduleName:localName"
105         if (moduleNameAndLocalName.length != 2) {
106             return null;
107         }
108         return URI.create(moduleNameAndLocalName[0]);
109     }
110
111     private String getLocalNameFor(String jsonElementName) {
112         String[] moduleNameAndLocalName = jsonElementName.split(":");
113         // it is not "moduleName:localName"
114         if (moduleNameAndLocalName.length != 2) {
115             return jsonElementName;
116         }
117         return moduleNameAndLocalName[1];
118     }
119
120     private Object resolveValueOfElement(String value) {
121         // it could be instance-identifier Built-In Type
122         if (value.startsWith("/")) {
123             IdentityValuesDTO resolvedValue = RestUtil.asInstanceIdentifier(value, new PrefixMapingFromJson());
124             if (resolvedValue != null) {
125                 return resolvedValue;
126             }
127         }
128         // it could be identityref Built-In Type
129         URI namespace = getNamespaceFor(value);
130         if (namespace != null) {
131             return new IdentityValuesDTO(namespace.toString(), getLocalNameFor(value), null,value);
132         }
133         // it is not "prefix:value" but just "value"
134         return value;
135     }
136
137 }