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());
}
}
use of org.wso2.carbon.apimgt.core.api.WorkflowExecutor in project carbon-apimgt by wso2.
the class KeyManagerUserOperationListener method doPreDeleteUser.
/**
* Deleting user from the identity database prerequisites. Remove pending approval requests for the user and remove
* the gateway key cache.
*/
@Override
public boolean doPreDeleteUser(String username, UserStoreManager userStoreManager) {
boolean isTenantFlowStarted = false;
ApiMgtDAO apiMgtDAO = getDAOInstance();
try {
String tenantDomain = getTenantDomain();
int tenantId = getTenantId();
Tenant tenant = getTenant(tenantId);
if (tenant == null && MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
tenant = new org.wso2.carbon.user.core.tenant.Tenant();
tenant.setDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
tenant.setId(MultitenantConstants.SUPER_TENANT_ID);
} else {
// Add tenant domain to the username if user is not from super tenant domain.
// When adding a user, tenant domain is appended to workflow reference only if that user
// is from a different tenant domain(not carbon.super).
username = UserCoreUtil.addTenantDomainToEntry(username, tenantDomain);
}
Map<String, String> userStoreProperties = userStoreManager.getProperties(tenant);
String userDomain = userStoreProperties.get(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME);
// therefore domain name should be manually added to the username if domain is PRIMARY
if (UserCoreConstants.PRIMARY_DEFAULT_DOMAIN_NAME.equals(userDomain)) {
username = userDomain.toUpperCase() + UserCoreConstants.DOMAIN_SEPARATOR + username;
} else {
username = IdentityUtil.addDomainToName(username, userDomain);
}
WorkflowExecutor userSignupWFExecutor = getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_USER_SIGNUP);
String workflowExtRef = apiMgtDAO.getExternalWorkflowReferenceForUserSignup(username);
userSignupWFExecutor.cleanUpPendingTask(workflowExtRef);
} catch (WorkflowException | APIManagementException | UserStoreException e) {
// exception is not thrown to the caller since this is a event Identity(IS) listener
log.error("Error while cleaning up workflow task for the user: " + username, e);
}
APIUtil.clearRoleCache(getUserName(username, userStoreManager));
return true;
}
use of org.wso2.carbon.apimgt.core.api.WorkflowExecutor in project carbon-apimgt by wso2.
the class APIProviderImpl method cleanUpPendingAPIStateChangeTask.
private void cleanUpPendingAPIStateChangeTask(int apiId, boolean isAPIProduct) throws WorkflowException, APIManagementException {
// Run cleanup task for workflow
WorkflowExecutor apiStateChangeWFExecutor;
WorkflowDTO wfDTO;
if (isAPIProduct) {
apiStateChangeWFExecutor = getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE);
wfDTO = apiMgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(apiId), WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE);
} else {
apiStateChangeWFExecutor = getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_API_STATE);
wfDTO = apiMgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(apiId), WorkflowConstants.WF_TYPE_AM_API_STATE);
}
if (wfDTO != null && WorkflowStatus.CREATED == wfDTO.getStatus()) {
apiStateChangeWFExecutor.cleanUpPendingTask(wfDTO.getExternalWorkflowReference());
}
}
use of org.wso2.carbon.apimgt.core.api.WorkflowExecutor in project carbon-apimgt by wso2.
the class APIProviderImpl method executeStateChangeWorkflow.
/**
* Execute state change workflow
*
* @param currentStatus Current Status of the API or API Product
* @param action LC state change action
* @param apiName Name of API or API Product
* @param apiContext Context of API or API Product
* @param apiType API Type
* @param apiVersion Version of API or API Product
* @param providerName Provider of API or API Product
* @param apiOrApiProductId Unique ID API or API Product
* @param uuid UUID of the API or API Product
* @param gatewayVendor Gateway vendor
* @param workflowType Workflow Type
* @return APIStateChangeResponse
* @throws APIManagementException Error when executing the state change workflow
*/
private APIStateChangeResponse executeStateChangeWorkflow(String currentStatus, String action, String apiName, String apiContext, String apiType, String apiVersion, String providerName, int apiOrApiProductId, String uuid, String gatewayVendor, String workflowType) throws APIManagementException {
APIStateChangeResponse response = new APIStateChangeResponse();
try {
WorkflowExecutor apiStateWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(workflowType);
APIStateWorkflowDTO apiStateWorkflow = setAPIStateWorkflowDTOParameters(currentStatus, action, apiName, apiContext, apiType, apiVersion, providerName, apiOrApiProductId, uuid, gatewayVendor, workflowType, apiStateWFExecutor);
WorkflowResponse workflowResponse = apiStateWFExecutor.execute(apiStateWorkflow);
response.setWorkflowResponse(workflowResponse);
} catch (WorkflowException e) {
handleException("Failed to execute workflow for life cycle status change : " + e.getMessage(), e);
}
return response;
}
use of org.wso2.carbon.apimgt.core.api.WorkflowExecutor in project carbon-apimgt by wso2.
the class APIProviderImpl method changeLifeCycleStatus.
public APIStateChangeResponse changeLifeCycleStatus(APIIdentifier apiIdentifier, String action, String organization) throws APIManagementException, FaultGatewaysException {
APIStateChangeResponse response = new APIStateChangeResponse();
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(this.username);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(this.tenantDomain, true);
GenericArtifact apiArtifact = getAPIArtifact(apiIdentifier);
String targetStatus;
if (apiArtifact != null) {
String providerName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
String apiName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
String apiContext = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_CONTEXT);
String apiType = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_TYPE);
String apiVersion = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
String currentStatus = apiArtifact.getLifecycleState();
String uuid = apiMgtDAO.getUUIDFromIdentifier(apiIdentifier, organization);
String gatewayVendor = apiMgtDAO.getGatewayVendorByAPIUUID(uuid);
int apiId = apiMgtDAO.getAPIID(uuid);
WorkflowStatus apiWFState = null;
WorkflowDTO wfDTO = apiMgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(apiId), WorkflowConstants.WF_TYPE_AM_API_STATE);
if (wfDTO != null) {
apiWFState = wfDTO.getStatus();
}
// if the workflow has started, then executor should not fire again
if (!WorkflowStatus.CREATED.equals(apiWFState)) {
try {
WorkflowProperties workflowProperties = getAPIManagerConfiguration().getWorkflowProperties();
WorkflowExecutor apiStateWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_API_STATE);
APIStateWorkflowDTO apiStateWorkflow = new APIStateWorkflowDTO();
apiStateWorkflow.setApiCurrentState(currentStatus);
apiStateWorkflow.setApiLCAction(action);
apiStateWorkflow.setApiName(apiName);
apiStateWorkflow.setApiContext(apiContext);
apiStateWorkflow.setApiType(apiType);
apiStateWorkflow.setApiVersion(apiVersion);
apiStateWorkflow.setApiProvider(providerName);
apiStateWorkflow.setGatewayVendor(gatewayVendor);
apiStateWorkflow.setCallbackUrl(workflowProperties.getWorkflowCallbackAPI());
apiStateWorkflow.setExternalWorkflowReference(apiStateWFExecutor.generateUUID());
apiStateWorkflow.setTenantId(tenantId);
apiStateWorkflow.setTenantDomain(this.tenantDomain);
apiStateWorkflow.setWorkflowType(WorkflowConstants.WF_TYPE_AM_API_STATE);
apiStateWorkflow.setStatus(WorkflowStatus.CREATED);
apiStateWorkflow.setCreatedTime(System.currentTimeMillis());
apiStateWorkflow.setWorkflowReference(Integer.toString(apiId));
apiStateWorkflow.setInvoker(this.username);
apiStateWorkflow.setApiUUID(uuid);
String workflowDescription = "Pending lifecycle state change action: " + action;
apiStateWorkflow.setWorkflowDescription(workflowDescription);
WorkflowResponse workflowResponse = apiStateWFExecutor.execute(apiStateWorkflow);
response.setWorkflowResponse(workflowResponse);
} catch (WorkflowException e) {
handleException("Failed to execute workflow for life cycle status change : " + e.getMessage(), e);
}
// get the workflow state once the executor is executed.
wfDTO = apiMgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(apiId), WorkflowConstants.WF_TYPE_AM_API_STATE);
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 = "";
apiArtifact.invokeAction(action, APIConstants.API_LIFE_CYCLE);
targetStatus = apiArtifact.getLifecycleState();
if (!currentStatus.equals(targetStatus)) {
apiMgtDAO.recordAPILifeCycleEvent(apiId, currentStatus.toUpperCase(), targetStatus.toUpperCase(), this.username, this.tenantId);
}
if (log.isDebugEnabled()) {
String logMessage = "API Status changed successfully. API Name: " + apiIdentifier.getApiName() + ", API Version " + apiIdentifier.getVersion() + ", New Status : " + targetStatus;
log.debug(logMessage);
}
APIEvent apiEvent = new APIEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.API_LIFECYCLE_CHANGE.name(), tenantId, tenantDomain, apiName, apiId, uuid, apiVersion, apiType, apiContext, providerName, targetStatus);
APIUtil.sendNotification(apiEvent, APIConstants.NotifierType.API.name());
return response;
}
}
} catch (GovernanceException e) {
String cause = e.getCause().getMessage();
if (!StringUtils.isEmpty(cause)) {
if (cause.contains("FaultGatewaysException:")) {
Map<String, Map<String, String>> faultMap = new HashMap<String, Map<String, String>>();
String faultJsonString;
if (!StringUtils.isEmpty(cause) && cause.split("FaultGatewaysException:").length > 1) {
faultJsonString = cause.split("FaultGatewaysException:")[1];
try {
JSONObject faultGatewayJson = (JSONObject) new JSONParser().parse(faultJsonString);
faultMap.putAll(faultGatewayJson);
throw new FaultGatewaysException(faultMap);
} catch (ParseException e1) {
log.error("Couldn't parse the Failed Environment json", e);
handleException("Couldn't parse the Failed Environment json : " + e.getMessage(), e);
}
}
} else if (cause.contains("APIManagementException:")) {
// This exception already logged from APIExecutor class hence this no need to logged again
handleException("Failed to change the life cycle status : " + cause.split("APIManagementException:")[1], e);
} else {
/* This exception already logged from APIExecutor class hence this no need to logged again
This block handles the all the exception which not have custom cause message*/
handleException("Failed to change the life cycle status : " + e.getMessage(), e);
}
}
return response;
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
return response;
}
Aggregations