Search in sources :

Example 11 with Pipeline

use of org.wso2.carbon.apimgt.core.models.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;
}
Also used : JWTClaimsCondition(org.wso2.carbon.apimgt.core.models.policy.JWTClaimsCondition) Condition(org.wso2.carbon.apimgt.core.models.policy.Condition) QueryParameterCondition(org.wso2.carbon.apimgt.core.models.policy.QueryParameterCondition) IPCondition(org.wso2.carbon.apimgt.core.models.policy.IPCondition) HeaderCondition(org.wso2.carbon.apimgt.core.models.policy.HeaderCondition) RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit) Pipeline(org.wso2.carbon.apimgt.core.models.policy.Pipeline)

Example 12 with Pipeline

use of org.wso2.carbon.apimgt.core.models.policy.Pipeline in project carbon-apimgt by wso2.

the class SampleTestObjectCreator method createNewIPRangePipeline.

/**
 * Creates a new {@link Pipeline} instance
 *
 * @return created Pipeline instance
 */
public static Pipeline createNewIPRangePipeline() {
    IPCondition ipCondition = new IPCondition(PolicyConstants.IP_RANGE_TYPE);
    ipCondition.setStartingIP("10.100.0.105");
    ipCondition.setEndingIP("10.100.0.115");
    Pipeline pipeline = new Pipeline();
    RequestCountLimit requestCountLimit = new RequestCountLimit(TIME_UNIT_SECONDS, 1, 1000);
    QuotaPolicy quotaPolicy = new QuotaPolicy();
    quotaPolicy.setType(REQUEST_COUNT_TYPE);
    quotaPolicy.setLimit(requestCountLimit);
    pipeline.setQuotaPolicy(quotaPolicy);
    pipeline.setConditions(Arrays.asList(ipCondition));
    return pipeline;
}
Also used : IPCondition(org.wso2.carbon.apimgt.core.models.policy.IPCondition) RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) Pipeline(org.wso2.carbon.apimgt.core.models.policy.Pipeline)

Example 13 with Pipeline

use of org.wso2.carbon.apimgt.core.models.policy.Pipeline in project carbon-apimgt by wso2.

the class SampleTestObjectCreator method createDefaultPipelines.

/**
 * create default pipeline for api policy
 *
 * @return list of Pipeline objects is returned
 */
public static List<Pipeline> createDefaultPipelines() {
    // Pipeline 1
    IPCondition ipCondition = new IPCondition(PolicyConstants.IP_RANGE_TYPE);
    ipCondition.setStartingIP("192.168.12.3");
    ipCondition.setEndingIP("192.168.88.19");
    IPCondition ipConditionSpecific = new IPCondition(PolicyConstants.IP_SPECIFIC_TYPE);
    ipConditionSpecific.setSpecificIP("123.42.14.56");
    // adding above conditions to condition list of pipeline 1
    // contains conditions for each pipeline
    List<Condition> conditionsList = new ArrayList<>();
    conditionsList.add(ipCondition);
    conditionsList.add(ipConditionSpecific);
    // set quota policy with bandwidth limit
    BandwidthLimit bandwidthLimit = new BandwidthLimit(TIME_UNIT_MONTH, 1, 1000, PolicyConstants.MB);
    QuotaPolicy quotaPolicy1 = new QuotaPolicy();
    quotaPolicy1.setType(PolicyConstants.BANDWIDTH_TYPE);
    quotaPolicy1.setLimit(bandwidthLimit);
    Pipeline pipeline1 = new Pipeline();
    pipeline1.setConditions(conditionsList);
    pipeline1.setQuotaPolicy(quotaPolicy1);
    // End of pipeline 1 -> Beginning of pipeline 2
    HeaderCondition headerCondition = new HeaderCondition();
    headerCondition.setHeader("Browser");
    headerCondition.setValue("Chrome");
    JWTClaimsCondition jwtClaimsCondition = new JWTClaimsCondition();
    jwtClaimsCondition.setClaimUrl("/path/path2");
    jwtClaimsCondition.setAttribute("attributed");
    QueryParameterCondition queryParameterCondition = new QueryParameterCondition();
    queryParameterCondition.setParameter("Location");
    queryParameterCondition.setValue("Colombo");
    // adding conditions to condition list of pipeline2
    conditionsList = new ArrayList<>();
    conditionsList.add(headerCondition);
    conditionsList.add(jwtClaimsCondition);
    conditionsList.add(queryParameterCondition);
    // pipeline 2 with request count as quota policy
    RequestCountLimit requestCountLimit = new RequestCountLimit(TIME_UNIT_SECONDS, 1, 1000);
    QuotaPolicy quotaPolicy2 = new QuotaPolicy();
    quotaPolicy2.setType(PolicyConstants.REQUEST_COUNT_TYPE);
    quotaPolicy2.setLimit(requestCountLimit);
    Pipeline pipeline2 = new Pipeline();
    pipeline2.setConditions(conditionsList);
    pipeline2.setQuotaPolicy(quotaPolicy2);
    // adding pipelines
    // contains all the default pipelines
    List<Pipeline> pipelineList = new ArrayList<>();
    pipelineList.add(pipeline1);
    pipelineList.add(pipeline2);
    return pipelineList;
}
Also used : JWTClaimsCondition(org.wso2.carbon.apimgt.core.models.policy.JWTClaimsCondition) IPCondition(org.wso2.carbon.apimgt.core.models.policy.IPCondition) Condition(org.wso2.carbon.apimgt.core.models.policy.Condition) QueryParameterCondition(org.wso2.carbon.apimgt.core.models.policy.QueryParameterCondition) HeaderCondition(org.wso2.carbon.apimgt.core.models.policy.HeaderCondition) JWTClaimsCondition(org.wso2.carbon.apimgt.core.models.policy.JWTClaimsCondition) RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) HeaderCondition(org.wso2.carbon.apimgt.core.models.policy.HeaderCondition) ArrayList(java.util.ArrayList) QueryParameterCondition(org.wso2.carbon.apimgt.core.models.policy.QueryParameterCondition) Pipeline(org.wso2.carbon.apimgt.core.models.policy.Pipeline) IPCondition(org.wso2.carbon.apimgt.core.models.policy.IPCondition) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)

