Search in sources :

Example 21 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicy in project carbon-apimgt by wso2.

the class PoliciesApiServiceImpl method policiesThrottlingSubscriptionPost.

/**
 * Add subscription level policy
 *
 * @param body              DTO object including the Policy meta information
 * @param request           msf4j request object
 * @return Response object
 * @throws NotFoundException if an error occurred when particular resource does not exits in the system.
 */
@Override
public Response policiesThrottlingSubscriptionPost(SubscriptionThrottlePolicyDTO body, Request request) throws NotFoundException {
    APIMgtAdminService.PolicyLevel tierLevel = APIMgtAdminService.PolicyLevel.subscription;
    if (log.isDebugEnabled()) {
        log.info("Received Subscription Policy POST request " + body + " with tierLevel = " + tierLevel);
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        SubscriptionPolicy subscriptionPolicy = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyDTOToModel(body);
        String policyId = apiMgtAdminService.addSubscriptionPolicy(subscriptionPolicy);
        return Response.status(Response.Status.CREATED).entity(SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyToDTO(apiMgtAdminService.getSubscriptionPolicyByUuid(policyId))).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while adding Subscription Policy. policy name: " + body.getPolicyName();
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 22 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicy in project carbon-apimgt by wso2.

the class APIImportExportTestCase method createApi.

private static API.APIBuilder createApi(String provider, String apiId, String name, String version, String description, Map<String, Endpoint> endpointTypeToIdMap) throws APIManagementException {
    Set<String> transport = new HashSet<>();
    transport.add("http");
    Set<Policy> policies = new HashSet<>();
    policies.add(new SubscriptionPolicy("Silver"));
    policies.add(new SubscriptionPolicy("Bronze"));
    Set<String> tags = new HashSet<>();
    tags.add("food");
    tags.add("beverage");
    BusinessInformation businessInformation = new BusinessInformation();
    businessInformation.setBusinessOwner("John Doe");
    businessInformation.setBusinessOwnerEmail("john.doe@annonymous.com");
    businessInformation.setTechnicalOwner("Jane Doe");
    businessInformation.setBusinessOwnerEmail("jane.doe@annonymous.com");
    CorsConfiguration corsConfiguration = new CorsConfiguration();
    corsConfiguration.setEnabled(true);
    corsConfiguration.setAllowMethods(Arrays.asList("GET", "POST", "DELETE"));
    corsConfiguration.setAllowHeaders(Arrays.asList("Authorization", "X-Custom"));
    corsConfiguration.setAllowCredentials(true);
    corsConfiguration.setAllowOrigins(Collections.singletonList("*"));
    API.APIBuilder apiBuilder = new API.APIBuilder(provider, name, version).id(apiId).context(UUID.randomUUID().toString()).description(description).lifeCycleStatus("CREATED").apiDefinition(api1Definition).wsdlUri("http://www.webservicex.net/globalweather.asmx?op=GetWeather?wsdl").isResponseCachingEnabled(true).cacheTimeout(120).isDefaultVersion(true).apiPolicy(new APIPolicy("Gold")).transport(transport).tags(tags).policies(policies).visibility(API.Visibility.RESTRICTED).visibleRoles(new HashSet<>(Arrays.asList("customer", "manager", "employee"))).businessInformation(businessInformation).corsConfiguration(corsConfiguration).createdTime(LocalDateTime.now()).createdBy("Adam Doe").lastUpdatedTime(LocalDateTime.now()).endpoint(endpointTypeToIdMap);
    apiBuilder.uriTemplates(Collections.emptyMap());
    return apiBuilder;
}
Also used : 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) BusinessInformation(org.wso2.carbon.apimgt.core.models.BusinessInformation) CorsConfiguration(org.wso2.carbon.apimgt.core.models.CorsConfiguration) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) API(org.wso2.carbon.apimgt.core.models.API) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) HashSet(java.util.HashSet)

Example 23 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicy in project carbon-apimgt by wso2.

the class MappingUtil method toAPI.

/**
 * This method converts the API model object from the DTO object.
 *
 * @param apidto APIDTO object with API data
 * @return APIBuilder object
 */
