Support resource parameters generated 83/35683/1
authorc00149107 <chenchuanyu@huawei.com>
Wed, 14 Mar 2018 07:53:12 +0000 (15:53 +0800)
committerc00149107 <chenchuanyu@huawei.com>
Wed, 14 Mar 2018 07:53:12 +0000 (15:53 +0800)
Support resource parameters generated

Change-Id: I9fb882024edc8e72d06dbc545e2342f32ad8f750
Issue-ID: SO-452
Signed-off-by: c00149107 <chenchuanyu@huawei.com>
bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/bpmn/common/resource/ResourceRequestBuilder.java

index 3f05720..55ab27b 100644 (file)
  * limitations under the License.
  * ============LICENSE_END=========================================================
  */
+
 package org.openecomp.mso.bpmn.common.resource;
 
-import com.google.gson.Gson;
-import com.google.gson.reflect.TypeToken;
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+import javax.ws.rs.core.Response;
+
+import org.camunda.bpm.engine.runtime.Execution;
 import org.jboss.resteasy.client.jaxrs.ResteasyClient;
 import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
 import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
+import org.openecomp.mso.bpmn.core.json.JsonUtils;
+import org.openecomp.mso.logger.MessageEnum;
 import org.openecomp.mso.logger.MsoLogger;
 import org.openecomp.sdc.tosca.parser.api.ISdcCsarHelper;
 import org.openecomp.sdc.tosca.parser.exceptions.SdcToscaParserException;
@@ -33,31 +45,76 @@ import org.openecomp.sdc.toscaparser.api.Property;
 import org.openecomp.sdc.toscaparser.api.functions.GetInput;
 import org.openecomp.sdc.toscaparser.api.parameters.Input;
 
