Remove Camunda and replace by Camel
[clamp.git] / src / main / java / org / onap / clamp / clds / client / req / sdc / SdcReq.java
index d4b5b08..3fb53a3 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP CLAMP
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
  *                             reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -25,8 +25,6 @@ package org.onap.clamp.clds.client.req.sdc;
 
 import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;
-import com.fasterxml.jackson.core.JsonParseException;
-import com.fasterxml.jackson.databind.JsonMappingException;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ArrayNode;
@@ -42,7 +40,6 @@ import java.util.List;
 import java.util.Map.Entry;
 
 import org.apache.commons.codec.DecoderException;
-import org.camunda.bpm.engine.delegate.DelegateExecution;
 import org.onap.clamp.clds.client.req.tca.TcaRequestFormatter;
 import org.onap.clamp.clds.model.CldsSdcResource;
 import org.onap.clamp.clds.model.CldsSdcServiceDetail;
@@ -51,15 +48,20 @@ import org.onap.clamp.clds.model.prop.ModelProperties;
 import org.onap.clamp.clds.model.prop.Tca;
 import org.onap.clamp.clds.model.refprop.RefProp;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
 
 /**
  * Construct a Sdc request given CLDS objects.
  */
+@Component
 public class SdcReq {
-    protected static final EELFLogger logger        = EELFManager.getInstance().getLogger(SdcReq.class);
+
+    protected static final EELFLogger logger = EELFManager.getInstance().getLogger(SdcReq.class);
     protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
     @Autowired
-    protected RefProp                 refProp;
+    private SdcCatalogServices sdcCatalogServices;
+    @Autowired
+    protected RefProp refProp;
 
     /**
      * Format the Blueprint from a Yaml
@@ -68,17 +70,11 @@ public class SdcReq {
      *            The ModelProperties describing the clds model
      * @param docText
      *            The Yaml file that must be converted
-     *
      * @return A String containing the BluePrint
-     * @throws JsonParseException
-     *             In case of issues
-     * @throws JsonMappingException
-     *             In case of issues
      * @throws IOException
      *             In case of issues
      */
-    public String formatBlueprint(ModelProperties prop, String docText)
-            throws JsonParseException, JsonMappingException, IOException {
+    public String formatBlueprint(ModelProperties prop, String docText) throws IOException {
         String yamlvalue = getYamlvalue(docText);
         String updatedBlueprint = "";
         Tca tca = prop.getType(Tca.class);
@@ -96,7 +92,6 @@ public class SdcReq {
      *            The ModelProperties describing the clds model
      * @param artifactName
      *            The name of the artifact
-     *
      * @return SDC Locations request in the JSON Format
      */
     public String formatSdcLocationsReq(ModelProperties prop, String artifactName) {
@@ -127,11 +122,8 @@ public class SdcReq {
      * @param artifactType
      *            The type of the artifact
      * @return formatted SDC Request
-     * @throws IOException
-     *             In case of issues
      */
-    public String formatSdcReq(String payloadData, String artifactName, String artifactLabel, String artifactType)
-            throws IOException {
+    public String formatSdcReq(String payloadData, String artifactName, String artifactLabel, String artifactType) {
         logger.info("artifact=" + payloadData);
         String base64Artifact = Base64.getEncoder().encodeToString(payloadData.getBytes(StandardCharsets.UTF_8));
         return "{ \n" + "\"payloadData\" : \"" + base64Artifact + "\",\n" + "\"artifactLabel\" : \"" + artifactLabel
@@ -144,46 +136,47 @@ public class SdcReq {
      * To get List of urls for all vfresources
      *
      * @param prop
+     *            The model properties JSON describing the closed loop flow
      * @param baseUrl
-     * @param sdcCatalogServices
-     * @param execution
-     * @return
+     *            The URL to trigger
+     * @return A list of Service URL
      * @throws GeneralSecurityException
      *             In case of issues when decrypting the password
      * @throws DecoderException
      *             In case of issues when decoding the Hex String
      */
-    public List<String> getSdcReqUrlsList(ModelProperties prop, String baseUrl, SdcCatalogServices sdcCatalogServices,
-            DelegateExecution execution) throws GeneralSecurityException, DecoderException {
-        // TODO : refact and regroup with very similar code
+    public List<String> getSdcReqUrlsList(ModelProperties prop, String baseUrl)
+            throws GeneralSecurityException, DecoderException {
         List<String> urlList = new ArrayList<>();
         Global globalProps = prop.getGlobal();
-        if (globalProps != null) {
-            if (globalProps.getService() != null) {
-                String serviceInvariantUUID = globalProps.getService();
-                execution.setVariable("serviceInvariantUUID", serviceInvariantUUID);
-                List<String> resourceVfList = globalProps.getResourceVf();
-                String serviceUUID = sdcCatalogServices.getServiceUuidFromServiceInvariantId(serviceInvariantUUID);
-                String sdcServicesInformation = sdcCatalogServices.getSdcServicesInformation(serviceUUID);
-                CldsSdcServiceDetail cldsSdcServiceDetail = sdcCatalogServices
-                        .getCldsSdcServiceDetailFromJson(sdcServicesInformation);
-                if (cldsSdcServiceDetail != null && resourceVfList != null) {
-                    List<CldsSdcResource> cldsSdcResourcesList = cldsSdcServiceDetail.getResources();
-                    if (cldsSdcResourcesList != null && !cldsSdcResourcesList.isEmpty()) {
-                        for (CldsSdcResource CldsSdcResource : cldsSdcResourcesList) {
-                            if (CldsSdcResource != null && CldsSdcResource.getResoucreType() != null
-                                    && CldsSdcResource.getResoucreType().equalsIgnoreCase("VF")
-                                    && resourceVfList.contains(CldsSdcResource.getResourceInvariantUUID())) {
-                                String normalizedResourceInstanceName = normalizeResourceInstanceName(
-                                        CldsSdcResource.getResourceInstanceName());
-                                String svcUrl = baseUrl + "/" + serviceUUID + "/resourceInstances/"
-                                        + normalizedResourceInstanceName + "/artifacts";
-                                urlList.add(svcUrl);
-                            }
+        if (globalProps != null && globalProps.getService() != null) {
+            String serviceInvariantUUID = globalProps.getService();
+            List<String> resourceVfList = globalProps.getResourceVf();
+            String serviceUUID = sdcCatalogServices.getServiceUuidFromServiceInvariantId(serviceInvariantUUID);
+            CldsSdcServiceDetail cldsSdcServiceDetail = sdcCatalogServices
+                    .getCldsSdcServiceDetailFromJson(sdcCatalogServices.getSdcServicesInformation(serviceUUID));
+            if (cldsSdcServiceDetail != null && resourceVfList != null) {
+                List<CldsSdcResource> cldsSdcResourcesList = cldsSdcServiceDetail.getResources();
+                if (cldsSdcResourcesList != null && !cldsSdcResourcesList.isEmpty()) {
+                    for (CldsSdcResource cldsSdcResource : cldsSdcResourcesList) {
+                        if (cldsSdcResource != null && cldsSdcResource.getResoucreType() != null
+                                && cldsSdcResource.getResoucreType().equalsIgnoreCase("VF")
+                                && resourceVfList.contains(cldsSdcResource.getResourceInvariantUUID())) {
+                            String normalizedResourceInstanceName = normalizeResourceInstanceName(
+                                    cldsSdcResource.getResourceInstanceName());
+                            String svcUrl = baseUrl + "/" + serviceUUID + "/resourceInstances/"
+                                    + normalizedResourceInstanceName + "/artifacts";
+                            urlList.add(svcUrl);
+                        } else {
+                            logger.warn("The VF Resource invariant UUID (" + cldsSdcResource.getResourceInvariantUUID()
+                                    + ") has not been found in the Service (Invariant ID:" + serviceInvariantUUID
+                                    + ")VF resource list");
                         }
                     }
                 }
             }
+        } else {
+            logger.warn("GlobalProperties json is empty, skipping getSdcReqUrlsList and returning empty list");
         }
         return urlList;
     }
@@ -201,16 +194,18 @@ public class SdcReq {
     }
 
     /**
-     * Method to get yaml/template properties value from json
+     * Method to get yaml/template properties value from json.
      *
-     * @param docText
-     * @return
+     * @param jsonGlobal
+     *            The Json containing a Yaml file
+     * @return The yaml extracted from the JSON
      * @throws IOException
+     *             In case of issues with the Json parser
      */
-    public String getYamlvalue(String docText) throws IOException {
+    protected String getYamlvalue(String jsonGlobal) throws IOException {
         ObjectMapper objectMapper = new ObjectMapper();
         String yamlFileValue = "";
-        ObjectNode root = objectMapper.readValue(docText, ObjectNode.class);
+        ObjectNode root = objectMapper.readValue(jsonGlobal, ObjectNode.class);
         Iterator<Entry<String, JsonNode>> entryItr = root.fields();
         while (entryItr.hasNext()) {
             Entry<String, JsonNode> entry = entryItr.next();