diff options
author | Francis Toth <francis.toth@yoppworks.com> | 2020-05-04 08:14:51 -0400 |
---|---|---|
committer | Ofir Sonsino <ofir.sonsino@intl.att.com> | 2020-05-11 07:22:48 +0000 |
commit | ae0f64936f9798b17b313754d643977b7567af17 (patch) | |
tree | 32573cf1f4281f9045345ee88f897e84d86ea4ae | |
parent | a7bd374357e0737e419d196a16ff09a644417f97 (diff) |
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 <francis.toth@yoppworks.com>
Issue-ID: SDC-2812
-rw-r--r-- | catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java | 90 |
1 files changed, 55 insertions, 35 deletions
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<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; } } |