From bf37c21229e1b60937202c3962b24f14b45c96e5 Mon Sep 17 00:00:00 2001 From: Francis Toth Date: Fri, 1 May 2020 12:42:58 -0400 Subject: Refactor CsarUtil::extractVfcArtifact Signed-off-by: Francis Toth Change-Id: Ica8d17d4f3c763920f406ccb89fb3e464d05eab4 Issue-ID: SDC-2812 --- .../java/org/openecomp/sdc/be/tosca/CsarUtils.java | 84 +++++++++++++--------- 1 file changed, 49 insertions(+), 35 deletions(-) (limited to 'catalog-be/src/main') 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 8a8fff797f..21c626bf53 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 @@ -753,7 +753,11 @@ public class CsarUtils { // filter CSAR entry by node type artifact path .filter(e -> Pattern.compile(VFC_NODE_TYPE_ARTIFACTS_PATH_PATTERN).matcher(e.getKey()).matches()) // extract ArtifactDefinition from CSAR entry for each entry with matching artifact path - .forEach(e -> addExtractedVfcArtifact(extractVfcArtifact(e, collectedWarningMessages), artifacts)); + .forEach(e -> + extractVfcArtifact(e, collectedWarningMessages).ifPresent(ip -> + addExtractedVfcArtifact(ip, artifacts) + ) + ); // add counter suffix to artifact labels handleWarningMessages(collectedWarningMessages); @@ -775,30 +779,30 @@ public class CsarUtils { } - private static void addExtractedVfcArtifact(ImmutablePair extractedVfcArtifact, Map> artifacts) { - if (extractedVfcArtifact != null) { - List currArtifactsList; - String vfcToscaNamespace = extractedVfcArtifact.getKey(); - if (artifacts.containsKey(vfcToscaNamespace)) { - currArtifactsList = artifacts.get(vfcToscaNamespace); - } else { - currArtifactsList = new ArrayList<>(); - artifacts.put(vfcToscaNamespace, currArtifactsList); - } - currArtifactsList.add(extractedVfcArtifact.getValue()); - } + private static void addExtractedVfcArtifact( + ImmutablePair extractedVfcArtifact, + Map> artifacts + ) { + String vfcToscaNamespace = extractedVfcArtifact.getKey(); + + artifacts.computeIfAbsent(vfcToscaNamespace, k -> new ArrayList<>()); + artifacts.get(vfcToscaNamespace).add(extractedVfcArtifact.getValue()); } - private static ImmutablePair extractVfcArtifact(Entry entry, Map>> collectedWarningMessages) { - ArtifactDefinition artifact; + private static Optional> extractVfcArtifact( + Entry entry, + Map>> collectedWarningMessages + ) { String[] parsedCsarArtifactPath = entry.getKey().split(PATH_DELIMITER); - Either eitherArtifactGroupType = detectArtifactGroupType(parsedCsarArtifactPath[2].toUpperCase(), collectedWarningMessages); - if (eitherArtifactGroupType.isLeft()) { - artifact = buildArtifactDefinitionFromCsarArtifactPath(entry, collectedWarningMessages, parsedCsarArtifactPath, eitherArtifactGroupType.left().value()); - } else { - return null; - } - return new ImmutablePair<>(parsedCsarArtifactPath[1], artifact); + String groupType = parsedCsarArtifactPath[2].toUpperCase(); + + return detectArtifactGroupType(groupType, collectedWarningMessages) + .left() + .map(buildArtifactDefinitionFromCsarArtifactPath(entry, collectedWarningMessages, parsedCsarArtifactPath)) + .either( + ad -> Optional.of(new ImmutablePair<>(parsedCsarArtifactPath[1], ad)), + b -> Optional.empty() + ); } private static Either detectArtifactGroupType(String groupType, Map>> collectedWarningMessages) { @@ -829,19 +833,29 @@ public class CsarUtils { return result; } - private static ArtifactDefinition buildArtifactDefinitionFromCsarArtifactPath(Entry entry, Map>> collectedWarningMessages, String[] parsedCsarArtifactPath, ArtifactGroupTypeEnum artifactGroupType) { - ArtifactDefinition artifact; - artifact = new ArtifactDefinition(); - artifact.setArtifactGroupType(artifactGroupType); - artifact.setArtifactType(detectArtifactTypeVFC(artifactGroupType, parsedCsarArtifactPath[3], parsedCsarArtifactPath[1], collectedWarningMessages)); - artifact.setArtifactName(ValidationUtils.normalizeFileName(parsedCsarArtifactPath[parsedCsarArtifactPath.length - 1])); - artifact.setPayloadData(Base64.encodeBase64String(entry.getValue())); - artifact.setArtifactDisplayName(artifact.getArtifactName().lastIndexOf('.') > 0 ? artifact.getArtifactName().substring(0, artifact.getArtifactName().lastIndexOf('.')) : artifact.getArtifactName()); - artifact.setArtifactLabel(ValidationUtils.normalizeArtifactLabel(artifact.getArtifactName())); - artifact.setDescription(ARTIFACT_CREATED_FROM_CSAR); - artifact.setIsFromCsar(true); - artifact.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(entry.getValue())); - return artifact; + private static F buildArtifactDefinitionFromCsarArtifactPath( + Entry entry, + Map>> collectedWarningMessages, + String[] parsedCsarArtifactPath + ) { + return artifactGroupType -> { + ArtifactDefinition artifact; + artifact = new ArtifactDefinition(); + artifact.setArtifactGroupType(artifactGroupType); + artifact.setArtifactType( + detectArtifactTypeVFC(artifactGroupType, parsedCsarArtifactPath[3], parsedCsarArtifactPath[1], + collectedWarningMessages)); + artifact.setArtifactName( + ValidationUtils.normalizeFileName(parsedCsarArtifactPath[parsedCsarArtifactPath.length - 1])); + artifact.setPayloadData(Base64.encodeBase64String(entry.getValue())); + artifact.setArtifactDisplayName(artifact.getArtifactName().lastIndexOf('.') > 0 ? artifact.getArtifactName() + .substring(0, artifact.getArtifactName().lastIndexOf('.')) : artifact.getArtifactName()); + artifact.setArtifactLabel(ValidationUtils.normalizeArtifactLabel(artifact.getArtifactName())); + artifact.setDescription(ARTIFACT_CREATED_FROM_CSAR); + artifact.setIsFromCsar(true); + artifact.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(entry.getValue())); + return artifact; + }; } @Getter -- cgit 1.2.3-korg