use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.
the class APIUtil method getTiersFromPolicies.
public static Map<String, Tier> getTiersFromPolicies(String policyLevel, int tenantId) throws APIManagementException {
Map<String, Tier> tierMap = new TreeMap<String, Tier>();
ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
Policy[] policies;
if (PolicyConstants.POLICY_LEVEL_SUB.equalsIgnoreCase(policyLevel)) {
policies = apiMgtDAO.getSubscriptionPolicies(tenantId);
} else if (PolicyConstants.POLICY_LEVEL_API.equalsIgnoreCase(policyLevel)) {
policies = apiMgtDAO.getAPIPolicies(tenantId);
} else if (PolicyConstants.POLICY_LEVEL_APP.equalsIgnoreCase(policyLevel)) {
policies = apiMgtDAO.getApplicationPolicies(tenantId);
} else {
throw new APIManagementException("No such a policy type : " + policyLevel);
}
for (Policy policy : policies) {
if (!APIConstants.UNLIMITED_TIER.equalsIgnoreCase(policy.getPolicyName())) {
Tier tier = new Tier(policy.getPolicyName());
tier.setDescription(policy.getDescription());
tier.setDisplayName(policy.getDisplayName());
Limit limit = policy.getDefaultQuotaPolicy().getLimit();
tier.setTimeUnit(limit.getTimeUnit());
tier.setUnitTime(limit.getUnitTime());
tier.setQuotaPolicyType(policy.getDefaultQuotaPolicy().getType());
// If the policy is a subscription policy
if (policy instanceof SubscriptionPolicy) {
SubscriptionPolicy subscriptionPolicy = (SubscriptionPolicy) policy;
tier.setRateLimitCount(subscriptionPolicy.getRateLimitCount());
tier.setRateLimitTimeUnit(subscriptionPolicy.getRateLimitTimeUnit());
setBillingPlanAndCustomAttributesToTier(subscriptionPolicy, tier);
if (StringUtils.equals(subscriptionPolicy.getBillingPlan(), APIConstants.COMMERCIAL_TIER_PLAN)) {
tier.setMonetizationAttributes(subscriptionPolicy.getMonetizationPlanProperties());
}
}
if (limit instanceof RequestCountLimit) {
RequestCountLimit countLimit = (RequestCountLimit) limit;
tier.setRequestsPerMin(countLimit.getRequestCount());
tier.setRequestCount(countLimit.getRequestCount());
} else if (limit instanceof BandwidthLimit) {
BandwidthLimit bandwidthLimit = (BandwidthLimit) limit;
tier.setRequestsPerMin(bandwidthLimit.getDataAmount());
tier.setRequestCount(bandwidthLimit.getDataAmount());
tier.setBandwidthDataUnit(bandwidthLimit.getDataUnit());
} else {
EventCountLimit eventCountLimit = (EventCountLimit) limit;
tier.setRequestCount(eventCountLimit.getEventCount());
tier.setRequestsPerMin(eventCountLimit.getEventCount());
}
if (PolicyConstants.POLICY_LEVEL_SUB.equalsIgnoreCase(policyLevel)) {
tier.setTierPlan(((SubscriptionPolicy) policy).getBillingPlan());
}
tierMap.put(policy.getPolicyName(), tier);
} else {
if (APIUtil.isEnabledUnlimitedTier()) {
Tier tier = new Tier(policy.getPolicyName());
tier.setDescription(policy.getDescription());
tier.setDisplayName(policy.getDisplayName());
tier.setRequestsPerMin(Integer.MAX_VALUE);
tier.setRequestCount(Integer.MAX_VALUE);
if (isUnlimitedTierPaid(getTenantDomainFromTenantId(tenantId))) {
tier.setTierPlan(APIConstants.COMMERCIAL_TIER_PLAN);
} else {
tier.setTierPlan(APIConstants.BILLING_PLAN_FREE);
}
tierMap.put(policy.getPolicyName(), tier);
}
}
}
if (PolicyConstants.POLICY_LEVEL_SUB.equalsIgnoreCase(policyLevel)) {
tierMap.remove(APIConstants.UNAUTHENTICATED_TIER);
}
return tierMap;
}
use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.
the class APIUtil method loadCommonOperationPolicies.
/**
* Load the common policies for the organization at the first startup. This will only copy the policies to the
* database if the total policies for this organization is zero.
*
* @param organization organization name
*/
public static void loadCommonOperationPolicies(String organization) throws APIManagementException {
ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
int policyCount = apiMgtDAO.getOperationPolicyCount(organization);
if (policyCount == 0) {
String policySpecLocation = CarbonUtils.getCarbonHome() + File.separator + APIConstants.COMMON_OPERATION_POLICY_SPECIFICATIONS_LOCATION;
String policyDefinitionLocation = CarbonUtils.getCarbonHome() + File.separator + APIConstants.COMMON_OPERATION_POLICY_DEFINITIONS_LOCATION;
File policiesDir = new File(policySpecLocation);
File[] files = policiesDir.listFiles();
if (files != null) {
for (File file : files) {
String jsonContent;
try {
jsonContent = FileUtils.readFileToString(file);
OperationPolicySpecification policySpec = getValidatedOperationPolicySpecification(jsonContent);
if (policySpec != null) {
OperationPolicyData policyData = new OperationPolicyData();
policyData.setSpecification(policySpec);
policyData.setOrganization(organization);
OperationPolicyDefinition synapsePolicyDefinition = getOperationPolicyDefinitionFromFile(policyDefinitionLocation, policySpec.getName(), APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION);
if (synapsePolicyDefinition != null) {
synapsePolicyDefinition.setGatewayType(OperationPolicyDefinition.GatewayType.Synapse);
policyData.setSynapsePolicyDefinition(synapsePolicyDefinition);
}
OperationPolicyDefinition ccPolicyDefinition = getOperationPolicyDefinitionFromFile(policyDefinitionLocation, policySpec.getName(), APIConstants.CC_POLICY_DEFINITION_EXTENSION);
if (ccPolicyDefinition != null) {
ccPolicyDefinition.setGatewayType(OperationPolicyDefinition.GatewayType.ChoreoConnect);
policyData.setCcPolicyDefinition(ccPolicyDefinition);
}
policyData.setMd5Hash(getMd5OfOperationPolicy(policyData));
apiMgtDAO.addCommonOperationPolicy(policyData);
log.info("Common operation policy " + policySpec.getName() + "_" + policySpec.getVersion() + " was added to the organization " + organization + " successfully");
}
} catch (IOException | APIManagementException e) {
log.error("Invalid policy specification for file " + file.getName() + ".Hence skipped from importing as a common operation policy.", e);
}
}
}
}
}
use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.
the class APIUtil method addDefaultSuperTenantAdvancedThrottlePolicies.
public static void addDefaultSuperTenantAdvancedThrottlePolicies() throws APIManagementException {
int tenantId = MultitenantConstants.SUPER_TENANT_ID;
ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
/* Check if 'Unlimited' policy is available in AM_POLICY_APPLICATION table, to determine whether the default policies are loaded into the database at lease once.
If yes, default policies won't be added to database again.
*/
if (apiMgtDAO.isPolicyExist(PolicyConstants.POLICY_LEVEL_APP, tenantId, APIConstants.DEFAULT_APP_POLICY_UNLIMITED)) {
log.debug("Default Throttling Policies are not written into the database again, as they were added once at initial server startup");
return;
}
long[] requestCount = new long[] { 50, 20, 10, Integer.MAX_VALUE };
// Adding application level throttle policies
String[] appPolicies = new String[] { APIConstants.DEFAULT_APP_POLICY_FIFTY_REQ_PER_MIN, APIConstants.DEFAULT_APP_POLICY_TWENTY_REQ_PER_MIN, APIConstants.DEFAULT_APP_POLICY_TEN_REQ_PER_MIN, APIConstants.DEFAULT_APP_POLICY_UNLIMITED };
String[] appPolicyDecs = new String[] { APIConstants.DEFAULT_APP_POLICY_LARGE_DESC, APIConstants.DEFAULT_APP_POLICY_MEDIUM_DESC, APIConstants.DEFAULT_APP_POLICY_SMALL_DESC, APIConstants.DEFAULT_APP_POLICY_UNLIMITED_DESC };
String policyName;
// Add application level throttle policies
for (int i = 0; i < appPolicies.length; i++) {
policyName = appPolicies[i];
if (!apiMgtDAO.isPolicyExist(PolicyConstants.POLICY_LEVEL_APP, tenantId, policyName)) {
ApplicationPolicy applicationPolicy = new ApplicationPolicy(policyName);
applicationPolicy.setDisplayName(policyName);
applicationPolicy.setDescription(appPolicyDecs[i]);
applicationPolicy.setTenantId(tenantId);
applicationPolicy.setDeployed(true);
QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
RequestCountLimit requestCountLimit = new RequestCountLimit();
requestCountLimit.setRequestCount(requestCount[i]);
requestCountLimit.setUnitTime(1);
requestCountLimit.setTimeUnit(APIConstants.TIME_UNIT_MINUTE);
defaultQuotaPolicy.setType(PolicyConstants.REQUEST_COUNT_TYPE);
defaultQuotaPolicy.setLimit(requestCountLimit);
applicationPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
apiMgtDAO.addApplicationPolicy(applicationPolicy);
}
}
// Adding Subscription level policies
long[] requestCountSubPolicies = new long[] { 5000, 2000, 1000, 500, Integer.MAX_VALUE };
String[] subPolicies = new String[] { APIConstants.DEFAULT_SUB_POLICY_GOLD, APIConstants.DEFAULT_SUB_POLICY_SILVER, APIConstants.DEFAULT_SUB_POLICY_BRONZE, APIConstants.DEFAULT_SUB_POLICY_UNAUTHENTICATED, APIConstants.DEFAULT_SUB_POLICY_UNLIMITED };
String[] subPolicyDecs = new String[] { APIConstants.DEFAULT_SUB_POLICY_GOLD_DESC, APIConstants.DEFAULT_SUB_POLICY_SILVER_DESC, APIConstants.DEFAULT_SUB_POLICY_BRONZE_DESC, APIConstants.DEFAULT_SUB_POLICY_UNAUTHENTICATED_DESC, APIConstants.DEFAULT_SUB_POLICY_UNLIMITED_DESC };
for (int i = 0; i < subPolicies.length; i++) {
policyName = subPolicies[i];
if (!apiMgtDAO.isPolicyExist(PolicyConstants.POLICY_LEVEL_SUB, tenantId, policyName)) {
SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(policyName);
subscriptionPolicy.setDisplayName(policyName);
subscriptionPolicy.setDescription(subPolicyDecs[i]);
subscriptionPolicy.setTenantId(tenantId);
subscriptionPolicy.setDeployed(true);
QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
RequestCountLimit requestCountLimit = new RequestCountLimit();
requestCountLimit.setRequestCount(requestCountSubPolicies[i]);
requestCountLimit.setUnitTime(1);
requestCountLimit.setTimeUnit(APIConstants.TIME_UNIT_MINUTE);
defaultQuotaPolicy.setType(PolicyConstants.REQUEST_COUNT_TYPE);
defaultQuotaPolicy.setLimit(requestCountLimit);
subscriptionPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
subscriptionPolicy.setStopOnQuotaReach(true);
subscriptionPolicy.setBillingPlan(APIConstants.BILLING_PLAN_FREE);
apiMgtDAO.addSubscriptionPolicy(subscriptionPolicy);
}
}
// Adding Event based subscription level policies for async policies (WS & SSE)
long[] eventCountSubPolicyValues = new long[] { 50000, 25000, 5000, Integer.MAX_VALUE };
String[] eventCountSubPolicyNames = new String[] { APIConstants.DEFAULT_SUB_POLICY_ASYNC_GOLD, APIConstants.DEFAULT_SUB_POLICY_ASYNC_SILVER, APIConstants.DEFAULT_SUB_POLICY_ASYNC_BRONZE, APIConstants.DEFAULT_SUB_POLICY_ASYNC_UNLIMITED };
String[] eventCountSubPolicyDescriptions = new String[] { APIConstants.DEFAULT_SUB_POLICY_ASYNC_GOLD_DESC, APIConstants.DEFAULT_SUB_POLICY_ASYNC_SILVER_DESC, APIConstants.DEFAULT_SUB_POLICY_ASYNC_BRONZE_DESC, APIConstants.DEFAULT_SUB_POLICY_ASYNC_UNLIMITED_DESC };
for (int i = 0; i < eventCountSubPolicyNames.length; i++) {
policyName = eventCountSubPolicyNames[i];
if (!apiMgtDAO.isPolicyExist(PolicyConstants.POLICY_LEVEL_SUB, tenantId, policyName)) {
SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(policyName);
subscriptionPolicy.setDisplayName(policyName);
subscriptionPolicy.setDescription(eventCountSubPolicyDescriptions[i]);
subscriptionPolicy.setTenantId(tenantId);
subscriptionPolicy.setDeployed(true);
QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
EventCountLimit eventCountLimit = new EventCountLimit();
eventCountLimit.setEventCount(eventCountSubPolicyValues[i]);
eventCountLimit.setUnitTime(1);
eventCountLimit.setTimeUnit(APIConstants.TIME_UNIT_DAY);
defaultQuotaPolicy.setType(PolicyConstants.EVENT_COUNT_TYPE);
defaultQuotaPolicy.setLimit(eventCountLimit);
subscriptionPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
subscriptionPolicy.setStopOnQuotaReach(true);
subscriptionPolicy.setBillingPlan(APIConstants.BILLING_PLAN_FREE);
apiMgtDAO.addSubscriptionPolicy(subscriptionPolicy);
}
}
// Adding Event based Webhooks API specific policies (WEBSUB)
long[] eventCountWHSubPolicyValues = new long[] { 10000, 5000, 1000, Integer.MAX_VALUE };
int[] subscriptionCountValues = new int[] { 1000, 500, 100, Integer.MAX_VALUE };
String[] eventCountWHSubPolicyNames = new String[] { APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_GOLD, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_SILVER, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_BRONZE, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_UNLIMITED };
String[] eventCountWHSubPolicyDescriptions = new String[] { APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_GOLD_DESC, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_SILVER_DESC, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_BRONZE_DESC, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_UNLIMITED_DESC };
for (int i = 0; i < eventCountWHSubPolicyNames.length; i++) {
policyName = eventCountWHSubPolicyNames[i];
if (!apiMgtDAO.isPolicyExist(PolicyConstants.POLICY_LEVEL_SUB, tenantId, policyName)) {
SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(policyName);
subscriptionPolicy.setDisplayName(policyName);
subscriptionPolicy.setDescription(eventCountWHSubPolicyDescriptions[i]);
subscriptionPolicy.setTenantId(tenantId);
subscriptionPolicy.setDeployed(true);
QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
EventCountLimit eventCountLimit = new EventCountLimit();
eventCountLimit.setEventCount(eventCountWHSubPolicyValues[i]);
eventCountLimit.setUnitTime(1);
eventCountLimit.setTimeUnit(APIConstants.TIME_UNIT_MONTH);
defaultQuotaPolicy.setType(PolicyConstants.EVENT_COUNT_TYPE);
defaultQuotaPolicy.setLimit(eventCountLimit);
subscriptionPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
subscriptionPolicy.setStopOnQuotaReach(true);
subscriptionPolicy.setBillingPlan(APIConstants.BILLING_PLAN_FREE);
subscriptionPolicy.setSubscriberCount(subscriptionCountValues[i]);
apiMgtDAO.addSubscriptionPolicy(subscriptionPolicy);
}
}
// Adding Resource level policies
String[] apiPolicies = new String[] { APIConstants.DEFAULT_API_POLICY_FIFTY_THOUSAND_REQ_PER_MIN, APIConstants.DEFAULT_API_POLICY_TWENTY_THOUSAND_REQ_PER_MIN, APIConstants.DEFAULT_API_POLICY_TEN_THOUSAND_REQ_PER_MIN, APIConstants.DEFAULT_API_POLICY_UNLIMITED };
String[] apiPolicyDecs = new String[] { APIConstants.DEFAULT_API_POLICY_ULTIMATE_DESC, APIConstants.DEFAULT_API_POLICY_PLUS_DESC, APIConstants.DEFAULT_API_POLICY_BASIC_DESC, APIConstants.DEFAULT_API_POLICY_UNLIMITED_DESC };
long[] requestCountApiPolicies = new long[] { 50000, 20000, 10000, Integer.MAX_VALUE };
for (int i = 0; i < apiPolicies.length; i++) {
policyName = apiPolicies[i];
if (!apiMgtDAO.isPolicyExist(PolicyConstants.POLICY_LEVEL_API, tenantId, policyName)) {
APIPolicy apiPolicy = new APIPolicy(policyName);
apiPolicy.setDisplayName(policyName);
apiPolicy.setDescription(apiPolicyDecs[i]);
apiPolicy.setTenantId(tenantId);
apiPolicy.setUserLevel(APIConstants.API_POLICY_API_LEVEL);
apiPolicy.setDeployed(true);
QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
RequestCountLimit requestCountLimit = new RequestCountLimit();
requestCountLimit.setRequestCount(requestCountApiPolicies[i]);
requestCountLimit.setUnitTime(1);
requestCountLimit.setTimeUnit(APIConstants.TIME_UNIT_MINUTE);
defaultQuotaPolicy.setType(PolicyConstants.REQUEST_COUNT_TYPE);
defaultQuotaPolicy.setLimit(requestCountLimit);
apiPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
apiMgtDAO.addAPIPolicy(apiPolicy);
}
}
}
use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.
the class ExternallyDeployedApiNotifier method undeployApiWhenRetiring.
/**
* Undeploy APIs from external gateway when life cycle state changed to retire
*
* @param apiEvent APIEvent to undeploy APIs from external gateway
* @throws NotifierException if error occurs when undeploying APIs from external gateway
*/
private void undeployApiWhenRetiring(APIEvent apiEvent) throws NotifierException {
apiMgtDAO = ApiMgtDAO.getInstance();
Map<String, Environment> gatewayEnvironments = APIUtil.getReadOnlyGatewayEnvironments();
boolean deleted;
String apiId = apiEvent.getUuid();
if (!APIConstants.RETIRED.equals(apiEvent.getApiStatus())) {
return;
}
try {
APIProvider apiProvider = APIManagerFactory.getInstance().getAPIProvider(CarbonContext.getThreadLocalCarbonContext().getUsername());
API api = apiProvider.getAPIbyUUID(apiId, apiMgtDAO.getOrganizationByAPIUUID(apiId));
List<APIRevisionDeployment> test = apiMgtDAO.getAPIRevisionDeploymentsByApiUUID(apiId);
for (APIRevisionDeployment deployment : test) {
String deploymentEnv = deployment.getDeployment();
if (gatewayEnvironments.containsKey(deploymentEnv)) {
ExternalGatewayDeployer deployer = ServiceReferenceHolder.getInstance().getExternalGatewayDeployer(gatewayEnvironments.get(deploymentEnv).getProvider());
if (deployer != null) {
try {
deleted = deployer.undeployWhenRetire(api, gatewayEnvironments.get(deploymentEnv));
if (!deleted) {
throw new NotifierException("Error while deleting API product from Solace broker");
}
} catch (DeployerException e) {
throw new NotifierException(e.getMessage());
}
}
}
}
} catch (APIManagementException e) {
throw new NotifierException(e.getMessage());
}
}
use of org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO in project carbon-apimgt by wso2.
the class APIProviderImplTest method testGetApplicationPolicyByUUID.
@Test
public void testGetApplicationPolicyByUUID() throws APIManagementException {
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
ApplicationPolicy applicationPolicy = Mockito.mock(ApplicationPolicy.class);
Mockito.when(apimgtDAO.getApplicationPolicyByUUID("1111")).thenReturn(applicationPolicy, null);
apiProvider.getApplicationPolicyByUUID("1111");
try {
assertNotNull(apiProvider.getApplicationPolicyByUUID("1111"));
} catch (APIManagementException e) {
assertEquals("Application Policy: 1111 was not found.", e.getMessage());
}
}
Aggregations