Bug 1224, Bug 1221: Added Test case scenarios for Data Change Events.
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / rest / impl / JsonToCompositeNodeReader.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 com.google.common.base.Splitter;
11 import com.google.common.collect.Iterators;
12 import com.google.gson.JsonElement;
13 import com.google.gson.JsonObject;
14 import com.google.gson.JsonPrimitive;
15 import com.google.gson.stream.JsonReader;
16
17 import java.io.InputStream;
18 import java.io.InputStreamReader;
19 import java.net.URI;
20 import java.util.Map.Entry;
21 import java.util.Iterator;
22 import java.util.Set;
23
24 import org.opendaylight.controller.sal.rest.gson.JsonParser;
25 import org.opendaylight.controller.sal.rest.impl.RestUtil.PrefixMapingFromJson;
26 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
27 import org.opendaylight.controller.sal.restconf.impl.EmptyNodeWrapper;
28 import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO;
29 import org.opendaylight.controller.sal.restconf.impl.SimpleNodeWrapper;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 class JsonToCompositeNodeReader {
34     private static final Logger LOG = LoggerFactory.getLogger(JsonReader.class);
35     private static final Splitter COLON_SPLITTER = Splitter.on(':');
36
37     private JsonToCompositeNodeReader() {
38
39     }
40
41     public static CompositeNodeWrapper read(final InputStream entityStream)
42                                                       throws UnsupportedFormatException {
43         JsonParser parser = new JsonParser();
44
45         JsonElement rootElement = parser.parse(new JsonReader(
46                                                     new InputStreamReader(entityStream)));
47         if (rootElement.isJsonNull()) {
48             // no content, so return null to indicate no input
49             return null;
50         }
51
52         if (!rootElement.isJsonObject()) {
53             throw new UnsupportedFormatException("Root element of Json has to be Object");
54         }
55
56         Set<Entry<String, JsonElement>> entrySetsOfRootJsonObject =
57                                                   rootElement.getAsJsonObject().entrySet();
58         if (entrySetsOfRootJsonObject.size() != 1) {
59             throw new UnsupportedFormatException("Json Object should contain one element");
60         }
61
62         Entry<String, JsonElement> childEntry = entrySetsOfRootJsonObject.iterator().next();
63         String firstElementName = childEntry.getKey();
64         JsonElement firstElementType = childEntry.getValue();
65         if (firstElementType.isJsonObject()) {
66             // container in yang
67             return createStructureWithRoot(firstElementName, firstElementType.getAsJsonObject());
68         }
69         if (firstElementType.isJsonArray()) {
70             // list in yang
71             if (firstElementType.getAsJsonArray().size() == 1) {
72                 JsonElement firstElementInArray = firstElementType.getAsJsonArray().get(0);
73                 if (firstElementInArray.isJsonObject()) {
74                     return createStructureWithRoot(firstElementName, firstElementInArray.getAsJsonObject());
75                 }
76                 throw new UnsupportedFormatException(
77                         "Array as the first element in Json Object can have only Object element");
78             }
79         }
80         throw new UnsupportedFormatException(
81                 "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet.");
82     }
83
84     private static CompositeNodeWrapper createStructureWithRoot(final String rootObjectName,
85                                                                 final JsonObject rootObject) {
86         CompositeNodeWrapper firstNode = new CompositeNodeWrapper(getNamespaceFor(rootObjectName),
87                 getLocalNameFor(rootObjectName));
88         for (Entry<String, JsonElement> childOfFirstNode : rootObject.entrySet()) {
89             addChildToParent(childOfFirstNode.getKey(), childOfFirstNode.getValue(), firstNode);
90         }
91         return firstNode;
92     }
93
94     private static void addChildToParent(final String childName, final JsonElement childType,
95                                          final CompositeNodeWrapper parent) {
96         if (childType.isJsonObject()) {
97             CompositeNodeWrapper child = new CompositeNodeWrapper(getNamespaceFor(childName),
98                     getLocalNameFor(childName));
99             parent.addValue(child);
100             for (Entry<String, JsonElement> childOfChild : childType.getAsJsonObject().entrySet()) {
101                 addChildToParent(childOfChild.getKey(), childOfChild.getValue(), child);
102             }
103         } else if (childType.isJsonArray()) {
104             if (childType.getAsJsonArray().size() == 1 && childType.getAsJsonArray().get(0).isJsonNull()) {
105                 parent.addValue(new EmptyNodeWrapper(getNamespaceFor(childName), getLocalNameFor(childName)));
106
107             } else {
108                 for (JsonElement childOfChildType : childType.getAsJsonArray()) {
109                     addChildToParent(childName, childOfChildType, parent);
110                 }
111             }
112         } else if (childType.isJsonPrimitive()) {
113             JsonPrimitive childPrimitive = childType.getAsJsonPrimitive();
114             String value = childPrimitive.getAsString().trim();
115             parent.addValue(new SimpleNodeWrapper(getNamespaceFor(childName), getLocalNameFor(childName),
116                     resolveValueOfElement(value)));
117         } else {
118             LOG.debug("Ignoring unhandled child type {}", childType);
119         }
120     }
121
122     private static URI getNamespaceFor(final String jsonElementName) {
123         final Iterator<String> it = COLON_SPLITTER.split(jsonElementName).iterator();
124
125         // The string needs to me in form "moduleName:localName"
126         if (it.hasNext()) {
127             final String maybeURI = it.next();
128             if (Iterators.size(it) == 1) {
129                 return URI.create(maybeURI);
130             }
131         }
132
133         return null;
134     }
135
136     private static String getLocalNameFor(final String jsonElementName) {
137         final Iterator<String> it = COLON_SPLITTER.split(jsonElementName).iterator();
138
139         // The string needs to me in form "moduleName:localName"
140         final String ret = Iterators.get(it, 1, null);
141         return ret != null && !it.hasNext() ? ret : jsonElementName;
142     }
143
144     private static Object resolveValueOfElement(final String value) {
145         // it could be instance-identifier Built-In Type
146         if (!value.isEmpty() && value.charAt(0) == '/') {
147             IdentityValuesDTO resolvedValue = RestUtil.asInstanceIdentifier(value, new PrefixMapingFromJson());
148             if (resolvedValue != null) {
149                 return resolvedValue;
150             }
151         }
152
153         // it could be identityref Built-In Type
154         URI namespace = getNamespaceFor(value);
155         if (namespace != null) {
156             return new IdentityValuesDTO(namespace.toString(), getLocalNameFor(value), null, value);
157         }
158
159         // it is not "prefix:value" but just "value"
160         return value;
161     }
162
163 }