public static API.APIBuilder toAPI(APIDTO apidto) throws JsonProcessingException {
    BusinessInformation businessInformation = new BusinessInformation();
    API_businessInformationDTO apiBusinessInformationDTO = apidto.getBusinessInformation();
    if (apiBusinessInformationDTO != null) {
        businessInformation.setBusinessOwner(apiBusinessInformationDTO.getBusinessOwner());
        businessInformation.setBusinessOwnerEmail(apiBusinessInformationDTO.getBusinessOwnerEmail());
        businessInformation.setTechnicalOwner(apiBusinessInformationDTO.getTechnicalOwner());
        businessInformation.setTechnicalOwnerEmail(apiBusinessInformationDTO.getTechnicalOwnerEmail());
    }
    API_corsConfigurationDTO apiCorsConfigurationDTO = apidto.getCorsConfiguration();
    CorsConfiguration corsConfiguration = new CorsConfiguration();
    if (apiCorsConfigurationDTO != null) {
        corsConfiguration.setAllowCredentials(apiCorsConfigurationDTO.getAccessControlAllowCredentials());
        corsConfiguration.setAllowHeaders(apiCorsConfigurationDTO.getAccessControlAllowHeaders());
        corsConfiguration.setAllowMethods(apiCorsConfigurationDTO.getAccessControlAllowMethods());
        corsConfiguration.setAllowOrigins(apiCorsConfigurationDTO.getAccessControlAllowOrigins());
        corsConfiguration.setEnabled(apiCorsConfigurationDTO.getCorsConfigurationEnabled());
    }
    List<API_operationsDTO> operationList = apidto.getOperations();
    Map<String, UriTemplate> uriTemplateList = new HashMap<>();
    for (API_operationsDTO operationsDTO : operationList) {
        UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder();
        uriTemplateBuilder.uriTemplate(operationsDTO.getUritemplate());
        uriTemplateBuilder.authType(operationsDTO.getAuthType());
        uriTemplateBuilder.httpVerb(operationsDTO.getHttpVerb());
        uriTemplateBuilder.policy(new APIPolicy(operationsDTO.getPolicy()));
        uriTemplateBuilder.scopes(operationsDTO.getScopes());
        if (operationsDTO.getEndpoint() != null && !operationsDTO.getEndpoint().isEmpty()) {
            uriTemplateBuilder.endpoint(fromEndpointListToMap(operationsDTO.getEndpoint()));
        }
        if (operationsDTO.getId() != null) {
            uriTemplateBuilder.templateId(operationsDTO.getId());
        } else {
            uriTemplateBuilder.templateId(APIUtils.generateOperationIdFromPath(operationsDTO.getUritemplate(), operationsDTO.getHttpVerb()));
        }
        uriTemplateList.put(uriTemplateBuilder.getTemplateId(), uriTemplateBuilder.build());
    }
    Set<Policy> subscriptionPolicies = new HashSet<>();
    apidto.getPolicies().forEach(v -> subscriptionPolicies.add(new SubscriptionPolicy(v)));
    API.APIBuilder apiBuilder = new API.APIBuilder(apidto.getProvider(), apidto.getName(), apidto.getVersion()).id(apidto.getId()).context(apidto.getContext()).description(apidto.getDescription()).lifeCycleStatus(apidto.getLifeCycleStatus()).endpoint(fromEndpointListToMap(apidto.getEndpoint())).visibleRoles(new HashSet<>(apidto.getVisibleRoles())).policies(subscriptionPolicies).apiPermission(apidto.getPermission()).tags(new HashSet<>(apidto.getTags())).labels(new ArrayList<>(apidto.getLabels())).transport(new HashSet<>(apidto.getTransport())).isResponseCachingEnabled(Boolean.valueOf(apidto.getResponseCaching())).businessInformation(businessInformation).uriTemplates(uriTemplateList).corsConfiguration(corsConfiguration).wsdlUri(apidto.getWsdlUri()).scopes(apidto.getScopes()).securityScheme(mapSecuritySchemeListToInt(apidto.getSecurityScheme()));
    if (apidto.getIsDefaultVersion() != null) {
        apiBuilder.isDefaultVersion(apidto.getIsDefaultVersion());
    }
    if (apidto.getVisibility() != null) {
        apiBuilder.visibility(API.Visibility.valueOf(apidto.getVisibility().toString()));
    }
    if (apidto.getCacheTimeout() != null) {
        apiBuilder.cacheTimeout(apidto.getCacheTimeout());
    }
    if (apidto.getApiPolicy() != null) {
        Policy policy = new APIPolicy(apidto.getApiPolicy());
        apiBuilder.apiPolicy(policy);
    }
    if (apidto.getHasOwnGateway() != null) {
        apiBuilder.hasOwnGateway(apidto.getHasOwnGateway());
    }
    if (apidto.getThreatProtectionPolicies() != null) {
        API_threatProtectionPoliciesDTO threatProtectionPoliciesDTO = apidto.getThreatProtectionPolicies();
        List<API_threatProtectionPolicies_listDTO> threatProtectionPolicies_listDTO = threatProtectionPoliciesDTO.getList();
        Set<String> policyIdSet = new HashSet<>();
        for (API_threatProtectionPolicies_listDTO listDTO : threatProtectionPolicies_listDTO) {
            policyIdSet.add(listDTO.getPolicyId());
        }
        apiBuilder.threatProtectionPolicies(policyIdSet);
    }
    return apiBuilder;
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) 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) HashMap(java.util.HashMap) API_threatProtectionPolicies_listDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.API_threatProtectionPolicies_listDTO) API_operationsDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.API_operationsDTO) API_threatProtectionPoliciesDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.API_threatProtectionPoliciesDTO) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) API_businessInformationDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.API_businessInformationDTO) API_corsConfigurationDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.API_corsConfigurationDTO) HashSet(java.util.HashSet) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) CorsConfiguration(org.wso2.carbon.apimgt.core.models.CorsConfiguration) API(org.wso2.carbon.apimgt.core.models.API) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy)

