Fix PolicyID for DCAE
[clamp.git] / src / main / java / org / onap / clamp / loop / CsarInstallerImpl.java
index 6e12f29..f407aa9 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;
@@ -122,12 +127,20 @@ public class CsarInstallerImpl implements CsarInstaller {
             blueprintArtifact.getResourceAttached().getResourceInstanceName(),
             blueprintArtifact.getBlueprintArtifactName()));
         newLoop.setLastComputedState(LoopState.DESIGN);
-        newLoop.setMicroServicePolicies(createMicroServicePolicies(csar, blueprintArtifact, newLoop));
+
+        List<MicroService> microServicesChain = chainGenerator
+            .getChainOfMicroServices(blueprintParser.getMicroServices(blueprintArtifact.getDcaeBlueprint()));
+        if (microServicesChain.isEmpty()) {
+            microServicesChain = blueprintParser.fallbackToOneMicroService(blueprintArtifact.getDcaeBlueprint());
+        }
+
+        newLoop
+            .setMicroServicePolicies(createMicroServicePolicies(microServicesChain, 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(svgFacade.getSvgImage(microServicesChain));
+        newLoop.setGlobalPropertiesJson(createGlobalPropertiesJson(blueprintArtifact, newLoop));
+        newLoop.setModelPropertiesJson(createModelPropertiesJson(csar));
         DcaeInventoryResponse dcaeResponse = queryDcaeToGetServiceTypeId(blueprintArtifact);
         newLoop.setDcaeBlueprintId(dcaeResponse.getTypeId());
         return newLoop;
@@ -141,24 +154,32 @@ public class CsarInstallerImpl implements CsarInstaller {
             blueprintArtifact.getBlueprintArtifactName()), newLoop, new JsonObject())));
     }
 
-    private HashSet<MicroServicePolicy> createMicroServicePolicies(CsarHandler csar,
-        BlueprintArtifact blueprintArtifact, Loop newLoop) throws IOException {
+    private HashSet<MicroServicePolicy> createMicroServicePolicies(List<MicroService> microServicesChain,
+        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))));
+
+        for (MicroService microService : microServicesChain) {
+            MicroServicePolicy microServicePolicy = new MicroServicePolicy(
+                Policy.generatePolicyName(microService.getName(), csar.getSdcNotification().getServiceName(),
+                    csar.getSdcNotification().getServiceVersion(),
+                    blueprintArtifact.getResourceAttached().getResourceInstanceName(),
+                    blueprintArtifact.getBlueprintArtifactName()),
+                microService.getModelType(), csar.getPolicyModelYaml().orElse(""), false,
+                new HashSet<>(Arrays.asList(newLoop)));
+
+            newSet.add(microServicePolicy);
+            microService.setMappedNameJpa(microServicePolicy.getName());
         }
         return newSet;
     }
 
-    private JsonObject createGlobalPropertiesJson(CsarHandler csar, BlueprintArtifact blueprintArtifact) {
+    private JsonObject createGlobalPropertiesJson(BlueprintArtifact blueprintArtifact, Loop newLoop) {
         JsonObject globalProperties = new JsonObject();
-        globalProperties.add("dcaeDeployParameters", getAllBlueprintParametersInJson(blueprintArtifact));
+        globalProperties.add("dcaeDeployParameters", getAllBlueprintParametersInJson(blueprintArtifact, newLoop));
         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",
@@ -177,7 +198,7 @@ public class CsarInstallerImpl implements CsarInstaller {
         return modelProperties;
     }
 
-    private JsonObject getAllBlueprintParametersInJson(BlueprintArtifact blueprintArtifact) {
+    private JsonObject getAllBlueprintParametersInJson(BlueprintArtifact blueprintArtifact, Loop newLoop) {
         JsonObject node = new JsonObject();
         Yaml yaml = new Yaml();
         Map<String, Object> inputsNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
@@ -190,13 +211,14 @@ public class CsarInstallerImpl implements CsarInstaller {
                 node.addProperty(elem.getKey(), "");
             }
         });
-        node.addProperty("policy_id", "AUTO_GENERATED_POLICY_ID_AT_SUBMIT");
+        // For Dublin only one micro service is expected
+        node.addProperty("policy_id", ((MicroServicePolicy) newLoop.getMicroServicePolicies().toArray()[0]).getName());
         return node;
     }
 
     /**
      * ll get the latest version of the artifact (version can be specified to DCAE
-     * call)
+     * call).
      *
      * @return The DcaeInventoryResponse object containing the dcae values
      */