use of org.wso2.carbon.apimgt.api.model.subscription.Subscription in project carbon-apimgt by wso2.
the class APIPublisherImpl method updateSubscriptionStatus.
/**
* Update the subscription status
*
* @param subId Subscription ID
* @param subStatus Subscription Status
* @throws APIManagementException If failed to update subscription status
*/
@Override
public void updateSubscriptionStatus(String subId, APIMgtConstants.SubscriptionStatus subStatus) throws APIManagementException {
try {
getApiSubscriptionDAO().updateSubscriptionStatus(subId, subStatus);
Subscription subscription = getApiSubscriptionDAO().getAPISubscription(subId);
if (subscription != null) {
API subscribedApi = subscription.getApi();
List<SubscriptionValidationData> subscriptionValidationDataList = getApiSubscriptionDAO().getAPISubscriptionsOfAPIForValidation(subscribedApi.getContext(), subscribedApi.getVersion(), subscription.getApplication().getId());
getApiGateway().updateAPISubscriptionStatus(subscriptionValidationDataList);
}
} catch (APIMgtDAOException e) {
throw new APIManagementException(e);
}
}
use of org.wso2.carbon.apimgt.api.model.subscription.Subscription 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);
}
use of org.wso2.carbon.apimgt.api.model.subscription.Subscription 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;
}
use of org.wso2.carbon.apimgt.api.model.subscription.Subscription in project carbon-apimgt by wso2.
the class AnalyticsDAOImplIT method testGetSubscriptionList.
@Test
public void testGetSubscriptionList() throws Exception {
Instant fromTimeStamp = Instant.ofEpochMilli(System.currentTimeMillis());
API testAPI = TestUtil.addTestAPI();
Application testApplication = TestUtil.addTestApplication();
Subscription subscription = TestUtil.subscribeToAPI(testAPI, testApplication);
Instant toTimeStamp = Instant.ofEpochMilli(System.currentTimeMillis() + DELAY_TIME);
AnalyticsDAO analyticsDAO = DAOFactory.getAnalyticsDAO();
List<SubscriptionInfo> subscriptionInfo = analyticsDAO.getSubscriptionInfo(fromTimeStamp, toTimeStamp, null);
Assert.assertEquals(subscriptionInfo.size(), 1);
SubscriptionInfo subscriptionInfoResult = subscriptionInfo.get(0);
Assert.assertEquals(subscription.getId(), subscriptionInfoResult.getId());
Assert.assertEquals(subscription.getApi().getName(), subscriptionInfoResult.getName());
Assert.assertEquals(subscription.getApplication().getName(), subscriptionInfoResult.getAppName());
}
use of org.wso2.carbon.apimgt.api.model.subscription.Subscription in project carbon-apimgt by wso2.
the class TestUtil method subscribeToAPI.
/**
* Subscribe an application to given API
*
* @param api API to be subscribed to
* @param application Application to subscribe to the API
* @return {@link Subscription} object
* @throws APIManagementException If failed to add subscription.
*/
public static Subscription subscribeToAPI(API api, Application application) throws APIManagementException {
APISubscriptionDAO subscriptionDAO = DAOFactory.getAPISubscriptionDAO();
String subscriptionId = UUID.randomUUID().toString();
subscriptionDAO.addAPISubscription(subscriptionId, api.getId(), application.getId(), api.getPolicies().iterator().next().getUuid(), APIMgtConstants.SubscriptionStatus.ACTIVE);
return subscriptionDAO.getAPISubscription(subscriptionId);
}
Aggregations