Add svg support
[clamp.git] / src / main / java / org / onap / clamp / loop / CsarInstallerImpl.java
index 9627445..07f1b77 100644 (file)
@@ -31,9 +31,9 @@ import com.google.gson.JsonObject;
 import java.io.IOException;
 import java.util.Arrays;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
-import java.util.Optional;
 
 import org.json.simple.parser.ParseException;
 import org.onap.clamp.clds.client.DcaeInventoryServices;
@@ -47,12 +47,14 @@ import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
 import org.onap.clamp.clds.sdc.controller.installer.CsarInstaller;
 import org.onap.clamp.clds.sdc.controller.installer.MicroService;
 import org.onap.clamp.clds.util.JsonUtils;
+import org.onap.clamp.clds.util.drawing.SvgFacade;
 import org.onap.clamp.policy.Policy;
 import org.onap.clamp.policy.microservice.MicroServicePolicy;
 import org.onap.clamp.policy.operational.OperationalPolicy;
 import org.onap.sdc.tosca.parser.enums.SdcTypes;
 import org.onap.sdc.toscaparser.api.NodeTemplate;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.transaction.annotation.Propagation;
 import org.springframework.transaction.annotation.Transactional;
 import org.yaml.snakeyaml.Yaml;
 
@@ -71,63 +73,43 @@ public class CsarInstallerImpl implements CsarInstaller {
     public static final String MODEL_NAME_PREFIX = "Loop_";
 
     @Autowired
-    protected LoopsRepository loopRepository;
+    LoopsRepository loopRepository;
 
     @Autowired
-    private BlueprintParser blueprintParser;
+    BlueprintParser blueprintParser;
 
     @Autowired
-    private ChainGenerator chainGenerator;
+    ChainGenerator chainGenerator;
 
     @Autowired
     DcaeInventoryServices dcaeInventoryService;
 
     @Autowired
-    public void CsarInstallerImpl(LoopsRepository loopRepository, BlueprintParser blueprintParser,
-        ChainGenerator chainGenerator, DcaeInventoryServices dcaeInventoryService) {
-        this.loopRepository = loopRepository;
-        this.blueprintParser = blueprintParser;
-        this.chainGenerator = chainGenerator;
-        this.dcaeInventoryService = dcaeInventoryService;
-    }
+    private SvgFacade svgFacade;
 
     @Override
     public boolean isCsarAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException {
         boolean alreadyInstalled = true;
         for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {
             alreadyInstalled = alreadyInstalled
-                && loopRepository.existsById(buildModelName(csar, blueprint.getValue()));
+                && loopRepository.existsById(Loop.generateLoopName(csar.getSdcNotification().getServiceName(),
+                    csar.getSdcNotification().getServiceVersion(),
+                    blueprint.getValue().getResourceAttached().getResourceInstanceName(),
+                    blueprint.getValue().getBlueprintArtifactName()));
         }
         return alreadyInstalled;
     }
 
-    public static String buildModelName(CsarHandler csar, BlueprintArtifact artifact) {
-
-        return (MODEL_NAME_PREFIX + "_" + csar.getSdcCsarHelper().getServiceMetadata().getValue("name") + "_v"
-            + csar.getSdcNotification().getServiceVersion() + "_"
-            + artifact.getResourceAttached().getResourceInstanceName().replaceAll(" ", "") + "_"
-            + artifact.getBlueprintArtifactName().replace(".yaml", "")).replace('.', '_');
-    }
-
-    public static String buildOperationalPolicyName(CsarHandler csar, BlueprintArtifact artifact) {
-
-        return (MODEL_NAME_PREFIX + "_" + csar.getSdcCsarHelper().getServiceMetadata().getValue("name") + "_v"
-            + csar.getSdcNotification().getServiceVersion() + "_"
-            + artifact.getResourceAttached().getResourceInstanceName().replaceAll(" ", "") + "_"
-            + artifact.getBlueprintArtifactName().replace(".yaml", "")).replace('.', '_');
-    }
-
     @Override
-    @Transactional
+    @Transactional(propagation = Propagation.REQUIRED)
     public void installTheCsar(CsarHandler csar)
         throws SdcArtifactInstallerException, InterruptedException, PolicyModelException {
         try {
             logger.info("Installing the CSAR " + csar.getFilePath());
             for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {
                 logger.info("Processing blueprint " + blueprint.getValue().getBlueprintArtifactName());
-                createLoopFromBlueprint(csar, blueprint.getValue());
+                loopRepository.save(createLoopFromBlueprint(csar, blueprint.getValue()));
             }
-            createPolicyModel(csar);
             logger.info("Successfully installed the CSAR " + csar.getFilePath());
         } catch (IOException e) {
             throw new SdcArtifactInstallerException("Exception caught during the Csar installation in database", e);
@@ -136,13 +118,14 @@ public class CsarInstallerImpl implements CsarInstaller {
         }
     }
 
-    private void createPolicyModel(CsarHandler csar) throws PolicyModelException {
-        try {
-            Optional<String> policyModelYaml = csar.getPolicyModelYaml();
-            // save policy model into the database
-        } catch (IOException e) {
-            throw new PolicyModelException("TransformerException when decoding the YamlText", e);
+    private String getSvgInLoop(BlueprintArtifact blueprintArtifact) {
+        List<MicroService> microServicesChain = chainGenerator
+            .getChainOfMicroServices(blueprintParser.getMicroServices(blueprintArtifact.getDcaeBlueprint()));
+        if (microServicesChain.isEmpty()) {
+            microServicesChain = blueprintParser.fallbackToOneMicroService(blueprintArtifact.getDcaeBlueprint());
         }
+        return svgFacade.getSvgImage(microServicesChain);
+
     }
 
     private Loop createLoopFromBlueprint(CsarHandler csar, BlueprintArtifact blueprintArtifact)
@@ -154,32 +137,52 @@ public class CsarInstallerImpl implements CsarInstaller {
             blueprintArtifact.getResourceAttached().getResourceInstanceName(),
             blueprintArtifact.getBlueprintArtifactName()));
         newLoop.setLastComputedState(LoopState.DESIGN);
-        for (MicroService microService : blueprintParser.getMicroServices(blueprintArtifact.getDcaeBlueprint())) {
-            newLoop.getMicroServicePolicies().add(new MicroServicePolicy(microService.getName(),
-                csar.getPolicyModelYaml().orElse(""), false, new JsonObject(), new HashSet<>(Arrays.asList(newLoop))));
-        }
-        newLoop.setOperationalPolicies(
-            new HashSet<>(Arrays.asList(new OperationalPolicy(Policy.generatePolicyName("OPERATIONAL",
-                csar.getSdcNotification().getServiceName(), csar.getSdcNotification().getServiceVersion(),
-                blueprintArtifact.getResourceAttached().getResourceInstanceName(),
-                blueprintArtifact.getBlueprintArtifactName()), newLoop, new JsonObject()))));
-        // Set SVG XML computed
-        // newLoop.setSvgRepresentation(svgRepresentation);
-        newLoop.setGlobalPropertiesJson(createGlobalPropertiesJson(csar, blueprintArtifact));
-        newLoop.setModelPropertiesJson(createModelPropertiesJson(csar, blueprintArtifact));
+        newLoop.setMicroServicePolicies(createMicroServicePolicies(csar, blueprintArtifact, newLoop));
+        newLoop.setOperationalPolicies(createOperationalPolicies(csar, blueprintArtifact, newLoop));
+
+        newLoop.setSvgRepresentation(getSvgInLoop(blueprintArtifact));
+        newLoop.setGlobalPropertiesJson(createGlobalPropertiesJson(blueprintArtifact));
+        newLoop.setModelPropertiesJson(createModelPropertiesJson(csar));
         DcaeInventoryResponse dcaeResponse = queryDcaeToGetServiceTypeId(blueprintArtifact);
         newLoop.setDcaeBlueprintId(dcaeResponse.getTypeId());
         return newLoop;
     }
 
-    private JsonObject createGlobalPropertiesJson(CsarHandler csar, BlueprintArtifact blueprintArtifact) {
+    private HashSet<OperationalPolicy> createOperationalPolicies(CsarHandler csar, BlueprintArtifact blueprintArtifact,
+        Loop newLoop) {
+        return new HashSet<>(Arrays.asList(new OperationalPolicy(Policy.generatePolicyName("OPERATIONAL",
+            csar.getSdcNotification().getServiceName(), csar.getSdcNotification().getServiceVersion(),
+            blueprintArtifact.getResourceAttached().getResourceInstanceName(),
+            blueprintArtifact.getBlueprintArtifactName()), newLoop, new JsonObject())));
+    }
+
+    private HashSet<MicroServicePolicy> createMicroServicePolicies(CsarHandler csar,
+        BlueprintArtifact blueprintArtifact, Loop newLoop) throws IOException {
+        HashSet<MicroServicePolicy> newSet = new HashSet<>();
+        List<MicroService> microServicesChain = chainGenerator
+            .getChainOfMicroServices(blueprintParser.getMicroServices(blueprintArtifact.getDcaeBlueprint()));
+        if (microServicesChain.isEmpty()) {
+            microServicesChain = blueprintParser.fallbackToOneMicroService(blueprintArtifact.getDcaeBlueprint());
+        }
+        for (MicroService microService : microServicesChain) {
+            newSet.add(new MicroServicePolicy(
+                Policy.generatePolicyName(microService.getName(), csar.getSdcNotification().getServiceName(),
+                    csar.getSdcNotification().getServiceVersion(),
+                    blueprintArtifact.getResourceAttached().getResourceInstanceName(),
+                    blueprintArtifact.getBlueprintArtifactName()),
+                csar.getPolicyModelYaml().orElse(""), false, new HashSet<>(Arrays.asList(newLoop))));
+        }
+        return newSet;
+    }
+
+    private JsonObject createGlobalPropertiesJson(BlueprintArtifact blueprintArtifact) {
         JsonObject globalProperties = new JsonObject();
         globalProperties.add("dcaeDeployParameters", getAllBlueprintParametersInJson(blueprintArtifact));
         return globalProperties;
 
     }
 
-    private JsonObject createModelPropertiesJson(CsarHandler csar, BlueprintArtifact blueprintArtifact) {
+    private JsonObject createModelPropertiesJson(CsarHandler csar) {
         JsonObject modelProperties = new JsonObject();
         Gson gson = new Gson();
         modelProperties.add("serviceDetails",