Search in sources :

Example 1 with APIStateChangeResponse

use of org.wso2.carbon.apimgt.api.model.APIStateChangeResponse in project carbon-apimgt by wso2.

the class PublisherCommonUtils method changeApiOrApiProductLifecycle.

/**
 * Change the lifecycle state of an API or API Product identified by UUID
 *
 * @param action       LC state change action
 * @param apiTypeWrapper API Type Wrapper (API or API Product)
 * @param lcChecklist  LC state change check list
 * @param organization Organization of logged-in user
 * @return APIStateChangeResponse
 * @throws APIManagementException Exception if there is an error when changing the LC state of API or API Product
 */
public static APIStateChangeResponse changeApiOrApiProductLifecycle(String action, ApiTypeWrapper apiTypeWrapper, String lcChecklist, String organization) throws APIManagementException {
    String[] checkListItems = lcChecklist != null ? lcChecklist.split(APIConstants.DELEM_COMMA) : new String[0];
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    Map<String, Object> apiLCData = apiProvider.getAPILifeCycleData(apiTypeWrapper.getUuid(), organization);
    String[] nextAllowedStates = (String[]) apiLCData.get(APIConstants.LC_NEXT_STATES);
    if (!ArrayUtils.contains(nextAllowedStates, action)) {
        throw new APIManagementException("Action '" + action + "' is not allowed. Allowed actions are " + Arrays.toString(nextAllowedStates), ExceptionCodes.from(ExceptionCodes.UNSUPPORTED_LIFECYCLE_ACTION, action));
    }
    // check and set lifecycle check list items including "Deprecate Old Versions" and "Require Re-Subscription".
    Map<String, Boolean> lcMap = new HashMap<>();
    for (String checkListItem : checkListItems) {
        String[] attributeValPair = checkListItem.split(APIConstants.DELEM_COLON);
        if (attributeValPair.length == 2) {
            String checkListItemName = attributeValPair[0].trim();
            boolean checkListItemValue = Boolean.parseBoolean(attributeValPair[1].trim());
            lcMap.put(checkListItemName, checkListItemValue);
        }
    }
    try {
        return apiProvider.changeLifeCycleStatus(organization, apiTypeWrapper, action, lcMap);
    } catch (FaultGatewaysException e) {
        throw new APIManagementException("Error while change the state of artifact with name - " + apiTypeWrapper.getName(), e);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) JSONObject(org.json.simple.JSONObject) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 2 with APIStateChangeResponse

use of org.wso2.carbon.apimgt.api.model.APIStateChangeResponse in project carbon-apimgt by wso2.

the class APIMappingUtil method toWorkflowResponseDTO.

/**
 * Returns workflow state DTO from the provided information.
 *
 * @param lifecycleStateDTO   Lifecycle state DTO
 * @param stateChangeResponse workflow response from API lifecycle change
 * @return workflow state DTO
 */
public static WorkflowResponseDTO toWorkflowResponseDTO(LifecycleStateDTO lifecycleStateDTO, APIStateChangeResponse stateChangeResponse) {
    WorkflowResponseDTO workflowResponseDTO = new WorkflowResponseDTO();
    if (WorkflowStatus.APPROVED.toString().equals(stateChangeResponse.getStateChangeStatus())) {
        workflowResponseDTO.setWorkflowStatus(WorkflowResponseDTO.WorkflowStatusEnum.APPROVED);
    } else if (WorkflowStatus.CREATED.toString().equals(stateChangeResponse.getStateChangeStatus())) {
        workflowResponseDTO.setWorkflowStatus(WorkflowResponseDTO.WorkflowStatusEnum.CREATED);
    } else if ((WorkflowStatus.REGISTERED.toString().equals(stateChangeResponse.getStateChangeStatus()))) {
        workflowResponseDTO.setWorkflowStatus(WorkflowResponseDTO.WorkflowStatusEnum.REGISTERED);
    } else if ((WorkflowStatus.REJECTED.toString().equals(stateChangeResponse.getStateChangeStatus()))) {
        workflowResponseDTO.setWorkflowStatus(WorkflowResponseDTO.WorkflowStatusEnum.REJECTED);
    } else {
        log.error("Unrecognized state : " + stateChangeResponse.getStateChangeStatus());
        workflowResponseDTO.setWorkflowStatus(WorkflowResponseDTO.WorkflowStatusEnum.CREATED);
    }
    workflowResponseDTO.setLifecycleState(lifecycleStateDTO);
    return workflowResponseDTO;
}
Also used : WorkflowResponseDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.WorkflowResponseDTO)

Example 3 with APIStateChangeResponse

use of org.wso2.carbon.apimgt.api.model.APIStateChangeResponse in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method changeAPILifecycle.

