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 java.io.InputStream;
11 import java.io.InputStreamReader;
13 import java.util.Map.Entry;
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;
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;
30 public CompositeNodeWrapper read(InputStream entityStream) throws UnsupportedFormatException {
31 JsonParser parser = new JsonParser();
33 JsonElement rootElement = parser.parse(new InputStreamReader(entityStream));
34 if( rootElement.isJsonNull() )
36 //no content, so return null to indicate no input
40 if (!rootElement.isJsonObject()) {
41 throw new UnsupportedFormatException("Root element of Json has to be Object");
44 Set<Entry<String, JsonElement>> entrySetsOfRootJsonObject = rootElement.getAsJsonObject().entrySet();
45 if (entrySetsOfRootJsonObject.size() != 1) {
46 throw new UnsupportedFormatException("Json Object should contain one element");
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());
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());
60 throw new UnsupportedFormatException(
61 "Array as the first element in Json Object can have only Object element");
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.");
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);
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);
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)));
90 for (JsonElement childOfChildType : childType.getAsJsonArray()) {
91 addChildToParent(childName, childOfChildType, parent);
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)));
102 private URI getNamespaceFor(String jsonElementName) {
103 String[] moduleNameAndLocalName = jsonElementName.split(":");
104 // it is not "moduleName:localName"
105 if (moduleNameAndLocalName.length != 2) {
108 return URI.create(moduleNameAndLocalName[0]);
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;
117 return moduleNameAndLocalName[1];
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;
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);
133 // it is not "prefix:value" but just "value"