Search in sources :

Example 6 with WorkflowExecutor

use of org.wso2.carbon.apimgt.core.api.WorkflowExecutor in project carbon-apimgt by wso2.

the class APIPublisherImpl method updateAPIStatus.

/**
 * @see org.wso2.carbon.apimgt.core.api.APIPublisher#updateAPIStatus(String, String, Map)
 */
@Override
public WorkflowResponse updateAPIStatus(String apiId, String status, Map<String, Boolean> checkListItemMap) throws APIManagementException {
    WorkflowResponse workflowResponse = null;
    try {
        API api = getApiDAO().getAPI(apiId);
        if (api != null && !APILCWorkflowStatus.PENDING.toString().equals(api.getWorkflowStatus())) {
            API.APIBuilder apiBuilder = new API.APIBuilder(api);
            apiBuilder.lastUpdatedTime(LocalDateTime.now());
            apiBuilder.updatedBy(getUsername());
            LifecycleState currentState = getApiLifecycleManager().getLifecycleDataForState(apiBuilder.getLifecycleInstanceId(), apiBuilder.getLifeCycleStatus());
            apiBuilder.lifecycleState(currentState);
            for (Map.Entry<String, Boolean> checkListItem : checkListItemMap.entrySet()) {
                getApiLifecycleManager().checkListItemEvent(api.getLifecycleInstanceId(), api.getLifeCycleStatus(), checkListItem.getKey(), checkListItem.getValue());
            }
            API originalAPI = apiBuilder.build();
            WorkflowExecutor executor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_API_STATE);
            APIStateChangeWorkflow workflow = new APIStateChangeWorkflow(getApiDAO(), getApiSubscriptionDAO(), getWorkflowDAO(), getApiLifecycleManager(), getApiGateway(), getLabelDAO());
            workflow.setApiName(originalAPI.getName());
            workflow.setApiProvider(originalAPI.getProvider());
            workflow.setApiVersion(originalAPI.getVersion());
            workflow.setCurrentState(currentState.getState());
            workflow.setTransitionState(status);
            workflow.setWorkflowReference(originalAPI.getId());
            workflow.setExternalWorkflowReference(UUID.randomUUID().toString());
            workflow.setCreatedTime(LocalDateTime.now());
            workflow.setWorkflowType(WorkflowConstants.WF_TYPE_AM_API_STATE);
            workflow.setInvoker(getUsername());
            // setting attributes for internal use. These are set to use from outside the executor's method
            // these will be saved in the AM_WORKFLOW table so these can be retrieved later
            workflow.setAttribute(WorkflowConstants.ATTRIBUTE_API_CUR_STATE, currentState.getState());
            workflow.setAttribute(WorkflowConstants.ATTRIBUTE_API_TARGET_STATE, status);
            workflow.setAttribute(WorkflowConstants.ATTRIBUTE_API_LC_INVOKER, getUsername());
            workflow.setAttribute(WorkflowConstants.ATTRIBUTE_API_NAME, originalAPI.getId());
            workflow.setAttribute(WorkflowConstants.ATTRIBUTE_API_HAS_OWN_GATEWAY, String.valueOf(originalAPI.hasOwnGateway()));
            if (originalAPI.hasOwnGateway()) {
                List<String> gwLabels = originalAPI.getLabels();
                for (String label : gwLabels) {
                    if (label.contains(ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX)) {
                        workflow.setAttribute(WorkflowConstants.ATTRIBUTE_API_AUTOGEN_LABEL, label);
                        break;
                    }
                }
            }
            workflow.setAttribute(WorkflowConstants.ATTRIBUTE_HAS_OWN_GATEWAY, String.valueOf(originalAPI.hasOwnGateway()));
            workflow.setAttribute(WorkflowConstants.ATTRIBUTE_API_LAST_UPTIME, originalAPI.getLastUpdatedTime().toString());
            String workflowDescription = "API [" + workflow.getApiName() + " - " + workflow.getApiVersion() + "] state change [" + workflow.getCurrentState() + " to " + workflow.getTransitionState() + "] request from " + getUsername();
            workflow.setWorkflowDescription(workflowDescription);
            workflowResponse = executor.execute(workflow);
            workflow.setStatus(workflowResponse.getWorkflowStatus());
            if (WorkflowStatus.CREATED != workflowResponse.getWorkflowStatus()) {
                completeWorkflow(executor, workflow);
            } else {
                // add entry to workflow table if it is only in pending state
                addWorkflowEntries(workflow);
                getApiDAO().updateAPIWorkflowStatus(api.getId(), APILCWorkflowStatus.PENDING);
            }
        } else if (api != null && APILCWorkflowStatus.PENDING.toString().equals(api.getWorkflowStatus())) {
            String message = "Pending state transition for api :" + api.getName();
            log.error(message);
            throw new APIManagementException(message, ExceptionCodes.WORKFLOW_PENDING);
        } else {
            throw new APIMgtResourceNotFoundException("Requested API " + apiId + " Not Available");
        }
    } catch (APIMgtDAOException e) {
        String errorMsg = "Couldn't change the status of api ID " + apiId;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    } catch (LifecycleException e) {
        String errorMsg = "Couldn't change the status of api ID " + apiId;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.APIMGT_LIFECYCLE_EXCEPTION);
    }
    return workflowResponse;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) LifecycleException(org.wso2.carbon.lcm.core.exception.LifecycleException) APIStateChangeWorkflow(org.wso2.carbon.apimgt.core.workflow.APIStateChangeWorkflow) LifecycleState(org.wso2.carbon.lcm.core.impl.LifecycleState) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) API(org.wso2.carbon.apimgt.core.models.API) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with WorkflowExecutor

