From 66a5398f95f8c60a41ffa81f46ccee95beb34822 Mon Sep 17 00:00:00 2001 From: Chris André Date: Fri, 15 May 2020 08:29:29 -0400 Subject: Work on unchecked Optional access - extract code to static methods - throw an explicit exception now if element not found Issue-ID: SDC-2987 Signed-off-by: Chris Andre Change-Id: I693bb53059b056235b567ac1f9f752a33512505a --- .../be/components/impl/ComponentBusinessLogic.java | 79 ++++++++++++++++------ 1 file changed, 58 insertions(+), 21 deletions(-) (limited to 'catalog-be/src/main/java/org') diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentBusinessLogic.java index 0d25bb02c4..4ac3815a2b 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentBusinessLogic.java @@ -22,10 +22,11 @@ package org.openecomp.sdc.be.components.impl; -import com.sun.org.apache.xpath.internal.operations.Bool; import fj.data.Either; +import io.vavr.Function3; +import java.util.Optional; +import java.util.function.BiFunction; import java.util.function.BooleanSupplier; -import java.util.function.Supplier; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; @@ -55,6 +56,7 @@ import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum; import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition; import org.openecomp.sdc.be.facade.operations.CatalogOperation; +import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.model.ArtifactDefinition; import org.openecomp.sdc.be.model.CapReqDef; import org.openecomp.sdc.be.model.Component; @@ -449,29 +451,64 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic { component = toscaElement.left().value(); } - Either generateToscaRes = null; - if (component.getToscaArtifacts() != null && !component.getToscaArtifacts().isEmpty()) { - ArtifactDefinition toscaArtifact = component.getToscaArtifacts().values().stream() - .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.TOSCA_TEMPLATE.getType())) - .findAny().get(); - generateToscaRes = saveToscaArtifactPayload(toscaArtifact, component, user, isInCertificationRequest, shouldLock, inTransaction, fetchTemplatesFromDB); - toscaArtifact = generateToscaRes.left().value(); - component.getToscaArtifacts().put(toscaArtifact.getArtifactLabel(), toscaArtifact); - if(!isAbstractResource(component)){ - toscaArtifact = component.getToscaArtifacts().values().stream() - .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.TOSCA_CSAR.getType())) - .findAny().get(); - generateToscaRes = saveToscaArtifactPayload(toscaArtifact, component, user, isInCertificationRequest, shouldLock, inTransaction, true); - if (generateToscaRes.isRight()) { - return generateToscaRes; - } - toscaArtifact = generateToscaRes.left().value(); - component.getToscaArtifacts().put(toscaArtifact.getArtifactLabel(), toscaArtifact); - } + Either generateToscaRes = + saveToscaArtifactAndPopulateToscaArtifactsWithResult(component, componentsUtils, + ArtifactTypeEnum.TOSCA_TEMPLATE, + (comp, toscaArtifact) -> + saveToscaArtifactPayload(toscaArtifact, comp, user, isInCertificationRequest, shouldLock, + inTransaction, fetchTemplatesFromDB)); + + if (!isAbstractResource(component)) { + generateToscaRes = saveToscaArtifactAndPopulateToscaArtifactsWithResult(component, componentsUtils, + ArtifactTypeEnum.TOSCA_CSAR, + (comp, toscaArtifactArg) -> saveToscaArtifactPayload(toscaArtifactArg, comp, user, + isInCertificationRequest, shouldLock, inTransaction, true)); } return generateToscaRes; } + private static Either saveToscaArtifactAndPopulateToscaArtifactsWithResult( + Component component, + final ComponentsUtils componentsUtils, + final ArtifactTypeEnum artifactEnum, + final BiFunction> saveToscaArtifactPayloadFunction) { + + ArtifactDefinition artifactDefinition = getToscaArtifactByTypeOrThrowException(component, artifactEnum, + componentsUtils); + + Either result = + saveToscaArtifactPayloadFunction.apply(component, artifactDefinition); + + if (result.isLeft()) { + ArtifactDefinition def = result.left().value(); + component.getToscaArtifacts().put(def.getArtifactLabel(), def); + } + + return result; + } + + private static Optional getToscaArtifactByType( + final Map toscaArtifacts, + final ArtifactTypeEnum typeEnum) { + return toscaArtifacts.values().stream() + .filter(p -> p.getArtifactType().equals(typeEnum.getType())) + .findAny(); + } + + private static ArtifactDefinition getToscaArtifactByTypeOrThrowException( + final Component component, + final ArtifactTypeEnum typeEnum, + final ComponentsUtils componentsUtils) { + + return Optional.ofNullable(component.getToscaArtifacts()) + .flatMap(toscaArtifacts -> getToscaArtifactByType(toscaArtifacts, typeEnum)) + .orElseThrow(() -> { + log.debug("Impossible to find a ToscaArtifact with type '{}' for {}", typeEnum.getType(), component); + return new ByResponseFormatComponentException( + componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_NOT_FOUND, typeEnum.name())); + }); + } + private boolean isAbstractResource(Component component) { return component.getComponentType() == ComponentTypeEnum.RESOURCE && ((Resource)component).isAbstract(); } -- cgit 1.2.3-korg