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;
17 import java.io.InputStream;
18 import java.io.InputStreamReader;
20 import java.util.Map.Entry;
21 import java.util.Iterator;
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;
33 class JsonToCompositeNodeReader {
34 private static final Logger LOG = LoggerFactory.getLogger(JsonReader.class);
35 private static final Splitter COLON_SPLITTER = Splitter.on(':');
37 private JsonToCompositeNodeReader() {
41 public static CompositeNodeWrapper read(final InputStream entityStream)
42 throws UnsupportedFormatException {
43 JsonParser parser = new JsonParser();
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
52 if (!rootElement.isJsonObject()) {
53 throw new UnsupportedFormatException("Root element of Json has to be Object");
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");
62 Entry<String, JsonElement> childEntry = entrySetsOfRootJsonObject.iterator().next();
63 String firstElementName = childEntry.getKey();
64 JsonElement firstElementType = childEntry.getValue();
65 if (firstElementType.isJsonObject()) {
67 return createStructureWithRoot(firstElementName, firstElementType.getAsJsonObject());
69 if (firstElementType.isJsonArray()) {
71 if (firstElementType.getAsJsonArray().size() == 1) {
72 JsonElement firstElementInArray = firstElementType.getAsJsonArray().get(0);
73 if (firstElementInArray.isJsonObject()) {
74 return createStructureWithRoot(firstElementName, firstElementInArray.getAsJsonObject());
76 throw new UnsupportedFormatException(
77 "Array as the first element in Json Object can have only Object element");
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.");
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);
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);
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)));
108 for (JsonElement childOfChildType : childType.getAsJsonArray()) {
109 addChildToParent(childName, childOfChildType, parent);
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)));
118 LOG.debug("Ignoring unhandled child type {}", childType);
122 private static URI getNamespaceFor(final String jsonElementName) {
123 final Iterator<String> it = COLON_SPLITTER.split(jsonElementName).iterator();
125 // The string needs to me in form "moduleName:localName"
127 final String maybeURI = it.next();
128 if (Iterators.size(it) == 1) {
129 return URI.create(maybeURI);
136 private static String getLocalNameFor(final String jsonElementName) {
137 final Iterator<String> it = COLON_SPLITTER.split(jsonElementName).iterator();
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;
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;
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);
159 // it is not "prefix:value" but just "value"