use of org.wso2.carbon.apimgt.core.api.WorkflowExecutor in project carbon-apimgt by wso2.

the class APIStoreImpl method cleanupPendingTaskForApplicationDeletion.

private void cleanupPendingTaskForApplicationDeletion(Application application) throws APIManagementException {
    WorkflowExecutor createApplicationWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION);
    WorkflowExecutor createSubscriptionWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION);
    WorkflowExecutor updateApplicationWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_APPLICATION_UPDATE);
    String appId = application.getId();
    // get subscriptions with pending status
    List<Subscription> pendingSubscriptions = getApiSubscriptionDAO().getPendingAPISubscriptionsByApplication(appId);
    String applicationStatus = application.getStatus();
    if (pendingSubscriptions == null || pendingSubscriptions.isEmpty()) {
        // check whether application is on hold state
        if (ApplicationStatus.APPLICATION_ONHOLD.equals(applicationStatus)) {
            // delete pending tasks for application creation if any
            cleanupPendingTask(createApplicationWFExecutor, appId, WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION);
        }
    } else {
        // approvals (cannot subscribe to a pending application)
        for (Iterator iterator = pendingSubscriptions.iterator(); iterator.hasNext(); ) {
            Subscription pendingSubscription = (Subscription) iterator.next();
            // delete pending tasks for subscripton creation if any
            cleanupPendingTask(createSubscriptionWFExecutor, pendingSubscription.getId(), WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION);
        }
    }
    // delete pending tasks for application update if any
    cleanupPendingTask(updateApplicationWFExecutor, appId, WorkflowConstants.WF_TYPE_AM_APPLICATION_UPDATE);
}
Also used : Iterator(java.util.Iterator) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) Subscription(org.wso2.carbon.apimgt.core.models.Subscription)

Example 8 with WorkflowExecutor

use of org.wso2.carbon.apimgt.core.api.WorkflowExecutor in project carbon-apimgt by wso2.

the class APIStoreImpl method deleteApplication.

/**
 * @see APIStore#deleteApplication(String)
 */
