summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org
diff options
context:
space:
mode:
authorFrancis Toth <francis.toth@yoppworks.com>2020-05-08 16:31:04 -0400
committerOfir Sonsino <ofir.sonsino@intl.att.com>2020-05-21 06:34:12 +0000
commit0a65c2fe97240e892c8c65af2c9e6048aa350857 (patch)
treeaa9d1f617c670b7c1b19bfcc0846bc59e0f6e3ef /catalog-be/src/main/java/org
parentb8cea3e1099d47a61c95709d26d1742446d8e2c5 (diff)
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 <francis.toth@yoppworks.com> Change-Id: I2503f5e2a697f793e4dcf32d7793f83c4a25ff46 Issue-ID: SDC-2812
Diffstat (limited to 'catalog-be/src/main/java/org')
-rw-r--r--catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java100
1 files changed, 49 insertions, 51 deletions
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<Component> 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<Component, ComponentInstance> getRelatedComponentComponentInstance(Component component, String resourceInstanceName) {
-
- ImmutablePair<Component, ComponentInstance> relatedComponentComponentInstancePair = null;
+ private ImmutablePair<Component, ComponentInstance> 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<Component, ComponentInstance> getRelatedComponentComponentInstance(ComponentTypeEnum componentType,
- String componentUuid, String resourceInstanceName) {
- ComponentInstance componentInstance;
- ImmutablePair<Component, ComponentInstance> relatedComponentComponentInstancePair;
+ private ImmutablePair<Component, ComponentInstance> 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<Throwable> componentNotFound(ComponentTypeEnum componentType, String componentUuid) {
+ return () -> {
+ log.debug(FAILED_FETCH_COMPONENT, componentType.getValue(), componentUuid);
+ return new ByActionStatusComponentException(ActionStatus.COMPONENT_NOT_FOUND, componentUuid);
+ };
+ }
+
+ private Supplier<Throwable> 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<ComponentInstance> findFirstMatching(Component component, Predicate<ComponentInstance> filter) {
+ return Option.ofOptional(component.getComponentInstances()
+ .stream()
+ .filter(filter)
+ .findFirst());
}
private byte[] downloadArtifact(Map<String, ArtifactDefinition> artifacts, String artifactUUID, String componentName) {