From 1de7b75829b3bf0c06ee7a8b3006b0edfb239420 Mon Sep 17 00:00:00 2001 From: Francis Toth Date: Mon, 4 May 2020 18:42:58 -0400 Subject: Extract artifact component belonging logic This commit extracts checkArtifactInComponent and checkArtifactInResourceInstance from ArtifactsBusinessLogic. Tests have been added along with a some utility functions designed to generate randomized test data. The former tests were deleted as they were not doing any assertions. Signed-off-by: Francis Toth Change-Id: I1b27c55d064cf4e9a40cd7b152e7ea6032a09276 Issue-ID: SDC-2961 --- .../be/components/impl/ArtifactsBusinessLogic.java | 107 +-------------------- .../be/components/impl/utils/ComponentUtils.java | 99 +++++++++++++++++++ 2 files changed, 102 insertions(+), 104 deletions(-) create mode 100644 catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/utils/ComponentUtils.java (limited to 'catalog-be/src/main') 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 17f8bdef48..1db7d447f6 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 @@ -69,6 +69,7 @@ import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentEx import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException; import org.openecomp.sdc.be.components.impl.exceptions.ComponentException; import org.openecomp.sdc.be.components.utils.ArtifactUtils; +import org.openecomp.sdc.be.components.impl.utils.ComponentUtils; import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic; import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction; import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction.LifecycleChanceActionEnum; @@ -855,10 +856,10 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic { switch (componentType) { case RESOURCE: case SERVICE: - found = checkArtifactInComponent(component, artifactId); + found = ComponentUtils.checkArtifactInComponent(component, artifactId); break; case RESOURCE_INSTANCE: - found = checkArtifactInResourceInstance(component, componentId, artifactId); + found = ComponentUtils.checkArtifactInResourceInstance(component, componentId, artifactId); break; default: found = false; @@ -2902,108 +2903,6 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic { return downloadArtifact(artifactDefinition); } - private boolean checkArtifactInComponent(Component component, String artifactId) { - boolean found = false; - Map artifactsS = component.getArtifacts(); - if (artifactsS != null) { - for (Map.Entry entry : artifactsS.entrySet()) { - if (entry.getValue().getUniqueId().equals(artifactId)) { - found = true; - break; - } - } - } - Map deploymentArtifactsS = component.getDeploymentArtifacts(); - if (!found && deploymentArtifactsS != null) { - for (Map.Entry entry : deploymentArtifactsS.entrySet()) { - if (entry.getValue().getUniqueId().equals(artifactId)) { - found = true; - break; - } - } - } - Map toscaArtifactsS = component.getToscaArtifacts(); - if (!found && toscaArtifactsS != null) { - for (Map.Entry entry : toscaArtifactsS.entrySet()) { - if (entry.getValue().getUniqueId().equals(artifactId)) { - found = true; - break; - } - } - } - - Map interfaces = component.getInterfaces(); - if (!found && interfaces != null) { - for (Map.Entry entry : interfaces.entrySet()) { - Map operations = entry.getValue().getOperationsMap(); - for (Map.Entry entryOp : operations.entrySet()) { - if (entryOp.getValue().getImplementation() != null && entryOp.getValue() - .getImplementation() - .getUniqueId() - .equals(artifactId)) { - found = true; - break; - } - } - } - } - switch (component.getComponentType()) { - case RESOURCE: - break; - case SERVICE: - Map apiArtifacts = ((Service) component).getServiceApiArtifacts(); - if (!found && apiArtifacts != null) { - for (Map.Entry entry : apiArtifacts.entrySet()) { - if (entry.getValue().getUniqueId().equals(artifactId)) { - found = true; - break; - } - } - } - break; - default: - - } - - return found; - } - - private boolean checkArtifactInResourceInstance(Component component, String resourceInstanceId, String artifactId) { - - boolean found = false; - List resourceInstances = component.getComponentInstances(); - ComponentInstance resourceInstance = null; - for (ComponentInstance ri : resourceInstances) { - if (ri.getUniqueId().equals(resourceInstanceId)) { - resourceInstance = ri; - break; - } - } - if (resourceInstance != null) { - Map artifacts = resourceInstance.getDeploymentArtifacts(); - if (artifacts != null) { - for (Map.Entry entry : artifacts.entrySet()) { - if (entry.getValue().getUniqueId().equals(artifactId)) { - found = true; - break; - } - } - } - if (!found) { - artifacts = resourceInstance.getArtifacts(); - if (artifacts != null) { - for (Map.Entry entry : artifacts.entrySet()) { - if (entry.getValue().getUniqueId().equals(artifactId)) { - found = true; - break; - } - } - } - } - } - return found; - } - private Component validateComponentExists(String componentId, AuditingActionEnum auditingAction, User user, String artifactId, ComponentTypeEnum componentType, String containerComponentType) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/utils/ComponentUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/utils/ComponentUtils.java new file mode 100644 index 0000000000..02176d902b --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/utils/ComponentUtils.java @@ -0,0 +1,99 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.components.impl.utils; + +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.function.Predicate; +import java.util.stream.Stream; +import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition; +import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.model.ArtifactDefinition; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.ComponentInstance; +import org.openecomp.sdc.be.model.Service; + +public final class ComponentUtils { + + private ComponentUtils() { + } + + public static boolean checkArtifactInComponent( + Component component, String artifactId + ) { + Predicate hasSameArtifactId = + ad -> ad != null && ad.getUniqueId().equals(artifactId); + + return exists(component.getArtifacts(), hasSameArtifactId) || + exists(component.getDeploymentArtifacts(), hasSameArtifactId) || + exists(component.getToscaArtifacts(), hasSameArtifactId) || + hasOperationImplementationWithUniqueId(component, artifactId) || + isServiceAndHasApiArtifactWithUniqueId(component, hasSameArtifactId); + } + + private static boolean isServiceAndHasApiArtifactWithUniqueId(Component component, + Predicate hasSameArtifactId) { + return component.getComponentType() == ComponentTypeEnum.SERVICE && exists( + ((Service) component).getServiceApiArtifacts(), hasSameArtifactId); + } + + private static boolean hasOperationImplementationWithUniqueId(Component component, String artifactId) { + return findFirst(valueStream(component.getInterfaces()) + .flatMap(v -> valueStream(v.getOperationsMap())) + .map(OperationDataDefinition::getImplementation), + e -> e != null && e.getUniqueId().equals(artifactId) + ).isPresent(); + } + + public static boolean checkArtifactInResourceInstance( + Component component, String resourceInstanceId, String artifactId + ) { + Predicate hasSameResourceId = + ri -> ri != null && ri.getUniqueId().equals(resourceInstanceId); + + Predicate hasSameArtifactId = + ad -> ad != null && ad.getUniqueId().equals(artifactId); + + return findFirst(component.getComponentInstances(), hasSameResourceId).map(ri -> + exists(ri.getDeploymentArtifacts(), hasSameArtifactId) || + exists(ri.getArtifacts(), hasSameArtifactId) + ).isPresent(); + } + + private static Optional findFirst(List ovs, Predicate p) { + return Optional.ofNullable(ovs).flatMap(vs -> findFirst(vs.stream(), p)); + } + + private static boolean exists(Map okvs, Predicate p) { + return Optional.ofNullable(okvs) + .flatMap(kvs -> findFirst(kvs.values().stream(), p)) + .isPresent(); + } + + private static Optional findFirst(Stream vs, Predicate p) { + return Optional.ofNullable(vs).flatMap(ms -> ms.filter(p).findFirst()); + } + + private static Stream valueStream(Map okvs) { + return Optional.ofNullable(okvs).map(kvs -> kvs.values().stream()).orElse(Stream.empty()); + } +} -- cgit 1.2.3-korg