Refactor CsarUtils::getEntryData 01/106601/2
authorFrancis Toth <francis.toth@yoppworks.com>
Fri, 24 Apr 2020 14:29:03 +0000 (10:29 -0400)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Tue, 28 Apr 2020 09:19:04 +0000 (09:19 +0000)
Change-Id: I1732ae05cfbe93c1bef8c249e16088a0cad34106
Issue-ID: SDC-2812
Signed-off-by: Francis Toth <francis.toth@yoppworks.com>
catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java

index 39c0a09..22b655a 100644 (file)
@@ -21,6 +21,7 @@
 package org.openecomp.sdc.be.tosca;
 
 
+import fj.F;
 import fj.data.Either;
 import java.io.BufferedOutputStream;
 import java.io.ByteArrayInputStream;
@@ -609,26 +610,23 @@ public class CsarUtils {
                return Either.left(zip);
        }
 
-       private Either<byte[], ActionStatus> getEntryData(String cassandraId, Component childComponent) {
-               byte[] content;
-               if (cassandraId == null || cassandraId.isEmpty()) {
-                       Either<ToscaRepresentation, ToscaError> exportRes = toscaExportUtils.exportComponent(childComponent);
-                       if (exportRes.isRight()) {
-                               log.debug("Failed to export tosca template for child component {} error {}",
-                                               childComponent.getUniqueId(), exportRes.right().value());
-                               return Either.right(componentsUtils.convertFromToscaError(exportRes.right().value()));
-                       }
-                       content = exportRes.left().value().getMainYaml().getBytes();
-               } else {
-                       Either<byte[], ActionStatus> fromCassandra = getFromCassandra(cassandraId);
-                       if (fromCassandra.isRight()) {
-                               return Either.right(fromCassandra.right().value());
-                       } else {
-                               content = fromCassandra.left().value();
-                       }
-               }
-               return Either.left(content);
-       }
+    private Either<byte[], ActionStatus> getEntryData(String cassandraId, Component childComponent) {
+        if (cassandraId == null || cassandraId.isEmpty()) {
+            return toscaExportUtils.exportComponent(childComponent)
+                .right().map(toscaErrorToActionStatus(childComponent))
+                .left().map(toscaRepresentation -> toscaRepresentation.getMainYaml().getBytes());
+        } else {
+            return getFromCassandra(cassandraId);
+        }
+    }
+
+    private F<ToscaError, ActionStatus> toscaErrorToActionStatus(Component childComponent) {
+        return toscaError -> {
+            log.debug("Failed to export tosca template for child component {} error {}",
+                childComponent.getUniqueId(), toscaError);
+            return componentsUtils.convertFromToscaError(toscaError);
+        };
+    }
 
     private Either<byte[], ResponseFormat> getLatestSchemaFilesFromCassandra() {
         Either<List<SdcSchemaFilesData>, CassandraOperationStatus> specificSchemaFiles = sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(getVersionFirstThreeOctets(), CONFORMANCE_LEVEL);