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

fix: add locks for Service Catalog API PATCH and update requests

parent 1e6f6e0f
Loading
Loading
Loading
Loading
Loading
+2 −1
Changes for src/main/java/org/etsi/osl/hypo/api/tmf/services/catalog/api/services/ServiceCandidateService.java: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -252,7 +252,8 @@ public class ServiceCandidateService {

    private ServiceCandidateEntity getCandidateByIdAndCheckIfNameAlreadyExists(
            String id, ServiceCandidateEntity newServiceCandidate) {
        Optional<ServiceCandidateEntity> serviceCandidate = serviceCandidateRepository.findById(id);
        Optional<ServiceCandidateEntity> serviceCandidate =
                serviceCandidateRepository.findByIdWithLockWrite(id);
        if (serviceCandidate.isPresent()) {
            if (serviceCandidateCheckExistsByName(newServiceCandidate.getName())) {
                throw new DuplicateKeyException(
+2 −1
Changes for src/main/java/org/etsi/osl/hypo/api/tmf/services/catalog/api/services/ServiceCatalogService.java: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -475,7 +475,8 @@ public class ServiceCatalogService {

    private ServiceCatalogEntity checkIfNameAndIdAlreadyExists(
            String id, ServiceCatalogEntity newServiceCatalogue) {
        Optional<ServiceCatalogEntity> serviceCatalogue = serviceCatalogRepository.findById(id);
        Optional<ServiceCatalogEntity> serviceCatalogue =
                serviceCatalogRepository.findByIdWithLockWrite(id);
        if (serviceCatalogue.isPresent()) {
            if (!serviceCatalogue.get().getName().equals(newServiceCatalogue.getName())
                    && serviceCatalogExistsByName(newServiceCatalogue.getName())) {
+3 −2
Changes for src/main/java/org/etsi/osl/hypo/api/tmf/services/catalog/api/services/ServiceCategoryService.java: 3 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -135,7 +135,7 @@ public class ServiceCategoryService {
                            .collect(Collectors.toSet());

            ServiceCategoryEntity updatedCategoryEntity =
                    updateServiceCategory(id, newServiceCategory, serviceCandidateIds);
                    this.updateServiceCategory(id, newServiceCategory, serviceCandidateIds);
            return serviceCatalogueApiEntityToResponseMapper.serviceCategoryEntityToResponseDTO(
                    updatedCategoryEntity);
        } catch (ServiceNotFoundException e) {
@@ -373,7 +373,8 @@ public class ServiceCategoryService {

    private ServiceCategoryEntity checkIfNameAndIdAlreadyExists(
            String id, ServiceCategoryEntity newServiceCategory) {
        Optional<ServiceCategoryEntity> serviceCategory = serviceCategoryRepository.findById(id);
        Optional<ServiceCategoryEntity> serviceCategory =
                serviceCategoryRepository.findByIdWithLockWrite(id);
        if (serviceCategory.isPresent()) {
            ServiceCategoryEntity serviceCategoryEntity = serviceCategory.get();
            if (!serviceCategoryEntity.getName().equals(newServiceCategory.getName())
+27 −16
Changes for src/main/java/org/etsi/osl/hypo/api/tmf/services/catalog/api/services/ServiceSpecificationService.java: 27 added lines, 16 removed lines.
Original line number Diff line number Diff line
@@ -71,8 +71,8 @@ public class ServiceSpecificationService {

    public static final String PEERED_SPECIFICATION_NAME_DELIMITER = ":";

    public static final String SERVICE_SPECIFICATION_WITH_ID = "Service Specification with id '";
    private static final String ALREADY_EXISTS = "' already exists";
    public static final String SERVICE_SPECIFICATION_WITH_ID = "Service Specification with id:";
    private static final String ALREADY_EXISTS = " already exists";
    public static final String SERVICE_CATEGORY_ID = "serviceCategoryId";
    public static final String SERVICE_CATALOG_ID = "serviceCatalogId";

@@ -531,7 +531,7 @@ public class ServiceSpecificationService {
    public ServiceSpecificationEntity updateServiceSpecificationEntity(
            String id, ServiceSpecificationEntity newServiceSpecification) {
        ServiceSpecificationEntity serviceSpecification =
                checkIfNameAndIdAlreadyExists(id, newServiceSpecification);
                this.checkIfNameAndIdAlreadyExists(id, newServiceSpecification);
        serviceCatalogUpdateEntityMapper.updateServiceSpecificationEntity(
                newServiceSpecification, serviceSpecification);
        return serviceSpecification;
@@ -540,7 +540,7 @@ public class ServiceSpecificationService {
    public void updatePeeredServiceSpecificationEntity(
            String id, ServiceSpecificationEntity newServiceSpecification) {
        ServiceSpecificationEntity serviceSpecification =
                checkIfNameAndIdAlreadyExists(id, newServiceSpecification);
                this.checkIfNameAndIdAlreadyExists(id, newServiceSpecification);
        // update entity except the name
        serviceCatalogUpdateEntityMapper.updateServiceSpecificationEntity(
                newServiceSpecification, serviceSpecification);
@@ -755,20 +755,31 @@ public class ServiceSpecificationService {

    private ServiceSpecificationEntity checkIfNameAndIdAlreadyExists(
            String id, ServiceSpecificationEntity newServiceSpecification) {
        Optional<ServiceSpecificationEntity> serviceSpecification =
                serviceSpecificationRepository.findById(id);
        if (serviceSpecification.isPresent()) {
            if (serviceSpecification.get().getName() != null
                    && !serviceSpecification.get().getName().equals(newServiceSpecification.getName())
                    && serviceSpecificationExistsByName(newServiceSpecification.getName())) {

        ServiceSpecificationEntity serviceSpecificationEntity =
                serviceSpecificationRepository
                        .findByIdWithLockWrite(id)
                        .orElseThrow(
                                () ->
                                        new ServiceNotFoundException(
                                                SERVICE_SPECIFICATION_WITH_ID + id + " not found"));

        String nameBeforeUpdate = serviceSpecificationEntity.getName();
        String nameToBeUpdated = newServiceSpecification.getName();

        // 1. Ensure new name is valid
        // 2. Check if name is actually changing (!equals)
        // 3. Check if the NEW name is already taken by another entity
        boolean isNameChanging = !Strings.CS.equals(nameBeforeUpdate, nameToBeUpdated);

        if (StringUtils.isNotBlank(nameToBeUpdated)
                && isNameChanging
                && serviceSpecificationExistsByName(nameToBeUpdated)) {
            throw new DuplicateKeyException(
                        "Service Specification with name '"
                                + newServiceSpecification.getName()
                                + ALREADY_EXISTS);
            }
            return serviceSpecification.get();
                    "Service Specification with name:" + nameToBeUpdated + ALREADY_EXISTS);
        }
        throw new ServiceNotFoundException(SERVICE_SPECIFICATION_WITH_ID + id + "' not found");

        return serviceSpecificationEntity;
    }

    private void determineIfRelatedSpecificationsExistAndCreateReferences(