From ae0f64936f9798b17b313754d643977b7567af17 Mon Sep 17 00:00:00 2001 From: Francis Toth Date: Mon, 4 May 2020 08:14:51 -0400 Subject: Refactor ArtifactsBusinessLogic::generateToscaArtifact 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 Issue-ID: SDC-2812 --- .../be/components/impl/ArtifactsBusinessLogic.java | 90 +++++++++++++--------- 1 file changed, 55 insertions(+), 35 deletions(-) (limited to 'catalog-be') diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java index 94c379ae29..a0fd1f2475 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java @@ -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 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 decodeToscaArtifactPayload( + Component parent, + boolean isInCertificationRequest, + boolean fetchTemplatesFromDB, + String artifactType + ) { log.debug("tosca artifact generation"); - if (ArtifactTypeEnum.TOSCA_CSAR.getType().equals(artifactInfo.getArtifactType())) { - Either 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 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 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 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 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; } } -- cgit 1.2.3-korg