Search in sources :

Example 21 with Application

use of org.wso2.carbon.apimgt.core.models.Application in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImpl method addApplicationPolicy.

@Override
public String addApplicationPolicy(ApplicationPolicy policy) throws APIManagementException {
    try {
        String policyUuid = policy.getUuid();
        if (policyUuid == null) {
            if (log.isDebugEnabled()) {
                log.debug("Policy id is null, hence generating a new UUID for the policy with name: " + policy.getPolicyName());
            }
            policyUuid = UUID.randomUUID().toString();
            policy.setUuid(policyUuid);
        }
        policyDAO.addApplicationPolicy(policy);
        PolicyValidationData policyValidationData = new PolicyValidationData(policyUuid, policy.getPolicyName(), false);
        apiGateway.addPolicy(policyValidationData);
        return policyUuid;
    } catch (APIMgtDAOException e) {
        String errorMessage = "Couldn't add Application for uuid: " + policy.getUuid();
        log.error(errorMessage, e);
        throw new APIManagementException(errorMessage, e, e.getErrorHandler());
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) PolicyValidationData(org.wso2.carbon.apimgt.core.models.PolicyValidationData)

Example 22 with Application

use of org.wso2.carbon.apimgt.core.models.Application in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImpl method updateApplicationPolicy.

@Override
public void updateApplicationPolicy(ApplicationPolicy policy) throws APIManagementException {
    try {
        policyDAO.updateApplicationPolicy(policy);
        PolicyValidationData policyValidationData = new PolicyValidationData(policy.getUuid(), policy.getPolicyName(), false);
        apiGateway.updatePolicy(policyValidationData);
    } catch (APIMgtDAOException e) {
        String errorMessage = "Couldn't update Application policy for uuid: " + policy.getUuid();
        log.error(errorMessage, e);
        throw new APIManagementException(errorMessage, e, e.getErrorHandler());
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) PolicyValidationData(org.wso2.carbon.apimgt.core.models.PolicyValidationData)

Example 23 with Application

use of org.wso2.carbon.apimgt.core.models.Application in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetes method addServicesToEndpointList.

/**
 * For each service in {@code serviceList} list, methods are called to add endpoints of different types,
 * for each of service's ports
 *
 * @param serviceList filtered list of services
 */
private void addServicesToEndpointList(List<Service> serviceList, List<Endpoint> endpointList) throws MalformedURLException {
    for (Service service : serviceList) {
        // Set the parameters that does not change with the service port
        String serviceName = service.getMetadata().getName();
        String namespace = service.getMetadata().getNamespace();
        Map<String, String> labelsMap = service.getMetadata().getLabels();
        String labels = (labelsMap != null) ? labelsMap.toString() : "";
        ServiceSpec serviceSpec = service.getSpec();
        String serviceType = serviceSpec.getType();
        if (includeExternalNameTypeServices && EXTERNAL_NAME.equals(serviceType)) {
            // Since only a "ExternalName" type service can have an "externalName" (the alias in kube-dns)
            addExternalNameEndpoint(serviceName, serviceSpec.getExternalName(), namespace, labels, endpointList);
        }
        for (ServicePort servicePort : serviceSpec.getPorts()) {
            String protocol = servicePort.getName();
            if (APIMgtConstants.HTTP.equals(protocol) || APIMgtConstants.HTTPS.equals(protocol)) {
                int port = servicePort.getPort();
                if (includeClusterIP && !EXTERNAL_NAME.equals(serviceType)) {
                    // Since almost every service has a cluster IP, except for ExternalName type
                    addClusterIPEndpoint(serviceName, serviceSpec.getClusterIP(), port, protocol, namespace, labels, endpointList);
                }
                if (NODE_PORT.equals(serviceType) || LOAD_BALANCER.equals(serviceType)) {
                    // Because both "NodePort" and "LoadBalancer" types of services have "NodePort" type URLs
                    addNodePortEndpoint(serviceName, servicePort.getNodePort(), protocol, namespace, labels, endpointList);
                }
                if (LOAD_BALANCER.equals(serviceType)) {
                    // Since only "LoadBalancer" type services have "LoadBalancer" type URLs
                    addLoadBalancerEndpoint(serviceName, service, port, protocol, namespace, labels, endpointList);
                }
                // A Special case (can be any of the service types above)
                addExternalIPEndpoint(serviceName, serviceSpec.getExternalIPs(), port, protocol, namespace, labels, endpointList);
            } else if (log.isDebugEnabled()) {
                log.debug("Service:{} Namespace:{} Port:{}/{}  Application level protocol not defined.", serviceName, namespace, servicePort.getPort(), protocol);
            }
        }
    }
}
Also used : ServicePort(io.fabric8.kubernetes.api.model.ServicePort) ServiceSpec(io.fabric8.kubernetes.api.model.ServiceSpec) Service(io.fabric8.kubernetes.api.model.Service) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint)

Example 24 with Application

use of org.wso2.carbon.apimgt.core.models.Application in project carbon-apimgt by wso2.

the class ThrottlerUtil method addDefaultAdvancedThrottlePolicies.

/**
 * Deploy default throttle polices at startup
 *
 * @throws APIManagementException throws if any exception occured
 */
public static void addDefaultAdvancedThrottlePolicies() throws APIManagementException {
    int[] requestCount = new int[] { 50, 20, 10, Integer.MAX_VALUE };
    // Adding application level throttle policies
    String[] appPolicies = new String[] { ThrottleConstants.DEFAULT_APP_POLICY_FIFTY_REQ_PER_MIN, ThrottleConstants.DEFAULT_APP_POLICY_TWENTY_REQ_PER_MIN, ThrottleConstants.DEFAULT_APP_POLICY_TEN_REQ_PER_MIN, ThrottleConstants.DEFAULT_APP_POLICY_UNLIMITED };
    String[] appPolicyDecs = new String[] { ThrottleConstants.DEFAULT_APP_POLICY_LARGE_DESC, ThrottleConstants.DEFAULT_APP_POLICY_MEDIUM_DESC, ThrottleConstants.DEFAULT_APP_POLICY_SMALL_DESC, ThrottleConstants.DEFAULT_APP_POLICY_UNLIMITED_DESC };
    PolicyDAO policyDAO = DAOFactory.getPolicyDAO();
    String policyName;
    // Add application level throttle policies
    for (int i = 0; i < appPolicies.length; i++) {
        policyName = appPolicies[i];
        if (!isPolicyExist(APIMgtAdminService.PolicyLevel.application, policyName)) {
            ApplicationPolicy applicationPolicy = new ApplicationPolicy(policyName);
            applicationPolicy.setUuid(UUID.randomUUID().toString());
            applicationPolicy.setDisplayName(policyName);
            applicationPolicy.setDescription(appPolicyDecs[i]);
            applicationPolicy.setDeployed(true);
            QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
            RequestCountLimit requestCountLimit = new RequestCountLimit(ThrottleConstants.TIME_UNIT_MINUTE, 1, requestCount[i]);
            defaultQuotaPolicy.setType(PolicyConstants.REQUEST_COUNT_TYPE);
            defaultQuotaPolicy.setLimit(requestCountLimit);
            applicationPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
            policyDAO.addApplicationPolicy(applicationPolicy);
        }
    }
    // Adding Subscription level policies
    int[] requestCountSubPolicies = new int[] { 5000, 2000, 1000, 500, Integer.MAX_VALUE };
    String[] subPolicies = new String[] { ThrottleConstants.DEFAULT_SUB_POLICY_GOLD, ThrottleConstants.DEFAULT_SUB_POLICY_SILVER, ThrottleConstants.DEFAULT_SUB_POLICY_BRONZE, ThrottleConstants.DEFAULT_SUB_POLICY_UNAUTHENTICATED, ThrottleConstants.DEFAULT_SUB_POLICY_UNLIMITED };
    String[] subPolicyDecs = new String[] { ThrottleConstants.DEFAULT_SUB_POLICY_GOLD_DESC, ThrottleConstants.DEFAULT_SUB_POLICY_SILVER_DESC, ThrottleConstants.DEFAULT_SUB_POLICY_BRONZE_DESC, ThrottleConstants.DEFAULT_SUB_POLICY_UNAUTHENTICATED_DESC, ThrottleConstants.DEFAULT_SUB_POLICY_UNLIMITED_DESC };
    for (int i = 0; i < subPolicies.length; i++) {
        policyName = subPolicies[i];
        if (!isPolicyExist(APIMgtAdminService.PolicyLevel.subscription, policyName)) {
            SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(policyName);
            subscriptionPolicy.setUuid(UUID.randomUUID().toString());
            subscriptionPolicy.setDisplayName(policyName);
            subscriptionPolicy.setDescription(subPolicyDecs[i]);
            subscriptionPolicy.setDeployed(true);
            QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
            RequestCountLimit requestCountLimit = new RequestCountLimit(ThrottleConstants.TIME_UNIT_MINUTE, 1, requestCountSubPolicies[i]);
            defaultQuotaPolicy.setType(PolicyConstants.REQUEST_COUNT_TYPE);
            defaultQuotaPolicy.setLimit(requestCountLimit);
            subscriptionPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
            subscriptionPolicy.setStopOnQuotaReach(true);
            subscriptionPolicy.setBillingPlan(ThrottleConstants.BILLING_PLAN_FREE);
            policyDAO.addSubscriptionPolicy(subscriptionPolicy);
        }
    }
    // Adding Resource level policies
    String[] apiPolicies = new String[] { ThrottleConstants.DEFAULT_API_POLICY_FIFTY_THOUSAND_REQ_PER_MIN, ThrottleConstants.DEFAULT_API_POLICY_TWENTY_THOUSAND_REQ_PER_MIN, ThrottleConstants.DEFAULT_API_POLICY_TEN_THOUSAND_REQ_PER_MIN, ThrottleConstants.DEFAULT_API_POLICY_UNLIMITED };
    String[] apiPolicyDecs = new String[] { ThrottleConstants.DEFAULT_API_POLICY_ULTIMATE_DESC, ThrottleConstants.DEFAULT_API_POLICY_PLUS_DESC, ThrottleConstants.DEFAULT_API_POLICY_BASIC_DESC, ThrottleConstants.DEFAULT_API_POLICY_UNLIMITED_DESC };
    int[] requestCountApiPolicies = new int[] { 50000, 20000, 10000, Integer.MAX_VALUE };
    for (int i = 0; i < apiPolicies.length; i++) {
        policyName = apiPolicies[i];
        if (!isPolicyExist(APIMgtAdminService.PolicyLevel.api, policyName)) {
            APIPolicy apiPolicy = new APIPolicy(policyName);
            apiPolicy.setUuid(UUID.randomUUID().toString());
            apiPolicy.setDisplayName(policyName);
            apiPolicy.setDescription(apiPolicyDecs[i]);
            apiPolicy.setUserLevel(ThrottleConstants.API_POLICY_API_LEVEL);
            apiPolicy.setDeployed(true);
            QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
            RequestCountLimit requestCountLimit = new RequestCountLimit(ThrottleConstants.TIME_UNIT_MINUTE, 1, requestCountApiPolicies[i]);
            defaultQuotaPolicy.setType(PolicyConstants.REQUEST_COUNT_TYPE);
            defaultQuotaPolicy.setLimit(requestCountLimit);
            apiPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
            policyDAO.addApiPolicy(apiPolicy);
        }
    }
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO)

Example 25 with Application

use of org.wso2.carbon.apimgt.core.models.Application in project carbon-apimgt by wso2.

the class ApplicationDeletionWorkflow method completeWorkflow.

public WorkflowResponse completeWorkflow(WorkflowExecutor workflowExecutor) throws APIManagementException {
    if (application == null) {
        // this is when complete method is executed through workflow rest api
        this.application = applicationDAO.getApplication(getWorkflowReference());
    }
    WorkflowResponse response = workflowExecutor.complete(this);
    setStatus(response.getWorkflowStatus());
    if (WorkflowStatus.APPROVED == response.getWorkflowStatus()) {
        if (log.isDebugEnabled()) {
            log.debug("Application Deletion workflow complete: Approved");
        }
        applicationDAO.deleteApplication(getWorkflowReference());
        try {
            getApiGateway().deleteApplication(application.getId());
        } catch (GatewayException ex) {
            // This log is not harm to therefore not rethrow
            log.warn("Failed to send the Application Deletion Event ", ex);
        }
    } else if (WorkflowStatus.REJECTED == response.getWorkflowStatus()) {
        if (log.isDebugEnabled()) {
            log.debug("Application Deletion workflow complete: Rejected");
        }
    }
    updateWorkflowEntries(this);
    return response;
}
Also used : GatewayException(org.wso2.carbon.apimgt.core.exception.GatewayException) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse)

Aggregations

Application (org.wso2.carbon.apimgt.core.models.Application)121 Test (org.testng.annotations.Test)91 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)61 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)59 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)58 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)57 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)40 ArrayList (java.util.ArrayList)35 SQLException (java.sql.SQLException)33 API (org.wso2.carbon.apimgt.core.models.API)33 Test (org.junit.Test)29 BeforeTest (org.testng.annotations.BeforeTest)29 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)29 APISubscriptionDAO (org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO)27 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)27 Connection (java.sql.Connection)26 PreparedStatement (java.sql.PreparedStatement)26 HashMap (java.util.HashMap)26 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)25 Subscription (org.wso2.carbon.apimgt.core.models.Subscription)25