use of org.wso2.carbon.apimgt.core.api.WorkflowExecutor in project carbon-apimgt by wso2.
the class SubscriptionDeletionWorkflow method completeWorkflow.
public WorkflowResponse completeWorkflow(WorkflowExecutor workflowExecutor) throws APIManagementException {
if (subscription == null) {
// this is when complete method is executed through workflow rest api
this.subscription = apiSubscriptionDAO.getAPISubscription(getWorkflowReference());
}
WorkflowResponse response = workflowExecutor.complete(this);
setStatus(response.getWorkflowStatus());
List<SubscriptionValidationData> subscriptionValidationDataList = null;
if (WorkflowStatus.APPROVED == response.getWorkflowStatus()) {
if (log.isDebugEnabled()) {
log.debug("Subscription deletion workflow complete: Approved");
}
if (subscription.getApi() != null && subscription.getApplication() != null) {
subscriptionValidationDataList = apiSubscriptionDAO.getAPISubscriptionsOfAPIForValidation(subscription.getApi().getContext(), subscription.getApi().getVersion(), subscription.getApplication().getId());
}
apiSubscriptionDAO.deleteAPISubscription(getWorkflowReference());
} else if (WorkflowStatus.REJECTED == response.getWorkflowStatus()) {
if (log.isDebugEnabled()) {
log.debug("Subscription deletion workflow complete: Rejected");
}
}
updateWorkflowEntries(this);
if (WorkflowStatus.APPROVED == response.getWorkflowStatus()) {
if (subscriptionValidationDataList != null && !subscriptionValidationDataList.isEmpty()) {
apiGateway.deleteAPISubscription(subscriptionValidationDataList);
if (log.isDebugEnabled()) {
log.debug("Subscription for API : " + subscription.getApi().getName() + " with " + "application : " + subscription.getApplication().getName() + " has been successfully " + "removed from gateway");
}
}
}
return response;
}
use of org.wso2.carbon.apimgt.core.api.WorkflowExecutor in project carbon-apimgt by wso2.
the class WorkflowConfigHolder method loadWorkflowConfigurations.
private void loadWorkflowConfigurations(WorkflowExecutorInfo workflowConfig, String workflowExecutorType) throws ClassNotFoundException, IllegalAccessException, InstantiationException, WorkflowException {
String executorClass = workflowConfig.getExecutor();
Class clazz = WorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
WorkflowExecutor workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
List<WorkflowConfigProperties> properties = workflowConfig.getProperty();
if (properties != null) {
loadProperties(properties, workFlowExecutor);
}
workflowExecutorMap.put(workflowExecutorType, workFlowExecutor);
}
use of org.wso2.carbon.apimgt.core.api.WorkflowExecutor in project carbon-apimgt by wso2.
the class WorkflowsApiServiceImpl method workflowsWorkflowReferenceIdPut.
@Override
public Response workflowsWorkflowReferenceIdPut(String workflowReferenceId, WorkflowRequestDTO body, Request request) throws NotFoundException {
try {
APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
Workflow workflow = apiMgtAdminService.retrieveWorkflow(workflowReferenceId);
if (workflow == null) {
String errorMessage = "Workflow entry not found for: " + workflowReferenceId;
APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.WORKFLOW_NOT_FOUND);
Map<String, String> paramList = new HashMap<>();
paramList.put(APIMgtConstants.ExceptionsConstants.WORKFLOW_REF_ID, workflowReferenceId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
} else {
if (WorkflowStatus.APPROVED == workflow.getStatus()) {
String errorMessage = "Workflow is already in complete state";
APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.WORKFLOW_ALREADY_COMPLETED);
Map<String, String> paramList = new HashMap<>();
paramList.put(APIMgtConstants.ExceptionsConstants.WORKFLOW_REF_ID, workflowReferenceId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
} else {
WorkflowExecutor workflowExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(workflow.getWorkflowType());
if (body == null) {
RestApiUtil.handleBadRequest("Request payload is missing", log);
}
if (body.getDescription() != null) {
workflow.setWorkflowDescription(body.getDescription());
}
if (body.getStatus() == null) {
String errorMessage = "Workflow status is not defined";
APIManagementException e = new APIManagementException(errorMessage, ExceptionCodes.WORKFLOW_STATE_MISSING);
Map<String, String> paramList = new HashMap<>();
paramList.put(APIMgtConstants.ExceptionsConstants.WORKFLOW_REF_ID, workflowReferenceId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
} else {
workflow.setStatus(WorkflowStatus.valueOf(body.getStatus().toString()));
}
if (body.getAttributes() != null) {
Map<String, String> existingAttributs = workflow.getAttributes();
Map<String, String> newAttributes = body.getAttributes();
if (existingAttributs == null) {
workflow.setAttributes(newAttributes);
} else {
newAttributes.forEach(existingAttributs::putIfAbsent);
workflow.setAttributes(existingAttributs);
}
}
WorkflowResponse response = apiMgtAdminService.completeWorkflow(workflowExecutor, workflow);
WorkflowResponseDTO workflowResponseDTO = WorkflowMappingUtil.toWorkflowResponseDTO(response);
return Response.ok().entity(workflowResponseDTO).build();
}
}
} catch (APIManagementException e) {
String errorMessage = "Error while completing workflow for reference : " + workflowReferenceId + ". " + e.getMessage();
Map<String, String> paramList = new HashMap<>();
paramList.put(APIMgtConstants.ExceptionsConstants.WORKFLOW_REF_ID, workflowReferenceId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.core.api.WorkflowExecutor in project carbon-apimgt by wso2.
the class APIPublisherImpl method cleanupPendingTaskForAPIStateChange.
private void cleanupPendingTaskForAPIStateChange(String apiId) throws APIManagementException {
Optional<String> workflowExtRef = getWorkflowDAO().getExternalWorkflowReferenceForPendingTask(apiId, WorkflowConstants.WF_TYPE_AM_API_STATE);
if (workflowExtRef.isPresent()) {
WorkflowExecutor executor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_API_STATE);
try {
executor.cleanUpPendingTask(workflowExtRef.get());
} catch (WorkflowException e) {
String warn = "Failed to clean pending api state change task for " + apiId;
// failed cleanup processes are ignored to prevent failing the deletion process
log.warn(warn, e.getLocalizedMessage());
}
getWorkflowDAO().deleteWorkflowEntryforExternalReference(workflowExtRef.get());
}
}
use of org.wso2.carbon.apimgt.core.api.WorkflowExecutor in project carbon-apimgt by wso2.
the class APIStoreImpl method deleteAPISubscription.
/**
* @see APIStore#deleteAPISubscription(String)
*/
@Override
public WorkflowResponse deleteAPISubscription(String subscriptionId) throws APIManagementException {
try {
WorkflowExecutor removeSubscriptionWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_DELETION);
// check for pending subscription creation
if (subscriptionId == null) {
String errorMsg = "Subscription Id is not provided";
log.error(errorMsg);
throw new APIManagementException(errorMsg, ExceptionCodes.PARAMETER_NOT_PROVIDED);
}
Subscription subscription = getApiSubscriptionDAO().getAPISubscription(subscriptionId);
if (subscription == null) {
String errorMsg = "Subscription not found for the id - " + subscriptionId;
log.error(errorMsg);
throw new APIManagementException(errorMsg, ExceptionCodes.SUBSCRIPTION_NOT_FOUND);
} else {
// remove pending tasks for subscription creation first
cleanupPendingTaskForSubscriptionDeletion(subscription);
SubscriptionDeletionWorkflow workflow = new SubscriptionDeletionWorkflow(getApiSubscriptionDAO(), getWorkflowDAO(), getApiGateway());
workflow.setWorkflowReference(subscriptionId);
workflow.setSubscription(subscription);
workflow.setWorkflowType(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_DELETION);
workflow.setStatus(WorkflowStatus.CREATED);
workflow.setCreatedTime(LocalDateTime.now());
workflow.setExternalWorkflowReference(UUID.randomUUID().toString());
workflow.setSubscriber(getUsername());
String workflowDescription = "API [ " + subscription.getApi().getName() + " - " + subscription.getApi().getVersion() + " ] subscription deletion request from subscriber - " + getUsername() + " for the application - " + subscription.getApplication().getName() + "";
workflow.setWorkflowDescription(workflowDescription);
WorkflowResponse response = removeSubscriptionWFExecutor.execute(workflow);
workflow.setStatus(response.getWorkflowStatus());
if (WorkflowStatus.CREATED != response.getWorkflowStatus()) {
completeWorkflow(removeSubscriptionWFExecutor, workflow);
} else {
// add entry to workflow table if it is only in pending state
// haven't changed the subscription's state to allow to use it till approval
addWorkflowEntries(workflow);
}
return response;
}
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while deleting api subscription - " + subscriptionId;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
}
}
Aggregations