Remove ECOMP in headers
[clamp.git] / src / main / java / org / onap / clamp / clds / sdc / controller / installer / CsarInstallerImpl.java
index d282588..0498ca7 100644 (file)
  * limitations under the License.
  * ============LICENSE_END============================================
  * ===================================================================
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
+ * 
  */
 
 package org.onap.clamp.clds.sdc.controller.installer;
 
 import com.att.aft.dme2.internal.apache.commons.io.IOUtils;
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
 
 import java.io.IOException;
 import java.util.ArrayList;
@@ -33,21 +35,25 @@ import java.util.Map;
 import java.util.Map.Entry;
 
 import javax.annotation.PostConstruct;
+import javax.xml.transform.TransformerException;
 
 import org.json.simple.parser.ParseException;
 import org.onap.clamp.clds.client.DcaeInventoryServices;
+import org.onap.clamp.clds.config.ClampProperties;
 import org.onap.clamp.clds.config.sdc.BlueprintParserFilesConfiguration;
 import org.onap.clamp.clds.config.sdc.BlueprintParserMappingConfiguration;
 import org.onap.clamp.clds.dao.CldsDao;
 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
 import org.onap.clamp.clds.model.CldsModel;
 import org.onap.clamp.clds.model.CldsTemplate;
+import org.onap.clamp.clds.model.properties.ModelProperties;
 import org.onap.clamp.clds.service.CldsService;
 import org.onap.clamp.clds.service.CldsTemplateService;
-import org.springframework.transaction.annotation.Transactional;
+import org.onap.clamp.clds.transform.XslTransformer;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.ApplicationContext;
+import org.springframework.transaction.annotation.Transactional;
 import org.yaml.snakeyaml.Yaml;
 
 /**
@@ -57,10 +63,13 @@ import org.yaml.snakeyaml.Yaml;
  */
 public class CsarInstallerImpl implements CsarInstaller {
 
+    private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarInstallerImpl.class);
     private Map<String, BlueprintParserFilesConfiguration> bpmnMapping = new HashMap<>();
     public static final String TEMPLATE_NAME_PREFIX = "DCAE-Designer-ClosedLoopTemplate-";
-    public static final String MODEL_NAME_PREFIX = "ClosedLoop-";
+    public static final String CONTROL_NAME_PREFIX = "ClosedLoop-";
     public static final String GET_INPUT_BLUEPRINT_PARAM = "get_input";
+    // This will be used later as the policy scope
+    public static final String MODEL_NAME_PREFIX = "CLAMP";
     /**
      * The file name that will be loaded by Spring.
      */
