Merge topic 'stable/helium'
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / rest / impl / JsonToCompositeNodeReader.java
index 082675b6d135e4fa3be05017a34a2ff584da9008..552e2bbd190d154280e1a8ce7a3bc22d89de908b 100644 (file)
@@ -13,14 +13,12 @@ import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonPrimitive;
 import com.google.gson.stream.JsonReader;
-
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.net.URI;
-import java.util.Map.Entry;
 import java.util.Iterator;
+import java.util.Map.Entry;
 import java.util.Set;
-
 import org.opendaylight.controller.sal.rest.gson.JsonParser;
 import org.opendaylight.controller.sal.rest.impl.RestUtil.PrefixMapingFromJson;
 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
@@ -31,19 +29,17 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 class JsonToCompositeNodeReader {
-    private static final Logger LOG = LoggerFactory.getLogger(JsonReader.class);
+    private static final Logger LOG = LoggerFactory.getLogger(JsonToCompositeNodeReader.class);
     private static final Splitter COLON_SPLITTER = Splitter.on(':');
 
     private JsonToCompositeNodeReader() {
 
     }
 
-    public static CompositeNodeWrapper read(final InputStream entityStream)
-                                                      throws UnsupportedFormatException {
+    public static CompositeNodeWrapper read(final InputStream entityStream) throws UnsupportedFormatException {
         JsonParser parser = new JsonParser();
 
-        JsonElement rootElement = parser.parse(new JsonReader(
-                                                    new InputStreamReader(entityStream)));
+        JsonElement rootElement = parser.parse(new JsonReader(new InputStreamReader(entityStream)));
         if (rootElement.isJsonNull()) {
             // no content, so return null to indicate no input
             return null;
@@ -53,8 +49,7 @@ class JsonToCompositeNodeReader {
             throw new UnsupportedFormatException("Root element of Json has to be Object");
         }
 
-        Set<Entry<String, JsonElement>> entrySetsOfRootJsonObject =
-                                                  rootElement.getAsJsonObject().entrySet();
+        Set<Entry<String, JsonElement>> entrySetsOfRootJsonObject = rootElement.getAsJsonObject().entrySet();
         if (entrySetsOfRootJsonObject.size() != 1) {
             throw new UnsupportedFormatException("Json Object should contain one element");
         }
@@ -81,8 +76,7 @@ class JsonToCompositeNodeReader {
                 "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet.");
     }
 
-    private static CompositeNodeWrapper createStructureWithRoot(final String rootObjectName,
-                                                                final JsonObject rootObject) {
+    private static CompositeNodeWrapper createStructureWithRoot(final String rootObjectName, final JsonObject rootObject) {
         CompositeNodeWrapper firstNode = new CompositeNodeWrapper(getNamespaceFor(rootObjectName),
                 getLocalNameFor(rootObjectName));
         for (Entry<String, JsonElement> childOfFirstNode : rootObject.entrySet()) {
@@ -92,7 +86,7 @@ class JsonToCompositeNodeReader {
     }
 
     private static void addChildToParent(final String childName, final JsonElement childType,
-                                         final CompositeNodeWrapper parent) {
+            final CompositeNodeWrapper parent) {
         if (childType.isJsonObject()) {
             CompositeNodeWrapper child = new CompositeNodeWrapper(getNamespaceFor(childName),
                     getLocalNameFor(childName));
@@ -119,14 +113,29 @@ class JsonToCompositeNodeReader {
         }
     }
 
+    /**
+     * Transform input value to URI instance.
+     *
+     * Input string has to be in format moduleName:localName. moduleName part is then transformed to URI instance.
+     * If moduleName part contains character like "<" or ">" then null value is returned because they
+     * aren't valid URI characters.
+     *
+     * @param jsonElementName
+     *  value in format moduleName:localName
+     * @return
+     */
     private static URI getNamespaceFor(final String jsonElementName) {
         final Iterator<String> it = COLON_SPLITTER.split(jsonElementName).iterator();
 
-        // The string needs to me in form "moduleName:localName"
+        // The string needs to be in form "moduleName:localName"
         if (it.hasNext()) {
             final String maybeURI = it.next();
             if (Iterators.size(it) == 1) {
-                return URI.create(maybeURI);
+                try {
+                    return URI.create(maybeURI);
+                } catch (IllegalArgumentException e) {
+                    LOG.debug("Value {} couldn't be interpreted as URI.", maybeURI);
+                }
             }
         }
 
@@ -150,7 +159,7 @@ class JsonToCompositeNodeReader {
             }
         }
 
-        // it could be identityref Built-In Type
+        // it could be identityref Built-In Type therefore it is necessary to look at value as module_name:local_name
         URI namespace = getNamespaceFor(value);
         if (namespace != null) {
             return new IdentityValuesDTO(namespace.toString(), getLocalNameFor(value), null, value);