Refactor CsarUtil::getLatestSchemaFilesFromCassandra 86/106986/4
authorFrancis Toth <francis.toth@yoppworks.com>
Fri, 1 May 2020 15:45:41 +0000 (11:45 -0400)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Tue, 5 May 2020 08:15:12 +0000 (08:15 +0000)
Signed-off-by: Francis Toth <francis.toth@yoppworks.com>
Change-Id: I3f96d76a07fd32bdbd8b59beb5409eab0b0f3aa8
Issue-ID: SDC-2812

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

index bb388fe..8044110 100644 (file)
@@ -39,6 +39,7 @@ import java.util.Objects;
 import java.util.Optional;
 import java.util.Set;
 import java.util.function.Function;
+import java.util.function.Predicate;
 import java.util.function.Supplier;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -630,25 +631,39 @@ public class CsarUtils {
     }
 
     private Either<byte[], ResponseFormat> getLatestSchemaFilesFromCassandra() {
-        Either<List<SdcSchemaFilesData>, CassandraOperationStatus> specificSchemaFiles = sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(getVersionFirstThreeOctets(), CONFORMANCE_LEVEL);
-
-        if(specificSchemaFiles.isRight()){
-            log.debug("Failed to get the schema files SDC-Version: {} Conformance-Level {}. Please fix DB table accordingly.", getVersionFirstThreeOctets(), CONFORMANCE_LEVEL);
-            StorageOperationStatus storageStatus = DaoStatusConverter.convertCassandraStatusToStorageStatus(specificSchemaFiles.right().value());
-            ActionStatus convertedFromStorageResponse = componentsUtils.convertFromStorageResponse(storageStatus);
-            return Either.right(componentsUtils.getResponseFormat(convertedFromStorageResponse));
-        }
-
-        List<SdcSchemaFilesData> listOfSchemas = specificSchemaFiles.left().value();
+        String fto = getVersionFirstThreeOctets();
+        return sdcSchemaFilesCassandraDao.getSpecificSchemaFiles(fto, CONFORMANCE_LEVEL)
+            .right().map(schemaFilesFetchDBError(fto))
+            .left().bind(iff(
+                List::isEmpty,
+                () -> schemaFileFetchError(fto),
+                s -> Either.left(s.iterator().next().getPayloadAsArray())
+                )
+            );
+    }
 
-        if(listOfSchemas.isEmpty()){
-            log.debug("Failed to get the schema files SDC-Version: {} Conformance-Level {}", getVersionFirstThreeOctets(), CONFORMANCE_LEVEL);
-            return Either.right(componentsUtils.getResponseFormat(ActionStatus.TOSCA_SCHEMA_FILES_NOT_FOUND, getVersionFirstThreeOctets(), CONFORMANCE_LEVEL));
-        }
+    private static <A, B> F<A, B> iff(Predicate<A> p, Supplier<B> s, Function<A, B> orElse) {
+        return a -> p.test(a) ? s.get() : orElse.apply(a);
+    }
 
-        SdcSchemaFilesData schemaFile = listOfSchemas.iterator().next();
+    private F<CassandraOperationStatus, ResponseFormat> schemaFilesFetchDBError(String firstThreeOctets) {
+        return cos -> {
+            log.debug(
+                "Failed to get the schema files SDC-Version: {} Conformance-Level {}. Please fix DB table accordingly.",
+                firstThreeOctets, CONFORMANCE_LEVEL);
+            StorageOperationStatus sos = DaoStatusConverter.convertCassandraStatusToStorageStatus(cos);
+            return componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(sos));
+        };
+    }
 
-        return Either.left(schemaFile.getPayloadAsArray());
+    private Either<byte[], ResponseFormat> schemaFileFetchError(String firstThreeOctets) {
+        log.debug("Failed to get the schema files SDC-Version: {} Conformance-Level {}",
+            firstThreeOctets, CONFORMANCE_LEVEL);
+        return Either.right(
+            componentsUtils.getResponseFormat(
+                ActionStatus.TOSCA_SCHEMA_FILES_NOT_FOUND, firstThreeOctets, CONFORMANCE_LEVEL
+            )
+        );
     }
 
     private Either<byte[], ActionStatus> getFromCassandra(String cassandraId) {