use of org.wso2.carbon.apimgt.keymgt.model.entity.APIPolicyList in project carbon-apimgt by wso2.
the class PolicyRetriever method getApiPolicy.
/**
* Get a API policy given the name.
*
* @param policyName policy name
* @param tenantDomain tenant domain
* @return API policy
* @throws ThrottlePolicyDeployerException if failure occurs
*/
public ApiPolicy getApiPolicy(String policyName, String tenantDomain) throws ThrottlePolicyDeployerException {
String path = APIConstants.SubscriptionValidationResources.API_POLICIES + "?policyName=" + policyName;
ApiPolicyList apiPolicyList = getPolicies(path, tenantDomain, ApiPolicyList.class);
if (apiPolicyList.getList() != null && !apiPolicyList.getList().isEmpty()) {
return apiPolicyList.getList().get(0);
}
return null;
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.APIPolicyList in project carbon-apimgt by wso2.
the class PolicyUtilTest method testDeployAllPolicies.
@Test
public void testDeployAllPolicies() throws ExecutionPlanConfigurationException, ThrottlePolicyDeployerException, ExecutionPlanDependencyValidationException {
ExecutionPlanConfigurationException executionPlanConfigurationException = Mockito.mock(ExecutionPlanConfigurationException.class);
Mockito.when(eventProcessorService.getActiveExecutionPlan(Mockito.anyString())).thenThrow(executionPlanConfigurationException);
Map<String, ExecutionPlanConfiguration> executionPlanConfigurationMap = new HashMap<>();
ExecutionPlanConfiguration executionPlanConfiguration = Mockito.mock(ExecutionPlanConfiguration.class);
executionPlanConfigurationMap.put("sample_policy", executionPlanConfiguration);
Mockito.when(eventProcessorService.getAllActiveExecutionConfigurations()).thenReturn(executionPlanConfigurationMap);
SubscriptionPolicyList subscriptionPolicyList = new SubscriptionPolicyList();
SubscriptionPolicy subscriptionPolicy = TestUtil.getPolicySubLevel();
List<SubscriptionPolicy> subscriptionPolicies = new ArrayList<>();
subscriptionPolicies.add(subscriptionPolicy);
subscriptionPolicyList.setList(subscriptionPolicies);
Mockito.when(policyRetriever.getAllSubscriptionPolicies()).thenReturn(subscriptionPolicyList);
ApplicationPolicyList applicationPolicyList = new ApplicationPolicyList();
ApplicationPolicy applicationPolicy = TestUtil.getPolicyAppLevel();
List<ApplicationPolicy> applicationPolicies = new ArrayList<>();
applicationPolicies.add(applicationPolicy);
applicationPolicyList.setList(applicationPolicies);
Mockito.when(policyRetriever.getAllApplicationPolicies()).thenReturn(applicationPolicyList);
ApiPolicyList apiPolicyList = new ApiPolicyList();
ApiPolicy apiPolicy = TestUtil.getPolicyAPILevel();
List<ApiPolicy> apiPolicies = new ArrayList<>();
apiPolicies.add(apiPolicy);
apiPolicyList.setList(apiPolicies);
Mockito.when(policyRetriever.getAllApiPolicies()).thenReturn(apiPolicyList);
GlobalPolicyList globalPolicyList = new GlobalPolicyList();
GlobalPolicy globalPolicy = TestUtil.getPolicyGlobalLevel();
List<GlobalPolicy> globalPolicies = new ArrayList<>();
globalPolicies.add(globalPolicy);
globalPolicyList.setList(globalPolicies);
Mockito.when(policyRetriever.getAllGlobalPolicies()).thenReturn(globalPolicyList);
PolicyUtil.deployAllPolicies();
Mockito.verify(eventProcessorService, Mockito.times(5)).deployExecutionPlan(Mockito.anyString());
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.APIPolicyList in project carbon-apimgt by wso2.
the class PolicyUtil method deployAllPolicies.
/**
* Deploy all the throttle policies retrieved from the database in the Traffic Manager.
*/
public static void deployAllPolicies() {
// Undeploy all existing policies
undeployAllPolicies();
PolicyRetriever policyRetriever = new PolicyRetriever();
try {
// Deploy all the policies retrieved from the database
SubscriptionPolicyList subscriptionPolicies = policyRetriever.getAllSubscriptionPolicies();
for (SubscriptionPolicy subscriptionPolicy : subscriptionPolicies.getList()) {
if (!(APIConstants.UNLIMITED_TIER.equalsIgnoreCase(subscriptionPolicy.getName()) || APIConstants.DEFAULT_SUB_POLICY_ASYNC_UNLIMITED.equalsIgnoreCase(subscriptionPolicy.getName()) || APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_UNLIMITED.equalsIgnoreCase(subscriptionPolicy.getName()))) {
deployPolicy(subscriptionPolicy, null);
}
}
ApplicationPolicyList applicationPolicies = policyRetriever.getAllApplicationPolicies();
for (ApplicationPolicy applicationPolicy : applicationPolicies.getList()) {
if (!APIConstants.UNLIMITED_TIER.equalsIgnoreCase(applicationPolicy.getName())) {
deployPolicy(applicationPolicy, null);
}
}
ApiPolicyList apiPolicies = policyRetriever.getAllApiPolicies();
for (ApiPolicy apiPolicy : apiPolicies.getList()) {
if (!APIConstants.UNLIMITED_TIER.equalsIgnoreCase(apiPolicy.getName())) {
deployPolicy(apiPolicy, null);
}
}
GlobalPolicyList globalPolicies = policyRetriever.getAllGlobalPolicies();
for (GlobalPolicy globalPolicy : globalPolicies.getList()) {
deployPolicy(globalPolicy, null);
}
} catch (ThrottlePolicyDeployerException e) {
log.error("Error in retrieving throttle policies", e);
}
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.APIPolicyList in project carbon-apimgt by wso2.
the class SubscriptionDataLoaderImpl method getAPIPolicy.
@Override
public ApiPolicy getAPIPolicy(String policyName, String tenantDomain) throws DataLoadingException {
String endPoint = APIConstants.SubscriptionValidationResources.API_POLICIES + "?policyName=" + policyName;
ApiPolicy apiPolicy = new ApiPolicy();
String responseString;
if (log.isDebugEnabled()) {
log.debug("getAPIPolicy for " + policyName + " for tenant " + tenantDomain);
}
try {
responseString = invokeService(endPoint, tenantDomain);
} catch (IOException e) {
String msg = "Error while executing the http client " + endPoint;
log.error(msg, e);
throw new DataLoadingException(msg, e);
}
if (responseString != null && !responseString.isEmpty()) {
APIPolicyList list = new Gson().fromJson(responseString, APIPolicyList.class);
if (list.getList() != null && !list.getList().isEmpty()) {
apiPolicy = list.getList().get(0);
}
}
return apiPolicy;
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.APIPolicyList in project carbon-apimgt by wso2.
the class SubscriptionDataStoreImpl method initializeLoadingTasks.
public void initializeLoadingTasks() {
Runnable apiTask = new PopulateTask<>(apiMap, () -> {
try {
log.debug("Calling loadAllApis. ");
List<API> apiList = new SubscriptionDataLoaderImpl().loadAllApis(tenantDomain);
apiByUUIDMap.clear();
for (API api : apiList) {
apiByUUIDMap.put(api.getUuid(), api);
String key = api.getApiName().concat(":").concat(api.getApiVersion());
apiNameVersionMap.put(key, api);
}
apisInitialized = true;
return apiList;
} catch (APIManagementException e) {
log.error("Exception while loading APIs " + e);
}
return null;
});
executorService.schedule(apiTask, 0, TimeUnit.SECONDS);
Runnable subscriptionLoadingTask = new PopulateTask<>(subscriptionMap, () -> {
try {
log.debug("Calling loadAllSubscriptions.");
return new SubscriptionDataLoaderImpl().loadAllSubscriptions(tenantDomain);
} catch (APIManagementException e) {
log.error("Exception while loading Subscriptions " + e);
}
return null;
});
executorService.schedule(subscriptionLoadingTask, 0, TimeUnit.SECONDS);
Runnable applicationLoadingTask = new PopulateTask<>(applicationMap, () -> {
try {
log.debug("Calling loadAllApplications.");
return new SubscriptionDataLoaderImpl().loadAllApplications(tenantDomain);
} catch (APIManagementException e) {
log.error("Exception while loading Applications " + e);
}
return null;
});
executorService.schedule(applicationLoadingTask, 0, TimeUnit.SECONDS);
Runnable keyMappingsTask = new PopulateTask<>(applicationKeyMappingMap, () -> {
try {
log.debug("Calling loadAllKeyMappings.");
return new SubscriptionDataLoaderImpl().loadAllKeyMappings(tenantDomain);
} catch (APIManagementException e) {
log.error("Exception while loading ApplicationKeyMapping " + e);
}
return null;
});
executorService.schedule(keyMappingsTask, 0, TimeUnit.SECONDS);
Runnable apiPolicyLoadingTask = new PopulateTask<>(apiPolicyMap, () -> {
try {
log.debug("Calling loadAllSubscriptionPolicies.");
List<ApiPolicy> apiPolicyList = new SubscriptionDataLoaderImpl().loadAllAPIPolicies(tenantDomain);
apiPoliciesInitialized = true;
return apiPolicyList;
} catch (APIManagementException e) {
log.error("Exception while loading api Policies " + e);
}
return null;
});
executorService.schedule(apiPolicyLoadingTask, 0, TimeUnit.SECONDS);
Runnable subPolicyLoadingTask = new PopulateTask<>(subscriptionPolicyMap, () -> {
try {
log.debug("Calling loadAllSubscriptionPolicies.");
return new SubscriptionDataLoaderImpl().loadAllSubscriptionPolicies(tenantDomain);
} catch (APIManagementException e) {
log.error("Exception while loading Subscription Policies " + e);
}
return null;
});
executorService.schedule(subPolicyLoadingTask, 0, TimeUnit.SECONDS);
Runnable appPolicyLoadingTask = new PopulateTask<>(appPolicyMap, () -> {
try {
log.debug("Calling loadAllAppPolicies.");
return new SubscriptionDataLoaderImpl().loadAllAppPolicies(tenantDomain);
} catch (APIManagementException e) {
log.error("Exception while loading Application Policies " + e);
}
return null;
});
executorService.schedule(appPolicyLoadingTask, 0, TimeUnit.SECONDS);
Runnable scopesLoadingTask = new PopulateTask<>(scopesMap, () -> {
try {
log.debug("Calling loadAllScopes.");
List<Scope> scopeList = new SubscriptionDataLoaderImpl().loadAllScopes(tenantDomain);
scopesInitialized = true;
return scopeList;
} catch (APIManagementException e) {
log.error("Exception while loading Scopes " + e);
}
return null;
});
executorService.schedule(scopesLoadingTask, 0, TimeUnit.SECONDS);
}
Aggregations