From: Francis Toth Date: Fri, 1 May 2020 15:45:41 +0000 (-0400) Subject: Refactor CsarUtil::getLatestSchemaFilesFromCassandra X-Git-Tag: 1.7.0~173 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=908793f04e9a595734a6e0e189b846ed062bfc7d;p=sdc.git Refactor CsarUtil::getLatestSchemaFilesFromCassandra Signed-off-by: Francis Toth Change-Id: I3f96d76a07fd32bdbd8b59beb5409eab0b0f3aa8 Issue-ID: SDC-2812 --- 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 bb388fe311..80441103cf 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 @@ -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 getLatestSchemaFilesFromCassandra() { - Either, 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 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 F iff(Predicate p, Supplier s, Function orElse) { + return a -> p.test(a) ? s.get() : orElse.apply(a); + } - SdcSchemaFilesData schemaFile = listOfSchemas.iterator().next(); + private F 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 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 getFromCassandra(String cassandraId) {