Search in sources :

Example 1 with ContractActionDto

use of io.apiman.manager.api.beans.actions.ContractActionDto in project apiman by apiman.

the class ActionService method approveContract.

public void approveContract(ContractActionDto action, String approverId) {
    // Must exist
    ContractBean contract = tryAction(() -> storage.getContract(action.getContractId()));
    if (contract == null) {
        throw ExceptionFactory.actionException(Messages.i18n.format("ContractDoesNotExist"));
    }
    // Must be in AwaitingApproval state (no need to approve otherwise!)
    if (contract.getStatus() != ContractStatus.AwaitingApproval) {
        throw ExceptionFactory.invalidContractStatus(ContractStatus.AwaitingApproval, contract.getStatus());
    }
    // We probably need an optimised query :-).
    ClientVersionBean cvb = contract.getClient();
    ApiVersionBean avb = contract.getApi();
    OrganizationBean org = avb.getApi().getOrganization();
    PlanVersionBean plan = contract.getPlan();
    OrganizationBean orgA = avb.getApi().getOrganization();
    OrganizationBean orgC = cvb.getClient().getOrganization();
    UserBean approver = tryAction(() -> storage.getUser(approverId));
    // Set the contract to approved state and send approved event.
    contract.setStatus(ContractStatus.Created);
    LOGGER.debug("{0} approved a contract: {1} -> {2}", approverId, contract, action);
    // In the second phase we need to check the other contracts to see whether they are all in the 'ready' state
    // If so, then it's ready to publish.
    List<ContractBean> contracts = tryAction(() -> Streams.stream((storage.getAllContracts(org.getId(), cvb.getClient().getId(), cvb.getVersion())))).collect(Collectors.toList());
    List<ContractBean> awaitingApprovalList = contracts.stream().filter(c -> c.getStatus().equals(ContractStatus.AwaitingApproval)).collect(Collectors.toList());
    if (awaitingApprovalList.size() > 0) {
        LOGGER.debug("A contract was approved, but {0} other contracts are still awaiting approval, " + "so client version {1} will remain in its pending state until the remaining contract approvals " + "are granted: {2}.", awaitingApprovalList.size(), cvb.getVersion(), awaitingApprovalList);
    } else {
        LOGGER.debug("All contracts for {0} have been approved", cvb.getVersion());
        tryAction(() -> {
            if (clientValidator.isReady(cvb)) {
                // Set client to ready status and fire change status event
                LOGGER.debug("Client set to ready as all contracts have been approved");
                cvb.setStatus(ClientStatus.Ready);
                clientAppService.fireClientStatusChangeEvent(cvb, ClientStatus.AwaitingApproval);
                // If auto-promote, then we immediately register.
                if (action.isAutoPromote()) {
                    LOGGER.debug("Auto approving: {0}", cvb);
                    registerClient(orgC.getId(), cvb.getClient().getId(), cvb.getVersion());
                }
            }
        });
    }
    // storage.flush();
    fireContractApprovedEvent(approver, contract, orgC, cvb, orgA, avb, plan);
}
Also used : ClientVersionBean(io.apiman.manager.api.beans.clients.ClientVersionBean) ContractApprovalEvent(io.apiman.manager.api.beans.events.ContractApprovalEvent) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) Date(java.util.Date) ApiVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean) ISecurityContext(io.apiman.manager.api.security.ISecurityContext) GatewayNotFoundException(io.apiman.manager.api.rest.exceptions.GatewayNotFoundException) IGatewayLinkFactory(io.apiman.manager.api.gateway.IGatewayLinkFactory) ApimanEventHeaders(io.apiman.manager.api.beans.events.ApimanEventHeaders) UserBean(io.apiman.manager.api.beans.idm.UserBean) Client(io.apiman.gateway.engine.beans.Client) ApiBean(io.apiman.manager.api.beans.apis.ApiBean) UserMapper(io.apiman.manager.api.beans.idm.UserMapper) IClientValidator(io.apiman.manager.api.core.IClientValidator) Map(java.util.Map) URI(java.net.URI) ApimanLoggerFactory(io.apiman.common.logging.ApimanLoggerFactory) Contract(io.apiman.gateway.engine.beans.Contract) OrganizationBean(io.apiman.manager.api.beans.orgs.OrganizationBean) Policy(io.apiman.gateway.engine.beans.Policy) Transactional(javax.transaction.Transactional) IGatewayLink(io.apiman.manager.api.gateway.IGatewayLink) EventService(io.apiman.manager.api.events.EventService) Set(java.util.Set) UUID(java.util.UUID) Streams(com.google.common.collect.Streams) Collectors(java.util.stream.Collectors) ContractStatus(io.apiman.manager.api.beans.contracts.ContractStatus) NotAuthorizedException(io.apiman.manager.api.rest.exceptions.NotAuthorizedException) List(java.util.List) IApimanLogger(io.apiman.common.logging.IApimanLogger) PolicySummaryBean(io.apiman.manager.api.beans.summary.PolicySummaryBean) ContractActionDto(io.apiman.manager.api.beans.actions.ContractActionDto) ApplicationScoped(javax.enterprise.context.ApplicationScoped) ActionException(io.apiman.manager.api.rest.exceptions.ActionException) HashMap(java.util.HashMap) ApiStatus(io.apiman.manager.api.beans.apis.ApiStatus) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) AuditUtils(io.apiman.manager.api.rest.impl.audit.AuditUtils) ArrayList(java.util.ArrayList) ContractBean(io.apiman.manager.api.beans.contracts.ContractBean) HashSet(java.util.HashSet) Inject(javax.inject.Inject) PlanVersionNotFoundException(io.apiman.manager.api.rest.exceptions.PlanVersionNotFoundException) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) IStorageQuery(io.apiman.manager.api.core.IStorageQuery) DataAccessUtilMixin(io.apiman.manager.api.rest.impl.util.DataAccessUtilMixin) PlanVersionBean(io.apiman.manager.api.beans.plans.PlanVersionBean) Iterator(java.util.Iterator) GatewayBean(io.apiman.manager.api.beans.gateways.GatewayBean) ClientStatus(io.apiman.manager.api.beans.clients.ClientStatus) ContractSummaryBean(io.apiman.manager.api.beans.summary.ContractSummaryBean) IStorage(io.apiman.manager.api.core.IStorage) ClientVersionBean(io.apiman.manager.api.beans.clients.ClientVersionBean) ClientVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException) Api(io.apiman.gateway.engine.beans.Api) PolicyType(io.apiman.manager.api.beans.policies.PolicyType) Messages(io.apiman.manager.api.rest.exceptions.i18n.Messages) PlanStatus(io.apiman.manager.api.beans.plans.PlanStatus) ExceptionFactory(io.apiman.manager.api.rest.exceptions.util.ExceptionFactory) UserBean(io.apiman.manager.api.beans.idm.UserBean) OrganizationBean(io.apiman.manager.api.beans.orgs.OrganizationBean) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean) ContractBean(io.apiman.manager.api.beans.contracts.ContractBean) PlanVersionBean(io.apiman.manager.api.beans.plans.PlanVersionBean)