Example 24 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicy in project carbon-apimgt by wso2.

the class PolicyExportManagerTest method testCreateArchiveFromExecutionPlans.

@Test(description = "testing creating archive from execution plans")
public void testCreateArchiveFromExecutionPlans() throws APIManagementException {
    APIMgtAdminService apiMgtAdminService = Mockito.mock(APIMgtAdminService.class);
    List<APIPolicy> apiPolicies = new ArrayList<>();
    List<ApplicationPolicy> applicationPolicies = new ArrayList<>();
    List<SubscriptionPolicy> subscriptionPolicies = new ArrayList<>();
    List<CustomPolicy> customPolicies = new ArrayList<>();
    apiPolicies.add(SampleTestObjectCreator.createDefaultAPIPolicy());
    applicationPolicies.add(SampleTestObjectCreator.createDefaultApplicationPolicy());
    subscriptionPolicies.add(SampleTestObjectCreator.createDefaultSubscriptionPolicy());
    customPolicies.add(SampleTestObjectCreator.createDefaultCustomPolicy());
    Mockito.when(apiMgtAdminService.getApiPolicies()).thenReturn(apiPolicies);
    Mockito.when(apiMgtAdminService.getApplicationPolicies()).thenReturn(applicationPolicies);
    Mockito.when(apiMgtAdminService.getCustomRules()).thenReturn(customPolicies);
    Mockito.when(apiMgtAdminService.getSubscriptionPolicies()).thenReturn(subscriptionPolicies);
    PolicyExportManager policyExportManager = new PolicyExportManager(apiMgtAdminService);
    String path = policyExportManager.createArchiveFromExecutionPlans("exportDir", exportRootDirectory, "policies");
    String unzippedPath = exportRootDirectory + File.separator + "export-policies";
    APIFileUtils.extractArchive(path, unzippedPath);
    Assert.assertEquals(new File(APIFileUtils.getDirectoryList(unzippedPath).iterator().next()).listFiles().length == 6, true, "Exported policy count is not equal to ");
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) ArrayList(java.util.ArrayList) APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) File(java.io.File) Test(org.testng.annotations.Test)

Example 25 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicy in project carbon-apimgt by wso2.

the class SampleTestObjectCreator method createDefaultSubscriptionPolicy.

/**
 * Create a subscription policy.
 *
 * @return SubscriptionPolicy object
 */
protected static SubscriptionPolicy createDefaultSubscriptionPolicy() {
    SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(SAMPLE_SUBSCRIPTION_POLICY);
    subscriptionPolicy.setUuid(UUID.randomUUID().toString());
    subscriptionPolicy.setDisplayName(SAMPLE_SUBSCRIPTION_POLICY);
    subscriptionPolicy.setDescription(SAMPLE_SUBSCRIPTION_POLICY_DESCRIPTION);
    QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
    defaultQuotaPolicy.setType(REQUEST_COUNT_TYPE);
    RequestCountLimit requestCountLimit = new RequestCountLimit(TIME_UNIT_SECONDS, 10000, 1000);
    defaultQuotaPolicy.setLimit(requestCountLimit);
    subscriptionPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
    return subscriptionPolicy;
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)

Aggregations

SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)74 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)42 Test (org.testng.annotations.Test)41 ArrayList (java.util.ArrayList)36 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)33 API (org.wso2.carbon.apimgt.core.models.API)30 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)30 Test (org.junit.Test)25 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)25 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)24 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)21 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)19 HashMap (java.util.HashMap)18 HashSet (java.util.HashSet)18 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)16 SQLException (java.sql.SQLException)15 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)15 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)15 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)15 QuotaPolicy (org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy)15