Search in sources :

Example 36 with ApplicationPolicy

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

the class PolicyDAOImpl method getApplicationPolicyByUuid.

@Override
public ApplicationPolicy getApplicationPolicyByUuid(String uuid) throws APIMgtDAOException {
    try {
        final String query = "SELECT UUID, NAME, QUOTA_TYPE, TIME_UNIT, UNIT_TIME, QUOTA, QUOTA_UNIT, DESCRIPTION, " + "DISPLAY_NAME, CUSTOM_ATTRIBUTES, IS_DEPLOYED from AM_APPLICATION_POLICY WHERE UUID = ?";
        ApplicationPolicy applicationPolicy;
        try (Connection conn = DAOUtil.getConnection();
            PreparedStatement statement = conn.prepareStatement(query)) {
            statement.setString(1, uuid);
            statement.execute();
            try (ResultSet rs = statement.getResultSet()) {
                if (rs.next()) {
                    applicationPolicy = new ApplicationPolicy(rs.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME));
                    setCommonPolicyDetails(applicationPolicy, rs);
                    InputStream inputStream = rs.getBinaryStream(APIMgtConstants.ThrottlePolicyConstants.COLUMN_CUSTOM_ATTRIB);
                    if (inputStream != null) {
                        applicationPolicy.setCustomAttributes(IOUtils.toByteArray(inputStream));
                    }
                    return applicationPolicy;
                } else {
                    // not found
                    String msg = "Application Policy not found for id: " + uuid;
                    log.warn(msg);
                    throw new APIMgtDAOException(msg, ExceptionCodes.POLICY_NOT_FOUND);
                }
            } catch (IOException e) {
                // error
                throw new APIMgtDAOException("Internal error", e);
            }
        }
    } catch (SQLException e) {
        String errorMsg = "Error in retrieving Application Policy with id: " + uuid;
        log.error(errorMsg, e);
        throw new APIMgtDAOException(errorMsg, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 37 with ApplicationPolicy

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

the class APIMgtAdminServiceImplTestCase method testAddApplicationPolicy.

@Test(description = "Test add application policy")
public void testAddApplicationPolicy() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO, apiGateway);
    ApplicationPolicy policy = SampleTestObjectCreator.createDefaultApplicationPolicy();
    adminService.addApplicationPolicy(policy);
    Mockito.verify(policyDAO, Mockito.times(1)).addApplicationPolicy(policy);
    // Error path
    Mockito.doThrow(APIMgtDAOException.class).when(policyDAO).addApplicationPolicy(policy);
    try {
        adminService.addApplicationPolicy(policy);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Couldn't add Application for uuid: " + policy.getUuid());
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 38 with ApplicationPolicy

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

the class APIMgtAdminServiceImplTestCase method testGetApplicationPolicy.

@Test(description = "Test getting Application policy")
public void testGetApplicationPolicy() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO);
    ApplicationPolicy applicationPolicy = SampleTestObjectCreator.createDefaultApplicationPolicy();
    Mockito.when(policyDAO.getApplicationPolicy(applicationPolicy.getPolicyName())).thenReturn(applicationPolicy);
    adminService.getApplicationPolicy(applicationPolicy.getPolicyName());
    Mockito.verify(policyDAO, Mockito.times(1)).getApplicationPolicy(applicationPolicy.getPolicyName());
    // Error path
    Mockito.when(policyDAO.getApplicationPolicy(applicationPolicy.getPolicyName())).thenThrow(APIMgtDAOException.class);
    try {
        adminService.getApplicationPolicy(applicationPolicy.getPolicyName());
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Couldn't retrieve Application policy with name: " + applicationPolicy.getPolicyName());
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 39 with ApplicationPolicy

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

the class PolicyExportManager method createArchiveFromExecutionPlans.

public String createArchiveFromExecutionPlans(String exportedPoliciesDirName, String archiveDir, String archiveName) throws APIManagementException {
    try {
        // retrieve all policies under each policy level
        List<APIPolicy> apiPolicies = apiMgtAdminService.getApiPolicies();
        List<ApplicationPolicy> applicationPolicies = apiMgtAdminService.getApplicationPolicies();
        List<SubscriptionPolicy> subscriptionPolicies = apiMgtAdminService.getSubscriptionPolicies();
        List<CustomPolicy> customPolicies = apiMgtAdminService.getCustomRules();
        // write all execution Plans/Siddhi Apps to exportPoliciesDirName directory
        String dirLocation = exportedPoliciesDirName + File.separator + EXPORT_POLICIES;
        APIFileUtils.createDirectory(dirLocation);
        if (!apiPolicies.isEmpty()) {
            for (Map<String, String> map : getApiPolicySiddhiApps(apiPolicies)) {
                prepareFile(dirLocation, map);
            }
        }
        if (!applicationPolicies.isEmpty()) {
            prepareFile(dirLocation, getAppPolicySiddhiApps(applicationPolicies));
        }
        if (!subscriptionPolicies.isEmpty()) {
            prepareFile(dirLocation, getSubscriptionPolicySiddhiApps(subscriptionPolicies));
        }
        if (!customPolicies.isEmpty()) {
            prepareFile(dirLocation, getCustomPolicySiddhiApps(customPolicies));
        }
        // create archive and get the archive location
        String zippedFilePath = createArchiveFromPolicies(exportedPoliciesDirName, archiveDir, archiveName);
        APIFileUtils.deleteDirectory(exportedPoliciesDirName);
        return zippedFilePath;
    } catch (APIManagementException e) {
        String errorMessage = "Error while exporting policies";
        log.error(errorMessage, e);
        throw new APIManagementException(errorMessage, e);
    }
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy)

Example 40 with ApplicationPolicy

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

the class PolicyExportManager method getAppPolicySiddhiApps.

/**
 * Get execution plan/ siddhi apps for custom policies.
 *
 * @param applicationPolicies ApplicationPolicy object list
 * @return Map<String, String> containing execution plan name and execution plans.
 * @throws APITemplateException If template generating fails
 */
private Map<String, String> getAppPolicySiddhiApps(List<ApplicationPolicy> applicationPolicies) throws APITemplateException {
    if (log.isDebugEnabled()) {
        log.debug("Get execution plans for application policy.");
    }
    Map<String, String> siddhiApps = new HashMap<>();
    String name;
    String executionPlan;
    ApplicationThrottlePolicyTemplateBuilder templateBuilder;
    for (ApplicationPolicy policy : applicationPolicies) {
        templateBuilder = new ApplicationThrottlePolicyTemplateBuilder(policy);
        name = APPLICATION + policy.getPolicyName();
        executionPlan = templateBuilder.getThrottlePolicyForAppLevel();
        siddhiApps.put(name, executionPlan);
    }
    return siddhiApps;
}
Also used : HashMap(java.util.HashMap) ApplicationThrottlePolicyTemplateBuilder(org.wso2.carbon.apimgt.core.template.ApplicationThrottlePolicyTemplateBuilder) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)

Aggregations

ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)56 Test (org.testng.annotations.Test)30 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)24 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)18 Application (org.wso2.carbon.apimgt.core.models.Application)17 BeforeTest (org.testng.annotations.BeforeTest)13 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)13 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)13 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)13 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)13 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)12 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)10 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)10 WorkflowDAO (org.wso2.carbon.apimgt.core.dao.WorkflowDAO)9 RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)9 Connection (java.sql.Connection)8 PreparedStatement (java.sql.PreparedStatement)8 SQLException (java.sql.SQLException)8 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)8 ResultSet (java.sql.ResultSet)6