diff options
author | Francis Toth <francis.toth@yoppworks.com> | 2020-05-04 18:42:58 -0400 |
---|---|---|
committer | Ofir Sonsino <ofir.sonsino@intl.att.com> | 2020-05-28 07:48:14 +0000 |
commit | 1de7b75829b3bf0c06ee7a8b3006b0edfb239420 (patch) | |
tree | 7c45b2830a80d92de1b5d2f1d65a0b84984f52d4 /catalog-be/src/main | |
parent | 9f7430db214092bba954fa07fd3e72e333116cfa (diff) |
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 <francis.toth@yoppworks.com>
Change-Id: I1b27c55d064cf4e9a40cd7b152e7ea6032a09276
Issue-ID: SDC-2961
Diffstat (limited to 'catalog-be/src/main')
2 files changed, 102 insertions, 104 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 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<String, ArtifactDefinition> artifactsS = component.getArtifacts(); - if (artifactsS != null) { - for (Map.Entry<String, ArtifactDefinition> entry : artifactsS.entrySet()) { - if (entry.getValue().getUniqueId().equals(artifactId)) { - found = true; - break; - } - } - } - Map<String, ArtifactDefinition> deploymentArtifactsS = component.getDeploymentArtifacts(); - if (!found && deploymentArtifactsS != null) { - for (Map.Entry<String, ArtifactDefinition> entry : deploymentArtifactsS.entrySet()) { - if (entry.getValue().getUniqueId().equals(artifactId)) { - found = true; - break; - } - } - } - Map<String, ArtifactDefinition> toscaArtifactsS = component.getToscaArtifacts(); - if (!found && toscaArtifactsS != null) { - for (Map.Entry<String, ArtifactDefinition> entry : toscaArtifactsS.entrySet()) { - if (entry.getValue().getUniqueId().equals(artifactId)) { - found = true; - break; - } - } - } - - Map<String, InterfaceDefinition> interfaces = component.getInterfaces(); - if (!found && interfaces != null) { - for (Map.Entry<String, InterfaceDefinition> entry : interfaces.entrySet()) { - Map<String, Operation> operations = entry.getValue().getOperationsMap(); - for (Map.Entry<String, Operation> 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<String, ArtifactDefinition> apiArtifacts = ((Service) component).getServiceApiArtifacts(); - if (!found && apiArtifacts != null) { - for (Map.Entry<String, ArtifactDefinition> 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<ComponentInstance> resourceInstances = component.getComponentInstances(); - ComponentInstance resourceInstance = null; - for (ComponentInstance ri : resourceInstances) { - if (ri.getUniqueId().equals(resourceInstanceId)) { - resourceInstance = ri; - break; - } - } - if (resourceInstance != null) { - Map<String, ArtifactDefinition> artifacts = resourceInstance.getDeploymentArtifacts(); - if (artifacts != null) { - for (Map.Entry<String, ArtifactDefinition> entry : artifacts.entrySet()) { - if (entry.getValue().getUniqueId().equals(artifactId)) { - found = true; - break; - } - } - } - if (!found) { - artifacts = resourceInstance.getArtifacts(); - if (artifacts != null) { - for (Map.Entry<String, ArtifactDefinition> 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<ArtifactDefinition> 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<ArtifactDefinition> 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<ComponentInstance> hasSameResourceId = + ri -> ri != null && ri.getUniqueId().equals(resourceInstanceId); + + Predicate<ArtifactDefinition> 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 <V> Optional<V> findFirst(List<V> ovs, Predicate<V> p) { + return Optional.ofNullable(ovs).flatMap(vs -> findFirst(vs.stream(), p)); + } + + private static <K, V> boolean exists(Map<K, V> okvs, Predicate<V> p) { + return Optional.ofNullable(okvs) + .flatMap(kvs -> findFirst(kvs.values().stream(), p)) + .isPresent(); + } + + private static <V> Optional<V> findFirst(Stream<V> vs, Predicate<V> p) { + return Optional.ofNullable(vs).flatMap(ms -> ms.filter(p).findFirst()); + } + + private static <K, V> Stream<V> valueStream(Map<K, V> okvs) { + return Optional.ofNullable(okvs).map(kvs -> kvs.values().stream()).orElse(Stream.empty()); + } +} |