use of org.wso2.carbon.apimgt.api.model.policy.Pipeline in project carbon-apimgt by wso2.
the class ThrottlePolicyTemplateBuilder method getPolicyCondition.
/**
* Produces final condition inside a pipeline
*
* @param conditions set of conditions
* @return combined condition string
*/
private static String getPolicyCondition(Set<Condition> conditions) {
StringBuilder conditionString = new StringBuilder();
int i = 0;
for (Condition condition : conditions) {
org.wso2.carbon.apimgt.api.model.policy.Condition mappedCondition = PolicyMappingUtil.mapCondition(condition);
if (i == 0) {
conditionString.append(mappedCondition.getCondition());
} else {
conditionString.append(" AND ").append(mappedCondition.getCondition());
}
i++;
}
return conditionString.toString();
}
use of org.wso2.carbon.apimgt.api.model.policy.Pipeline in project carbon-apimgt by wso2.
the class ThrottlePolicyTemplateBuilder method getPolicyConditionForDefault.
/**
* Produces final condition inside a pipeline for default policy with null string
*
* @param conditions set of conditions
* @return default policy condition string
*/
private static String getPolicyConditionForDefault(Set<Condition> conditions) {
StringBuilder conditionString = new StringBuilder();
int i = 0;
for (Condition condition : conditions) {
org.wso2.carbon.apimgt.api.model.policy.Condition mappedCondition = PolicyMappingUtil.mapCondition(condition);
String conditionStringComplete = mappedCondition.getCondition();
if (i == 0) {
conditionString.append(conditionStringComplete);
} else {
conditionString.append(" AND ").append(conditionStringComplete);
}
i++;
}
return conditionString.toString();
}
use of org.wso2.carbon.apimgt.api.model.policy.Pipeline in project carbon-apimgt by wso2.
the class ThrottlePolicyTemplateBuilder method getThrottlePolicyForAPILevel.
/**
* Generate policy for API level throttling
*
* @param policy Policy with level 'api'. Policy can have multiple pipelines and a default condition which will be
* used as else condition
* @return a Map containing a set of policies for each condition group
* @throws APITemplateException if failed to generate policy
*/
public Map<String, String> getThrottlePolicyForAPILevel(ApiPolicy policy) throws APITemplateException {
if (log.isDebugEnabled()) {
log.debug("Generating policy for API Level :" + policy.toString());
}
Map<String, String> policyArray = new HashMap<>();
try {
VelocityEngine velocityengine = new VelocityEngine();
APIUtil.initializeVelocityContext(velocityengine);
velocityengine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome());
velocityengine.init();
Template template = velocityengine.getTemplate(getTemplatePathForAPI());
StringWriter writer;
VelocityContext context;
if (policy.getConditionGroups() != null) {
for (APIPolicyConditionGroup conditionGroup : policy.getConditionGroups()) {
if (conditionGroup.getDefaultLimit() == null) {
continue;
}
context = new VelocityContext();
setConstantContext(context);
context.put("policy", policy);
context.put("quotaPolicy", conditionGroup.getDefaultLimit());
context.put("pipeline", "condition_" + conditionGroup.getConditionGroupId());
String conditionString = getPolicyCondition(conditionGroup.getCondition());
JSONArray conditions = new JSONArray();
conditions.add(getPolicyConditionJson(conditionGroup.getCondition()));
context.put("condition", " AND " + conditionString);
context.put("evaluatedConditions", new String(Base64.encodeBase64(conditions.toJSONString().getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8));
writer = new StringWriter();
template.merge(context, writer);
if (log.isDebugEnabled()) {
log.debug("Policy : " + writer.toString());
}
String policyName = policy.getTenantDomain() + APIConstants.DELEM_UNDERSCORE + PolicyConstants.POLICY_LEVEL_RESOURCE + APIConstants.DELEM_UNDERSCORE + policy.getName() + APIConstants.THROTTLE_POLICY_CONDITION + conditionGroup.getConditionGroupId();
policyArray.put(policyName, writer.toString());
}
}
} catch (VelocityException e) {
log.error("Velocity Error", e);
throw new APITemplateException("Velocity Error", e);
}
return policyArray;
}
use of org.wso2.carbon.apimgt.api.model.policy.Pipeline in project carbon-apimgt by wso2.
the class PolicyDAOImpl method setQueryParameterConditions.
/**
* Add Query parameter conditions of pipeline with pipeline Id: <code>pipelineId</code> to a
* provided {@link Condition} array
*
* @param pipelineId Id of the pipeline
* @param conditions condition array to populate
* @throws SQLException
*/
private void setQueryParameterConditions(int pipelineId, ArrayList<Condition> conditions, Connection connection) throws SQLException {
final String query = "SELECT " + "PARAMETER_NAME,PARAMETER_VALUE , IS_PARAM_MAPPING " + "FROM " + "AM_QUERY_PARAMETER_CONDITION " + "WHERE " + "CONDITION_GROUP_ID =?";
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setInt(1, pipelineId);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
QueryParameterCondition queryParameterCondition = new QueryParameterCondition();
queryParameterCondition.setParameter(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_PARAMETER_NAME));
queryParameterCondition.setValue(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_PARAMETER_VALUE));
queryParameterCondition.setInvertCondition(resultSet.getBoolean(APIMgtConstants.ThrottlePolicyConstants.COLUMN_IS_PARAM_MAPPING));
conditions.add(queryParameterCondition);
}
}
}
}
use of org.wso2.carbon.apimgt.api.model.policy.Pipeline in project carbon-apimgt by wso2.
the class PolicyDAOImpl method getPipelines.
/**
* Retrieves list of pipelines for the policy with policy Id: <code>policyId</code>
*
* @param policyId policy id of the pipelines
* @return list of pipelines
* @throws SQLException
*/
private ArrayList<Pipeline> getPipelines(String policyId, Connection connection) throws SQLException {
ArrayList<Pipeline> pipelines = new ArrayList<>();
final String sqlQuery = "SELECT CONDITION_GROUP_ID,QUOTA_TYPE,QUOTA,QUOTA_UNIT,UNIT_TIME,TIME_UNIT," + "DESCRIPTION FROM AM_CONDITION_GROUP WHERE UUID =?";
try (PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
int unitTime;
int quota;
int pipelineId;
String timeUnit;
String quotaUnit;
String description;
preparedStatement.setString(1, policyId);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
Pipeline pipeline = new Pipeline();
ArrayList<Condition> conditions;
QuotaPolicy quotaPolicy = new QuotaPolicy();
quotaPolicy.setType(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE));
timeUnit = resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_TIME_UNIT);
quotaUnit = resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_QUOTA_UNIT);
unitTime = resultSet.getInt(APIMgtConstants.ThrottlePolicyConstants.COLUMN_UNIT_TIME);
quota = resultSet.getInt(APIMgtConstants.ThrottlePolicyConstants.COLUMN_QUOTA);
pipelineId = resultSet.getInt(APIMgtConstants.ThrottlePolicyConstants.COLUMN_CONDITION_ID);
description = resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_DESCRIPTION);
if (PolicyConstants.REQUEST_COUNT_TYPE.equals(quotaPolicy.getType())) {
RequestCountLimit requestCountLimit = new RequestCountLimit(timeUnit, unitTime, quota);
quotaPolicy.setLimit(requestCountLimit);
} else if (PolicyConstants.BANDWIDTH_TYPE.equals(quotaPolicy.getType())) {
BandwidthLimit bandwidthLimit = new BandwidthLimit(timeUnit, unitTime, quota, quotaUnit);
quotaPolicy.setLimit(bandwidthLimit);
}
conditions = getConditions(pipelineId, connection);
pipeline.setConditions(conditions);
pipeline.setQuotaPolicy(quotaPolicy);
pipeline.setId(pipelineId);
pipeline.setDescription(description);
pipelines.add(pipeline);
}
}
}
return pipelines;
}
Aggregations