From 0ff3fcba8b7d645e497a7300776bde86590ec70c Mon Sep 17 00:00:00 2001 From: Francis Toth Date: Tue, 5 May 2020 12:44:59 -0400 Subject: Refactor tosca representation retrieval in CsarUtils::populateZip This commit aims to simplify CsarUtils::populateZip by extracting how a tosca representation is fetched to another function. this enables to reduce the overall cyclomatic complexity of the function. Some additional work still needs to be done especially regarding the ToscaRepresentation and MainYamlWithDependencies classes (These happen to be really similar). This will be done in another change in the near future. Signed-off-by: Francis Toth Change-Id: I93600d0752565de523cf2285b1fa2ba43a96334f Issue-ID: SDC-2812 --- .../java/org/openecomp/sdc/be/tosca/CsarUtils.java | 86 ++++++++++++++-------- 1 file changed, 56 insertions(+), 30 deletions(-) (limited to 'catalog-be') 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 cba6b4f308..8a8fff797f 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 @@ -241,47 +241,30 @@ public class CsarUtils { .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 result = null; - if (getFromCS || !(lifecycleState == LifecycleStateEnum.NOT_CERTIFIED_CHECKIN || lifecycleState == LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT)) { - result = getArtifactFromCassandra(artifactDef); - } else { - result = exportComponent(component); - } + Either toscaRepresentation = + fetchToscaRepresentation(component, getFromCS, artifactDef); + // This should not be done but in order to keep the refactoring small enough we stop here. // TODO: Refactor the rest of this function byte[] mainYaml; - List> 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); + List> dependencies; + if(toscaRepresentation.isLeft()) { + mainYaml = toscaRepresentation.left().value().mainYaml; + dependencies = toscaRepresentation.left().value().dependencies.orElse(null); } else { - return Either.right(result.right().value()); + return Either.right(toscaRepresentation.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); } - if (dependencies == null) { - Either dependenciesRes = toscaExportUtils.getDependencies(component); - if (dependenciesRes.isRight()) { - log.debug("Failed to retrieve dependencies for component {}, error {}", component.getUniqueId(), - dependenciesRes.right().value()); - ActionStatus convertFromToscaError = componentsUtils.convertFromToscaError(dependenciesRes.right().value()); - ResponseFormat responseFormat = componentsUtils.getResponseFormat(convertFromToscaError); - return Either.right(responseFormat); - } - dependencies = dependenciesRes.left().value().getDependencies(); - } - //UID Map> innerComponentsCache = new HashMap<>(); @@ -322,20 +305,59 @@ public class CsarUtils { return writeAllFilesToCsar(component, collectedComponentCsarDefinition.left().value(), zip, isInCertificationRequest); } - private Either exportComponent(Component component) { + private Either fetchToscaRepresentation( + Component component, + boolean getFromCS, + ArtifactDefinition artifactDef + ) { + LifecycleStateEnum lifecycleState = component.getLifecycleState(); + + boolean shouldBeFetchedFromCassandra = getFromCS || + !(lifecycleState == LifecycleStateEnum.NOT_CERTIFIED_CHECKIN || + lifecycleState == LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); + + Either toscaRepresentation = + shouldBeFetchedFromCassandra ? + fetchToscaRepresentation(artifactDef) : + generateToscaRepresentation(component); + + return toscaRepresentation.left().bind(iff( + myd -> !myd.dependencies.isPresent(), + myd -> fetchToscaTemplateDependencies(myd.mainYaml, component) + )); + } + + private static F> iff(Predicate p, Function> ifTrue) { + return l -> p.test(l) ? ifTrue.apply(l) : Either.left(l); + } + + private Either fetchToscaTemplateDependencies( + byte[] mainYml, + Component component + ) { + return toscaExportUtils.getDependencies(component).right().map(toscaError -> { + 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)); + } + + private Either generateToscaRepresentation(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 getArtifactFromCassandra(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; @@ -347,13 +369,17 @@ public class CsarUtils { this.dependencies = dependencies; } - static public MainYamlWithDependencies make(byte[] mainYaml) { + public static MainYamlWithDependencies make(byte[] mainYaml) { return new MainYamlWithDependencies(mainYaml, Optional.empty()); } - static public MainYamlWithDependencies make(ToscaRepresentation tr) { + 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())); + } } /** -- cgit 1.2.3-korg