Refactor ArtifactsBusinessLogic::generateToscaArtifact 35/107035/4
authorFrancis Toth <francis.toth@yoppworks.com>
Mon, 4 May 2020 12:14:51 +0000 (08:14 -0400)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Mon, 11 May 2020 07:22:48 +0000 (07:22 +0000)
This commit aims to refactor how Either is used in ArtifactsBusinessLogic::generateToscaArtifact. In order to keep the refactoring relatively small, we evalute its result at the call site.

Change-Id: I669b30b44e9c69d1b3968fd719c5340b2dff83f5
Signed-off-by: Francis Toth <francis.toth@yoppworks.com>
Issue-ID: SDC-2812

catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java

index 94c379a..a0fd1f2 100644 (file)
@@ -356,38 +356,50 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic {
         ArtifactDefinition artifactDefinition, Component component,
         User user, boolean isInCertificationRequest, boolean shouldLock, boolean inTransaction,
         boolean fetchTemplatesFromDB) {
-        generateToscaArtifact(component, artifactDefinition, isInCertificationRequest, fetchTemplatesFromDB);
-        byte[] decodedPayload = artifactDefinition.getPayloadData();
-        artifactDefinition.setEsId(artifactDefinition.getUniqueId());
-        artifactDefinition.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(decodedPayload));
-        return lockComponentAndUpdateArtifact(component.getUniqueId(), artifactDefinition, AuditingActionEnum.ARTIFACT_PAYLOAD_UPDATE, artifactDefinition.getUniqueId(),
-                user, component.getComponentType(), component, decodedPayload, null, null, shouldLock, inTransaction);
+
+        Either<byte[], ComponentException> decodedPayload = decodeToscaArtifactPayload(
+            component, isInCertificationRequest, fetchTemplatesFromDB, artifactDefinition.getArtifactType()
+        );
+        // TODO: This should not be done, but in order to keep this refactoring relatively small, we stop here
+        if(decodedPayload.isRight())
+            throw decodedPayload.right().value();
+        else {
+            byte[] payload = decodedPayload.left().value();
+            artifactDefinition.setPayload(payload);
+            artifactDefinition.setEsId(artifactDefinition.getUniqueId());
+            artifactDefinition.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(payload));
+            return lockComponentAndUpdateArtifact(component.getUniqueId(), artifactDefinition,
+                AuditingActionEnum.ARTIFACT_PAYLOAD_UPDATE, artifactDefinition.getUniqueId(),
+                user, component.getComponentType(), component, payload, null, null, shouldLock, inTransaction
+            );
+        }
     }
 
-    private ArtifactDefinition generateToscaArtifact(Component parent, ArtifactDefinition artifactInfo, boolean isInCertificationRequest, boolean fetchTemplatesFromDB) {
+    private Either<byte[], ComponentException> decodeToscaArtifactPayload(
+        Component parent,
+        boolean isInCertificationRequest,
+        boolean fetchTemplatesFromDB,
+        String artifactType
+    ) {
         log.debug("tosca artifact generation");
-        if (ArtifactTypeEnum.TOSCA_CSAR.getType().equals(artifactInfo.getArtifactType())) {
-            Either<byte[], ResponseFormat> generated = csarUtils.createCsar(parent, fetchTemplatesFromDB, isInCertificationRequest);
-            if (generated.isRight()) {
-                ResponseFormat error = generated.right().value();
-                log.debug("Failed to generate tosca csar for component {} error {}", parent.getUniqueId(), error);
-                throw new ByResponseFormatComponentException(error);
-            }
-            artifactInfo.setPayload(generated.left().value());
-
-        }
-        else {
-            Either<ToscaRepresentation, ToscaError> exportComponent = toscaExportUtils.exportComponent(parent);
-            if (exportComponent.isRight()) {
-                ToscaError toscaError = exportComponent.right().value();
-                log.debug("Failed export tosca yaml for component {} error {}", parent.getUniqueId(), toscaError);
-                ActionStatus status = componentsUtils.convertFromToscaError(toscaError);
-                throw new ByActionStatusComponentException(status);
-            }
-            log.debug("Tosca yaml exported for component {} ", parent.getUniqueId());
-            artifactInfo.setPayloadData(exportComponent.left().value().getMainYaml());
+        if (ArtifactTypeEnum.TOSCA_CSAR.getType().equals(artifactType)) {
+            return csarUtils
+                .createCsar(parent, fetchTemplatesFromDB, isInCertificationRequest)
+                .right().map(error -> {
+                    log.debug("Failed to generate tosca csar for component {} error {}", parent.getUniqueId(), error);
+                    return new ByResponseFormatComponentException(error);
+                });
+        } else {
+            return toscaExportUtils
+                .exportComponent(parent)
+                .left().map(toscaRepresentation -> {
+                    log.debug("Tosca yaml exported for component {} ", parent.getUniqueId());
+                    return toscaRepresentation.getMainYaml().getBytes();
+                }).right().map(toscaError -> {
+                    log.debug("Failed export tosca yaml for component {} error {}", parent.getUniqueId(), toscaError);
+                    return new ByActionStatusComponentException(componentsUtils.convertFromToscaError(toscaError));
+                });
         }
-        return artifactInfo;
     }
 
     private Either<ArtifactDefinition, Operation> doAction(String componentId, ComponentTypeEnum componentType, ArtifactOperationInfo operation, String artifactId, ArtifactDefinition artifactInfo, String origMd5,
@@ -647,20 +659,28 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic {
         return ActionStatus.OK;
     }
 
-    ArtifactDefinition generateNotSavedArtifact(Component parent, ArtifactDefinition artifactInfo) {
-        if (artifactInfo.getArtifactGroupType() == ArtifactGroupTypeEnum.TOSCA) {
-            return generateToscaArtifact(parent, artifactInfo, false, false);
+    ArtifactDefinition generateNotSavedArtifact(Component parent, ArtifactDefinition artifactDefinition) {
+        if (artifactDefinition.getArtifactGroupType() == ArtifactGroupTypeEnum.TOSCA) {
+            Either<byte[], ComponentException> decodedPayload = decodeToscaArtifactPayload(parent, false,
+                false, artifactDefinition.getArtifactType());
+            // TODO: This should not be done, but in order to keep this refactoring relatively small, we stop here
+            if(decodedPayload.isRight())
+                throw decodedPayload.right().value();
+            else {
+                artifactDefinition.setPayload(decodedPayload.left().value());
+                return artifactDefinition;
+            }
         }
         else {
-            String heatArtifactId = artifactInfo.getGeneratedFromId();
+            String heatArtifactId = artifactDefinition.getGeneratedFromId();
             Either<ArtifactDefinition, StorageOperationStatus> heatRes = artifactToscaOperation.getArtifactById(parent.getUniqueId(), heatArtifactId);
             if (heatRes.isRight()) {
-                log.debug("Failed to fetch heat artifact by generated id {} for heat env {}", heatArtifactId, artifactInfo.getUniqueId());
+                log.debug("Failed to fetch heat artifact by generated id {} for heat env {}", heatArtifactId, artifactDefinition.getUniqueId());
                 throw new StorageException(heatRes.right().value());
             }
             String generatedPayload = generateHeatEnvPayload(heatRes.left().value());
-            artifactInfo.setPayloadData(generatedPayload);
-            return artifactInfo;
+            artifactDefinition.setPayloadData(generatedPayload);
+            return artifactDefinition;
         }
     }