Search in sources :

Example 91 with APIPolicy

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

the class SampleTestObjectCreator method createDefaultAPI.

public static API.APIBuilder createDefaultAPI() {
    Set<String> transport = new HashSet<>();
    transport.add(HTTP);
    transport.add(HTTPS);
    Set<String> tags = new HashSet<>();
    tags.add(TAG_CLIMATE);
    Set<Policy> policies = new HashSet<>();
    policies.add(goldSubscriptionPolicy);
    policies.add(silverSubscriptionPolicy);
    policies.add(bronzeSubscriptionPolicy);
    BusinessInformation businessInformation = new BusinessInformation();
    businessInformation.setBusinessOwner(NAME_BUSINESS_OWNER_1);
    businessInformation.setBusinessOwnerEmail(EMAIL_BUSINESS_OWNER_1);
    businessInformation.setTechnicalOwner(NAME_TECHNICAL_OWNER_1);
    businessInformation.setTechnicalOwnerEmail(EMAIL_TECHNICAL_OWNER_1);
    String permissionJson = "[{\"groupId\" : \"developer\", \"permission\" : " + "[\"READ\",\"UPDATE\"]},{\"groupId\" : \"admin\", \"permission\" : [\"READ\",\"UPDATE\"," + "\"DELETE\", \"MANAGE_SUBSCRIPTION\"]}]";
    Set<String> visibleRoles = new HashSet<>();
    visibleRoles.add("testRple");
    List<String> labels = new ArrayList<>();
    labels.add("testLabel");
    CorsConfiguration corsConfiguration = new CorsConfiguration();
    corsConfiguration.setEnabled(true);
    corsConfiguration.setAllowMethods(Arrays.asList(APIMgtConstants.FunctionsConstants.GET, APIMgtConstants.FunctionsConstants.POST, APIMgtConstants.FunctionsConstants.DELETE));
    corsConfiguration.setAllowHeaders(Arrays.asList(ALLOWED_HEADER_AUTHORIZATION, ALLOWED_HEADER_CUSTOM));
    corsConfiguration.setAllowCredentials(true);
    corsConfiguration.setAllowOrigins(Arrays.asList("*"));
    Map<String, Endpoint> endpointMap = new HashMap<>();
    endpointMap.put("TestEndpoint", createMockEndpoint());
    API.APIBuilder apiBuilder = new API.APIBuilder(ADMIN, "WeatherAPI", API_VERSION).id(UUID.randomUUID().toString()).context("weather").description("Get Weather Info").lifeCycleStatus(APIStatus.CREATED.getStatus()).lifecycleInstanceId(UUID.randomUUID().toString()).endpoint(Collections.emptyMap()).wsdlUri("http://localhost:9443/echo?wsdl").isResponseCachingEnabled(false).cacheTimeout(60).isDefaultVersion(false).apiPolicy(unlimitedApiPolicy).transport(transport).tags(tags).policies(policies).visibility(API.Visibility.PUBLIC).visibleRoles(visibleRoles).businessInformation(businessInformation).corsConfiguration(corsConfiguration).createdTime(LocalDateTime.now()).createdBy(ADMIN).updatedBy(ADMIN).lastUpdatedTime(LocalDateTime.now()).apiPermission(permissionJson).uriTemplates(getMockUriTemplates()).apiDefinition(apiDefinition).workflowStatus(WORKFLOW_STATUS).labels(labels).endpoint(endpointMap);
    Map map = new HashMap();
    map.put(DEVELOPER_ROLE_ID, 6);
    map.put(ADMIN_ROLE_ID, 15);
    apiBuilder.permissionMap(map);
    return apiBuilder;
}
Also used : ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) BusinessInformation(org.wso2.carbon.apimgt.core.models.BusinessInformation) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CorsConfiguration(org.wso2.carbon.apimgt.core.models.CorsConfiguration) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 92 with APIPolicy

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

the class SampleTestObjectCreator method createDefaultAPIPolicyWithBandwidthLimit.

/**
 * Create default api policy with bandwidth limit as quota policy
 *
 * @return APIPolicy object with bandwidth limit as quota policy is returned
 */
public static APIPolicy createDefaultAPIPolicyWithBandwidthLimit() {
    BandwidthLimit bandwidthLimit = new BandwidthLimit(TIME_UNIT_MONTH, 1, 1000, PolicyConstants.MB);
    QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
    defaultQuotaPolicy.setType(PolicyConstants.BANDWIDTH_TYPE);
    defaultQuotaPolicy.setLimit(bandwidthLimit);
    // set default API Policy
    APIPolicy apiPolicy = new APIPolicy(SAMPLE_API_POLICY);
    apiPolicy.setUuid(UUID.randomUUID().toString());
    apiPolicy.setDisplayName(SAMPLE_API_POLICY);
    apiPolicy.setDescription(SAMPLE_API_POLICY_DESCRIPTION);
    apiPolicy.setUserLevel(APIMgtConstants.ThrottlePolicyConstants.API_LEVEL);
    apiPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
    return apiPolicy;
}
Also used : QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)

