From 0a65c2fe97240e892c8c65af2c9e6048aa350857 Mon Sep 17 00:00:00 2001 From: Francis Toth Date: Fri, 8 May 2020 16:31:04 -0400 Subject: Refactor ArtifactsBusinessLogic::getRelatedComponentInstance This commit aims to refactor the ArtifactsBusinessLogic::getRelatedComponentInstance, and getRelatedComponentComponentInstance. It improves how Either is used, fixes a potential NullPointerException, reduces some duplication along with cyclomatic complexity. Signed-off-by: Francis Toth Change-Id: I2503f5e2a697f793e4dcf32d7793f83c4a25ff46 Issue-ID: SDC-2812 --- .../be/components/impl/ArtifactsBusinessLogic.java | 100 ++++++++++----------- 1 file changed, 49 insertions(+), 51 deletions(-) (limited to 'catalog-be') 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 ea7d947287..c56517c0af 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 @@ -29,6 +29,7 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import fj.F; import fj.data.Either; +import io.vavr.control.Option; import java.io.ByteArrayInputStream; import java.io.IOException; import java.math.BigDecimal; @@ -4645,65 +4646,62 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic { return handleArtifactsResult; } - private ComponentInstance getRelatedComponentInstance(ComponentTypeEnum componentType, String componentUuid, String resourceInstanceName) { - ComponentInstance componentInstance; + private ComponentInstance getRelatedComponentInstance( + ComponentTypeEnum componentType, String componentUuid, String resourceInstanceName + ) { String normalizedName = ValidationUtils.normalizeComponentInstanceName(resourceInstanceName); - Component component = getComponentByUuid(componentType, componentUuid); - componentInstance = (component == null) ? null : component.getComponentInstances() - .stream() - .filter(ci -> ValidationUtils.normalizeComponentInstanceName(ci.getName()) - .equals(normalizedName)) - .findFirst() - .orElse(null); - if (componentInstance == null) { - log.debug(COMPONENT_INSTANCE_NOT_FOUND, resourceInstanceName, component.getName()); - throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, resourceInstanceName, - RESOURCE_INSTANCE, component.getComponentType().getValue(), component.getName()); - } - return componentInstance; + Option oComponent = Option.of(getComponentByUuid(componentType, componentUuid)); + return oComponent + .toTry(componentNotFound(componentType, componentUuid)) + .flatMap(component -> findFirstMatching(component, + ci -> ValidationUtils.normalizeComponentInstanceName(ci.getName()).equals(normalizedName) + ).toTry(componentInstanceNotFound(componentType, resourceInstanceName, component)) + ).get(); } - private ImmutablePair getRelatedComponentComponentInstance(Component component, String resourceInstanceName) { - - ImmutablePair relatedComponentComponentInstancePair = null; + private ImmutablePair getRelatedComponentComponentInstance( + Component component, String resourceInstanceName + ) { String normalizedName = ValidationUtils.normalizeComponentInstanceName(resourceInstanceName); - ComponentInstance componentInstance = component.getComponentInstances() - .stream() - .filter(ci -> ValidationUtils.normalizeComponentInstanceName(ci.getName()) - .equals(normalizedName)) - .findFirst() - .orElse(null); - if (componentInstance == null) { - log.debug(COMPONENT_INSTANCE_NOT_FOUND, resourceInstanceName, component.getName()); - throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, resourceInstanceName, - RESOURCE_INSTANCE, component.getComponentType().getValue(), component.getName()); - } - else { - relatedComponentComponentInstancePair = new ImmutablePair<>(component, componentInstance); - } - return relatedComponentComponentInstancePair; + ComponentInstance componentInstance = findFirstMatching(component, + ci -> ValidationUtils.normalizeComponentInstanceName(ci.getName()).equals(normalizedName) + ).toTry(componentInstanceNotFound(component.getComponentType(), resourceInstanceName, component)).get(); + return new ImmutablePair<>(component, componentInstance); } - private ImmutablePair getRelatedComponentComponentInstance(ComponentTypeEnum componentType, - String componentUuid, String resourceInstanceName) { - ComponentInstance componentInstance; - ImmutablePair relatedComponentComponentInstancePair; + private ImmutablePair getRelatedComponentComponentInstance( + ComponentTypeEnum componentType, String componentUuid, String resourceInstanceName + ) { Component component = getLatestComponentByUuid(componentType, componentUuid); - componentInstance = component.getComponentInstances() - .stream() - .filter(ci -> ci.getNormalizedName().equals(resourceInstanceName)) - .findFirst() - .orElse(null); - if (componentInstance == null) { + ComponentInstance componentInstance = findFirstMatching(component, + ci -> ci.getNormalizedName().equals(resourceInstanceName) + ).toTry(componentInstanceNotFound(component.getComponentType(), resourceInstanceName, component)).get(); + return new ImmutablePair<>(component, componentInstance); + } + + private Supplier componentNotFound(ComponentTypeEnum componentType, String componentUuid) { + return () -> { + log.debug(FAILED_FETCH_COMPONENT, componentType.getValue(), componentUuid); + return new ByActionStatusComponentException(ActionStatus.COMPONENT_NOT_FOUND, componentUuid); + }; + } + + private Supplier componentInstanceNotFound( + ComponentTypeEnum componentType, String resourceInstanceName, Component component + ) { + return () -> { log.debug(COMPONENT_INSTANCE_NOT_FOUND, resourceInstanceName, component.getName()); - throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, - resourceInstanceName, RESOURCE_INSTANCE, component - .getComponentType().getValue(), component.getName()); - } - else { - relatedComponentComponentInstancePair = new ImmutablePair<>(component, componentInstance); - } - return relatedComponentComponentInstancePair; + return new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, + resourceInstanceName, + RESOURCE_INSTANCE, componentType.getValue(), component.getName()); + }; + } + + private static Option findFirstMatching(Component component, Predicate filter) { + return Option.ofOptional(component.getComponentInstances() + .stream() + .filter(filter) + .findFirst()); } private byte[] downloadArtifact(Map artifacts, String artifactUUID, String componentName) { -- cgit 1.2.3-korg