@Override
public WorkflowResponse deleteApplication(String appId) throws APIManagementException {
    try {
        if (appId == null) {
            String message = "Application Id is not provided";
            throw new APIManagementException(message, ExceptionCodes.PARAMETER_NOT_PROVIDED);
        }
        // get app info
        Application application = getApplicationDAO().getApplication(appId);
        if (application == null) {
            String message = "Application cannot be found for id :" + appId;
            throw new APIManagementException(message, ExceptionCodes.APPLICATION_NOT_FOUND);
        }
        // delete application creation pending tasks
        cleanupPendingTaskForApplicationDeletion(application);
        WorkflowExecutor removeApplicationWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_APPLICATION_DELETION);
        ApplicationDeletionWorkflow workflow = new ApplicationDeletionWorkflow(getApplicationDAO(), getWorkflowDAO(), getApiGateway());
        workflow.setApplication(application);
        workflow.setWorkflowType(APIMgtConstants.WorkflowConstants.WF_TYPE_AM_APPLICATION_DELETION);
        workflow.setWorkflowReference(application.getId());
        workflow.setExternalWorkflowReference(UUID.randomUUID().toString());
        workflow.setCreatedTime(LocalDateTime.now());
        String workflowDescription = "Application [ " + application.getName() + " ] deletion request from  - " + application.getName();
        workflow.setWorkflowDescription(workflowDescription);
        WorkflowResponse response = removeApplicationWFExecutor.execute(workflow);
        workflow.setStatus(response.getWorkflowStatus());
        if (WorkflowStatus.CREATED != response.getWorkflowStatus()) {
            completeWorkflow(removeApplicationWFExecutor, workflow);
        } else {
            // add entry to workflow table if it is only in pending state
            addWorkflowEntries(workflow);
        }
        return response;
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while deleting the application - " + appId;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) Application(org.wso2.carbon.apimgt.core.models.Application) ApplicationDeletionWorkflow(org.wso2.carbon.apimgt.core.workflow.ApplicationDeletionWorkflow)

Example 9 with WorkflowExecutor

use of org.wso2.carbon.apimgt.core.api.WorkflowExecutor in project carbon-apimgt by wso2.

the class APIStoreImpl method addApiSubscription.

@Override
public SubscriptionResponse addApiSubscription(String apiId, String applicationId, String tier) throws APIManagementException {
    SubscriptionResponse subScriptionResponse;
    // Generate UUID for application
    String subscriptionId = UUID.randomUUID().toString();
    try {
        API api = getAPIbyUUID(apiId);
        Application application = getApplicationByUuid(applicationId);
        if (application == null) {
            String errorMsg = "Cannot find an application for given applicationId - " + applicationId;
            log.error(errorMsg);
            throw new APIManagementException(errorMsg, ExceptionCodes.APPLICATION_NOT_FOUND);
        }
        Policy policy = getPolicyDAO().getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, tier);
        if (policy == null) {
            String errorMsg = "Cannot find an subscription policy for given policy name - " + tier;
            log.error(errorMsg);
            throw new APIManagementException(errorMsg, ExceptionCodes.POLICY_NOT_FOUND);
        }
        getApiSubscriptionDAO().addAPISubscription(subscriptionId, apiId, applicationId, policy.getUuid(), APIMgtConstants.SubscriptionStatus.ON_HOLD);
        WorkflowExecutor addSubscriptionWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION);
        // Instead of quering the db, we create same subscription object
        Subscription subscription = new Subscription(subscriptionId, application, api, policy);
        subscription.setStatus(APIMgtConstants.SubscriptionStatus.ON_HOLD);
        SubscriptionCreationWorkflow workflow = new SubscriptionCreationWorkflow(getApiSubscriptionDAO(), getWorkflowDAO(), getApiGateway());
        workflow.setCreatedTime(LocalDateTime.now());
        workflow.setExternalWorkflowReference(UUID.randomUUID().toString());
        workflow.setWorkflowReference(subscriptionId);
        workflow.setWorkflowType(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION);
        workflow.setSubscription(subscription);
        workflow.setSubscriber(getUsername());
        String workflowDescription = "API [ " + subscription.getApi().getName() + " - " + subscription.getApi().getVersion() + " ] subscription creation request from subscriber - " + getUsername() + "  for the application - " + subscription.getApplication().getName() + "";
        workflow.setWorkflowDescription(workflowDescription);
        WorkflowResponse response = addSubscriptionWFExecutor.execute(workflow);
        workflow.setStatus(response.getWorkflowStatus());
        if (WorkflowStatus.CREATED != response.getWorkflowStatus()) {
            completeWorkflow(addSubscriptionWFExecutor, workflow);
        } else {
            // only add entry to workflow table if it is a pending task
            addWorkflowEntries(workflow);
        }
        subScriptionResponse = new SubscriptionResponse(subscriptionId, response);
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while adding api subscription for api - " + apiId;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    }
    return subScriptionResponse;
}
Also used : Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) SubscriptionCreationWorkflow(org.wso2.carbon.apimgt.core.workflow.SubscriptionCreationWorkflow) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) SubscriptionResponse(org.wso2.carbon.apimgt.core.models.SubscriptionResponse) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) Application(org.wso2.carbon.apimgt.core.models.Application)

Example 10 with WorkflowExecutor

use of org.wso2.carbon.apimgt.core.api.WorkflowExecutor in project carbon-apimgt by wso2.

the class APIStoreImpl method addApplication.

