Commit f28383f5 authored by Anastasios Poimenidis's avatar Anastasios Poimenidis
Browse files

feat: introduce BaseRepository.findByIdWithLockWrite for updates, simplify...

feat: introduce BaseRepository.findByIdWithLockWrite for updates, simplify logic in ServiceOrderAndServiceRepositories, ServiceInventoryService and ServiceOrderService
parent 23a47c18
Loading
Loading
Loading
Loading
+110 −4
Original line number Diff line number Diff line
@@ -5,14 +5,19 @@ import static org.etsi.osl.hypo.core.common.constants.ServiceOrderWithPackageMan

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Strings;
import org.etsi.osl.hypo.api.tmf.common.api.model.RelatedPartyDTO;
import org.etsi.osl.hypo.api.tmf.party.api.services.OrganizationService;
import org.etsi.osl.hypo.api.tmf.party.organization.schema.Organization;
import org.etsi.osl.hypo.api.tmf.services.catalog.api.repository.ServiceSpecificationRepository;
import org.etsi.osl.hypo.api.tmf.services.catalog.api.services.ServiceCategoryService;
import org.etsi.osl.hypo.api.tmf.services.catalog.api.services.ServiceSpecificationService;
import org.etsi.osl.hypo.api.tmf.services.catalog.schema.ServiceCandidateEntity;
@@ -20,23 +25,37 @@ import org.etsi.osl.hypo.api.tmf.services.catalog.schema.ServiceCategoryEntity;
import org.etsi.osl.hypo.api.tmf.services.catalog.schema.ServiceSpecificationEntity;
import org.etsi.osl.hypo.api.tmf.services.common.model.ServiceRefOrValue;
import org.etsi.osl.hypo.api.tmf.services.common.schema.ServiceSpecificationRef;
import org.etsi.osl.hypo.api.tmf.services.common.service.RelatedPartyHelper;
import org.etsi.osl.hypo.api.tmf.services.inventory.api.mappers.ServiceMapper;
import org.etsi.osl.hypo.api.tmf.services.inventory.schema.Service;
import org.etsi.osl.hypo.api.tmf.services.model.ServiceWithPeeredOrganization;
import org.etsi.osl.hypo.api.tmf.services.order.model.common.ServiceOrderItem;
import org.etsi.osl.hypo.api.tmf.services.order.schema.ServiceOrderItemEntity;

@ApplicationScoped
public class ServiceSpecificationHelpers {
public class ServiceCatalogHelper {

    private final ServiceCategoryService serviceCategoryService;
    private final ServiceSpecificationService serviceSpecificationService;
    private final ServiceSpecificationRepository serviceSpecificationRepository;
    private final OrganizationService organizationService;
    private final RelatedPartyHelper relatedPartyHelper;
    private final ServiceMapper serviceMapper;

