summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java
diff options
context:
space:
mode:
authorFrancis Toth <francis.toth@yoppworks.com>2020-05-02 10:16:17 -0400
committerOfir Sonsino <ofir.sonsino@intl.att.com>2020-05-27 06:41:28 +0000
commit18ae7612481c386db22fe983dedf331a29141444 (patch)
treeb197c3e9d16f122dc66bd53c803169ec92a59074 /catalog-be/src/main/java
parent9373e4f17236dc7496cd7dcfbbd159675e839e55 (diff)
Refactor CsarUtils::WriteArtifactDefinition
Signed-off-by: Francis Toth <francis.toth@yoppworks.com> Change-Id: I167a05d8fbc2085a3e25eeb5bfd6b30f0e80462e Issue-ID: SDC-2812
Diffstat (limited to 'catalog-be/src/main/java')
-rw-r--r--catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java108
1 files changed, 58 insertions, 50 deletions
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<ZipOutputStream, ResponseFormat> 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<ArtifactGroupTypeEnum, Map<String, List<ArtifactDefinition>>> 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<String, List<ArtifactDefinition>> artifactTypesMap = artifactsInfo.get(artifactGroupTypeEnum);
for (final String artifactType : artifactTypesMap.keySet()) {
final List<ArtifactDefinition> 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<ZipOutputStream, ResponseFormat> 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<ArtifactDefinition> 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<ZipOutputStream, ResponseFormat> writeArtifactDefinition(Component mainComponent, ZipOutputStream zip, List<ArtifactDefinition> 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<byte[], ActionStatus> 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<ArtifactDefinition> filterArtifactDefinitionToZip(Component mainComponent,
+ List<ArtifactDefinition> artifactDefinitionList, boolean isInCertificationRequest) {
+ return artifactDefinitionList
+ .stream()
+ .filter(shouldBeInZip(isInCertificationRequest, mainComponent))
+ .map(this::fetchPayLoadData)
+ .filter(Either::isLeft)
+ .map(e -> e.left().value());
+ }
+
+ private Predicate<ArtifactDefinition> shouldBeInZip(boolean isInCertificationRequest, Component component) {
+ return artifactDefinition ->
+ !(!isInCertificationRequest
+ && component.isService()
+ && artifactDefinition.isHeatEnvType()
+ || artifactDefinition.hasNoMandatoryEsId());
+ }
+
+ private Either<ArtifactDefinition, ActionStatus> 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 ******************************************************************/