Aggregations

Streams (com.google.common.collect.Streams)1 ApimanLoggerFactory (io.apiman.common.logging.ApimanLoggerFactory)1 IApimanLogger (io.apiman.common.logging.IApimanLogger)1 Api (io.apiman.gateway.engine.beans.Api)1 Client (io.apiman.gateway.engine.beans.Client)1 Contract (io.apiman.gateway.engine.beans.Contract)1 Policy (io.apiman.gateway.engine.beans.Policy)1 PublishingException (io.apiman.gateway.engine.beans.exceptions.PublishingException)1 ContractActionDto (io.apiman.manager.api.beans.actions.ContractActionDto)1 ApiBean (io.apiman.manager.api.beans.apis.ApiBean)1 ApiGatewayBean (io.apiman.manager.api.beans.apis.ApiGatewayBean)1 ApiStatus (io.apiman.manager.api.beans.apis.ApiStatus)1 ApiVersionBean (io.apiman.manager.api.beans.apis.ApiVersionBean)1 ClientStatus (io.apiman.manager.api.beans.clients.ClientStatus)1 ClientVersionBean (io.apiman.manager.api.beans.clients.ClientVersionBean)1 ContractBean (io.apiman.manager.api.beans.contracts.ContractBean)1 ContractStatus (io.apiman.manager.api.beans.contracts.ContractStatus)1 ApimanEventHeaders (io.apiman.manager.api.beans.events.ApimanEventHeaders)1 ContractApprovalEvent (io.apiman.manager.api.beans.events.ContractApprovalEvent)1 GatewayBean (io.apiman.manager.api.beans.gateways.GatewayBean)1