Refactor CsarUtil::addInnerComponentsToCSAR 80/107880/2
authorFrancis Toth <francis.toth@yoppworks.com>
Tue, 19 May 2020 14:36:58 +0000 (10:36 -0400)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Mon, 25 May 2020 10:24:06 +0000 (10:24 +0000)
Signed-off-by: Francis Toth <francis.toth@yoppworks.com>
Change-Id: I7f32d729a96f696c217631cd019105c17cb36a8c
Issue-ID: SDC-2812

catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java

index 21c626b..65ade95 100644 (file)
@@ -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<ZipOutputStream, ResponseFormat> responseFormat = addInnerComponentsToCSAR(zip, innerComponentsCache);
-            if (responseFormat != null) return responseFormat;
+            Either<ZipOutputStream, ResponseFormat> responseFormat = addInnerComponentsToCSAR(zip,
+                innerComponentsCache);
+            if (responseFormat != null) {
+                return responseFormat;
+            }
         }
         return null;
     }
 
-    private Either<ZipOutputStream, ResponseFormat> addInnerComponentsToCSAR(ZipOutputStream zip, Map<String, ImmutableTriple<String, String, Component>> innerComponentsCache) throws IOException {
-        for (Entry<String, ImmutableTriple<String, String, Component>> innerComponentTripleEntry : innerComponentsCache.entrySet()) {
-
-            ImmutableTriple<String, String, Component> innerComponentTriple = innerComponentTripleEntry.getValue();
-
-            Component innerComponent = innerComponentTriple.getRight();
-            String icFileName = innerComponentTriple.getMiddle();
+    private Either<ZipOutputStream, ResponseFormat> addInnerComponentsToCSAR(
+        ZipOutputStream zip,
+        Map<String, ImmutableTriple<String, String, Component>> innerComponentsCache
+    ) throws IOException {
+        for (Entry<String, ImmutableTriple<String, String, Component>> entry : innerComponentsCache.entrySet()) {
+            ImmutableTriple<String, String, Component> ict = entry.getValue();
+            Component innerComponent = ict.getRight();
 
+            String icFileName = ict.getMiddle();
             // add component to zip
-            Either<byte[], ActionStatus> 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<Tuple2<byte[], ZipEntry>, 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<byte[], ZipEntry> 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<Tuple2<byte[], ZipEntry>, ResponseFormat> toZipEntry(
+        ImmutableTriple<String, String, Component> 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<String> nodesFromPackage) {