From 18ae7612481c386db22fe983dedf331a29141444 Mon Sep 17 00:00:00 2001 From: Francis Toth Date: Sat, 2 May 2020 10:16:17 -0400 Subject: Refactor CsarUtils::WriteArtifactDefinition Signed-off-by: Francis Toth Change-Id: I167a05d8fbc2085a3e25eeb5bfd6b30f0e80462e Issue-ID: SDC-2812 --- .../java/org/openecomp/sdc/be/tosca/CsarUtils.java | 108 +++++++++++---------- 1 file changed, 58 insertions(+), 50 deletions(-) (limited to 'catalog-be/src/main/java') diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java index 65ade9539d..492ebcf0e4 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java @@ -44,6 +44,8 @@ import java.util.function.Predicate; import java.util.function.Supplier; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.Stream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; @@ -1114,73 +1116,79 @@ public class CsarUtils { } private Either writeArtifactsInfoToSpecifiedPath(final Component mainComponent, - final ArtifactsInfo currArtifactsInfo, - final ZipOutputStream zip, - final String path, - final boolean isInCertificationRequest) throws IOException { + final ArtifactsInfo currArtifactsInfo, + final ZipOutputStream zip, + final String path, + final boolean isInCertificationRequest) throws IOException { + final Map>> artifactsInfo = currArtifactsInfo.getArtifactsInfo(); for (final ArtifactGroupTypeEnum artifactGroupTypeEnum : artifactsInfo.keySet()) { - final String groupTypeFolder = path + WordUtils.capitalizeFully(artifactGroupTypeEnum.getType()) + PATH_DELIMITER; + final String groupTypeFolder = + path + WordUtils.capitalizeFully(artifactGroupTypeEnum.getType()) + PATH_DELIMITER; final Map> artifactTypesMap = artifactsInfo.get(artifactGroupTypeEnum); for (final String artifactType : artifactTypesMap.keySet()) { final List artifactDefinitionList = artifactTypesMap.get(artifactType); - String artifactTypeFolder = groupTypeFolder + artifactType + PATH_DELIMITER; - - if(ArtifactTypeEnum.WORKFLOW.getType().equals(artifactType) && path.contains(ARTIFACTS_PATH + RESOURCES_PATH)){ - // Ignore this packaging as BPMN artifacts needs to be packaged in different manner - continue; - } - if (ArtifactTypeEnum.WORKFLOW.getType().equals(artifactType)) { - artifactTypeFolder += OperationArtifactUtil.BPMN_ARTIFACT_PATH + File.separator; - } + String artifactTypeFolder = groupTypeFolder + artifactType + PATH_DELIMITER; - Either writeArtifactDefinition = - writeArtifactDefinition(mainComponent, zip, artifactDefinitionList, artifactTypeFolder, isInCertificationRequest); + if (ArtifactTypeEnum.WORKFLOW.getType().equals(artifactType) && path + .contains(ARTIFACTS_PATH + RESOURCES_PATH)) { + // Ignore this packaging as BPMN artifacts needs to be packaged in different manner + continue; + } + if (ArtifactTypeEnum.WORKFLOW.getType().equals(artifactType)) { + artifactTypeFolder += OperationArtifactUtil.BPMN_ARTIFACT_PATH + File.separator; + } - if (writeArtifactDefinition.isRight()) { - return writeArtifactDefinition; + // TODO: We should not do this but in order to keep this refactoring small enough, + // we'll leave this as is for now + List collect = + filterArtifactDefinitionToZip(mainComponent, artifactDefinitionList, isInCertificationRequest) + .collect(Collectors.toList()); + for (ArtifactDefinition ad : collect) { + zip.putNextEntry(new ZipEntry(artifactTypeFolder + ad.getArtifactName())); + zip.write(ad.getPayloadData()); } } } - return Either.left(zip); } - private Either writeArtifactDefinition(Component mainComponent, ZipOutputStream zip, List artifactDefinitionList, - String artifactPathAndFolder, boolean isInCertificationRequest) throws IOException { - - ComponentTypeEnum componentType = mainComponent.getComponentType(); - String heatEnvType = ArtifactTypeEnum.HEAT_ENV.getType(); - - for (ArtifactDefinition artifactDefinition : artifactDefinitionList) { - if (!isInCertificationRequest && componentType == ComponentTypeEnum.SERVICE - && artifactDefinition.getArtifactType().equals(heatEnvType) || - //this is placeholder - (artifactDefinition.getEsId() == null && artifactDefinition.getMandatory())){ - continue; - } - - byte[] payloadData = artifactDefinition.getPayloadData(); - String artifactFileName = artifactDefinition.getArtifactName(); - - if (payloadData == null) { - Either fromCassandra = getFromCassandra(artifactDefinition.getEsId()); - - if (fromCassandra.isRight()) { - log.debug(ARTIFACT_NAME_UNIQUE_ID, artifactDefinition.getArtifactName(), artifactDefinition.getUniqueId()); - log.debug("Failed to get {} payload from DB reason: {}", artifactFileName, fromCassandra.right().value()); - continue; - } - payloadData = fromCassandra.left().value(); - } - zip.putNextEntry(new ZipEntry(artifactPathAndFolder + artifactFileName)); - zip.write(payloadData); + private Stream filterArtifactDefinitionToZip(Component mainComponent, + List artifactDefinitionList, boolean isInCertificationRequest) { + return artifactDefinitionList + .stream() + .filter(shouldBeInZip(isInCertificationRequest, mainComponent)) + .map(this::fetchPayLoadData) + .filter(Either::isLeft) + .map(e -> e.left().value()); + } + + private Predicate shouldBeInZip(boolean isInCertificationRequest, Component component) { + return artifactDefinition -> + !(!isInCertificationRequest + && component.isService() + && artifactDefinition.isHeatEnvType() + || artifactDefinition.hasNoMandatoryEsId()); + } + + private Either fetchPayLoadData(ArtifactDefinition ad) { + byte[] payloadData = ad.getPayloadData(); + if(payloadData == null) { + return getFromCassandra(ad.getEsId()) + .left().map(pd -> { + ad.setPayload(pd); + return ad; + }).right().map(as -> { + log.debug(ARTIFACT_NAME_UNIQUE_ID, ad.getArtifactName(), ad.getUniqueId()); + log.debug("Failed to get {} payload from DB reason: {}", ad.getArtifactName(), as); + return as; + }); + } else { + return Either.left(ad); } - - return Either.left(zip); } /************************************ Artifacts Structure ******************************************************************/ -- cgit 1.2.3-korg