2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.rest.impl;
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 import java.io.InputStream;
17 import java.io.InputStreamReader;
19 import java.util.Iterator;
20 import java.util.Map.Entry;
22 import org.opendaylight.controller.sal.rest.gson.JsonParser;
23 import org.opendaylight.controller.sal.rest.impl.RestUtil.PrefixMapingFromJson;
24 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
25 import org.opendaylight.controller.sal.restconf.impl.EmptyNodeWrapper;
26 import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO;
27 import org.opendaylight.controller.sal.restconf.impl.SimpleNodeWrapper;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 class JsonToCompositeNodeReader {
32 private static final Logger LOG = LoggerFactory.getLogger(JsonToCompositeNodeReader.class);
33 private static final Splitter COLON_SPLITTER = Splitter.on(':');
35 private JsonToCompositeNodeReader() {
39 public static CompositeNodeWrapper read(final InputStream entityStream) throws UnsupportedFormatException {
40 JsonParser parser = new JsonParser();
42 JsonElement rootElement = parser.parse(new JsonReader(new InputStreamReader(entityStream)));
43 if (rootElement.isJsonNull()) {
44 // no content, so return null to indicate no input
48 if (!rootElement.isJsonObject()) {
49 throw new UnsupportedFormatException("Root element of Json has to be Object");
52 Set<Entry<String, JsonElement>> entrySetsOfRootJsonObject = rootElement.getAsJsonObject().entrySet();
53 if (entrySetsOfRootJsonObject.size() != 1) {
54 throw new UnsupportedFormatException("Json Object should contain one element");
57 Entry<String, JsonElement> childEntry = entrySetsOfRootJsonObject.iterator().next();
58 String firstElementName = childEntry.getKey();
59 JsonElement firstElementType = childEntry.getValue();
60 if (firstElementType.isJsonObject()) {
62 return createStructureWithRoot(firstElementName, firstElementType.getAsJsonObject());
64 if (firstElementType.isJsonArray()) {
66 if (firstElementType.getAsJsonArray().size() == 1) {
67 JsonElement firstElementInArray = firstElementType.getAsJsonArray().get(0);
68 if (firstElementInArray.isJsonObject()) {
69 return createStructureWithRoot(firstElementName, firstElementInArray.getAsJsonObject());
71 throw new UnsupportedFormatException(
72 "Array as the first element in Json Object can have only Object element");
75 throw new UnsupportedFormatException(
76 "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet.");
79 private static CompositeNodeWrapper createStructureWithRoot(final String rootObjectName, final JsonObject rootObject) {
80 CompositeNodeWrapper firstNode = new CompositeNodeWrapper(getNamespaceFor(rootObjectName),
81 getLocalNameFor(rootObjectName));
82 for (Entry<String, JsonElement> childOfFirstNode : rootObject.entrySet()) {
83 addChildToParent(childOfFirstNode.getKey(), childOfFirstNode.getValue(), firstNode);
88 private static void addChildToParent(final String childName, final JsonElement childType,
89 final CompositeNodeWrapper parent) {
90 if (childType.isJsonObject()) {
91 CompositeNodeWrapper child = new CompositeNodeWrapper(getNamespaceFor(childName),
92 getLocalNameFor(childName));
93 parent.addValue(child);
94 for (Entry<String, JsonElement> childOfChild : childType.getAsJsonObject().entrySet()) {
95 addChildToParent(childOfChild.getKey(), childOfChild.getValue(), child);
97 } else if (childType.isJsonArray()) {
98 if (childType.getAsJsonArray().size() == 1 && childType.getAsJsonArray().get(0).isJsonNull()) {
99 parent.addValue(new EmptyNodeWrapper(getNamespaceFor(childName), getLocalNameFor(childName)));
102 for (JsonElement childOfChildType : childType.getAsJsonArray()) {
103 addChildToParent(childName, childOfChildType, parent);
106 } else if (childType.isJsonPrimitive()) {
107 JsonPrimitive childPrimitive = childType.getAsJsonPrimitive();
108 String value = childPrimitive.getAsString().trim();
109 parent.addValue(new SimpleNodeWrapper(getNamespaceFor(childName), getLocalNameFor(childName),
110 resolveValueOfElement(value)));
112 LOG.debug("Ignoring unhandled child type {}", childType);
117 * Transform input value to URI instance.
119 * Input string has to be in format moduleName:localName. moduleName part is then transformed to URI instance.
120 * If moduleName part contains character like "<" or ">" then null value is returned because they
121 * aren't valid URI characters.
123 * @param jsonElementName
124 * value in format moduleName:localName
127 private static URI getNamespaceFor(final String jsonElementName) {
128 final Iterator<String> it = COLON_SPLITTER.split(jsonElementName).iterator();
130 // The string needs to be in form "moduleName:localName"
132 final String maybeURI = it.next();
133 if (Iterators.size(it) == 1) {
135 return URI.create(maybeURI);
136 } catch (IllegalArgumentException e) {
137 LOG.debug("Value {} couldn't be interpreted as URI.", maybeURI);
145 private static String getLocalNameFor(final String jsonElementName) {
146 final Iterator<String> it = COLON_SPLITTER.split(jsonElementName).iterator();
148 // The string needs to me in form "moduleName:localName"
149 final String ret = Iterators.get(it, 1, null);
150 return ret != null && !it.hasNext() ? ret : jsonElementName;
153 private static Object resolveValueOfElement(final String value) {
154 // it could be instance-identifier Built-In Type
155 if (!value.isEmpty() && value.charAt(0) == '/') {
156 IdentityValuesDTO resolvedValue = RestUtil.asInstanceIdentifier(value, new PrefixMapingFromJson());
157 if (resolvedValue != null) {
158 return resolvedValue;
162 // it could be identityref Built-In Type therefore it is necessary to look at value as module_name:local_name
163 URI namespace = getNamespaceFor(value);
164 if (namespace != null) {
165 return new IdentityValuesDTO(namespace.toString(), getLocalNameFor(value), null, value);
168 // it is not "prefix:value" but just "value"