From c9858db21d5b2e5a4f7cd2f2963fdbcb8c4e943c Mon Sep 17 00:00:00 2001 From: Francis Toth Date: Tue, 19 May 2020 10:36:58 -0400 Subject: Refactor CsarUtil::addInnerComponentsToCSAR Signed-off-by: Francis Toth Change-Id: I7f32d729a96f696c217631cd019105c17cb36a8c Issue-ID: SDC-2812 --- .../java/org/openecomp/sdc/be/tosca/CsarUtils.java | 56 ++++++++++++++-------- 1 file changed, 36 insertions(+), 20 deletions(-) (limited to 'catalog-be/src') 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 21c626bf53..65ade9539d 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 @@ -23,6 +23,7 @@ package org.openecomp.sdc.be.tosca; import fj.F; import fj.data.Either; +import io.vavr.Tuple2; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.File; @@ -485,40 +486,55 @@ public class CsarUtils { } //add inner components to CSAR - Either responseFormat = addInnerComponentsToCSAR(zip, innerComponentsCache); - if (responseFormat != null) return responseFormat; + Either responseFormat = addInnerComponentsToCSAR(zip, + innerComponentsCache); + if (responseFormat != null) { + return responseFormat; + } } return null; } - private Either addInnerComponentsToCSAR(ZipOutputStream zip, Map> innerComponentsCache) throws IOException { - for (Entry> innerComponentTripleEntry : innerComponentsCache.entrySet()) { - - ImmutableTriple innerComponentTriple = innerComponentTripleEntry.getValue(); - - Component innerComponent = innerComponentTriple.getRight(); - String icFileName = innerComponentTriple.getMiddle(); + private Either addInnerComponentsToCSAR( + ZipOutputStream zip, + Map> innerComponentsCache + ) throws IOException { + for (Entry> entry : innerComponentsCache.entrySet()) { + ImmutableTriple ict = entry.getValue(); + Component innerComponent = ict.getRight(); + String icFileName = ict.getMiddle(); // add component to zip - Either entryData = getEntryData(innerComponentTriple.getLeft(), innerComponent); - if (entryData.isRight()) { - ResponseFormat responseFormat = componentsUtils.getResponseFormat(entryData.right().value()); - log.debug("Failed adding to zip component {}, error {}", innerComponentTriple.getLeft(), - entryData.right().value()); - return Either.right(responseFormat); + Either, ResponseFormat> zipEntry = toZipEntry(ict); + // TODO: this should not be done, we should instead compose this either further, + // but in order to keep this refactoring small, we'll stop here. + if (zipEntry.isRight()) { + return Either.right(zipEntry.right().value()); } - byte[] content = entryData.left().value(); - zip.putNextEntry(new ZipEntry(DEFINITIONS_PATH + icFileName)); - zip.write(content); - + Tuple2 value = zipEntry.left().value(); + zip.putNextEntry(value._2); + zip.write(value._1); // add component interface to zip if (!ModelConverter.isAtomicComponent(innerComponent)) { - writeComponentInterface(innerComponent, zip, icFileName, true); + writeComponentInterface(innerComponent, zip, icFileName, true); } } return null; } + private Either, ResponseFormat> toZipEntry( + ImmutableTriple cachedEntry + ) { + String cassandraId = cachedEntry.getLeft(); + String fileName = cachedEntry.getMiddle(); + Component innerComponent = cachedEntry.getRight(); + return getEntryData(cassandraId, innerComponent) + .right().map(status -> { + log.debug("Failed adding to zip component {}, error {}", cassandraId, status); + return componentsUtils.getResponseFormat(status); + }).left().map(content -> new Tuple2<>(content, new ZipEntry(DEFINITIONS_PATH + fileName))); + } + private void addSchemaFilesFromCassandra(final ZipOutputStream zip, final byte[] schemaFileZip, final List nodesFromPackage) { -- cgit 1.2.3-korg