use of org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit in project carbon-apimgt by wso2.
the class ApiMgtDAO method updateSubscriptionPolicy.
/**
* Updates Subscription level policy.
* <p>policy name and tenant id should be specified in <code>policy</code></p>
*
* @param policy updated policy object
* @throws APIManagementException
*/
public void updateSubscriptionPolicy(SubscriptionPolicy policy) throws APIManagementException {
Connection connection = null;
PreparedStatement updateStatement = null;
boolean hasCustomAttrib = false;
String updateQuery;
try {
if (policy.getCustomAttributes() != null) {
hasCustomAttrib = true;
}
if (!StringUtils.isBlank(policy.getPolicyName()) && policy.getTenantId() != -1) {
updateQuery = SQLConstants.UPDATE_SUBSCRIPTION_POLICY_SQL;
if (hasCustomAttrib) {
updateQuery = SQLConstants.UPDATE_SUBSCRIPTION_POLICY_WITH_CUSTOM_ATTRIBUTES_SQL;
}
} else if (!StringUtils.isBlank(policy.getUUID())) {
updateQuery = SQLConstants.UPDATE_SUBSCRIPTION_POLICY_BY_UUID_SQL;
if (hasCustomAttrib) {
updateQuery = SQLConstants.UPDATE_SUBSCRIPTION_POLICY_WITH_CUSTOM_ATTRIBUTES_BY_UUID_SQL;
}
} else {
String errorMsg = "Policy object doesn't contain mandatory parameters. At least UUID or Name,Tenant Id" + " should be provided. Name: " + policy.getPolicyName() + ", Tenant Id: " + policy.getTenantId() + ", UUID: " + policy.getUUID();
log.error(errorMsg);
throw new APIManagementException(errorMsg);
}
connection = APIMgtDBUtil.getConnection();
connection.setAutoCommit(false);
updateStatement = connection.prepareStatement(updateQuery);
if (!StringUtils.isEmpty(policy.getDisplayName())) {
updateStatement.setString(1, policy.getDisplayName());
} else {
updateStatement.setString(1, policy.getPolicyName());
}
updateStatement.setString(2, policy.getDescription());
updateStatement.setString(3, policy.getDefaultQuotaPolicy().getType());
if (PolicyConstants.REQUEST_COUNT_TYPE.equalsIgnoreCase(policy.getDefaultQuotaPolicy().getType())) {
RequestCountLimit limit = (RequestCountLimit) policy.getDefaultQuotaPolicy().getLimit();
updateStatement.setLong(4, limit.getRequestCount());
updateStatement.setString(5, null);
} else if (PolicyConstants.BANDWIDTH_TYPE.equalsIgnoreCase(policy.getDefaultQuotaPolicy().getType())) {
BandwidthLimit limit = (BandwidthLimit) policy.getDefaultQuotaPolicy().getLimit();
updateStatement.setLong(4, limit.getDataAmount());
updateStatement.setString(5, limit.getDataUnit());
} else if (PolicyConstants.EVENT_COUNT_TYPE.equalsIgnoreCase(policy.getDefaultQuotaPolicy().getType())) {
EventCountLimit limit = (EventCountLimit) policy.getDefaultQuotaPolicy().getLimit();
updateStatement.setLong(4, limit.getEventCount());
updateStatement.setString(5, null);
}
updateStatement.setLong(6, policy.getDefaultQuotaPolicy().getLimit().getUnitTime());
updateStatement.setString(7, policy.getDefaultQuotaPolicy().getLimit().getTimeUnit());
updateStatement.setInt(8, policy.getRateLimitCount());
updateStatement.setString(9, policy.getRateLimitTimeUnit());
updateStatement.setBoolean(10, policy.isStopOnQuotaReach());
updateStatement.setInt(11, policy.getGraphQLMaxDepth());
updateStatement.setInt(12, policy.getGraphQLMaxComplexity());
updateStatement.setString(13, policy.getBillingPlan());
if (hasCustomAttrib) {
long lengthOfStream = policy.getCustomAttributes().length;
updateStatement.setBinaryStream(14, new ByteArrayInputStream(policy.getCustomAttributes()), lengthOfStream);
if (!StringUtils.isBlank(policy.getPolicyName()) && policy.getTenantId() != -1) {
updateStatement.setString(15, policy.getMonetizationPlan());
updateStatement.setString(16, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.FIXED_PRICE));
updateStatement.setString(17, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.BILLING_CYCLE));
updateStatement.setString(18, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.PRICE_PER_REQUEST));
updateStatement.setString(19, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.CURRENCY));
updateStatement.setInt(20, policy.getSubscriberCount());
updateStatement.setString(21, policy.getPolicyName());
updateStatement.setInt(22, policy.getTenantId());
} else if (!StringUtils.isBlank(policy.getUUID())) {
updateStatement.setString(15, policy.getMonetizationPlan());
updateStatement.setString(16, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.FIXED_PRICE));
updateStatement.setString(17, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.BILLING_CYCLE));
updateStatement.setString(18, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.PRICE_PER_REQUEST));
updateStatement.setString(19, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.CURRENCY));
updateStatement.setInt(20, policy.getSubscriberCount());
updateStatement.setString(21, policy.getUUID());
}
} else {
if (!StringUtils.isBlank(policy.getPolicyName()) && policy.getTenantId() != -1) {
updateStatement.setString(14, policy.getMonetizationPlan());
updateStatement.setString(15, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.FIXED_PRICE));
updateStatement.setString(16, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.BILLING_CYCLE));
updateStatement.setString(17, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.PRICE_PER_REQUEST));
updateStatement.setString(18, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.CURRENCY));
updateStatement.setInt(19, policy.getSubscriberCount());
updateStatement.setString(20, policy.getPolicyName());
updateStatement.setInt(21, policy.getTenantId());
} else if (!StringUtils.isBlank(policy.getUUID())) {
updateStatement.setString(14, policy.getMonetizationPlan());
updateStatement.setString(15, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.FIXED_PRICE));
updateStatement.setString(16, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.BILLING_CYCLE));
updateStatement.setString(17, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.PRICE_PER_REQUEST));
updateStatement.setString(18, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.CURRENCY));
updateStatement.setInt(19, policy.getSubscriberCount());
updateStatement.setString(20, policy.getUUID());
}
}
updateStatement.executeUpdate();
connection.commit();
} catch (SQLException e) {
if (connection != null) {
try {
connection.rollback();
} catch (SQLException ex) {
// Rollback failed. Exception will be thrown later for upper exception
log.error("Failed to rollback the update Subscription Policy: " + policy.toString(), ex);
}
}
handleException("Failed to update subscription policy: " + policy.getPolicyName() + '-' + policy.getTenantId(), e);
} finally {
APIMgtDBUtil.closeAllConnections(updateStatement, connection, null);
}
}
use of org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit in project carbon-apimgt by wso2.
the class APIUtil method getPolicyByName.
public static Tier getPolicyByName(String policyLevel, String policyName, String organization) throws APIManagementException {
int tenantId = APIUtil.getInternalOrganizationId(organization);
ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
Policy policy;
if (PolicyConstants.POLICY_LEVEL_SUB.equalsIgnoreCase(policyLevel)) {
policy = apiMgtDAO.getSubscriptionPolicy(policyName, tenantId);
} else if (PolicyConstants.POLICY_LEVEL_API.equalsIgnoreCase(policyLevel)) {
policy = apiMgtDAO.getAPIPolicy(policyName, tenantId);
} else if (PolicyConstants.POLICY_LEVEL_APP.equalsIgnoreCase(policyLevel)) {
policy = apiMgtDAO.getApplicationPolicy(policyName, tenantId);
} else {
throw new APIManagementException("No such a policy type : " + policyLevel);
}
if (policy != null) {
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());
}
return 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);
}
return tier;
}
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit 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.api.model.policy.BandwidthLimit in project carbon-apimgt by wso2.
the class SubscriptionValidationDataUtil method getThrottleLimitDTO.
/**
* Converts a quota policy object of a policy into a Throttle Limit DTO object.
*
* @param policy policy model object
* @return Throttle Limit DTO
*/
private static ThrottleLimitDTO getThrottleLimitDTO(Policy policy) {
QuotaPolicy quotaPolicy = policy.getQuotaPolicy();
ThrottleLimitDTO defaultLimit = new ThrottleLimitDTO();
defaultLimit.setQuotaType(quotaPolicy.getType());
if (PolicyConstants.REQUEST_COUNT_TYPE.equals(quotaPolicy.getType())) {
RequestCountLimit requestCountLimit = (RequestCountLimit) quotaPolicy.getLimit();
defaultLimit.setRequestCount(fromRequestCountLimitToDTO(requestCountLimit));
} else if (PolicyConstants.BANDWIDTH_TYPE.equals(quotaPolicy.getType())) {
BandwidthLimit bandwidthLimit = (BandwidthLimit) quotaPolicy.getLimit();
defaultLimit.setBandwidth(fromBandwidthLimitToDTO(bandwidthLimit));
} else if (PolicyConstants.EVENT_COUNT_TYPE.equals(quotaPolicy.getType())) {
EventCountLimit eventCountLimit = (EventCountLimit) quotaPolicy.getLimit();
defaultLimit.setEventCount(fromEventCountLimitToDTO(eventCountLimit));
}
return defaultLimit;
}
use of org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit in project carbon-apimgt by wso2.
the class SubscriptionValidationDAO method setCommonProperties.
private void setCommonProperties(APIPolicyConditionGroup apiPolicyConditionGroup, ResultSet resultSet) throws SQLException {
QuotaPolicy quotaPolicy = new QuotaPolicy();
quotaPolicy.setType(resultSet.getString(ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE));
if (quotaPolicy.getType() != null) {
if (PolicyConstants.REQUEST_COUNT_TYPE.equalsIgnoreCase(quotaPolicy.getType())) {
RequestCountLimit reqLimit = new RequestCountLimit();
reqLimit.setUnitTime(resultSet.getInt(ThrottlePolicyConstants.COLUMN_UNIT_TIME));
reqLimit.setTimeUnit(resultSet.getString(ThrottlePolicyConstants.COLUMN_TIME_UNIT));
reqLimit.setRequestCount(resultSet.getInt(ThrottlePolicyConstants.COLUMN_QUOTA));
quotaPolicy.setLimit(reqLimit);
} else if (PolicyConstants.BANDWIDTH_TYPE.equalsIgnoreCase(quotaPolicy.getType())) {
BandwidthLimit bandLimit = new BandwidthLimit();
bandLimit.setUnitTime(resultSet.getInt(ThrottlePolicyConstants.COLUMN_UNIT_TIME));
bandLimit.setTimeUnit(resultSet.getString(ThrottlePolicyConstants.COLUMN_TIME_UNIT));
bandLimit.setDataAmount(resultSet.getInt(ThrottlePolicyConstants.COLUMN_QUOTA));
bandLimit.setDataUnit(resultSet.getString(ThrottlePolicyConstants.COLUMN_QUOTA_UNIT));
quotaPolicy.setLimit(bandLimit);
}
apiPolicyConditionGroup.setQuotaPolicy(quotaPolicy);
}
}
Aggregations