diff options
author | Chris André <chris.andre@yoppworks.com> | 2020-05-15 08:29:29 -0400 |
---|---|---|
committer | Ofir Sonsino <ofir.sonsino@intl.att.com> | 2020-05-27 06:38:04 +0000 |
commit | 66a5398f95f8c60a41ffa81f46ccee95beb34822 (patch) | |
tree | a9691d8c6edef306a6985489dfba067f998c4f13 /catalog-be/src | |
parent | 1803aa8530b53d9f22dc5815155dd3abdfd99b5a (diff) |
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 <chris.andre@yoppworks.com>
Change-Id: I693bb53059b056235b567ac1f9f752a33512505a
Diffstat (limited to 'catalog-be/src')
-rw-r--r-- | catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentBusinessLogic.java | 79 |
1 files changed, 58 insertions, 21 deletions
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<ArtifactDefinition, Operation> 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<ArtifactDefinition, Operation> 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<ArtifactDefinition, Operation> saveToscaArtifactAndPopulateToscaArtifactsWithResult( + Component component, + final ComponentsUtils componentsUtils, + final ArtifactTypeEnum artifactEnum, + final BiFunction<Component, ArtifactDefinition, Either<ArtifactDefinition, Operation>> saveToscaArtifactPayloadFunction) { + + ArtifactDefinition artifactDefinition = getToscaArtifactByTypeOrThrowException(component, artifactEnum, + componentsUtils); + + Either<ArtifactDefinition, Operation> result = + saveToscaArtifactPayloadFunction.apply(component, artifactDefinition); + + if (result.isLeft()) { + ArtifactDefinition def = result.left().value(); + component.getToscaArtifacts().put(def.getArtifactLabel(), def); + } + + return result; + } + + private static Optional<ArtifactDefinition> getToscaArtifactByType( + final Map<String, ArtifactDefinition> 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(); } |