Example 14 with Pipeline

use of org.wso2.carbon.apimgt.core.models.policy.Pipeline in project carbon-apimgt by wso2.

the class APIThrottlePolicyTemplateBuilder method getThrottlePolicyTemplateForAPILevelDefaultCondition.

/**
 * Generate default policy for api level throttling
 *
 * @return throttle policies for default api
 * @throws APITemplateException throws if generation failure occur
 */
public String getThrottlePolicyTemplateForAPILevelDefaultCondition() throws APITemplateException {
    if (log.isDebugEnabled()) {
        log.debug("Generating Siddhi App for apiLevel :" + apiPolicy.toString());
    }
    // get velocity template for API policy and generate the template
    Set<String> conditionsSet = new HashSet<String>();
    List<Pipeline> pipelines = apiPolicy.getPipelines();
    VelocityEngine velocityengine = initVelocityEngine();
    Template template = velocityengine.getTemplate(getTemplatePathForAPIDefaultPolicy());
    StringWriter writer;
    VelocityContext context;
    // when APIPolicy contains pipelines, get template as a string
    if (pipelines != null) {
        for (Pipeline pipeline : pipelines) {
            String conditionString = getPolicyConditionForDefault(pipeline.getConditions());
            if (!StringUtils.isEmpty(conditionString)) {
                conditionsSet.add(conditionString);
            }
        }
    }
    // for default API policy
    context = new VelocityContext();
    setConstantContext(context);
    // default policy is defined as 'elseCondition' , set values for velocity context
    context.put(PIPELINE, ELSE_CONDITION);
    context.put(PIPELINE_ITEM, null);
    context.put(POLICY, apiPolicy);
    context.put(QUOTA_POLICY, apiPolicy.getDefaultQuotaPolicy());
    String conditionSetString = getConditionForDefault(conditionsSet);
    if (!StringUtils.isEmpty(conditionSetString)) {
        context.put(CONDITION, AND + conditionSetString);
    } else {
        context.put(CONDITION, EMPTY_STRING);
    }
    writer = new StringWriter();
    template.merge(context, writer);
    if (log.isDebugEnabled()) {
        log.debug("Generated Siddhi App : " + writer.toString());
    }
    return writer.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) HashSet(java.util.HashSet) Pipeline(org.wso2.carbon.apimgt.core.models.policy.Pipeline) Template(org.apache.velocity.Template)

Example 15 with Pipeline

use of org.wso2.carbon.apimgt.core.models.policy.Pipeline in project carbon-apimgt by wso2.

the class AdvancedThrottlePolicyMappingUtil method fromAdvancedPolicyDTOToPolicy.

/**
 * Converts a single Advanced Policy DTO into a model object
 *
 * @param dto Advanced policy DTO object
 * @return Converted Advanced policy model object
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 * @throws UnsupportedThrottleConditionTypeException - If error occurs
 */
public static APIPolicy fromAdvancedPolicyDTOToPolicy(AdvancedThrottlePolicyDTO dto) throws UnsupportedThrottleLimitTypeException, UnsupportedThrottleConditionTypeException {
    APIPolicy apiPolicy = new APIPolicy(dto.getId(), dto.getPolicyName());
    apiPolicy = CommonThrottleMappingUtil.updateFieldsFromDTOToPolicy(dto, apiPolicy);
    List<Pipeline> pipelines = CommonThrottleMappingUtil.fromConditionalGroupDTOListToPipelineList(dto.getConditionalGroups());
    apiPolicy.setPipelines(pipelines);
    if (dto.getDefaultLimit() != null) {
        apiPolicy.setDefaultQuotaPolicy(CommonThrottleMappingUtil.fromDTOToQuotaPolicy(dto.getDefaultLimit()));
    }
    return apiPolicy;
}
Also used : APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) Pipeline(org.wso2.carbon.apimgt.core.models.policy.Pipeline)

Aggregations

Pipeline (org.wso2.carbon.apimgt.core.models.policy.Pipeline)9 Condition (org.wso2.carbon.apimgt.core.models.policy.Condition)7 IPCondition (org.wso2.carbon.apimgt.core.models.policy.IPCondition)7 PreparedStatement (java.sql.PreparedStatement)6 ResultSet (java.sql.ResultSet)6 HeaderCondition (org.wso2.carbon.apimgt.core.models.policy.HeaderCondition)6 JWTClaimsCondition (org.wso2.carbon.apimgt.core.models.policy.JWTClaimsCondition)6 QueryParameterCondition (org.wso2.carbon.apimgt.core.models.policy.QueryParameterCondition)6 RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)5 BandwidthLimit (org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)4 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)4 ArrayList (java.util.ArrayList)3 StringWriter (java.io.StringWriter)2 Template (org.apache.velocity.Template)2 VelocityContext (org.apache.velocity.VelocityContext)2 VelocityEngine (org.apache.velocity.app.VelocityEngine)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)1 Limit (org.wso2.carbon.apimgt.core.models.policy.Limit)1