Add svg support

Add SVG support to the CSAR installer so that UI could render the loop

Issue-ID: CLAMP-306
Change-Id: Ief963c4ad8e4c142f20c16b2049cad3a8aeedfb0
Signed-off-by: sebdet <sebastien.determe@intl.att.com>
diff --git a/extra/sql/bulkload/create-tables.sql b/extra/sql/bulkload/create-tables.sql
index 93c80cb..b356063 100644
--- a/extra/sql/bulkload/create-tables.sql
+++ b/extra/sql/bulkload/create-tables.sql
@@ -23,7 +23,7 @@
         global_properties_json json,
         last_computed_state varchar(255) not null,
         model_properties_json json,
-        svg_representation varchar(255),
+        svg_representation MEDIUMTEXT,
         primary key (name)
     ) engine=InnoDB;
 
diff --git a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/ChainGenerator.java b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/ChainGenerator.java
index b05b80f..27c5b9c 100644
--- a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/ChainGenerator.java
+++ b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/ChainGenerator.java
@@ -26,21 +26,23 @@
 import java.util.List;
 import java.util.Set;
 import java.util.stream.Collectors;
+
 import org.springframework.stereotype.Component;
 
 @Component
 public class ChainGenerator {
 
-    ChainGenerator() {}
+    ChainGenerator() {
+    }
 
-    List<MicroService> getChainOfMicroServices(Set<MicroService> input) {
+    public List<MicroService> getChainOfMicroServices(Set<MicroService> input) {
         LinkedList<MicroService> returnList = new LinkedList<>();
-        if(preValidate(input)) {
+        if (preValidate(input)) {
             LinkedList<MicroService> theList = new LinkedList<>();
             for (MicroService ms : input) {
                 insertNodeTemplateIntoChain(ms, theList);
             }
-            if(postValidate(theList)) {
+            if (postValidate(theList)) {
                 returnList = theList;
             }
         }
@@ -48,16 +50,16 @@
     }
 
     private boolean preValidate(Set<MicroService> input) {
-        List<MicroService> noInputs =
-            input.stream().filter(ms -> "".equals(ms.getInputFrom())).collect(Collectors.toList());
+        List<MicroService> noInputs = input.stream().filter(ms -> "".equals(ms.getInputFrom()))
+            .collect(Collectors.toList());
         return noInputs.size() == 1;
     }
 
     private boolean postValidate(LinkedList<MicroService> microServices) {
         for (int i = 1; i < microServices.size() - 1; i++) {
-            MicroService prev = microServices.get(i-1);
+            MicroService prev = microServices.get(i - 1);
             MicroService current = microServices.get(i);
-            if(!current.getInputFrom().equals(prev.getName())) {
+            if (!current.getInputFrom().equals(prev.getName())) {
                 return false;
             }
         }
diff --git a/src/main/java/org/onap/clamp/loop/CsarInstallerImpl.java b/src/main/java/org/onap/clamp/loop/CsarInstallerImpl.java
index 6e12f29..07f1b77 100644
--- a/src/main/java/org/onap/clamp/loop/CsarInstallerImpl.java
+++ b/src/main/java/org/onap/clamp/loop/CsarInstallerImpl.java
@@ -31,6 +31,7 @@
 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;
 
@@ -46,6 +47,7 @@
 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;
@@ -82,6 +84,9 @@
     @Autowired
     DcaeInventoryServices dcaeInventoryService;
 
+    @Autowired
+    private SvgFacade svgFacade;
+
     @Override
     public boolean isCsarAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException {
         boolean alreadyInstalled = true;
@@ -113,6 +118,16 @@
         }
     }
 
+    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)
         throws IOException, ParseException, InterruptedException {
         Loop newLoop = new Loop();
@@ -124,10 +139,10 @@
         newLoop.setLastComputedState(LoopState.DESIGN);
         newLoop.setMicroServicePolicies(createMicroServicePolicies(csar, blueprintArtifact, newLoop));
         newLoop.setOperationalPolicies(createOperationalPolicies(csar, blueprintArtifact, newLoop));
-        // Set SVG XML computed
-        // newLoop.setSvgRepresentation(svgRepresentation);
-        newLoop.setGlobalPropertiesJson(createGlobalPropertiesJson(csar, blueprintArtifact));
-        newLoop.setModelPropertiesJson(createModelPropertiesJson(csar, blueprintArtifact));
+
+        newLoop.setSvgRepresentation(getSvgInLoop(blueprintArtifact));
+        newLoop.setGlobalPropertiesJson(createGlobalPropertiesJson(blueprintArtifact));
+        newLoop.setModelPropertiesJson(createModelPropertiesJson(csar));
         DcaeInventoryResponse dcaeResponse = queryDcaeToGetServiceTypeId(blueprintArtifact);
         newLoop.setDcaeBlueprintId(dcaeResponse.getTypeId());
         return newLoop;
@@ -144,21 +159,30 @@
     private HashSet<MicroServicePolicy> createMicroServicePolicies(CsarHandler csar,
         BlueprintArtifact blueprintArtifact, Loop newLoop) throws IOException {
         HashSet<MicroServicePolicy> newSet = new HashSet<>();
-        for (MicroService microService : blueprintParser.getMicroServices(blueprintArtifact.getDcaeBlueprint())) {
-            newSet.add(new MicroServicePolicy(microService.getName(), csar.getPolicyModelYaml().orElse(""), false,
-                new HashSet<>(Arrays.asList(newLoop))));
+        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(CsarHandler csar, BlueprintArtifact blueprintArtifact) {
+    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",
diff --git a/src/main/java/org/onap/clamp/loop/Loop.java b/src/main/java/org/onap/clamp/loop/Loop.java
index a4cd86d..473364a 100644
--- a/src/main/java/org/onap/clamp/loop/Loop.java
+++ b/src/main/java/org/onap/clamp/loop/Loop.java
@@ -78,7 +78,7 @@
     @Column(name = "dcae_blueprint_id")
     private String dcaeBlueprintId;
 
-    @Column(name = "svg_representation")
+    @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation")
     private String svgRepresentation;
 
     @Expose
diff --git a/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java b/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java
index d1a4bdc..afa0723 100644
--- a/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java
+++ b/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java
@@ -170,7 +170,10 @@
         // set
         Loop loop = loopsRepo
             .findById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca.yaml")).get();
-
+        assertThat(loop.getSvgRepresentation()).startsWith("<svg ");
+        assertThat(loop.getGlobalPropertiesJson().get("dcaeDeployParameters")).isNotNull();
+        assertThat(loop.getMicroServicePolicies()).hasSize(1);
+        assertThat(loop.getOperationalPolicies()).hasSize(1);
         assertThat(loop.getModelPropertiesJson().get("serviceDetails")).isNotNull();
         assertThat(loop.getModelPropertiesJson().get("resourceDetails")).isNotNull();
     }