@Override
public Response changeAPILifecycle(String action, String apiId, String lifecycleChecklist, String ifMatch, MessageContext messageContext) {
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        ApiTypeWrapper apiWrapper = new ApiTypeWrapper(apiProvider.getAPIbyUUID(apiId, organization));
        APIStateChangeResponse stateChangeResponse = PublisherCommonUtils.changeApiOrApiProductLifecycle(action, apiWrapper, lifecycleChecklist, organization);
        // returns the current lifecycle state
        // todo try to prevent this call
        LifecycleStateDTO stateDTO = getLifecycleState(apiId, organization);
        WorkflowResponseDTO workflowResponseDTO = APIMappingUtil.toWorkflowResponseDTO(stateDTO, stateChangeResponse);
        return Response.ok().entity(workflowResponseDTO).build();
    } catch (APIManagementException e) {
        // Auth failure occurs when cross tenant accessing APIs. Sends 404, since we don't need to expose the existence of the resource
        if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
        } else if (isAuthorizationFailure(e)) {
            RestApiUtil.handleAuthorizationFailure("Authorization failure while updating the lifecycle of API " + apiId, e, log);
        } else {
            RestApiUtil.handleInternalServerError("Error while updating lifecycle of API " + apiId, e, log);
        }
    }
    return null;
}
Also used : WorkflowResponseDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.WorkflowResponseDTO) LifecycleStateDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.LifecycleStateDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) APIStateChangeResponse(org.wso2.carbon.apimgt.api.model.APIStateChangeResponse) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 4 with APIStateChangeResponse

use of org.wso2.carbon.apimgt.api.model.APIStateChangeResponse in project carbon-apimgt by wso2.

the class ApiProductsApiServiceImpl method changeAPIProductLifecycle.

@Override
public Response changeAPIProductLifecycle(String action, String apiProductId, String lifecycleChecklist, String ifMatch, MessageContext messageContext) throws APIManagementException {
    String organization = RestApiUtil.getValidatedOrganization(messageContext);
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    ApiTypeWrapper productWrapper = new ApiTypeWrapper(apiProvider.getAPIProductbyUUID(apiProductId, organization));
    APIStateChangeResponse stateChangeResponse = PublisherCommonUtils.changeApiOrApiProductLifecycle(action, productWrapper, lifecycleChecklist, organization);
    LifecycleStateDTO stateDTO = getLifecycleState(apiProductId, organization);
    WorkflowResponseDTO workflowResponseDTO = APIMappingUtil.toWorkflowResponseDTO(stateDTO, stateChangeResponse);
    return Response.ok().entity(workflowResponseDTO).build();
}
Also used : WorkflowResponseDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.WorkflowResponseDTO) LifecycleStateDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.LifecycleStateDTO) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) APIStateChangeResponse(org.wso2.carbon.apimgt.api.model.APIStateChangeResponse) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 5 with APIStateChangeResponse

use of org.wso2.carbon.apimgt.api.model.APIStateChangeResponse in project carbon-apimgt by wso2.

the class APIProviderImpl method changeLifeCycleStatus.

/**
 * This method is to change registry lifecycle states for an API or API Product artifact
 *
 * @param orgId          UUID of the organization
 * @param apiTypeWrapper API Type Wrapper
 * @param action         Action which need to execute from registry lifecycle
 * @param checklist      checklist items
 * @return APIStateChangeResponse API workflow state and WorkflowResponse
 */
