Refactor CsarUtils::populateZip 03/106603/5
authorFrancis Toth <francis.toth@yoppworks.com>
Fri, 24 Apr 2020 15:11:01 +0000 (11:11 -0400)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Tue, 28 Apr 2020 09:18:59 +0000 (09:18 +0000)
Signed-off-by: Francis Toth <francis.toth@yoppworks.com>
Change-Id: I136ebda0ff92d6c42368f102ccf2b10f37524808
Issue-ID: SDC-2812

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

index 92a1f80..39c0a09 100644 (file)
@@ -35,6 +35,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Objects;
+import java.util.Optional;
 import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -234,47 +235,37 @@ public class CsarUtils {
 
     private Either<ZipOutputStream, ResponseFormat> populateZip(Component component, boolean getFromCS, ZipOutputStream zip, boolean isInCertificationRequest) throws IOException {
 
-        LifecycleStateEnum lifecycleState = component.getLifecycleState();
-        String componentYaml;
-        Either<ToscaRepresentation, ToscaError> exportComponent;
-        byte[] mainYaml;
-        // <file name, cassandraId, component>
-        List<Triple<String, String, Component>> dependencies = null;
-
-        Map<String, ArtifactDefinition> toscaArtifacts = component.getToscaArtifacts();
-        ArtifactDefinition artifactDefinition = toscaArtifacts.get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE);
-        String fileName = artifactDefinition.getArtifactName();
+        ArtifactDefinition artifactDef = component
+            .getToscaArtifacts()
+            .get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE);
 
+        LifecycleStateEnum lifecycleState = component.getLifecycleState();
+        // Assigning to null is a bad practice but in order to keep the refactoring small enough we keep this for now.
+        Either<MainYamlWithDependencies, ResponseFormat> result = null;
         if (getFromCS || !(lifecycleState == LifecycleStateEnum.NOT_CERTIFIED_CHECKIN || lifecycleState == LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT)) {
-            String cassandraId = artifactDefinition.getEsId();
-            Either<byte[], ActionStatus> fromCassandra = getFromCassandra(cassandraId);
-            if (fromCassandra.isRight()) {
-                log.debug(ARTIFACT_NAME_UNIQUE_ID, artifactDefinition.getArtifactName(), artifactDefinition.getUniqueId());
-                ResponseFormat responseFormat = componentsUtils.getResponseFormat(fromCassandra.right().value());
-                return Either.right(responseFormat);
-            }
-            mainYaml = fromCassandra.left().value();
+            result = getArtifactFromCassandra(artifactDef);
+        } else {
+            result = exportComponent(component);
+        }
 
+        // TODO: Refactor the rest of this function
+        byte[] mainYaml;
+        List<Triple<String, String, Component>> dependencies = null;
+        // This should not be done but in order to keep the refactoring small enough we stop here.
+        if(result.isLeft()) {
+            mainYaml = result.left().value().mainYaml;
+            dependencies = result.left().value().dependencies.orElse(null);
         } else {
-            exportComponent = toscaExportUtils.exportComponent(component);
-            if (exportComponent.isRight()) {
-                log.debug("exportComponent failed", exportComponent.right().value());
-                ActionStatus convertedFromToscaError = componentsUtils.convertFromToscaError(exportComponent.right().value());
-                ResponseFormat responseFormat = componentsUtils.getResponseFormat(convertedFromToscaError);
-                return Either.right(responseFormat);
-            }
-            ToscaRepresentation exportResult = exportComponent.left().value();
-            componentYaml = exportResult.getMainYaml();
-            mainYaml = componentYaml.getBytes();
-            dependencies = exportResult.getDependencies();
+            return Either.right(result.right().value());
         }
 
+        String fileName = artifactDef.getArtifactName();
         zip.putNextEntry(new ZipEntry(DEFINITIONS_PATH + fileName));
         zip.write(mainYaml);
         //US798487 - Abstraction of complex types
         if (!ModelConverter.isAtomicComponent(component)){
             log.debug("Component {} is complex - generating abstract type for it..", component.getName());
-                       writeComponentInterface(component, zip, fileName, false);
+                             writeComponentInterface(component, zip, fileName, false);
         }
 
         if (dependencies == null) {
@@ -329,6 +320,40 @@ public class CsarUtils {
         return writeAllFilesToCsar(component, collectedComponentCsarDefinition.left().value(), zip, isInCertificationRequest);
     }
 
+    private Either<MainYamlWithDependencies, ResponseFormat> exportComponent(Component component) {
+        return toscaExportUtils.exportComponent(component).right().map(toscaError -> {
+            log.debug("exportComponent failed", toscaError);
+            return componentsUtils.getResponseFormat(componentsUtils.convertFromToscaError(toscaError));
+        }).left().map(MainYamlWithDependencies::make);
+    }
+
+    private Either<MainYamlWithDependencies, ResponseFormat> getArtifactFromCassandra(ArtifactDefinition artifactDef) {
+        return getFromCassandra(artifactDef.getEsId()).right().map(as -> {
+            log.debug(ARTIFACT_NAME_UNIQUE_ID, artifactDef.getArtifactName(), artifactDef.getUniqueId());
+            return componentsUtils.getResponseFormat(as);
+        }).left().map(MainYamlWithDependencies::make);
+    }
+
+    private static class MainYamlWithDependencies {
+
+        private final byte[] mainYaml;
+        private final Optional<List<Triple<String, String, Component>>> dependencies;
+
+        private MainYamlWithDependencies(byte[] mainYaml,
+            Optional<List<Triple<String, String, Component>>> dependencies) {
+            this.mainYaml = mainYaml;
+            this.dependencies = dependencies;
+        }
+
+        static public MainYamlWithDependencies make(byte[] mainYaml) {
+            return new MainYamlWithDependencies(mainYaml, Optional.empty());
+        }
+
+        static public MainYamlWithDependencies make(ToscaRepresentation tr) {
+            return new MainYamlWithDependencies(tr.getMainYaml().getBytes(), Optional.ofNullable(tr.getDependencies()));
+        }
+    }
+
     /**
      * Create a list of all derived nodes found on the package
      *