@@ -76,6 +85,10 @@ public class CsarInstallerImpl implements CsarInstaller {
     CldsService cldsService;
     @Autowired
     DcaeInventoryServices dcaeInventoryService;
+    @Autowired
+    private XslTransformer cldsBpmnTransformer;
+    @Autowired
+    private ClampProperties refProp;
 
     @PostConstruct
     public void loadConfiguration() throws IOException {
@@ -90,19 +103,22 @@ public class CsarInstallerImpl implements CsarInstaller {
     }
 
     public static String buildModelName(CsarHandler csar) {
-        return csar.getSdcCsarHelper().getServiceMetadata().getValue("name") + " v"
-                + csar.getSdcNotification().getServiceVersion();
+        return MODEL_NAME_PREFIX + csar.getSdcCsarHelper().getServiceMetadata().getValue("name") + "_v"
+                + csar.getSdcNotification().getServiceVersion().replace('.', '_');
     }
 
     @Override
     @Transactional
     public void installTheCsar(CsarHandler csar) throws SdcArtifactInstallerException {
         try {
+            logger.info("Installing the CSAR " + csar.getFilePath());
             for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {
+                logger.info("Processing blueprint " + blueprint.getValue().getBlueprintArtifactName());
                 String serviceTypeId = queryDcaeToGetServiceTypeId(blueprint.getValue());
                 createFakeCldsModel(csar, blueprint.getValue(), createFakeCldsTemplate(csar, blueprint.getValue(),
                         this.searchForRightMapping(blueprint.getValue())), serviceTypeId);
             }
+            logger.info("Successfully installed the CSAR " + csar.getFilePath());
         } catch (IOException e) {
             throw new SdcArtifactInstallerException("Exception caught during the Csar installation in database", e);
         } catch (ParseException | InterruptedException e) {
@@ -127,6 +143,8 @@ public class CsarInstallerImpl implements CsarInstaller {
         } else if (listConfig.isEmpty()) {
             throw new SdcArtifactInstallerException("There is no recognized MicroService found in the blueprint");
         }
+        logger.info("Mapping found for blueprint " + blueprintArtifact.getBlueprintArtifactName() + " is "
+                + listConfig.get(0).getBpmnXmlFilePath());
         return listConfig.get(0);
     }
 
@@ -156,13 +174,16 @@ public class CsarInstallerImpl implements CsarInstaller {
             throw new SdcArtifactInstallerException(
                     "There is no recognized Policy MicroService found in the blueprint");
         }
+        logger.info("policyName found in blueprint " + blueprintArtifact.getBlueprintArtifactName() + " is "
+                + policyNameList.get(0));
         return policyNameList.get(0);
     }
 
-    private String queryDcaeToGetServiceTypeId(BlueprintArtifact blueprintArtifact) throws IOException, ParseException {
+    private String queryDcaeToGetServiceTypeId(BlueprintArtifact blueprintArtifact)
+            throws IOException, ParseException, InterruptedException {
         return dcaeInventoryService.getDcaeInformation(blueprintArtifact.getBlueprintArtifactName(),
                 blueprintArtifact.getBlueprintInvariantServiceUuid(),
-                blueprintArtifact.getBlueprintInvariantResourceUuid()).getTypeId();
+                blueprintArtifact.getResourceAttached().getResourceInvariantUUID()).getTypeId();
     }
 
     private CldsTemplate createFakeCldsTemplate(CsarHandler csar, BlueprintArtifact blueprintArtifact,
@@ -177,31 +198,44 @@ public class CsarInstallerImpl implements CsarInstaller {
                 IOUtils.toString(appContext.getResource(configFiles.getSvgXmlFilePath()).getInputStream()));
         template.setName(TEMPLATE_NAME_PREFIX + buildModelName(csar));
         template.save(cldsDao, null);
+        logger.info("Fake Clds Template created for blueprint " + blueprintArtifact.getBlueprintArtifactName()
+                + " with name " + template.getName());
         return template;
     }
 
     private CldsModel createFakeCldsModel(CsarHandler csar, BlueprintArtifact blueprintArtifact,
             CldsTemplate cldsTemplate, String serviceTypeId) throws SdcArtifactInstallerException {
-        CldsModel cldsModel = new CldsModel();
-        String policyName = searchForPolicyName(blueprintArtifact);
-        if (policyName.contains("*")) {
-            // It's a filter must add a specific prefix
-            cldsModel.setControlNamePrefix(policyName);
-        } else {
-            cldsModel.setControlNamePrefix(MODEL_NAME_PREFIX);
+        try {
+            CldsModel cldsModel = new CldsModel();
+            cldsModel.setName(buildModelName(csar));
+            cldsModel.setBlueprintText(blueprintArtifact.getDcaeBlueprint());
+            cldsModel.setTemplateName(cldsTemplate.getName());
+            cldsModel.setTemplateId(cldsTemplate.getId());
+            cldsModel.setBpmnText(cldsTemplate.getBpmnText());
+            cldsModel.setTypeId(serviceTypeId);
+            ModelProperties modelProp = new ModelProperties(cldsModel.getName(), "test", "PUT", false,
+                    cldsBpmnTransformer.doXslTransformToString(cldsTemplate.getBpmnText()), "{}");
+            String policyName = searchForPolicyName(blueprintArtifact);
+            String inputParams = "";
+            if (policyName.contains("*")) {
+                // It's a filter must add a specific prefix
+                cldsModel.setControlNamePrefix(policyName);
+            } else {
+                cldsModel.setControlNamePrefix(CONTROL_NAME_PREFIX);
+                inputParams = "{\"name\":\"deployParameters\",\"value\":{\n" + "\"policy_id\": \""
+                        + modelProp.getPolicyNameForDcaeDeploy(refProp) + "\"" + "}}";
+            }
+            cldsModel.setPropText("{\"global\":[{\"name\":\"service\",\"value\":[\""
+                    + blueprintArtifact.getBlueprintInvariantServiceUuid() + "\"]},{\"name\":\"vf\",\"value\":[\""
+                    + blueprintArtifact.getResourceAttached().getResourceInvariantUUID()
+                    + "\"]},{\"name\":\"actionSet\",\"value\":[\"vnfRecipe\"]},{\"name\":\"location\",\"value\":[\"DC1\"]},"
+                    + inputParams + "]}");
+            cldsModel.save(cldsDao, null);
+            logger.info("Fake Clds Model created for blueprint " + blueprintArtifact.getBlueprintArtifactName()
+                    + " with name " + cldsModel.getName());
+            return cldsModel;
+        } catch (TransformerException e) {
+            throw new SdcArtifactInstallerException("TransformerException when decoding the BpmnText", e);
         }
-        cldsModel.setName(buildModelName(csar));
-        cldsModel.setBlueprintText(blueprintArtifact.getDcaeBlueprint());
-        cldsModel.setTemplateName(cldsTemplate.getName());
-        cldsModel.setTemplateId(cldsTemplate.getId());
-        cldsModel.setPropText("{\"global\":[{\"name\":\"service\",\"value\":[\""
-                + blueprintArtifact.getBlueprintInvariantServiceUuid() + "\"]},{\"name\":\"vf\",\"value\":[\""
-                + blueprintArtifact.getBlueprintInvariantResourceUuid()
-                + "\"]},{\"name\":\"actionSet\",\"value\":[\"vnfRecipe\"]},{\"name\":\"location\",\"value\":[\"DC1\"]},{\"name\":\"deployParameters\",\"value\":{\n"
-                + "        \"policy_id\": \"" + "test" + "\"" + "      }}]}");
-        cldsModel.setBpmnText(cldsTemplate.getBpmnText());
-        cldsModel.setTypeId(serviceTypeId);
-        cldsModel.save(cldsDao, null);
-        return cldsModel;
     }
 }