-import javax.ws.rs.core.Response;
-import java.io.File;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
 
 public class ResourceRequestBuilder {
 
     public static String CUSTOMIZATION_UUID = "customizationUUID";
+
     public static String SERVICE_URL_TOSCA_CSAR = "http://localhost:8080/ecomp/mso/catalog/v3/serviceToscaCsar?serviceModelUuid=";
 
     private static MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
 
-    public static Map<String, Object> buildResouceRequest(String serviceUuid,
-                                              String resourceCustomizationUuid,
-                                              Map<String, Object> serviceInputs) throws SdcToscaParserException {
+    static JsonUtils jsonUtil = new JsonUtils();
+
+        /**
+     * build the resource Parameters detail.
+     * It's a json string for resource instantiant
+     * {
+     *     "locationConstraints":[...]
+     *     "requestInputs":{K,V}
+     * }
+     * <br>
+     * 
+     * @param serviceUuid The service template uuid
+     * @param resourceucstomizationUuid The resource customization uuid
+     * @param serviceParameters the service parameters passed from the API
+     * @return the resource instantiate parameters
+     * @since ONAP Beijing Release
+     */
+    @SuppressWarnings("unchecked")
+    public static String buildResourceRequestParameters(Execution execution, String serviceUuid, String resourceucstomizationUuid, String serviceParameters) {
+        List<String> resourceList = jsonUtil.StringArrayToList(execution, (String)JsonUtils.getJsonValue(serviceParameters, "resources")); 
+        //Get the right location str for resource. default is an empty array.
+        String locationConstraints ="[]";
+        String resourceInputsFromUui = "";
+        for(String resource: resourceList){
+            String resCusUuid = (String)JsonUtils.getJsonValue(resource, "resourceCustomizationUuid");
+            if(resourceucstomizationUuid.equals(resCusUuid)){
+                String resourceParameters = JsonUtils.getJsonValue(resource, "parameters");
+                locationConstraints = JsonUtils.getJsonValue(resourceParameters, "locationConstraints");
+                resourceInputsFromUui = JsonUtils.getJsonValue(resourceParameters, "requestInputs");
+            }
+        }
+        Map<String, Object> serviceInput =getJsonObject((String)JsonUtils.getJsonValue(serviceParameters, "requestInputs"), Map.class);
+        Map<String, Object> resourceInputsFromUuiMap = getJsonObject(resourceInputsFromUui, Map.class);
+        try {
+            Map<String, Object> resourceInputsFromServiceDeclaredLevel = buildResouceRequest(serviceUuid, resourceucstomizationUuid, serviceInput);
+            resourceInputsFromUuiMap.putAll(resourceInputsFromServiceDeclaredLevel);
+        } catch(SdcToscaParserException e) {
+            e.printStackTrace();
+        }
+        String resourceInputsStr = getJsonString(resourceInputsFromUuiMap);
+        String result = "{\n"
+                + "\"locationConstraints\":" + locationConstraints +",\n"
+                + "\"requestInputs\":" + resourceInputsStr +"\n"
+                +"}";
+        return result;
+    }
+
+    public static Map<String, Object> buildResouceRequest(String serviceUuid, String resourceCustomizationUuid, Map<String, Object> serviceInputs)
+            throws SdcToscaParserException {
 
         Map<String, Object> resouceRequest = new HashMap<>();
 
         String csarpath = null;
         try {
             csarpath = getCsarFromUuid(serviceUuid);
-        } catch (Exception e) {
+        } catch(Exception e) {
             LOGGER.debug("csar file is not available for service uuid:" + serviceUuid, e);
             return resouceRequest;
         }
@@ -67,14 +124,13 @@ public class ResourceRequestBuilder {
 
         List<Input> serInput = iSdcCsarHelper.getServiceInputs();
         Optional<NodeTemplate> nodeTemplateOpt = iSdcCsarHelper.getServiceNodeTemplates().stream()
-                .filter(e -> e.getMetaData().getValue(CUSTOMIZATION_UUID).equals(resourceCustomizationUuid))
-                .findFirst();
+                .filter(e -> e.getMetaData().getValue(CUSTOMIZATION_UUID).equals(resourceCustomizationUuid)).findFirst();
 
-        if (nodeTemplateOpt.isPresent()) {
+        if(nodeTemplateOpt.isPresent()) {
             NodeTemplate nodeTemplate = nodeTemplateOpt.get();
             LinkedHashMap<String, Property> resourceProperties = nodeTemplate.getProperties();
 
-            for (String key: resourceProperties.keySet()) {
+            for(String key : resourceProperties.keySet()) {
                 Property property = resourceProperties.get(key);
 
                 Object value = getValue(property.getValue(), serviceInputs, serInput);
@@ -84,26 +140,25 @@ public class ResourceRequestBuilder {
         return resouceRequest;
     }
 
-    private static Object getValue(Object value, Map<String, Object> serviceInputs,
-                                   List<Input> servInputs) {
-        if (value instanceof Map) {
+    private static Object getValue(Object value, Map<String, Object> serviceInputs, List<Input> servInputs) {
+        if(value instanceof Map) {
             Map<String, Object> valueMap = new HashMap<>();
 
-            Map<String, Object> propertyMap = (Map<String, Object>) value;
+            Map<String, Object> propertyMap = (Map<String, Object>)value;
 
-            for (String key: propertyMap.keySet()) {
+            for(String key : propertyMap.keySet()) {
                 valueMap.put(key, getValue(propertyMap.get(key), serviceInputs, servInputs));
             }
             return valueMap; // return if the value is nested hashmap
-        } else if (value instanceof GetInput) {
-            String inputName = ((GetInput) value).getInputName();
+        } else if(value instanceof GetInput) {
+            String inputName = ((GetInput)value).getInputName();
 
-            if (serviceInputs.get(inputName) != null) {
+            if(serviceInputs.get(inputName) != null) {
                 value = serviceInputs.get(inputName);
             } else {
-                for (Input input: servInputs) {
-                    if (input.getName().equals(inputName)) {
-                        return input.getDefault();  // return default value
+                for(Input input : servInputs) {
+                    if(input.getName().equals(inputName)) {
+                        return input.getDefault(); // return default value
                     }
                 }
             }
@@ -118,14 +173,38 @@ public class ResourceRequestBuilder {
         Response response = target.request().get();
         String value = response.readEntity(String.class);
 
-        HashMap<String,String> map = new Gson().fromJson(value, new TypeToken<HashMap<String, String>>(){}.getType());
+        HashMap<String, String> map = new Gson().fromJson(value, new TypeToken<HashMap<String, String>>() {}.getType());
 
         File csarFile = new File(System.getProperty("mso.config.path") + "ASDC/" + map.get("name"));
 
-        if (!csarFile.exists()) {
+        if(!csarFile.exists()) {
             throw new Exception("csar file does not exist.");
         }
 
         return csarFile.getAbsolutePath();
     }
+    
+    public static <T> T getJsonObject(String jsonstr, Class<T> type) {
+        ObjectMapper mapper = new ObjectMapper();
+        mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
+        try {
+            return mapper.readValue(jsonstr, type);
+        } catch(IOException e) {
+            LOGGER.error(MessageEnum.RA_NS_EXC, "", "", MsoLogger.ErrorCode.BusinessProcesssError, "fail to unMarshal json", e);
+        }
+        return null;
+    }
+
+    public static String getJsonString(Object srcObj)  {
+        ObjectMapper mapper = new ObjectMapper();
+        mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
+        String jsonStr = null;
+        try {
+            jsonStr = mapper.writeValueAsString(srcObj);
+        } catch(JsonProcessingException e) {
+
+            e.printStackTrace();
+        }
+        return jsonStr;
+    }
 }