@Override
public APIStateChangeResponse changeLifeCycleStatus(String orgId, ApiTypeWrapper apiTypeWrapper, String action, Map<String, Boolean> checklist) throws APIManagementException, FaultGatewaysException {
    APIStateChangeResponse response = new APIStateChangeResponse();
    try {
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(this.username);
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(this.tenantDomain, true);
        String targetStatus;
        String providerName;
        String apiName;
        String apiContext;
        String apiType;
        String apiVersion;
        String currentStatus;
        String uuid;
        int apiOrApiProductId;
        boolean isApiProduct = apiTypeWrapper.isAPIProduct();
        String workflowType;
        if (isApiProduct) {
            APIProduct apiProduct = apiTypeWrapper.getApiProduct();
            providerName = apiProduct.getId().getProviderName();
            apiName = apiProduct.getId().getName();
            apiContext = apiProduct.getContext();
            apiType = apiProduct.getType();
            apiVersion = apiProduct.getId().getVersion();
            currentStatus = apiProduct.getState();
            uuid = apiProduct.getUuid();
            apiOrApiProductId = apiMgtDAO.getAPIProductId(apiTypeWrapper.getApiProduct().getId());
            workflowType = WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE;
        } else {
            API api = apiTypeWrapper.getApi();
            providerName = api.getId().getProviderName();
            apiName = api.getId().getApiName();
            apiContext = api.getContext();
            apiType = api.getType();
            apiVersion = api.getId().getVersion();
            currentStatus = api.getStatus();
            uuid = api.getUuid();
            apiOrApiProductId = apiMgtDAO.getAPIID(uuid);
            workflowType = WorkflowConstants.WF_TYPE_AM_API_STATE;
        }
        String gatewayVendor = apiMgtDAO.getGatewayVendorByAPIUUID(uuid);
        WorkflowStatus apiWFState = null;
        WorkflowDTO wfDTO = apiMgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(apiOrApiProductId), workflowType);
        if (wfDTO != null) {
            apiWFState = wfDTO.getStatus();
        }
        // if the workflow has started, then executor should not fire again
        if (!WorkflowStatus.CREATED.equals(apiWFState)) {
            response = executeStateChangeWorkflow(currentStatus, action, apiName, apiContext, apiType, apiVersion, providerName, apiOrApiProductId, uuid, gatewayVendor, workflowType);
            // get the workflow state once the executor is executed.
            wfDTO = apiMgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(apiOrApiProductId), workflowType);
            if (wfDTO != null) {
                apiWFState = wfDTO.getStatus();
                response.setStateChangeStatus(apiWFState.toString());
            } else {
                response.setStateChangeStatus(WorkflowStatus.APPROVED.toString());
            }
        }
        // apiWFState is null when simple wf executor is used because wf state is not stored in the db.
        if (WorkflowStatus.APPROVED.equals(apiWFState) || apiWFState == null) {
            targetStatus = LCManagerFactory.getInstance().getLCManager().getStateForTransition(action);
            apiPersistenceInstance.changeAPILifeCycle(new Organization(orgId), uuid, targetStatus);
            sendLCStateChangeNotification(apiName, apiType, apiContext, apiVersion, targetStatus, providerName, apiOrApiProductId, uuid);
            if (!isApiProduct) {
                API api = apiTypeWrapper.getApi();
                api.setOrganization(orgId);
                changeLifeCycle(api, currentStatus, targetStatus, checklist);
                // Sending Notifications to existing subscribers
                if (APIConstants.PUBLISHED.equals(targetStatus)) {
                    sendEmailNotification(api);
                }
            } else {
                APIProduct apiProduct = apiTypeWrapper.getApiProduct();
                apiProduct.setOrganization(orgId);
                changeLifecycle(apiProduct, currentStatus, targetStatus);
            }
            addLCStateChangeInDatabase(currentStatus, targetStatus, uuid);
            if (log.isDebugEnabled()) {
                String logMessage = "LC Status changed successfully for artifact with name: " + apiName + ", version " + apiVersion + ", New Status : " + targetStatus;
                log.debug(logMessage);
            }
            extractRecommendationDetails(apiTypeWrapper);
            return response;
        }
    } catch (APIPersistenceException e) {
        handleException("Error while accessing persistence layer", e);
    } catch (PersistenceException e) {
        handleException("Error while accessing lifecycle information ", e);
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
    return response;
}
Also used : PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) WorkflowDTO(org.wso2.carbon.apimgt.impl.dto.WorkflowDTO) APIStateWorkflowDTO(org.wso2.carbon.apimgt.impl.workflow.APIStateWorkflowDTO) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) APIStateChangeResponse(org.wso2.carbon.apimgt.api.model.APIStateChangeResponse) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) GraphQLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) PersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) AsyncSpecPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) WorkflowStatus(org.wso2.carbon.apimgt.impl.workflow.WorkflowStatus)

Aggregations

APIStateChangeResponse (org.wso2.carbon.apimgt.api.model.APIStateChangeResponse)8 WorkflowDTO (org.wso2.carbon.apimgt.impl.dto.WorkflowDTO)4 Test (org.junit.Test)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)3 ApiTypeWrapper (org.wso2.carbon.apimgt.api.model.ApiTypeWrapper)3 APIStateWorkflowDTO (org.wso2.carbon.apimgt.impl.workflow.APIStateWorkflowDTO)3 WorkflowResponseDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.WorkflowResponseDTO)3 HashMap (java.util.HashMap)2 JSONObject (org.json.simple.JSONObject)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 FaultGatewaysException (org.wso2.carbon.apimgt.api.FaultGatewaysException)2 WorkflowResponse (org.wso2.carbon.apimgt.api.WorkflowResponse)2 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)2 APIProduct (org.wso2.carbon.apimgt.api.model.APIProduct)2 WorkflowException (org.wso2.carbon.apimgt.impl.workflow.WorkflowException)2 WorkflowExecutor (org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor)2 WorkflowStatus (org.wso2.carbon.apimgt.impl.workflow.WorkflowStatus)2 PublisherAPIProduct (org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct)2 LifecycleStateDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.LifecycleStateDTO)2