From df10d0be06468d55818fa734321c14827fef61bb Mon Sep 17 00:00:00 2001 From: Francis Toth Date: Fri, 26 Jun 2020 19:53:26 -0400 Subject: Clean CsarUtils::MainYamlWithDependencies Signed-off-by: Francis Toth Change-Id: I593dd7bb6a20445899a14da793e8c550e1e14e09 Issue-ID: SDC-2812 --- .../be/components/impl/ArtifactsBusinessLogic.java | 2 +- .../java/org/openecomp/sdc/be/tosca/CsarUtils.java | 58 ++++++---------------- .../openecomp/sdc/be/tosca/ToscaExportHandler.java | 16 +----- .../sdc/be/tosca/ToscaRepresentation.java | 26 ++++++---- .../java/org/openecomp/sdc/be/tosca/ZipWriter.java | 6 ++- 5 files changed, 40 insertions(+), 68 deletions(-) (limited to 'catalog-be/src/main') diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java index d8f85617c0..cac36cbe0e 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java @@ -396,7 +396,7 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic { .exportComponent(parent) .left().map(toscaRepresentation -> { log.debug("Tosca yaml exported for component {} ", parent.getUniqueId()); - return toscaRepresentation.getMainYaml().getBytes(); + return toscaRepresentation.getMainYaml(); }).right().map(toscaError -> { log.debug("Failed export tosca yaml for component {} error {}", parent.getUniqueId(), toscaError); return new ByActionStatusComponentException(componentsUtils.convertFromToscaError(toscaError)); 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 291d2bb508..241148b531 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 @@ -253,7 +253,7 @@ public class CsarUtils { .getToscaArtifacts() .get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE); - Either toscaRepresentation = + Either toscaRepresentation = fetchToscaRepresentation(component, getFromCS, artifactDef); // This should not be done but in order to keep the refactoring small enough we stop here. @@ -261,8 +261,8 @@ public class CsarUtils { byte[] mainYaml; List> dependencies; if(toscaRepresentation.isLeft()) { - mainYaml = toscaRepresentation.left().value().mainYaml; - dependencies = toscaRepresentation.left().value().dependencies.orElse(null); + mainYaml = toscaRepresentation.left().value().getMainYaml(); + dependencies = toscaRepresentation.left().value().getDependencies().getOrElse(new ArrayList<>()); } else { return Either.right(toscaRepresentation.right().value()); } @@ -315,7 +315,7 @@ public class CsarUtils { return writeAllFilesToCsar(component, collectedComponentCsarDefinition.left().value(), zip, isInCertificationRequest); } - private Either fetchToscaRepresentation( + private Either fetchToscaRepresentation( Component component, boolean getFromCS, ArtifactDefinition artifactDef @@ -326,14 +326,14 @@ public class CsarUtils { !(lifecycleState == LifecycleStateEnum.NOT_CERTIFIED_CHECKIN || lifecycleState == LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); - Either toscaRepresentation = + Either toscaRepresentation = shouldBeFetchedFromCassandra ? fetchToscaRepresentation(artifactDef) : generateToscaRepresentation(component); return toscaRepresentation.left().bind(iff( - myd -> !myd.dependencies.isPresent(), - myd -> fetchToscaTemplateDependencies(myd.mainYaml, component) + myd -> !myd.getDependencies().isDefined(), + myd -> fetchToscaTemplateDependencies(myd.getMainYaml(), component) )); } @@ -341,7 +341,7 @@ public class CsarUtils { return l -> p.test(l) ? ifTrue.apply(l) : Either.left(l); } - private Either fetchToscaTemplateDependencies( + private Either fetchToscaTemplateDependencies( byte[] mainYml, Component component ) { @@ -349,47 +349,21 @@ public class CsarUtils { log.debug("Failed to retrieve dependencies for component {}, error {}", component.getUniqueId(), toscaError); return componentsUtils.getResponseFormat(componentsUtils.convertFromToscaError(toscaError)); - }).left().map(tt -> MainYamlWithDependencies.make(mainYml, tt)); + }).left().map(tt -> ToscaRepresentation.make(mainYml, tt)); } - private Either generateToscaRepresentation(Component component) { + private Either generateToscaRepresentation(Component component) { return toscaExportUtils.exportComponent(component).right().map(toscaError -> { - log.debug("exportComponent failed", toscaError); + log.debug("exportComponent failed {}", toscaError); return componentsUtils.getResponseFormat(componentsUtils.convertFromToscaError(toscaError)); - }).left().map(MainYamlWithDependencies::make); + }); } - private Either fetchToscaRepresentation(ArtifactDefinition artifactDef) { + private Either fetchToscaRepresentation(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); - } - - // TODO: Refactor the ToscaRepresentation class in order to remove the following one - // This will be done in a separate change - private static class MainYamlWithDependencies { - - private final byte[] mainYaml; - private final Optional>> dependencies; - - private MainYamlWithDependencies(byte[] mainYaml, - Optional>> dependencies) { - this.mainYaml = mainYaml; - this.dependencies = dependencies; - } - - public static MainYamlWithDependencies make(byte[] mainYaml) { - return new MainYamlWithDependencies(mainYaml, Optional.empty()); - } - - public static MainYamlWithDependencies make(ToscaRepresentation tr) { - return new MainYamlWithDependencies(tr.getMainYaml().getBytes(), Optional.ofNullable(tr.getDependencies())); - } - - public static MainYamlWithDependencies make(byte[] mainYaml, ToscaTemplate tt) { - return new MainYamlWithDependencies(mainYaml, Optional.ofNullable(tt.getDependencies())); - } + }).left().map(ToscaRepresentation::make); } /** @@ -686,7 +660,7 @@ public class CsarUtils { boolean isAssociatedComponent, ZipWriter zw ) { - Either yml = toscaExportUtils + Either yml = toscaExportUtils .exportComponentInterface(component, isAssociatedComponent) .left().map(ToscaRepresentation::getMainYaml); @@ -705,7 +679,7 @@ public class CsarUtils { if (cassandraId == null || cassandraId.isEmpty()) { return toscaExportUtils.exportComponent(childComponent) .right().map(toscaErrorToActionStatus(childComponent)) - .left().map(toscaRepresentation -> toscaRepresentation.getMainYaml().getBytes()); + .left().map(ToscaRepresentation::getMainYaml); } else { return getFromCassandra(cassandraId); } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java index a492f01953..aa9cd8ce49 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java @@ -170,15 +170,7 @@ public class ToscaExportHandler { public ToscaExportHandler(){} public Either exportComponent(Component component) { - - Either toscaTemplateRes = convertToToscaTemplate(component); - if (toscaTemplateRes.isRight()) { - return Either.right(toscaTemplateRes.right().value()); - } - - ToscaTemplate toscaTemplate = toscaTemplateRes.left().value(); - ToscaRepresentation toscaRepresentation = this.createToscaRepresentation(toscaTemplate); - return Either.left(toscaRepresentation); + return convertToToscaTemplate(component).left().map(this::createToscaRepresentation); } public Either exportComponentInterface(final Component component, @@ -227,11 +219,7 @@ public class ToscaExportHandler { sb.append(yamlAsString); sb.append(ConfigurationManager.getConfigurationManager().getConfiguration().getHeatEnvArtifactFooter()); - ToscaRepresentation toscaRepresentation = new ToscaRepresentation(); - toscaRepresentation.setMainYaml(sb.toString()); - toscaRepresentation.setDependencies(toscaTemplate.getDependencies()); - - return toscaRepresentation; + return ToscaRepresentation.make(sb.toString().getBytes(), toscaTemplate); } public Either getDependencies(Component component) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaRepresentation.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaRepresentation.java index 49fd91cfed..1ca95576a8 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaRepresentation.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaRepresentation.java @@ -20,30 +20,36 @@ package org.openecomp.sdc.be.tosca; +import io.vavr.control.Option; +import lombok.Getter; import org.apache.commons.lang3.tuple.Triple; import org.openecomp.sdc.be.model.Component; import java.util.List; +import org.openecomp.sdc.be.tosca.model.ToscaTemplate; public class ToscaRepresentation { - private String mainYaml; - private List> dependencies; + @Getter + private final byte[] mainYaml; - public String getMainYaml() { - return mainYaml; - } + @Getter + private final Option>> dependencies; - public void setMainYaml(String mainYaml) { + private ToscaRepresentation(byte[] mainYaml, Option>> dependencies) { this.mainYaml = mainYaml; + this.dependencies = dependencies; } - public List> getDependencies() { - return dependencies; + public static ToscaRepresentation make(byte[] mainYaml) { + return new ToscaRepresentation(mainYaml, Option.none()); } - public void setDependencies(List> dependancies) { - this.dependencies = dependancies; + public static ToscaRepresentation make(byte[] mainYaml, List> dependencies) { + return new ToscaRepresentation(mainYaml, Option.of(dependencies)); } + public static ToscaRepresentation make(byte[] mainYaml, ToscaTemplate tt) { + return new ToscaRepresentation(mainYaml, Option.of(tt.getDependencies())); + } } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ZipWriter.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ZipWriter.java index 4740fb2042..916895fff4 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ZipWriter.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ZipWriter.java @@ -54,10 +54,14 @@ public interface ZipWriter { * * @param entryName The entry's name to use in the zip file */ - default Function> write(String entryName) { + default Function> writeString(String entryName) { return payload -> write(entryName, payload.getBytes()); } + default Function> write(String entryName) { + return payload -> write(entryName, payload); + } + /** * Builds a ZipWriter that outputs the data on a {@link java.util.zip.ZipOutputStream} * @param zos the target {@link java.util.zip.ZipOutputStream} -- cgit 1.2.3-korg