Add svg support 43/82043/2
authorsebdet <sebastien.determe@intl.att.com>
Tue, 12 Mar 2019 15:35:25 +0000 (16:35 +0100)
committersebdet <sebastien.determe@intl.att.com>
Tue, 12 Mar 2019 15:47:15 +0000 (16:47 +0100)
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>
extra/sql/bulkload/create-tables.sql
src/main/java/org/onap/clamp/clds/sdc/controller/installer/ChainGenerator.java
src/main/java/org/onap/clamp/loop/CsarInstallerImpl.java
src/main/java/org/onap/clamp/loop/Loop.java
src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java

index 93c80cb..b356063 100644 (file)
@@ -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;
 
index b05b80f..27c5b9c 100644 (file)
@@ -26,21 +26,23 @@ import java.util.LinkedList;
 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 @@ public class ChainGenerator {
     }
 
     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;
             }
         }
index 6e12f29..07f1b77 100644 (file)
@@ -31,6 +31,7 @@ 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;
 
@@ -46,6 +47,7 @@ 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;
@@ -82,6 +84,9 @@ public class CsarInstallerImpl implements CsarInstaller {
     @Autowired
     DcaeInventoryServices dcaeInventoryService;
 
+    @Autowired
+    private SvgFacade svgFacade;
+
     @Override
     public boolean isCsarAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException {
         boolean alreadyInstalled = true;
@@ -113,6 +118,16 @@ public class CsarInstallerImpl implements CsarInstaller {
         }
     }
 
+    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 @@ public class CsarInstallerImpl implements CsarInstaller {
         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 @@ public class CsarInstallerImpl implements CsarInstaller {
     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",
index a4cd86d..473364a 100644 (file)
@@ -78,7 +78,7 @@ public class Loop implements Serializable {
     @Column(name = "dcae_blueprint_id")
     private String dcaeBlueprintId;
 
-    @Column(name = "svg_representation")
+    @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation")
     private String svgRepresentation;
 
     @Expose
index d1a4bdc..afa0723 100644 (file)
@@ -170,7 +170,10 @@ public class CsarInstallerItCase {
         // 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();
     }