Refactor ArtifactsBusinessLogic::getRelatedComponentInstance 10/107410/3
authorFrancis Toth <francis.toth@yoppworks.com>
Fri, 8 May 2020 20:31:04 +0000 (16:31 -0400)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Thu, 21 May 2020 06:34:12 +0000 (06:34 +0000)
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

catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java

index ea7d947..c56517c 100644 (file)
@@ -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) {