Search in sources :

Example 36 with Subscription

use of org.wso2.eventing.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);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) API(org.wso2.carbon.apimgt.core.models.API) SubscriptionValidationData(org.wso2.carbon.apimgt.core.models.SubscriptionValidationData) Subscription(org.wso2.carbon.apimgt.core.models.Subscription)

Example 37 with Subscription

use of org.wso2.eventing.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);
}
Also used : Iterator(java.util.Iterator) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) Subscription(org.wso2.carbon.apimgt.core.models.Subscription)

Example 38 with Subscription

use of org.wso2.eventing.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;
}
Also used : Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) SubscriptionCreationWorkflow(org.wso2.carbon.apimgt.core.workflow.SubscriptionCreationWorkflow) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) SubscriptionResponse(org.wso2.carbon.apimgt.core.models.SubscriptionResponse) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) Application(org.wso2.carbon.apimgt.core.models.Application)

Example 39 with Subscription

use of org.wso2.eventing.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());
}
Also used : AnalyticsDAO(org.wso2.carbon.apimgt.core.dao.AnalyticsDAO) Instant(java.time.Instant) API(org.wso2.carbon.apimgt.core.models.API) SubscriptionInfo(org.wso2.carbon.apimgt.core.models.analytics.SubscriptionInfo) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) Application(org.wso2.carbon.apimgt.core.models.Application) Test(org.testng.annotations.Test)

Example 40 with Subscription

use of org.wso2.eventing.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);
}
Also used : APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO)

Aggregations

Test (org.testng.annotations.Test)58 Subscription (org.wso2.carbon.apimgt.core.models.Subscription)58 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)37 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)35 APISubscriptionDAO (org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO)34 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)34 Application (org.wso2.carbon.apimgt.core.models.Application)30 API (org.wso2.carbon.apimgt.core.models.API)28 ArrayList (java.util.ArrayList)27 Test (org.junit.Test)22 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)22 Response (javax.ws.rs.core.Response)21 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)21 SQLException (java.sql.SQLException)20 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)18 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)17 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)15 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)15 Connection (java.sql.Connection)14 PreparedStatement (java.sql.PreparedStatement)14