@Override
public ApplicationCreationResponse addApplication(Application application) throws APIManagementException {
    ApplicationCreationResponse applicationResponse = null;
    try {
        if (getApplicationDAO().isApplicationNameExists(application.getName())) {
            String message = "An application already exists with a duplicate name - " + application.getName();
            log.error(message);
            throw new APIMgtResourceAlreadyExistsException(message, ExceptionCodes.APPLICATION_ALREADY_EXISTS);
        }
        // Tier validation
        Policy tier = application.getPolicy();
        if (tier == null) {
            String message = "Tier name cannot be null - " + application.getName();
            log.error(message);
            throw new APIManagementException(message, ExceptionCodes.TIER_CANNOT_BE_NULL);
        } else {
            Policy policy = getPolicyDAO().getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, tier.getPolicyName());
            if (policy == null) {
                String message = "Specified tier " + tier.getPolicyName() + " is invalid";
                log.error(message);
                throw new APIManagementException(message, ExceptionCodes.TIER_CANNOT_BE_NULL);
            }
            application.setPolicy(policy);
        }
        // Generate UUID for application
        String generatedUuid = UUID.randomUUID().toString();
        application.setId(generatedUuid);
        String permissionString = application.getPermissionString();
        if (permissionString != null && !("").equals(permissionString)) {
            HashMap roleNamePermissionList;
            roleNamePermissionList = getAPIPermissionArray(permissionString);
            application.setPermissionMap(roleNamePermissionList);
        }
        application.setCreatedTime(LocalDateTime.now());
        getApplicationDAO().addApplication(application);
        WorkflowExecutor appCreationWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION);
        ApplicationCreationWorkflow workflow = new ApplicationCreationWorkflow(getApplicationDAO(), getWorkflowDAO(), getApiGateway());
        workflow.setApplication(application);
        workflow.setCreatedBy(getUsername());
        workflow.setWorkflowReference(application.getId());
        workflow.setExternalWorkflowReference(UUID.randomUUID().toString());
        workflow.setCreatedTime(LocalDateTime.now());
        String workflowDescription = "Application [ " + application.getName() + " ] creation request from application creator - " + getUsername() + " with throttling tier - " + tier.getPolicyName() + "";
        workflow.setWorkflowDescription(workflowDescription);
        WorkflowResponse response = appCreationWFExecutor.execute(workflow);
        workflow.setStatus(response.getWorkflowStatus());
        if (WorkflowStatus.CREATED != response.getWorkflowStatus()) {
            completeWorkflow(appCreationWFExecutor, workflow);
        } else {
            getApplicationDAO().updateApplicationState(generatedUuid, APIMgtConstants.ApplicationStatus.APPLICATION_ONHOLD);
            addWorkflowEntries(workflow);
        }
        APIUtils.logDebug("successfully added application with appId " + application.getId(), log);
        applicationResponse = new ApplicationCreationResponse(application.getId(), response);
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while creating the application - " + application.getName();
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    } catch (ParseException e) {
        String errorMsg = "Error occurred while parsing the permission json from swagger in application - " + application.getName();
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.SWAGGER_PARSE_EXCEPTION);
    } catch (WorkflowException e) {
        String errorMsg = "Error occurred in workflow";
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.WORKFLOW_EXCEPTION);
    }
    return applicationResponse;
}
Also used : Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) ApplicationCreationResponse(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) WorkflowException(org.wso2.carbon.apimgt.core.exception.WorkflowException) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceAlreadyExistsException) ApplicationCreationWorkflow(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationWorkflow) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) ParseException(org.json.simple.parser.ParseException)

Aggregations

WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)15 WorkflowExecutor (org.wso2.carbon.apimgt.core.api.WorkflowExecutor)13 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)8 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)6 Application (org.wso2.carbon.apimgt.core.models.Application)4 HashMap (java.util.HashMap)3 Test (org.testng.annotations.Test)3 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)3 WorkflowDAO (org.wso2.carbon.apimgt.core.dao.WorkflowDAO)3 API (org.wso2.carbon.apimgt.core.models.API)3 Subscription (org.wso2.carbon.apimgt.core.models.Subscription)3 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)3 Iterator (java.util.Iterator)2 Map (java.util.Map)2 APISubscriptionDAO (org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO)2 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException)2 GatewayException (org.wso2.carbon.apimgt.core.exception.GatewayException)2 WorkflowException (org.wso2.carbon.apimgt.core.exception.WorkflowException)2 SubscriptionValidationData (org.wso2.carbon.apimgt.core.models.SubscriptionValidationData)2 WorkflowConfigProperties (org.wso2.carbon.apimgt.core.models.WorkflowConfigProperties)2