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

refactor: simplify ServiceOrderService by removing redundant fields and methods

parent d478340b
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ 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.common.schema.RelatedPartyEntity;
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;
@@ -343,4 +344,12 @@ public class ServiceCatalogHelper {
                    serviceSpecificationRef, serviceSpecificationName);
        };
    }

    public List<RelatedPartyEntity> findAllRelatedPartyListByIds(List<String> relatedPartyIds) {
        return relatedPartyHelper.getRelatedPartiesByIds(relatedPartyIds);
    }

    public Set<RelatedPartyEntity> findAllRelatedPartySetByIds(Set<String> relatedPartyIds) {
        return relatedPartyHelper.getRelatedPartySetByIds(relatedPartyIds);
    }
}
+0 −8
Original line number Diff line number Diff line
package org.etsi.osl.hypo.api.tmf.services.order.api.repository;

import static org.etsi.osl.hypo.api.tmf.common.constants.ColumnConstants.ID;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.persistence.LockModeType;
import java.util.Optional;
import org.etsi.osl.hypo.api.tmf.common.api.repository.AbstractBaseRepository;
import org.etsi.osl.hypo.api.tmf.services.order.schema.ServiceOrder;

@@ -14,8 +10,4 @@ public class ServiceOrderRepository extends AbstractBaseRepository<ServiceOrder>
    public ServiceOrderRepository() {
        super(ServiceOrder.class);
    }

    public Optional<ServiceOrder> findByIdWithLock(String id) {
        return find(ID, id).withLock(LockModeType.PESSIMISTIC_WRITE).singleResultOptional();
    }
}
+2 −8
Original line number Diff line number Diff line
@@ -29,7 +29,6 @@ import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;
import org.etsi.osl.hypo.api.tmf.common.api.services.UtilService;
import org.etsi.osl.hypo.api.tmf.common.model.ErrorMessage;
import org.etsi.osl.hypo.api.tmf.services.order.api.services.ServiceOrderAndServiceHandlerService;
import org.etsi.osl.hypo.api.tmf.services.order.api.services.ServiceOrderService;
import org.etsi.osl.hypo.api.tmf.services.order.model.ServiceOrderDTO;
import org.etsi.osl.hypo.api.tmf.services.order.model.ServiceOrderResponse;
@@ -49,7 +48,6 @@ public class ServiceOrderResource {
    private static final Logger logger = Logger.getLogger(ServiceOrderResource.class);

    private final ServiceOrderService serviceOrderService;
    private final ServiceOrderAndServiceHandlerService serviceOrderAndServiceHandlerService;
    private final ObjectMapper objectMapper;

    public static final String SERVICE_ORDER = "/serviceOrder";
@@ -57,12 +55,8 @@ public class ServiceOrderResource {
    public static final String BASE_URL =
            SERVICE_ORDERING + ApplicationProperties.TMF_URL_VERSION + SERVICE_ORDER;

    public ServiceOrderResource(
            ServiceOrderService serviceOrderService,
            ServiceOrderAndServiceHandlerService serviceOrderAndServiceHandlerService,
            ObjectMapper objectMapper) {
    public ServiceOrderResource(ServiceOrderService serviceOrderService, ObjectMapper objectMapper) {
        this.serviceOrderService = serviceOrderService;
        this.serviceOrderAndServiceHandlerService = serviceOrderAndServiceHandlerService;
        this.objectMapper = objectMapper;
    }

@@ -94,7 +88,7 @@ public class ServiceOrderResource {
                "CreateServiceOrder POST Request Received : %s",
                objectMapper.writer().writeValueAsString(serviceOrderRequest));
        ServiceOrderResponse createdServiceOrder =
                serviceOrderAndServiceHandlerService.createAndPersistServiceOrder(
                serviceOrderService.createAndPersistServiceOrder(
                        serviceOrderRequest, uriInfo, authHeader, requestContext.getMethod());
        logger.infof("CreateServiceOrder Response : %s", createdServiceOrder.toString());

+0 −60
Original line number Diff line number Diff line
package org.etsi.osl.hypo.api.tmf.services.order.api.services;

import static org.etsi.osl.hypo.api.tmf.common.api.constants.AlarmLogsConstants.POST_SERVICE_ORDER_FAILED;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.core.UriInfo;
import org.apache.commons.lang3.StringUtils;
import org.etsi.osl.hypo.api.tmf.common.exception.ValidationException;
import org.etsi.osl.hypo.api.tmf.services.order.api.rules.engine.ServiceOrderEngine;
import org.etsi.osl.hypo.api.tmf.services.order.api.rules.facts.ServiceOrderFacts;
import org.etsi.osl.hypo.api.tmf.services.order.model.ServiceOrderDTO;
import org.etsi.osl.hypo.api.tmf.services.order.model.ServiceOrderResponse;
import org.jboss.logging.Logger;

@ApplicationScoped
public class ServiceOrderAndServiceHandlerService {
    private static final Logger logger = Logger.getLogger(ServiceOrderAndServiceHandlerService.class);

    private final ServiceOrderEngine serviceOrderEngine;

    public ServiceOrderAndServiceHandlerService(ServiceOrderEngine serviceOrderEngine) {
        this.serviceOrderEngine = serviceOrderEngine;
    }

    public ServiceOrderResponse createAndPersistServiceOrder(
            ServiceOrderDTO serviceOrderDTO, UriInfo uriInfo, String authHeader, String httpMethod)
            throws ValidationException {
        try {

            ServiceOrderFacts serviceOrderFacts =
                    serviceOrderEngine.applyRules(serviceOrderDTO, authHeader, httpMethod, uriInfo);

            String validationError = serviceOrderFacts.getValidationMessage();
            if (StringUtils.isNotBlank(validationError)) {
                String errorMessageFormat = String.join(POST_SERVICE_ORDER_FAILED, "Validation Error %s:");
                String errorMessage = String.format(errorMessageFormat, validationError);
                logger.error(errorMessage);
                throw new ValidationException(errorMessage);
            }
            return transformServiceOrderResponse(serviceOrderFacts.getServiceOrderResponse());
        } catch (Exception e) {
            String debugMessageFormat =
                    String.join(POST_SERVICE_ORDER_FAILED, " Exception class message : %s");
            logger.debugf(debugMessageFormat, e.getMessage());
            String errorMessageFormat =
                    String.join(POST_SERVICE_ORDER_FAILED, " Exception class Type : %s");
            String errorMessage = String.format(errorMessageFormat, e.getClass().getSimpleName());
            logger.error(errorMessage);
            throw new ValidationException(errorMessage);
        }
    }

    private ServiceOrderResponse transformServiceOrderResponse(
            ServiceOrderResponse serviceOrderResponse) {
        ServiceOrderResponse serviceOrderResponseTransformed = new ServiceOrderResponse();
        serviceOrderResponseTransformed.setId(serviceOrderResponse.getId());
        serviceOrderResponseTransformed.setHref(serviceOrderResponse.getHref());
        return serviceOrderResponseTransformed;
    }
}
+38 −8
Original line number Diff line number Diff line
package org.etsi.osl.hypo.api.tmf.services.order.api.services;

import static org.etsi.osl.hypo.api.tmf.common.api.constants.AlarmLogsConstants.POST_SERVICE_ORDER_FAILED;
import static org.etsi.osl.hypo.core.common.constants.ServiceOrderWithPackageManagerConstants.SERVICE_SPEC_END_USER_CFS;

import io.opentelemetry.instrumentation.annotations.WithSpan;
@@ -28,7 +29,6 @@ import org.etsi.osl.hypo.api.tmf.common.api.constants.ApiConstants;
import org.etsi.osl.hypo.api.tmf.common.api.helpers.ServiceCatalogHelper;
import org.etsi.osl.hypo.api.tmf.common.api.helpers.ServiceUtilsHelper;
import org.etsi.osl.hypo.api.tmf.common.api.helpers.SystemClockHelper;
import org.etsi.osl.hypo.api.tmf.common.api.services.RelatedPartyService;
import org.etsi.osl.hypo.api.tmf.common.api.services.UtilService;
import org.etsi.osl.hypo.api.tmf.common.exception.ServiceNotFoundException;
import org.etsi.osl.hypo.api.tmf.common.exception.UpdateStateNotAllowedException;
@@ -46,6 +46,8 @@ import org.etsi.osl.hypo.api.tmf.services.inventory.schema.Service;
import org.etsi.osl.hypo.api.tmf.services.order.api.helpers.ServiceOrderUpdateRequestedDatesHandler;
import org.etsi.osl.hypo.api.tmf.services.order.api.mappers.ServiceOrderMapper;
import org.etsi.osl.hypo.api.tmf.services.order.api.repository.ServiceOrderRepository;
import org.etsi.osl.hypo.api.tmf.services.order.api.rules.engine.ServiceOrderEngine;
import org.etsi.osl.hypo.api.tmf.services.order.api.rules.facts.ServiceOrderFacts;
import org.etsi.osl.hypo.api.tmf.services.order.model.ServiceOrderDTO;
import org.etsi.osl.hypo.api.tmf.services.order.model.ServiceOrderResponse;
import org.etsi.osl.hypo.api.tmf.services.order.model.common.ServiceOrderItem;
@@ -63,26 +65,26 @@ public class ServiceOrderService {
    private final ServiceOrderRepository serviceOrderRepository;
    private final ServiceOrderMapper serviceOrderMapper;
    private final ServiceInventoryService serviceInventoryService;
    private final RelatedPartyService relatedPartyService;
    private final ServiceOrderUpdateRequestedDatesHandler serviceOrderUpdateRequestedDatesHandler;
    private final ServiceCatalogHelper serviceCatalogHelper;
    private final ResourceInventoryService resourceInventoryService;
    private final ServiceOrderEngine serviceOrderEngine;

    public ServiceOrderService(
            ServiceOrderRepository serviceOrderRepository,
            ServiceOrderMapper serviceOrderMapper,
            ServiceInventoryService serviceInventoryService,
            RelatedPartyService relatedPartyService,
            ServiceOrderUpdateRequestedDatesHandler serviceOrderUpdateRequestedDatesHandler,
            ServiceCatalogHelper serviceCatalogHelper,
            ResourceInventoryService resourceInventoryService) {
            ResourceInventoryService resourceInventoryService,
            ServiceOrderEngine serviceOrderEngine) {
        this.serviceOrderRepository = serviceOrderRepository;
        this.serviceOrderMapper = serviceOrderMapper;
        this.serviceInventoryService = serviceInventoryService;
        this.relatedPartyService = relatedPartyService;
        this.serviceOrderUpdateRequestedDatesHandler = serviceOrderUpdateRequestedDatesHandler;
        this.serviceCatalogHelper = serviceCatalogHelper;
        this.resourceInventoryService = resourceInventoryService;
        this.serviceOrderEngine = serviceOrderEngine;
    }

    private static final Logger logger = Logger.getLogger(ServiceOrderService.class);
@@ -116,6 +118,34 @@ public class ServiceOrderService {
        return serviceOrderMapper.serviceOrderEntityToResponse(serviceOrder);
    }

    public ServiceOrderResponse createAndPersistServiceOrder(
            ServiceOrderDTO serviceOrderDTO, UriInfo uriInfo, String authHeader, String httpMethod)
            throws ValidationException {
        try {

            ServiceOrderFacts serviceOrderFacts =
                    serviceOrderEngine.applyRules(serviceOrderDTO, authHeader, httpMethod, uriInfo);

            String validationError = serviceOrderFacts.getValidationMessage();
            if (StringUtils.isNotBlank(validationError)) {
                String errorMessageFormat = String.join(POST_SERVICE_ORDER_FAILED, "Validation Error %s:");
                String errorMessage = String.format(errorMessageFormat, validationError);
                logger.error(errorMessage);
                throw new ValidationException(errorMessage);
            }
            return serviceOrderFacts.getServiceOrderResponse();
        } catch (Exception e) {
            String debugMessageFormat =
                    String.join(POST_SERVICE_ORDER_FAILED, " Exception class message : %s");
            logger.debugf(debugMessageFormat, e.getMessage());
            String errorMessageFormat =
                    String.join(POST_SERVICE_ORDER_FAILED, " Exception class Type : %s");
            String errorMessage = String.format(errorMessageFormat, e.getClass().getSimpleName());
            logger.error(errorMessage);
            throw new ValidationException(errorMessage);
        }
    }

    @Transactional
    public List<ServiceOrder> findAll() {
        return serviceOrderRepository.findAll().list();
@@ -275,7 +305,7 @@ public class ServiceOrderService {
    private void populateServiceOrderWithRelatedParties(
            ServiceOrder serviceOrder, List<String> relatedPartyIds) {
        List<RelatedPartyEntity> relatedParties =
                relatedPartyService.findAllRelatedPartiesByIds(relatedPartyIds);
                serviceCatalogHelper.findAllRelatedPartyListByIds(relatedPartyIds);

        serviceOrder.getRelatedParty().clear();
        serviceOrder.getRelatedParty().addAll(relatedParties);
@@ -568,7 +598,7 @@ public class ServiceOrderService {
        List<String> relatedPartyIDs =
                serviceOrder.getRelatedParty().stream().map(BaseEntity::getId).toList();
        List<RelatedPartyEntity> relatedPartiesRetrieved =
                relatedPartyService.findAllRelatedPartiesByIds(relatedPartyIDs);
                serviceCatalogHelper.findAllRelatedPartyListByIds(relatedPartyIDs);

        serviceOrder.getRelatedParty().clear();
        serviceOrder.getRelatedParty().addAll(relatedPartiesRetrieved);
@@ -604,7 +634,7 @@ public class ServiceOrderService {
            Set<String> relatedPartiesIDs =
                    relatedPartyEntities.stream().map(BaseEntity::getId).collect(Collectors.toSet());
            Set<RelatedPartyEntity> relatedPartiesRetrieved =
                    relatedPartyService.findAllRelatedPartiesByIds(relatedPartiesIDs);
                    serviceCatalogHelper.findAllRelatedPartySetByIds(relatedPartiesIDs);

            serviceCFS.getRelatedParty().clear();
            serviceCFS.getRelatedParty().addAll(relatedPartiesRetrieved);
Loading