Example 93 with APIPolicy

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

the class SampleTestObjectCreator method updateAPIPolicy.

/**
 * Updated the given API policy
 *
 * @param apiPolicy {@link APIPolicy} instance to be updated
 * @return updated {@link APIPolicy} instance
 */
public static APIPolicy updateAPIPolicy(APIPolicy apiPolicy) {
    apiPolicy.setDisplayName(UPDATED_SAMPLE_API_POLICY);
    apiPolicy.setDescription(UPDATED_SAMPLE_API_POLICY_DESCRIPTION);
    QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
    defaultQuotaPolicy.setType(PolicyConstants.BANDWIDTH_TYPE);
    BandwidthLimit bandwidthLimit = new BandwidthLimit(TIME_UNIT_SECONDS, 1, 1000, "KB");
    defaultQuotaPolicy.setLimit(bandwidthLimit);
    apiPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
    apiPolicy.setPipelines(createDefaultPipelines());
    apiPolicy.getPipelines().add(createNewIPRangePipeline());
    return apiPolicy;
}
Also used : QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)

Example 94 with APIPolicy

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

the class SampleTestObjectCreator method createDefaultAPI.

public static API.APIBuilder createDefaultAPI() {
    Set<String> transport = new HashSet<>();
    transport.add(HTTP);
    transport.add(HTTPS);
    Set<String> tags = new HashSet<>();
    tags.add(TAG_CLIMATE);
    Set<Policy> policies = new HashSet<>();
    policies.add(goldSubscriptionPolicy);
    policies.add(silverSubscriptionPolicy);
    policies.add(bronzeSubscriptionPolicy);
    BusinessInformation businessInformation = new BusinessInformation();
    CorsConfiguration corsConfiguration = new CorsConfiguration();
    String permissionJson = "[{\"groupId\" : \"developer\", \"permission\" : " + "[\"READ\",\"UPDATE\"]},{\"groupId\" : \"admin\", \"permission\" : [\"READ\",\"UPDATE\"," + "\"DELETE\", \"MANAGE_SUBSCRIPTION\"]}]";
    List<String> defaultLabels = getDefaultLabels();
    API.APIBuilder apiBuilder = new API.APIBuilder(ADMIN, "WeatherAPI", API_VERSION).id(UUID.randomUUID().toString()).context("weather").description("Get Weather Info").lifeCycleStatus(APIStatus.CREATED.getStatus()).lifecycleInstanceId(UUID.randomUUID().toString()).endpoint(Collections.emptyMap()).isResponseCachingEnabled(false).cacheTimeout(60).isDefaultVersion(false).apiPolicy(unlimitedApiPolicy).transport(transport).tags(tags).labels(defaultLabels).policies(policies).visibility(API.Visibility.PUBLIC).visibleRoles(new HashSet<>()).businessInformation(businessInformation).corsConfiguration(corsConfiguration).createdTime(LocalDateTime.now()).createdBy(ADMIN).updatedBy(ADMIN).lastUpdatedTime(LocalDateTime.now()).apiPermission(permissionJson).uriTemplates(getMockUriTemplates()).apiDefinition(apiDefinition).securityScheme(3).threatProtectionPolicies(threatProtectionPolicies);
    Map map = new HashMap();
    map.put(DEVELOPER_ROLE_ID, 6);
    map.put(ADMIN_ROLE_ID, 15);
    apiBuilder.permissionMap(map);
    return apiBuilder;
}
Also used : ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) BusinessInformation(org.wso2.carbon.apimgt.core.models.BusinessInformation) CorsConfiguration(org.wso2.carbon.apimgt.core.models.CorsConfiguration) HashMap(java.util.HashMap) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) Map(java.util.Map) HashedMap(org.apache.commons.collections.map.HashedMap) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 95 with APIPolicy

use of org.wso2.carbon.apimgt.core.models.policy.APIPolicy 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)

Aggregations

APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)87 Test (org.testng.annotations.Test)44 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)40 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)39 ArrayList (java.util.ArrayList)38 API (org.wso2.carbon.apimgt.core.models.API)33 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)30 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)27 HashMap (java.util.HashMap)25 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)22 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)22 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)22 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)21 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)20 HashSet (java.util.HashSet)19 Test (org.junit.Test)19 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)19 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)18 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)17 PreparedStatement (java.sql.PreparedStatement)15