use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.
the class WorkflowUtils method sendNotificationAfterWFComplete.
public static void sendNotificationAfterWFComplete(WorkflowDTO workflowDTO, String wfType) throws APIManagementException {
if (WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION.equalsIgnoreCase(wfType)) {
String applicationId = workflowDTO.getWorkflowReference();
int appId = Integer.parseInt(applicationId);
ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
Application application = apiMgtDAO.getApplicationById(appId);
ApplicationWorkflowDTO appWFDto = (ApplicationWorkflowDTO) workflowDTO;
appWFDto.setApplication(application);
ApplicationEvent applicationEvent = new ApplicationEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.APPLICATION_CREATE.name(), appWFDto.getTenantId(), appWFDto.getTenantDomain(), appWFDto.getApplication().getId(), appWFDto.getApplication().getUUID(), appWFDto.getApplication().getName(), appWFDto.getApplication().getTokenType(), appWFDto.getApplication().getTier(), appWFDto.getApplication().getGroupId(), appWFDto.getApplication().getApplicationAttributes(), application.getSubscriber().getName());
APIUtil.sendNotification(applicationEvent, APIConstants.NotifierType.APPLICATION.name());
} else if (WorkflowConstants.WF_TYPE_AM_APPLICATION_DELETION.equalsIgnoreCase(wfType)) {
ApplicationWorkflowDTO appWFDto = (ApplicationWorkflowDTO) workflowDTO;
ApplicationEvent applicationEvent = new ApplicationEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.APPLICATION_DELETE.name(), appWFDto.getTenantId(), appWFDto.getTenantDomain(), appWFDto.getApplication().getId(), appWFDto.getApplication().getUUID(), appWFDto.getApplication().getName(), appWFDto.getApplication().getTokenType(), appWFDto.getApplication().getTier(), appWFDto.getApplication().getGroupId(), appWFDto.getApplication().getApplicationAttributes(), "");
APIUtil.sendNotification(applicationEvent, APIConstants.NotifierType.APPLICATION.name());
} else if (WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION.equalsIgnoreCase(wfType)) {
SubscriptionWorkflowDTO subWFDto = (SubscriptionWorkflowDTO) workflowDTO;
SubscribedAPI sub = ApiMgtDAO.getInstance().getSubscriptionById(Integer.parseInt(subWFDto.getWorkflowReference()));
SubscriptionEvent subscriptionEvent;
if (sub.getApiId() != null) {
subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_CREATE.name(), subWFDto.getTenantId(), subWFDto.getTenantDomain(), Integer.parseInt(subWFDto.getWorkflowReference()), sub.getUUID(), sub.getIdentifier().getId(), sub.getIdentifier().getUUID(), sub.getApplication().getId(), sub.getApplication().getUUID(), sub.getTier().getName(), sub.getSubCreatedStatus());
} else {
subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_CREATE.name(), subWFDto.getTenantId(), subWFDto.getTenantDomain(), Integer.parseInt(subWFDto.getWorkflowReference()), sub.getUUID(), sub.getProductId().getId(), sub.getProductId().getUUID(), sub.getApplication().getId(), sub.getApplication().getUUID(), sub.getTier().getName(), sub.getSubCreatedStatus());
}
APIUtil.sendNotification(subscriptionEvent, APIConstants.NotifierType.SUBSCRIPTIONS.name());
} else if (WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_UPDATE.equalsIgnoreCase(wfType)) {
SubscriptionWorkflowDTO subWFDto = (SubscriptionWorkflowDTO) workflowDTO;
SubscribedAPI sub = ApiMgtDAO.getInstance().getSubscriptionById(Integer.parseInt(subWFDto.getWorkflowReference()));
SubscriptionEvent subscriptionEvent;
if (sub.getApiId() != null) {
subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_UPDATE.name(), subWFDto.getTenantId(), subWFDto.getTenantDomain(), Integer.parseInt(subWFDto.getWorkflowReference()), sub.getUUID(), sub.getIdentifier().getId(), sub.getIdentifier().getUUID(), sub.getApplication().getId(), sub.getApplication().getUUID(), sub.getTier().getName(), sub.getSubCreatedStatus());
} else {
subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_UPDATE.name(), subWFDto.getTenantId(), subWFDto.getTenantDomain(), Integer.parseInt(subWFDto.getWorkflowReference()), sub.getUUID(), sub.getProductId().getId(), sub.getProductId().getUUID(), sub.getApplication().getId(), sub.getApplication().getUUID(), sub.getTier().getName(), sub.getSubCreatedStatus());
}
APIUtil.sendNotification(subscriptionEvent, APIConstants.NotifierType.SUBSCRIPTIONS.name());
} else if (WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_DELETION.equalsIgnoreCase(wfType)) {
SubscriptionWorkflowDTO subWFDto = (SubscriptionWorkflowDTO) workflowDTO;
SubscribedAPI sub = ApiMgtDAO.getInstance().getSubscriptionById(Integer.parseInt(subWFDto.getWorkflowReference()));
SubscriptionEvent subscriptionEvent;
if (sub.getApiId() != null) {
subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_DELETE.name(), subWFDto.getTenantId(), subWFDto.getTenantDomain(), Integer.parseInt(subWFDto.getWorkflowReference()), sub.getUUID(), sub.getIdentifier().getId(), sub.getIdentifier().getUUID(), sub.getApplication().getId(), sub.getApplication().getUUID(), sub.getTier().getName(), sub.getSubCreatedStatus());
} else {
subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_DELETE.name(), subWFDto.getTenantId(), subWFDto.getTenantDomain(), Integer.parseInt(subWFDto.getWorkflowReference()), sub.getUUID(), sub.getProductId().getId(), sub.getProductId().getUUID(), sub.getApplication().getId(), sub.getApplication().getUUID(), sub.getTier().getName(), sub.getSubCreatedStatus());
}
APIUtil.sendNotification(subscriptionEvent, APIConstants.NotifierType.SUBSCRIPTIONS.name());
} else if (WorkflowConstants.WF_TYPE_AM_API_STATE.equalsIgnoreCase(wfType) || WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE.equalsIgnoreCase(wfType)) {
APIStateWorkflowDTO apiStateWFDto = (APIStateWorkflowDTO) workflowDTO;
APIEvent apiEvent = new APIEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.API_LIFECYCLE_CHANGE.name(), apiStateWFDto.getTenantId(), apiStateWFDto.getTenantDomain(), apiStateWFDto.getApiName(), Integer.parseInt(apiStateWFDto.getWorkflowReference()), apiStateWFDto.getApiUUID(), apiStateWFDto.getApiVersion(), apiStateWFDto.getApiType(), apiStateWFDto.getApiContext(), apiStateWFDto.getApiProvider(), apiStateWFDto.getApiLCAction());
APIUtil.sendNotification(apiEvent, APIConstants.NotifierType.API.name());
} else if (WorkflowConstants.WF_TYPE_AM_APPLICATION_REGISTRATION_PRODUCTION.equalsIgnoreCase(wfType)) {
ApplicationRegistrationWorkflowDTO appRegWFDto = (ApplicationRegistrationWorkflowDTO) workflowDTO;
ApplicationRegistrationEvent applicationRegistrationEvent = new ApplicationRegistrationEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.APPLICATION_REGISTRATION_CREATE.name(), appRegWFDto.getTenantId(), appRegWFDto.getTenantDomain(), appRegWFDto.getApplication().getId(), appRegWFDto.getApplication().getUUID(), appRegWFDto.getApplicationInfo().getClientId(), appRegWFDto.getApplication().getTokenType(), appRegWFDto.getKeyManager());
APIUtil.sendNotification(applicationRegistrationEvent, APIConstants.NotifierType.APPLICATION_REGISTRATION.name());
} else if (WorkflowConstants.WF_TYPE_AM_APPLICATION_REGISTRATION_SANDBOX.equalsIgnoreCase(wfType)) {
ApplicationRegistrationWorkflowDTO appRegWFDto = (ApplicationRegistrationWorkflowDTO) workflowDTO;
ApplicationRegistrationEvent applicationRegistrationEvent = new ApplicationRegistrationEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.APPLICATION_REGISTRATION_CREATE.name(), appRegWFDto.getTenantId(), appRegWFDto.getTenantDomain(), appRegWFDto.getApplication().getId(), appRegWFDto.getApplication().getUUID(), appRegWFDto.getApplicationInfo().getClientId(), appRegWFDto.getApplication().getTokenType(), appRegWFDto.getKeyManager());
APIUtil.sendNotification(applicationRegistrationEvent, APIConstants.NotifierType.APPLICATION_REGISTRATION.name());
}
}
use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.
the class WorkflowUtils method completeStateChangeWorkflow.
/**
* Complete the lifecycle state change workflow
*
* @param workflowDTO Workflow DTO object
* @throws WorkflowException Exception when completing the workflow
*/
protected static void completeStateChangeWorkflow(WorkflowDTO workflowDTO) throws WorkflowException {
String externalWorkflowRef = workflowDTO.getExternalWorkflowReference();
try {
ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
Workflow workflow = apiMgtDAO.getworkflowReferenceByExternalWorkflowReference(externalWorkflowRef);
String apiName = workflow.getMetadata("ApiName");
String action = workflow.getMetadata("Action");
String providerName = workflow.getMetadata("ApiProvider");
String version = workflow.getMetadata("ApiVersion");
String invoker = workflow.getMetadata("Invoker");
String currentStatus = workflow.getMetadata("CurrentState");
int tenantId = workflowDTO.getTenantId();
try {
// tenant flow is already started from the rest api service impl. no need to start from here
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(invoker);
Registry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceUserRegistry(invoker, tenantId);
APIIdentifier apiIdentifier = new APIIdentifier(providerName, apiName, version);
GenericArtifact apiArtifact = APIUtil.getAPIArtifact(apiIdentifier, registry);
if (WorkflowStatus.APPROVED.equals(workflowDTO.getStatus())) {
String targetStatus;
apiArtifact.invokeAction(action, APIConstants.API_LIFE_CYCLE);
targetStatus = apiArtifact.getLifecycleState();
if (!currentStatus.equals(targetStatus)) {
apiMgtDAO.recordAPILifeCycleEvent(apiArtifact.getId(), currentStatus.toUpperCase(), targetStatus.toUpperCase(), invoker, tenantId);
}
if (log.isDebugEnabled()) {
String logMessage = "API Status changed successfully. API Name: " + apiIdentifier.getApiName() + ", API " + "Version " + apiIdentifier.getVersion() + ", New Status : " + targetStatus;
log.debug(logMessage);
}
}
} catch (RegistryException e) {
String errorMsg = "Could not complete api state change workflow";
log.error(errorMsg, e);
throw new WorkflowException(errorMsg, e);
}
} catch (APIManagementException e) {
String errorMsg = "Could not complete api state change workflow";
log.error(errorMsg, e);
}
}
use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.
the class APIStateChangeWSWorkflowExecutor method complete.
/**
* Complete the API state change workflow process.
*/
@Override
public WorkflowResponse complete(WorkflowDTO workflowDTO) throws WorkflowException {
if (log.isDebugEnabled()) {
log.debug("Completing API State change Workflow..");
log.debug("response: " + workflowDTO.toString());
}
workflowDTO.setUpdatedTime(System.currentTimeMillis());
super.complete(workflowDTO);
String action = workflowDTO.getAttributes().get(PayloadConstants.VARIABLE_API_LC_ACTION);
String apiName = workflowDTO.getAttributes().get(PayloadConstants.VARIABLE_APINAME);
String providerName = workflowDTO.getAttributes().get(PayloadConstants.VARIABLE_APIPROVIDER);
String version = workflowDTO.getAttributes().get(PayloadConstants.VARIABLE_APIVERSION);
String invoker = workflowDTO.getAttributes().get(PayloadConstants.VARIABLE_INVOKER);
String currentStatus = workflowDTO.getAttributes().get(PayloadConstants.VARIABLE_APISTATE);
int tenantId = workflowDTO.getTenantId();
ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
try {
// tenant flow is already started from the rest api service impl. no need to start from here
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(invoker);
Registry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceUserRegistry(invoker, tenantId);
APIIdentifier apiIdentifier = new APIIdentifier(providerName, apiName, version);
GenericArtifact apiArtifact = APIUtil.getAPIArtifact(apiIdentifier, registry);
if (WorkflowStatus.APPROVED.equals(workflowDTO.getStatus())) {
String targetStatus;
apiArtifact.invokeAction(action, APIConstants.API_LIFE_CYCLE);
targetStatus = apiArtifact.getLifecycleState();
if (!currentStatus.equals(targetStatus)) {
apiMgtDAO.recordAPILifeCycleEvent(apiArtifact.getId(), currentStatus.toUpperCase(), targetStatus.toUpperCase(), invoker, tenantId);
}
if (log.isDebugEnabled()) {
String logMessage = "API Status changed successfully. API Name: " + apiIdentifier.getApiName() + ", API Version " + apiIdentifier.getVersion() + ", New Status : " + targetStatus;
log.debug(logMessage);
}
}
} catch (RegistryException e) {
String errorMsg = "Could not complete api state change workflow";
log.error(errorMsg, e);
throw new WorkflowException(errorMsg, e);
} catch (APIManagementException e) {
String errorMsg = "Could not complete api state change workflow";
log.error(errorMsg, e);
throw new WorkflowException(errorMsg, e);
}
return new GeneralWorkflowResponse();
}
use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.
the class APIStateChangeWSWorkflowExecutor method cleanUpPendingTask.
/**
* Handle cleanup task for api state change workflow ws executor. This queries the BPMN process related to the given
* workflow reference id and delete that process
*/
@Override
public void cleanUpPendingTask(String workflowExtRef) throws WorkflowException {
if (log.isDebugEnabled()) {
log.debug("Starting cleanup task for APIStateChangeWSWorkflowExecutor for :" + workflowExtRef);
}
String errorMsg;
if (serviceEndpoint == null) {
// set the bps endpoint from the global configurations
WorkflowProperties workflowProperties = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration().getWorkflowProperties();
serviceEndpoint = workflowProperties.getServerUrl();
}
URL serviceEndpointURL = new URL(serviceEndpoint);
HttpClient httpClient = APIUtil.getHttpClient(serviceEndpointURL.getPort(), serviceEndpointURL.getProtocol());
// get the basic auth header value to connect to the bpmn process
String authHeader = getBasicAuthHeader();
JSONParser parser = new JSONParser();
HttpGet httpGet = null;
HttpDelete httpDelete = null;
try {
// Get the process instance details related to the given workflow reference id. If there is a process that
// is already started with the given wf reference as the businesskey, that process needes to be deleted
httpGet = new HttpGet(serviceEndpoint + RUNTIME_INSTANCE_RESOURCE_PATH + "?businessKey=" + workflowExtRef);
httpGet.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
String processId = null;
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// already exists a process related to the given workflow reference
String responseStr = EntityUtils.toString(entity);
if (log.isDebugEnabled()) {
log.debug("Process instance details for ref : " + workflowExtRef + ": " + responseStr);
}
JSONObject obj = (JSONObject) parser.parse(responseStr);
JSONArray data = (JSONArray) obj.get(PayloadConstants.DATA);
if (data != null) {
JSONObject instanceDetails = (JSONObject) data.get(0);
// extract the id related to that process. this id is used to delete the process
processId = (String) instanceDetails.get(PayloadConstants.ID);
}
if (processId != null) {
// delete the process using the id
httpDelete = new HttpDelete(serviceEndpoint + RUNTIME_INSTANCE_RESOURCE_PATH + "/" + processId);
httpDelete.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
response = httpClient.execute(httpDelete);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_NO_CONTENT) {
errorMsg = "Error while deleting process instance details for " + workflowExtRef + " code: " + response.getStatusLine().getStatusCode();
log.error(errorMsg);
throw new WorkflowException(errorMsg);
}
if (log.isDebugEnabled()) {
log.debug("Successfully deleted process instance for : " + workflowExtRef);
}
// remove entry from the db
ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
apiMgtDAO.removeWorkflowEntry(workflowExtRef, WorkflowConstants.WF_TYPE_AM_API_STATE.toString());
}
} else {
errorMsg = "Error while getting process instance details for " + workflowExtRef + " code: " + response.getStatusLine().getStatusCode();
log.error(errorMsg);
throw new WorkflowException(errorMsg);
}
} catch (ClientProtocolException e) {
log.error("Error while creating the http client", e);
throw new WorkflowException("Error while creating the http client", e);
} catch (IOException e) {
log.error("Error while connecting to the BPMN process server from the WorkflowExecutor.", e);
throw new WorkflowException("Error while connecting to the external service", e);
} catch (ParseException e) {
log.error("Error while parsing response from BPS server", e);
throw new WorkflowException("Error while parsing response from BPS server", e);
} catch (APIManagementException e) {
log.error("Error removing the workflow entry", e);
throw new WorkflowException("Error removing the workflow entry", e);
} finally {
if (httpGet != null) {
httpGet.reset();
}
if (httpDelete != null) {
httpDelete.reset();
}
}
}
use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.
the class SubscriptionUpdateApprovalWorkflowExecutor method complete.
/**
* Complete the Approval workflow executor for Subscription creation.
*
* @param workflowDTO
*/
@Override
public WorkflowResponse complete(WorkflowDTO workflowDTO) throws WorkflowException {
workflowDTO.setUpdatedTime(System.currentTimeMillis());
super.complete(workflowDTO);
if (log.isDebugEnabled()) {
String logMessage = "Subscription Update [Complete] Workflow Invoked. Workflow ID : " + workflowDTO.getExternalWorkflowReference() + " Workflow State : " + workflowDTO.getStatus();
log.debug(logMessage);
}
SubscriptionWorkflowDTO subscriptionWorkflowDTO = (SubscriptionWorkflowDTO) workflowDTO;
if (WorkflowStatus.APPROVED.equals(workflowDTO.getStatus())) {
ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
try {
apiMgtDAO.updateSubscriptionStatusAndTier(Integer.parseInt(subscriptionWorkflowDTO.getWorkflowReference()), APIConstants.SubscriptionStatus.UNBLOCKED);
} catch (APIManagementException e) {
log.error("Could not complete subscription update workflow", e);
throw new WorkflowException("Could not complete subscription update workflow", e);
}
} else if (WorkflowStatus.REJECTED.equals(workflowDTO.getStatus())) {
ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
try {
apiMgtDAO.updateSubscriptionStatus(Integer.parseInt(subscriptionWorkflowDTO.getWorkflowReference()), APIConstants.SubscriptionStatus.UNBLOCKED);
} catch (APIManagementException e) {
log.error("Could not complete subscription update workflow", e);
throw new WorkflowException("Could not complete subscription update workflow", e);
}
}
return new GeneralWorkflowResponse();
}
Aggregations