CLAMP should not display all CDS workflow properties
[clamp.git] / src / main / java / org / onap / clamp / clds / tosca / update / execution / cds / ToscaMetadataCdsProcess.java
index 81e30cf..39fa25a 100644 (file)
 
 package org.onap.clamp.clds.tosca.update.execution.cds;
 
+import static org.onap.clamp.clds.tosca.ToscaSchemaConstants.TYPE;
+import static org.onap.clamp.clds.tosca.ToscaSchemaConstants.TYPE_LIST;
+
 import com.google.gson.JsonArray;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 import java.util.Map;
+import java.util.Set;
 import org.onap.clamp.clds.tosca.update.execution.ToscaMetadataProcess;
 import org.onap.clamp.loop.service.Service;
-import org.onap.clamp.tosca.DictionaryService;
-import org.springframework.beans.factory.annotation.Autowired;
 
 /**
  * This class is there to add the JsonObject for CDS in the json Schema according to what is found in the Tosca model.
  */
 public class ToscaMetadataCdsProcess extends ToscaMetadataProcess {
 
-    @Autowired
-    private DictionaryService dictionaryService;
-
     @Override
     public void executeProcess(String parameters, JsonObject childObject, Service serviceModel) {
         switch (parameters) {
@@ -54,13 +53,16 @@ public class ToscaMetadataCdsProcess extends ToscaMetadataProcess {
             case "operation":
                 generateOperation(childObject, serviceModel);
                 break;
+            default:
         }
     }
 
     private static void generatePayload(JsonObject childObject, Service serviceModel) {
-        generatePayloadPerResource(childObject, "VF", serviceModel);
-        generatePayloadPerResource(childObject, "PNF", serviceModel);
-        addToJsonArray(childObject, "anyOf", createBlankEntry());
+        JsonArray schemaAnyOf = new JsonArray();
+        schemaAnyOf.addAll(createBlankEntry());
+        schemaAnyOf.addAll(generatePayloadPerResource("VF", serviceModel));
+        schemaAnyOf.addAll(generatePayloadPerResource("PNF", serviceModel));
+        addToJsonArray(childObject, "anyOf", schemaAnyOf);
     }
 
     private static void generateOperation(JsonObject childObject, Service serviceModel) {
@@ -76,7 +78,7 @@ public class ToscaMetadataCdsProcess extends ToscaMetadataProcess {
                 .entrySet()) {
             JsonObject controllerProperties = entry.getValue().getAsJsonObject()
                     .getAsJsonObject("controllerProperties");
-            if (controllerProperties != null) {
+            if (controllerProperties != null && controllerProperties.getAsJsonObject("workflows") != null) {
                 for (String workflowsEntry : controllerProperties.getAsJsonObject("workflows").keySet()) {
                     schemaEnum.add(workflowsEntry);
                     schemaTitle.add(workflowsEntry + " (CDS operation)");
@@ -92,26 +94,28 @@ public class ToscaMetadataCdsProcess extends ToscaMetadataProcess {
 
     }
 
-    private static void generatePayloadPerResource(JsonObject childObject, String resourceName,
-                                                   Service serviceModel) {
+    private static JsonArray generatePayloadPerResource(String resourceName,
+                                                        Service serviceModel) {
         JsonArray schemaAnyOf = new JsonArray();
 
         for (Map.Entry<String, JsonElement> entry : serviceModel.getResourceDetails().getAsJsonObject(resourceName)
                 .entrySet()) {
             JsonObject controllerProperties = entry.getValue().getAsJsonObject()
                     .getAsJsonObject("controllerProperties");
-            if (controllerProperties != null) {
+            if (controllerProperties != null && controllerProperties.getAsJsonObject("workflows") != null) {
                 for (Map.Entry<String, JsonElement> workflowsEntry : controllerProperties.getAsJsonObject("workflows")
                         .entrySet()) {
                     JsonObject obj = new JsonObject();
                     obj.addProperty("title", workflowsEntry.getKey());
-                    obj.add("properties", createPayloadProperty(workflowsEntry.getValue().getAsJsonObject(),
-                            controllerProperties));
+                    obj.add("properties",
+                            createInputPropertiesForPayload(workflowsEntry.getValue().getAsJsonObject(),
+                                                            controllerProperties,
+                                                            workflowsEntry.getKey()));
                     schemaAnyOf.add(obj);
                 }
             }
         }
-        addToJsonArray(childObject, "anyOf", schemaAnyOf);
+        return schemaAnyOf;
     }
 
     private static JsonArray createBlankEntry() {
@@ -123,43 +127,94 @@ public class ToscaMetadataCdsProcess extends ToscaMetadataProcess {
         return result;
     }
 
-    private static JsonObject createPayloadProperty(JsonObject workFlow, JsonObject controllerProperties) {
-        JsonObject payloadResult = new JsonObject();
-
-        payloadResult.add("artifact_name",
-                createAnyOfJsonProperty("artifact_name", controllerProperties.get("sdnc_model_name").getAsString()));
-        payloadResult.add("artifact_version",
-                createAnyOfJsonProperty("artifact_version",
-                        controllerProperties.get("sdnc_model_version").getAsString()));
-        payloadResult.add("mode", createAnyOfJsonProperty("mode", "async"));
-
-        payloadResult.add("data", createAnyOfJsonObject("data", workFlow.getAsJsonObject("inputs")));
-        return payloadResult;
-    }
-
-    private static JsonObject createAnyOfJsonProperty(String name, String defaultValue) {
+    private static JsonObject createAnyOfJsonProperty(String name,
+                                                      String defaultValue,
+                                                      boolean readOnlyFlag) {
         JsonObject result = new JsonObject();
         result.addProperty("title", name);
         result.addProperty("type", "string");
         result.addProperty("default", defaultValue);
-        result.addProperty("readOnly", "True");
-        return result;
-    }
-
-    private static JsonObject createAnyOfJsonObject(String name, JsonObject allProperties) {
-        JsonObject result = new JsonObject();
-        result.addProperty("title", name);
-        result.addProperty("type", "object");
-        result.add("properties", allProperties);
+        result.addProperty("readOnly", readOnlyFlag);
         return result;
     }
 
     private static void addToJsonArray(JsonObject childObject, String section, JsonArray value) {
         if (childObject.getAsJsonArray(section) != null) {
             childObject.getAsJsonArray(section).addAll(value);
-        }
-        else {
+        } else {
             childObject.add(section, value);
         }
     }
+
+    /**
+     * Returns the properties of payload based on the cds work flows.
+     *
+     * @param workFlow cds work flows to update payload
+     * @param controllerProperties cds properties to get blueprint name and
+     *                            version
+     * @param workFlowName work flow name
+     * @return returns the properties of payload
+     */
+    public static JsonObject createInputPropertiesForPayload(JsonObject workFlow,
+                                                             JsonObject controllerProperties,
+                                                             String workFlowName) {
+        String artifactName = controllerProperties.get("sdnc_model_name").getAsString();
+        String artifactVersion = controllerProperties.get("sdnc_model_version").getAsString();
+        JsonObject inputs = workFlow.getAsJsonObject("inputs");
+        JsonObject jsonObject = new JsonObject();
+        jsonObject.add("artifact_name", createAnyOfJsonProperty(
+                "artifact name", artifactName, true));
+        jsonObject.add("artifact_version", createAnyOfJsonProperty(
+                "artifact version", artifactVersion, true));
+        jsonObject.add("mode", createAnyOfJsonProperty(
+                "mode", "async", false));
+        jsonObject.add("data", createDataProperty(inputs, workFlowName));
+        return jsonObject;
+    }
+
+    private static JsonObject createDataProperty(JsonObject inputs, String workFlowName) {
+        JsonObject data = new JsonObject();
+        data.addProperty("title", "data");
+        data.addProperty("type", "string");
+        data.addProperty("format", "textarea");
+        JsonObject defaultValue = new JsonObject();
+        addDefaultValueForData(inputs, defaultValue, workFlowName);
+        data.addProperty("default", defaultValue.toString());
+        return data;
+    }
+
+    private static void addDefaultValueForData(JsonObject inputs,
+                                               JsonObject defaultValue,
+                                               String workFlowName) {
+        Set<Map.Entry<String, JsonElement>> entrySet = inputs.entrySet();
+        for (Map.Entry<String, JsonElement> entry : entrySet) {
+            String key = entry.getKey();
+            JsonObject inputProperty = inputs.getAsJsonObject(key);
+            if (key.equalsIgnoreCase(workFlowName + "-properties")) {
+                addDefaultValueForData(entry.getValue().getAsJsonObject().get("properties")
+                        .getAsJsonObject(), defaultValue, workFlowName);
+            } else if ("object".equalsIgnoreCase(inputProperty.get(TYPE).getAsString())) {
+                JsonObject object = new JsonObject();
+                addDefaultValueForData(entry.getValue().getAsJsonObject().get("properties")
+                        .getAsJsonObject(), object, workFlowName);
+                defaultValue.add(entry.getKey(), object);
+            } else if (TYPE_LIST.equalsIgnoreCase(inputProperty.get(TYPE).getAsString())) {
+                defaultValue.add(entry.getKey(), handleListType(entry.getValue().getAsJsonObject(), workFlowName));
+            } else {
+                defaultValue.addProperty(entry.getKey(), "");
+            }
+        }
+    }
+
+    private static JsonArray handleListType(JsonObject inputs,
+                                            String workFlowName) {
+
+        JsonObject object = new JsonObject();
+        if (inputs.get("properties") != null) {
+            addDefaultValueForData(inputs.get("properties").getAsJsonObject(), object, workFlowName);
+        }
+        JsonArray arr = new JsonArray();
+        arr.add(object);
+        return arr;
+    }
 }