    @Inject
    public ServiceSpecificationHelpers(
    public ServiceCatalogHelper(
            ServiceCategoryService serviceCategoryService,
            ServiceSpecificationService serviceInventoryService,
            OrganizationService organizationService) {
            ServiceSpecificationRepository serviceSpecificationRepository,
            OrganizationService organizationService,
            RelatedPartyHelper relatedPartyHelper,
            ServiceMapper serviceMapper) {
        this.serviceCategoryService = serviceCategoryService;
        this.serviceSpecificationService = serviceInventoryService;
        this.serviceSpecificationRepository = serviceSpecificationRepository;
        this.organizationService = organizationService;
        this.relatedPartyHelper = relatedPartyHelper;
        this.serviceMapper = serviceMapper;
    }

    public boolean hasSpecificationOrParentSpecificationByName(
@@ -45,6 +64,12 @@ public class ServiceSpecificationHelpers {
                serviceSpecificationRef, serviceSpecificationName);
    }

    public List<Service> filterKubernetesServices(List<Service> services, Boolean isKubernetes) {
        return isKubernetes != null && isKubernetes
                ? services.stream().filter(this::hasSpecificationKubernetesCFS).toList()
                : services;
    }

    public boolean hasSpecificationKubernetesCFS(Service service) {
        AtomicReference<Boolean> isK8sSpecification = new AtomicReference<>();
        isK8sSpecification.set(Boolean.FALSE);
@@ -234,7 +259,88 @@ public class ServiceSpecificationHelpers {
        return serviceSpecificationEntity ->
                service.getServiceSpecification() != null
                        && service.getServiceSpecification().getId() != null
                        && StringUtils.equals(
                        && Strings.CS.equals(
                                serviceSpecificationEntity.getId(), service.getServiceSpecification().getId());
    }

    public List<ServiceWithPeeredOrganization> filterKubernetesServicesWithPeeredOrganization(
            List<Service> services, Boolean isKubernetes) {
        return services.stream()
                .filter(
                        service -> {
                            boolean hasToBeKubernetesService =
                                    isKubernetes != null
                                            && isKubernetes
                                            && (this.hasSpecificationKubernetesCFS(service)
                                                    || this.hasSpecificationKubernetesRegistrationCFS(service));
                            boolean doesNotHaveTobeKubernetesService = isKubernetes == null || !isKubernetes;
                            return hasToBeKubernetesService || doesNotHaveTobeKubernetesService;
                        })
                .map(
                        service -> {
                            ServiceWithPeeredOrganization serviceWithPeeredOrganization =
                                    serviceMapper.toServiceWithPeeredOrganizationInfo(service);

                            this.populateServiceWithPeeredOrganizationInfo(serviceWithPeeredOrganization);
                            return serviceWithPeeredOrganization;
                        })
                .toList();
    }

    private void populateServiceWithPeeredOrganizationInfo(
            ServiceWithPeeredOrganization serviceWithPeeredOrganization) {
        org.etsi.osl.hypo.api.tmf.common.model.ServiceSpecificationRef serviceSpecificationRef =
                serviceWithPeeredOrganization.getServiceSpecification();

        if (Objects.nonNull(serviceSpecificationRef)) {
            Set<RelatedPartyDTO> relatedParties =
                    serviceSpecificationService.getRelatedPartyBySpecificationId(
                            serviceSpecificationRef.getId());
            String relatedPartyId =
                    CollectionUtils.isNotEmpty(relatedParties)
                            ? relatedParties.iterator().next().getId()
                            : null;

            if (Objects.nonNull(relatedPartyId)) {
                relatedPartyHelper
                        .getOptionalOrganizationById(relatedPartyId)
                        .ifPresent(
                                organization -> {
                                    if (relatedPartyHelper.isPeeredOrganization(organization)) {
                                        serviceWithPeeredOrganization.setOrganizationProvider(organization.getName());
                                    }
                                });
            }
        }
    }

    public List<ServiceSpecificationEntity> getServiceSpecificationEntitiesByOrganizationId(
            String organizationId) {
        return serviceSpecificationRepository.findServiceSpecificationIdsByOrganizationId(
                organizationId);
    }

    public Optional<ServiceSpecificationEntity> findSpecificationById(String specificationId) {
        return serviceSpecificationRepository.findById(specificationId);
    }

    public Predicate<ServiceOrderItemEntity>
            getServiceOrderItemByServiceSpecificationNameOrParentName(String serviceSpecificationName) {
        return serviceOrderItem -> {
            ServiceSpecificationRef serviceSpecificationRef =
                    serviceOrderItem.getService().getServiceSpecification();
            return serviceSpecificationService.serviceSpecificationMatchesByNameOrParentName(
                    serviceSpecificationRef, serviceSpecificationName);
        };
    }

    public Predicate<ServiceOrderItem> getServiceOrderItemByServiceSpecification(
            String serviceSpecificationName) {
        return serviceOrderItem -> {
            org.etsi.osl.hypo.api.tmf.common.model.ServiceSpecificationRef serviceSpecificationRef =
                    serviceOrderItem.getService().getServiceSpecification();
            return serviceSpecificationService.serviceSpecificationMatchesOrParentMatches(
                    serviceSpecificationRef, serviceSpecificationName);
        };
    }
}
+4 −1
Original line number Diff line number Diff line
@@ -18,7 +18,10 @@ public interface BaseRepository<T> extends PanacheRepository<T> {
    }

    default Optional<T> findByIdWithLockWrite(String id) {
        return find(ID, id).withLock(LockModeType.PESSIMISTIC_WRITE).singleResultOptional();
        return find(ID, id)
                .withLock(LockModeType.PESSIMISTIC_WRITE)
                .withHint("jakarta.persistence.lock.timeout", 3000)
                .singleResultOptional();
    }

    default Optional<T> findByName(String name) {
+217 −403

File changed.

Preview size limit exceeded, changes collapsed.

+8 −11
Original line number Diff line number Diff line
package org.etsi.osl.hypo.api.tmf.services.common;

import jakarta.enterprise.context.ApplicationScoped;
import java.util.Arrays;
import java.util.Objects;
import lombok.Getter;
import org.etsi.osl.hypo.api.tmf.services.order.schema.ServiceOrderStateType;
import org.jboss.logging.Logger;
import org.jgrapht.DirectedGraph;
import org.jgrapht.graph.DefaultDirectedGraph;
import org.jgrapht.graph.DefaultEdge;

@Getter
@ApplicationScoped
public final class ServiceOrderStatesGraph {
    private ServiceOrderStatesGraph() {
        /* This utility class should not be instantiated */
        throw new UnsupportedOperationException("This is a final class and it cannot be instantiated");
    }

    private final DirectedGraph<ServiceOrderStateType, DefaultEdge> serviceStatesDirectedGraph;
    private static final DirectedGraph<ServiceOrderStateType, DefaultEdge>
            serviceStatesDirectedGraph = initializeServiceStatesTransitionGraph();

    private static final Logger logger = Logger.getLogger(ServiceOrderStatesGraph.class);

    public ServiceOrderStatesGraph() {
        this.serviceStatesDirectedGraph = initializeServiceStatesTransitionGraph();
    }

    public boolean isValidServiceOrderStateTransition(
    public static boolean isValidServiceOrderStateTransition(
            ServiceOrderStateType oldServiceState, ServiceOrderStateType newServiceState) {
        DefaultEdge serviceStateDirectedEdge =
                serviceStatesDirectedGraph.getEdge(oldServiceState, newServiceState);
@@ -35,7 +32,7 @@ public final class ServiceOrderStatesGraph {
        return isValidTransition;
    }

    private DefaultDirectedGraph<ServiceOrderStateType, DefaultEdge>
    private static DefaultDirectedGraph<ServiceOrderStateType, DefaultEdge>
            initializeServiceStatesTransitionGraph() {

        DefaultDirectedGraph<ServiceOrderStateType, DefaultEdge> serviceStatesDirectedGraphVariable =
+4 −2
Original line number Diff line number Diff line
@@ -2,15 +2,17 @@ package org.etsi.osl.hypo.api.tmf.services.common;

import java.util.Arrays;
import java.util.Objects;
import lombok.Getter;
import org.etsi.osl.hypo.api.tmf.services.common.schema.ServiceStateType;
import org.jboss.logging.Logger;
import org.jgrapht.DirectedGraph;
import org.jgrapht.graph.DefaultDirectedGraph;
import org.jgrapht.graph.DefaultEdge;

@Getter
public final class ServiceStatesGraph {
    private ServiceStatesGraph() {
        /* This utility class should not be instantiated */
        throw new UnsupportedOperationException("This is a final class and it cannot be instantiated");
    }

    public static final DirectedGraph<ServiceStateType, DefaultEdge> serviceStatesDirectedGraph =
            initializeServiceStatesTransitionGraph();
Loading