use of org.wso2.carbon.apimgt.core.api.WorkflowExecutor in project carbon-apimgt by wso2.
the class APIStoreImpl method updateApplication.
@Override
public WorkflowResponse updateApplication(String uuid, Application application) throws APIManagementException {
try {
// get old app
Application existingApplication = getApplicationDAO().getApplication(uuid);
if (existingApplication != null) {
WorkflowExecutor executor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_APPLICATION_UPDATE);
ApplicationUpdateWorkflow workflow = new ApplicationUpdateWorkflow(getApplicationDAO(), getWorkflowDAO(), getApiGateway());
application.setId(uuid);
application.setUpdatedUser(getUsername());
application.setUpdatedTime(LocalDateTime.now());
Policy appTier = application.getPolicy();
if (appTier != null && !appTier.getPolicyName().equals(existingApplication.getPolicy().getPolicyName())) {
Policy policy = getPolicyDAO().getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, appTier.getPolicyName());
if (policy == null) {
String message = "Specified tier " + appTier + " is invalid";
log.error(message);
throw new APIManagementException(message, ExceptionCodes.TIER_NAME_INVALID);
}
application.setPolicy(policy);
}
workflow.setExistingApplication(existingApplication);
workflow.setUpdatedApplication(application);
workflow.setCreatedBy(getUsername());
workflow.setWorkflowReference(application.getId());
workflow.setExternalWorkflowReference(UUID.randomUUID().toString());
workflow.setCreatedTime(LocalDateTime.now());
String workflowDescription = "Update application " + existingApplication.getName() + " with tier " + existingApplication.getPolicy().getPolicyName() + " and description \'" + existingApplication.getDescription() + "\' To " + application.getName() + " with tier " + application.getPolicy().getPolicyName() + " and description \'" + application.getDescription() + "\' by " + getUsername();
workflow.setWorkflowDescription(workflowDescription);
// 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 for external wf approval
// scenarios. this won't get stored for simple wfs
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_NAME, application.getName());
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_UPDATEDBY, application.getUpdatedUser());
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_TIER, application.getPolicy().getPolicyName());
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_POLICY_ID, application.getPolicy().getUuid());
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_DESCRIPTION, application.getDescription());
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_PERMISSION, application.getPermissionString());
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_EXISTIN_APP_STATUS, existingApplication.getStatus());
WorkflowResponse response = executor.execute(workflow);
workflow.setStatus(response.getWorkflowStatus());
if (WorkflowStatus.CREATED != response.getWorkflowStatus()) {
completeWorkflow(executor, workflow);
} else {
getApplicationDAO().updateApplicationState(uuid, ApplicationStatus.APPLICATION_ONHOLD);
addWorkflowEntries(workflow);
}
return response;
} else {
String errorMsg = "Applicaiton does not exist - " + uuid;
log.error(errorMsg);
throw new APIManagementException(errorMsg, ExceptionCodes.APPLICATION_NOT_FOUND);
}
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while updating the application - " + uuid;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
}
}
use of org.wso2.carbon.apimgt.core.api.WorkflowExecutor in project carbon-apimgt by wso2.
the class APIStoreImplTestCase method testCompleteApplicationWorkflowWithoutReference.
// End of exception testing
@Test(description = "Exception when completing application creation workflow without a reference", expectedExceptions = APIManagementException.class)
public void testCompleteApplicationWorkflowWithoutReference() throws Exception {
WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
ApplicationDAO applicationDAO = Mockito.mock(ApplicationDAO.class);
APIStore apiStore = getApiStoreImpl(applicationDAO, workflowDAO);
APIGateway apiGateway = Mockito.mock(APIGateway.class);
WorkflowExecutor executor = new DefaultWorkflowExecutor();
Workflow workflow = new ApplicationCreationWorkflow(applicationDAO, workflowDAO, apiGateway);
workflow.setWorkflowReference(null);
apiStore.completeWorkflow(executor, workflow);
}
use of org.wso2.carbon.apimgt.core.api.WorkflowExecutor in project carbon-apimgt by wso2.
the class DefaultWorkflowExecutorTestCase method testWorkflowResponses.
@Test(description = "Test workflow responses")
public void testWorkflowResponses() throws WorkflowException {
WorkflowExecutor executor = new DefaultWorkflowExecutor();
APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
APIGateway apiGateway = Mockito.mock(APIGateway.class);
Workflow workflow = new SubscriptionCreationWorkflow(apiSubscriptionDAO, workflowDAO, apiGateway);
WorkflowResponse response = executor.execute(workflow);
Assert.assertEquals(response.getJSONPayload(), "");
Assert.assertEquals(response.getWorkflowStatus(), WorkflowStatus.APPROVED);
workflow.setStatus(WorkflowStatus.APPROVED);
response = executor.complete(workflow);
Assert.assertEquals(response.getWorkflowStatus(), WorkflowStatus.APPROVED);
executor.cleanUpPendingTask(workflow.getExternalWorkflowReference());
}
use of org.wso2.carbon.apimgt.core.api.WorkflowExecutor in project carbon-apimgt by wso2.
the class APIStateChangeWorkflow method completeWorkflow.
@Override
public WorkflowResponse completeWorkflow(WorkflowExecutor workflowExecutor) throws APIManagementException {
WorkflowResponse response = workflowExecutor.complete(this);
setStatus(response.getWorkflowStatus());
if (WorkflowStatus.APPROVED == response.getWorkflowStatus()) {
if (log.isDebugEnabled()) {
log.debug("API state change workflow complete: Approved");
}
String invoker = getAttribute(APIMgtConstants.WorkflowConstants.ATTRIBUTE_API_LC_INVOKER);
String currentState = getAttribute(APIMgtConstants.WorkflowConstants.ATTRIBUTE_API_CUR_STATE);
String targetState = getAttribute(APIMgtConstants.WorkflowConstants.ATTRIBUTE_API_TARGET_STATE);
boolean hasOwnGateway = Boolean.valueOf(getAttribute(APIMgtConstants.WorkflowConstants.ATTRIBUTE_HAS_OWN_GATEWAY));
String label = getAttribute(APIMgtConstants.WorkflowConstants.ATTRIBUTE_API_AUTOGEN_LABEL);
if (hasOwnGateway) {
// (CREATED to DEPRECATED)
if ((currentState.equalsIgnoreCase(APIStatus.CREATED.getStatus()) || currentState.equalsIgnoreCase(APIStatus.MAINTENANCE.getStatus()) || currentState.equalsIgnoreCase(APIStatus.PROTOTYPED.getStatus())) && (targetState.equalsIgnoreCase(APIStatus.PUBLISHED.getStatus()) || targetState.equalsIgnoreCase(APIStatus.PROTOTYPED.getStatus()) || targetState.equalsIgnoreCase(APIStatus.DEPRECATED.getStatus()))) {
try {
// No need to auto-generate the label again As hasOwnGateway is true.
// create the gateway
API api = apiDAO.getAPI(getWorkflowReference());
apiGateway.createContainerBasedGateway(label, api);
} catch (ContainerBasedGatewayException e) {
// Revert already added labels
DedicatedGateway dedicatedGateway = new DedicatedGateway();
dedicatedGateway.setEnabled(false);
dedicatedGateway.setApiId(getWorkflowReference());
dedicatedGateway.setUpdatedBy(invoker);
List<String> labels = new ArrayList<>();
labels.add(labelDAO.getLabelIdByNameAndType(APIMgtConstants.DEFAULT_LABEL_NAME, APIMgtConstants.LABEL_TYPE_GATEWAY));
labels.add(labelDAO.getLabelIdByNameAndType(APIMgtConstants.DEFAULT_LABEL_NAME, APIMgtConstants.LABEL_TYPE_STORE));
apiDAO.updateDedicatedGateway(dedicatedGateway, labels);
throw new APIManagementException("Error while updating lifecycle state in Private Jet Mode", e, ExceptionCodes.DEDICATED_CONTAINER_GATEWAY_CREATION_FAILED);
}
}
}
String localTime = getAttribute(APIMgtConstants.WorkflowConstants.ATTRIBUTE_API_LAST_UPTIME);
LocalDateTime time = LocalDateTime.parse(localTime);
updateAPIStatusForWorkflowComplete(getWorkflowReference(), targetState, invoker, time);
// After publishing the state change to the Gateway, remove the gateway for following occasions.
if (hasOwnGateway) {
if ((currentState.equalsIgnoreCase(APIStatus.PUBLISHED.getStatus()) || currentState.equalsIgnoreCase(APIStatus.PROTOTYPED.getStatus()) || currentState.equalsIgnoreCase(APIStatus.DEPRECATED.getStatus())) && (targetState.equalsIgnoreCase(APIStatus.CREATED.getStatus()) || targetState.equalsIgnoreCase(APIStatus.MAINTENANCE.getStatus()) || targetState.equalsIgnoreCase(APIStatus.PROTOTYPED.getStatus())) || targetState.equalsIgnoreCase(APIStatus.RETIRED.getStatus())) {
// remove gateway
API api = apiDAO.getAPI(getWorkflowReference());
apiGateway.removeContainerBasedGateway(label, api);
}
}
} else if (WorkflowStatus.REJECTED == response.getWorkflowStatus()) {
if (log.isDebugEnabled()) {
log.debug("API state change workflow complete: Rejected");
}
apiDAO.updateAPIWorkflowStatus(getWorkflowReference(), APIMgtConstants.APILCWorkflowStatus.REJECTED);
}
updateWorkflowEntries(this);
return response;
}
use of org.wso2.carbon.apimgt.core.api.WorkflowExecutor in project carbon-apimgt by wso2.
the class ApplicationCreationWorkflow method completeWorkflow.
public WorkflowResponse completeWorkflow(WorkflowExecutor workflowExecutor) throws APIManagementException {
if (application == null) {
// this is when complete method is executed through workflow rest api
application = applicationDAO.getApplication(getWorkflowReference());
}
WorkflowResponse response = workflowExecutor.complete(this);
// setting the workflow status from the one getting from the executor. this gives the executor developer
// to change the state as well.
setStatus(response.getWorkflowStatus());
String applicationState = "";
if (WorkflowStatus.APPROVED == response.getWorkflowStatus()) {
if (log.isDebugEnabled()) {
log.debug("Application Creation workflow complete: Approved");
}
getApiGateway().addApplication(application);
applicationState = APIMgtConstants.ApplicationStatus.APPLICATION_APPROVED;
} else if (WorkflowStatus.REJECTED == response.getWorkflowStatus()) {
if (log.isDebugEnabled()) {
log.debug("Application Creation workflow complete: Rejected");
}
applicationState = APIMgtConstants.ApplicationStatus.APPLICATION_REJECTED;
}
applicationDAO.updateApplicationState(getWorkflowReference(), applicationState);
updateWorkflowEntries(this);
return response;
}
Aggregations