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);
}
}
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());
}
}
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());
}
}
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);
}
}
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;
}
Aggregations