CLAMP should not display all CDS workflow properties
[clamp.git] / src / main / java / org / onap / clamp / clds / client / CdsServices.java
index f25e8b8..fd3b353 100644 (file)
@@ -22,6 +22,8 @@
 \r
 package org.onap.clamp.clds.client;\r
 \r
+import static java.lang.Boolean.parseBoolean;\r
+\r
 import com.att.eelf.configuration.EELFLogger;\r
 import com.att.eelf.configuration.EELFManager;\r
 import com.google.gson.JsonElement;\r
@@ -34,6 +36,7 @@ import java.util.Map;
 import org.apache.camel.CamelContext;\r
 import org.apache.camel.Exchange;\r
 import org.apache.camel.builder.ExchangeBuilder;\r
+import org.onap.clamp.clds.exception.cds.CdsParametersException;\r
 import org.onap.clamp.clds.model.cds.CdsBpWorkFlowListResponse;\r
 import org.onap.clamp.clds.util.JsonUtils;\r
 import org.onap.clamp.clds.util.LoggingUtils;\r
@@ -51,6 +54,10 @@ public class CdsServices {
 \r
     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CdsServices.class);\r
 \r
+    private static final String TYPE = "type";\r
+    private static final String PROPERTIES = "properties";\r
+    private static final String LIST = "list";\r
+\r
     /**\r
      * Constructor.\r
      */\r
@@ -83,8 +90,11 @@ public class CdsServices {
             Date startTime = new Date();\r
             LoggingUtils.setTimeContext(startTime, new Date());\r
             return JsonUtils.GSON_JPA_MODEL.fromJson(cdsResponse, CdsBpWorkFlowListResponse.class);\r
+        } else {\r
+            logger.error("CDS getBlueprintWorkflowList FAILED");\r
+            return null;\r
         }\r
-        return null;\r
+\r
     }\r
 \r
     /**\r
@@ -113,44 +123,92 @@ public class CdsServices {
             Date startTime = new Date();\r
             LoggingUtils.setTimeContext(startTime, new Date());\r
             return parseCdsResponse(cdsResponse);\r
+        } else {\r
+            logger.error("CDS getWorkflowInputProperties FAILED");\r
+            return null;\r
         }\r
-        return null;\r
     }\r
 \r
-    private JsonObject parseCdsResponse(String response) {\r
+    protected JsonObject parseCdsResponse(String response) {\r
         JsonObject root = JsonParser.parseString(response).getAsJsonObject();\r
         JsonObject inputs = root.getAsJsonObject("workFlowData").getAsJsonObject("inputs");\r
         JsonObject dataTypes = root.getAsJsonObject("dataTypes");\r
 \r
         JsonObject workFlowProperties = new JsonObject();\r
-        workFlowProperties.add("inputs", getInputProperties(inputs, dataTypes));\r
+        workFlowProperties.add("inputs", getInputProperties(inputs, dataTypes, new JsonObject()));\r
         return workFlowProperties;\r
     }\r
 \r
-    private JsonObject getInputProperties(JsonObject inputs, JsonObject dataTypes) {\r
-        JsonObject inputObject = new JsonObject();\r
+    private JsonObject getInputProperties(JsonObject inputs, JsonObject dataTypes,\r
+                                          JsonObject inputObject) {\r
+        if (inputs == null) {\r
+            return inputObject;\r
+        }\r
+\r
         for (Map.Entry<String, JsonElement> entry : inputs.entrySet()) {\r
             String key = entry.getKey();\r
             JsonObject inputProperty = inputs.getAsJsonObject(key);\r
-            String type = inputProperty.get("type").getAsString();\r
+            String type = inputProperty.get(TYPE).getAsString();\r
             if (isComplexType(type, dataTypes)) {\r
                 inputObject.add(key, handleComplexType(type, dataTypes));\r
-            } else {\r
+            } else if (LIST.equalsIgnoreCase(type)) {\r
+                handleListType(key, inputProperty, dataTypes, inputObject);\r
+            } else if (isInputParam(inputProperty)) {\r
                 inputObject.add(key, entry.getValue());\r
             }\r
         }\r
         return inputObject;\r
     }\r
 \r
+    private void handleListType(String propertyName,\r
+                                      JsonObject inputProperty,\r
+                                      JsonObject dataTypes,\r
+                                      JsonObject inputObject) {\r
+        if (inputProperty.get("entry_schema") == null) {\r
+            throw new CdsParametersException("Entry schema is null for " + propertyName);\r
+        }\r
+\r
+        String type = inputProperty.get("entry_schema").getAsJsonObject().get(\r
+                TYPE).getAsString();\r
+        if (dataTypes.get(type) != null) {\r
+            JsonObject jsonObject = new JsonObject();\r
+            jsonObject.addProperty(TYPE, LIST);\r
+            jsonObject.add(PROPERTIES, getPropertiesObject(type, dataTypes));\r
+            inputObject.add(propertyName, jsonObject);\r
+        } else if (isInputParam(inputProperty)) {\r
+            inputObject.add(propertyName, inputProperty);\r
+        }\r
+    }\r
+\r
     private JsonObject handleComplexType(String key, JsonObject dataTypes) {\r
-        JsonObject properties = dataTypes.get(key).getAsJsonObject().get("properties").getAsJsonObject();\r
-        return getInputProperties(properties, dataTypes);\r
+        JsonObject jsonObject = new JsonObject();\r
+        jsonObject.addProperty(TYPE, "object");\r
+        jsonObject.add(PROPERTIES, getPropertiesObject(key, dataTypes));\r
+        return jsonObject;\r
+    }\r
+\r
+    private JsonObject getPropertiesObject(String key, JsonObject dataTypes) {\r
+        JsonObject properties = dataTypes.get(key).getAsJsonObject().get(PROPERTIES).getAsJsonObject();\r
+        JsonObject object = new JsonObject();\r
+        getInputProperties(properties, dataTypes, object);\r
+        return object;\r
     }\r
 \r
     private boolean isComplexType(String type, JsonObject dataTypes) {\r
+        if (dataTypes == null) {\r
+            return false;\r
+        }\r
         return dataTypes.get(type) != null;\r
     }\r
 \r
+    private boolean isInputParam(JsonObject inputProperty) {\r
+        JsonElement inputParam = inputProperty.get("input-param");\r
+        if (inputParam == null) {\r
+            return false;\r
+        }\r
+        return parseBoolean(inputParam.getAsString());\r
+    }\r
+\r
     /**\r
      * Creates payload to query CDS to get workflow input properties.\r
      *\r