Commit 44902b90 authored by Anastasios Poimenidis's avatar Anastasios Poimenidis
Browse files

fix: use new schema entity relationships Service.supportingResource and...

fix: use new schema entity relationships Service.supportingResource and ServiceRefOrValue.supportingResource
parent 163adba8
Loading
Loading
Loading
Loading
Loading
+38 −30
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.etsi.osl.hypo.api.tmf.common.api.constants.ApiConstants;
@@ -174,19 +175,19 @@ public class ServiceOrderAndServiceRepositories {
                        .filter(Objects::nonNull)
                        .toList();

        List<String> resourcesIdsList =
        Set<String> resourcesIdsSet =
                serviceOrderItem.getService().getSupportingResource().stream()
                        .map(BaseEntity::getTmfId)
                        .filter(Objects::nonNull)
                        .toList();
                        .collect(Collectors.toSet());

        List<ServiceRefOrValue> serviceRefOrValues =
                serviceOrderHelper.findServiceReferencesByIds(servicesIdsList);
        serviceCFS.setSupportingService(serviceRefOrValues);

        List<ResourceRef> resourceRefOrValueList =
                resourceHelper.getResourceRefListByIds(resourcesIdsList);
        serviceCFS.setSupportingResource(resourceRefOrValueList);
        Set<ResourceRef> resourceRefOrValueList =
                resourceHelper.getResourceRefSetByIds(resourcesIdsSet);
        serviceCFS.getSupportingResource().addAll(resourceRefOrValueList);

        List<RelatedPartyEntity> relatedPartyEntities = serviceOrder.getRelatedParty();
        List<String> relatedPartiesIDs =
@@ -539,9 +540,20 @@ public class ServiceOrderAndServiceRepositories {
    public void createServiceReferenceForService(Service service) {
        ServiceRefOrValue serviceReference =
                serviceMapperHelper.getServiceReferenceFromService(service);
        assignSupportingResource(service, serviceReference);
        serviceOrderHelper.persistServiceReference(serviceReference);
    }

    private void assignSupportingResource(Service service, ServiceRefOrValue serviceReference) {
        Set<ResourceRef> supportingResource = service.getSupportingResource();
        if (CollectionUtils.isNotEmpty(supportingResource)) {
            Set<String> resourceRefIds =
                    supportingResource.stream().map(BaseEntity::getTmfId).collect(Collectors.toSet());
            Set<ResourceRef> resourceRefs = resourceHelper.getResourceRefSetByIds(resourceRefIds);
            serviceReference.getSupportingResource().addAll(resourceRefs);
        }
    }

    @Transactional
    public Optional<ServiceRefOrValue> findServiceReferenceByID(String id) {
        return serviceOrderHelper.findServiceReferenceById(id);
@@ -638,6 +650,7 @@ public class ServiceOrderAndServiceRepositories {
                                    resourceHelper.createResourceForService(resourceDashboardData, service);
                            ResourceRef resourceReference =
                                    serviceMapperHelper.getResourceReferenceFromResource(createdResource);
                            resourceHelper.persistResourceRefs(List.of(resourceReference));

                            service.getSupportingResource().add(resourceReference);
                        },
@@ -1152,18 +1165,18 @@ public class ServiceOrderAndServiceRepositories {
    }

    private void setSupportingResource(ServiceRefOrValue service) {
        List<String> resourceReferenceIds =
                Objects.isNull(service.getSupportingResource())
                        ? Collections.emptyList()
                        : service.getSupportingResource().stream()
        Set<ResourceRef> supportingResource = service.getSupportingResource();
        Set<String> resourceReferenceIds =
                Objects.isNull(supportingResource)
                        ? Collections.emptySet()
                        : supportingResource.stream()
                                .map(BaseEntity::getTmfId)
                                .filter(Objects::nonNull)
                                .toList();
                                .collect(Collectors.toSet());

        if (CollectionUtils.isNotEmpty(resourceReferenceIds)) {
            List<ResourceRef> resourceRefSet =
                    resourceHelper.getResourceRefListByIds(resourceReferenceIds);
            service.setSupportingResource(resourceRefSet);
            Set<ResourceRef> resourceRefSet = resourceHelper.getResourceRefSetByIds(resourceReferenceIds);
            service.getSupportingResource().addAll(resourceRefSet);
        }
    }

@@ -1251,22 +1264,26 @@ public class ServiceOrderAndServiceRepositories {
        if (CollectionUtils.isNotEmpty(kubernetesItems)) {
            kubernetesItems.forEach(
                    serviceOrderItem -> {
                        ResourceRef resourceRef = new ResourceRef();
                        resourceRef.setType(ResourceType.KUBERNETES_RESOURCE.getName());
                        ResourceRef resourceRef = createResourceRef();
                        serviceOrderItem.getService().getSupportingResource().add(resourceRef);
                        resourceRefList.add(resourceRef);
                    });
        }

        persistResourceReferences(resourceRefList);
        return resourceRefList;
    }

    public void createResourceReferences(Service service) {
        ResourceRef resourceRef = createResourceRef();
        service.getSupportingResource().add(resourceRef);
    }

    @Transactional
    public ResourceRef createResourceRef() {
        ResourceRef resourceRef = new ResourceRef();
        resourceRef.setType(ResourceType.KUBERNETES_RESOURCE.getName());
        List<ResourceRef> supportingResource = service.getSupportingResource();
        supportingResource.add(resourceRef);
        resourceHelper.persistResourceRefs(List.of(resourceRef));
        return resourceRef;
    }

    public void addKubernetesIdToServiceRef(
@@ -1298,24 +1315,15 @@ public class ServiceOrderAndServiceRepositories {
        Optional<Service> kubernetesServiceOptional =
                serviceHelper.findServiceById(kubernetesServiceId);

        AtomicReference<Optional<ResourceRef>> resourceRefAtomicReference = new AtomicReference<>();
        resourceRefAtomicReference.set(Optional.empty());

        kubernetesServiceOptional.ifPresent(
                kubernetesService -> {
                    // we expect at most one such resource reference, thus findFirst call suffices
                    Optional<ResourceRef> resourceRef =
                    Set<ResourceRef> resourceRefs =
                            kubernetesService.getSupportingResource().stream()
                                    .filter(getK8sResourceRefPredicate())
                                    .findFirst();
                                    .collect(Collectors.toSet());

                    resourceRefAtomicReference.set(resourceRef);
                    endUserReference.getSupportingResource().addAll(resourceRefs);
                });

        Optional<ResourceRef> resourceRefOptional = resourceRefAtomicReference.get();

        resourceRefOptional.ifPresent(
                resourceRef -> endUserReference.getSupportingResource().add(resourceRef));
    }

    private static Predicate<ResourceRef> getK8sResourceRefPredicate() {
+5 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ package org.etsi.osl.hypo.api.tmf.services.common.resource;
import jakarta.enterprise.context.ApplicationScoped;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.etsi.osl.hypo.api.tmf.resources.inventory.api.services.ResourceInventoryService;
import org.etsi.osl.hypo.api.tmf.resources.inventory.schema.Resource;
import org.etsi.osl.hypo.api.tmf.services.common.schema.ResourceRef;
@@ -36,6 +37,10 @@ public class ResourceHelper {
        return resourceRefRepository.findByIdsList(resourcesIdsList);
    }

    public Set<ResourceRef> getResourceRefSetByIds(Set<String> resourcesIds) {
        return resourceRefRepository.findByIdsSet(resourcesIds);
    }

    public void persistResourceRefs(List<ResourceRef> resourceRefs) {
        resourceRefRepository.persist(resourceRefs);
    }
+1 −0
Original line number Diff line number Diff line
@@ -60,6 +60,7 @@ public interface ServiceOrderToServiceMapper {
            target = "serviceCharacteristic",
            source = "serviceCharacteristic",
            qualifiedByName = SERVICE_CHARACTERISTIC_TO_SERVICE_REFERENCE_CHARACTERISTIC)
    @Mapping(target = "supportingResource", ignore = true)
    @BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
    ServiceRefOrValue serviceToServiceRef(Service service);

+1 −1
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ import static org.etsi.osl.hypo.api.tmf.services.inventory.api.rules.RulesConsta

import jakarta.enterprise.context.ApplicationScoped;
import java.util.Objects;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.StringUtils;
import org.etsi.osl.hypo.api.tmf.common.api.helpers.ServiceUtilsHelper;
import org.etsi.osl.hypo.api.tmf.services.order.api.rules.annotations.NewServiceOrder;
import org.etsi.osl.hypo.api.tmf.services.order.api.rules.facts.ServiceOrderFacts;
+1 −1
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ import static org.etsi.osl.hypo.api.tmf.services.inventory.api.rules.RulesConsta

import jakarta.enterprise.context.ApplicationScoped;
import java.util.Objects;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.StringUtils;
import org.etsi.osl.hypo.api.tmf.common.api.helpers.ServiceUtilsHelper;
import org.etsi.osl.hypo.api.tmf.services.order.api.rules.annotations.NewServiceOrder;
import org.etsi.osl.hypo.api.tmf.services.order.api.rules.facts.ServiceOrderFacts;
Loading