Search in sources :

Example 1 with Monetization

use of org.wso2.carbon.apimgt.api.model.Monetization in project carbon-apimgt by wso2.

the class MonetizationAPIMappinUtil method fromStatusToDTO.

/**
 * Set value to DTO of the monetization usage publisher
 *
 * @param status status of the request
 * @param msg    description of the the status
 * @return the DTO for monetization usage publish API
 */
public static PublishStatusDTO fromStatusToDTO(String status, String msg) {
    PublishStatusDTO publishStatusDTO = new PublishStatusDTO();
    publishStatusDTO.setStatus(status);
    publishStatusDTO.setMessage(msg);
    return publishStatusDTO;
}
Also used : PublishStatusDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.PublishStatusDTO)

Example 2 with Monetization

use of org.wso2.carbon.apimgt.api.model.Monetization in project carbon-apimgt by wso2.

the class PublisherCommonUtils method updateApiProduct.

/**
 * Update an API Product.
 *
 * @param originalAPIProduct    Existing API Product
 * @param apiProductDtoToUpdate New API Product DTO to update
 * @param apiProvider           API Provider
 * @param username              Username
 * @throws APIManagementException If an error occurs while retrieving and updating an existing API Product
 * @throws FaultGatewaysException If an error occurs while updating an existing API Product
 */
public static APIProduct updateApiProduct(APIProduct originalAPIProduct, APIProductDTO apiProductDtoToUpdate, APIProvider apiProvider, String username, String orgId) throws APIManagementException, FaultGatewaysException {
    List<String> apiSecurity = apiProductDtoToUpdate.getSecurityScheme();
    // validation for tiers
    List<String> tiersFromDTO = apiProductDtoToUpdate.getPolicies();
    if (apiSecurity.contains(APIConstants.DEFAULT_API_SECURITY_OAUTH2) || apiSecurity.contains(APIConstants.API_SECURITY_API_KEY)) {
        if (tiersFromDTO == null || tiersFromDTO.isEmpty()) {
            throw new APIManagementException("No tier defined for the API Product", ExceptionCodes.TIER_CANNOT_BE_NULL);
        }
    }
    // check whether the added API Products's tiers are all valid
    Set<Tier> definedTiers = apiProvider.getTiers();
    List<String> invalidTiers = PublisherCommonUtils.getInvalidTierNames(definedTiers, tiersFromDTO);
    if (!invalidTiers.isEmpty()) {
        throw new APIManagementException("Specified tier(s) " + Arrays.toString(invalidTiers.toArray()) + " are invalid", ExceptionCodes.TIER_NAME_INVALID);
    }
    if (apiProductDtoToUpdate.getAdditionalProperties() != null) {
        String errorMessage = PublisherCommonUtils.validateAdditionalProperties(apiProductDtoToUpdate.getAdditionalProperties());
        if (!errorMessage.isEmpty()) {
            throw new APIManagementException(errorMessage, ExceptionCodes.from(ExceptionCodes.INVALID_ADDITIONAL_PROPERTIES, originalAPIProduct.getId().getName(), originalAPIProduct.getId().getVersion()));
        }
    }
    APIProduct product = APIMappingUtil.fromDTOtoAPIProduct(apiProductDtoToUpdate, username);
    product.setState(originalAPIProduct.getState());
    // We do not allow to modify provider,name,version  and uuid. Set the origial value
    APIProductIdentifier productIdentifier = originalAPIProduct.getId();
    product.setID(productIdentifier);
    product.setUuid(originalAPIProduct.getUuid());
    product.setOrganization(orgId);
    Map<API, List<APIProductResource>> apiToProductResourceMapping = apiProvider.updateAPIProduct(product);
    apiProvider.updateAPIProductSwagger(originalAPIProduct.getUuid(), apiToProductResourceMapping, product, orgId);
    // preserve monetization status in the update flow
    apiProvider.configureMonetizationInAPIProductArtifact(product);
    return apiProvider.getAPIProduct(productIdentifier);
}
Also used : APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Tier(org.wso2.carbon.apimgt.api.model.Tier) API(org.wso2.carbon.apimgt.api.model.API) List(java.util.List) ArrayList(java.util.ArrayList)

Example 3 with Monetization

use of org.wso2.carbon.apimgt.api.model.Monetization in project carbon-apimgt by wso2.

the class SettingsMappingUtil method getMonetizationAttributes.

/**
 * This method returns the monetization properties from configuration.
 *
 * @return List<String> monetization properties
 * @throws APIManagementException
 */
private List<MonetizationAttributeDTO> getMonetizationAttributes() {
    List<MonetizationAttributeDTO> monetizationAttributeDTOSList = new ArrayList<MonetizationAttributeDTO>();
    JSONArray monetizationAttributes = APIUtil.getMonetizationAttributes();
    for (int i = 0; i < monetizationAttributes.size(); i++) {
        JSONObject monetizationAttribute = (JSONObject) monetizationAttributes.get(i);
        MonetizationAttributeDTO monetizationAttributeDTO = new MonetizationAttributeDTO();
        monetizationAttributeDTO.setName((String) monetizationAttribute.get(APIConstants.Monetization.ATTRIBUTE));
        monetizationAttributeDTO.setDisplayName((String) monetizationAttribute.get(APIConstants.Monetization.ATTRIBUTE_DISPLAY_NAME));
        monetizationAttributeDTO.setDescription((String) monetizationAttribute.get(APIConstants.Monetization.ATTRIBUTE_DESCRIPTION));
        monetizationAttributeDTO.setRequired((Boolean) monetizationAttribute.get(APIConstants.Monetization.IS_ATTRIBITE_REQUIRED));
        monetizationAttributeDTO.setHidden((Boolean) monetizationAttribute.get(APIConstants.Monetization.IS_ATTRIBUTE_HIDDEN));
        monetizationAttributeDTOSList.add(monetizationAttributeDTO);
    }
    return monetizationAttributeDTOSList;
}
Also used : JSONObject(org.json.simple.JSONObject) MonetizationAttributeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.MonetizationAttributeDTO) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray)

Example 4 with Monetization

use of org.wso2.carbon.apimgt.api.model.Monetization in project carbon-apimgt by wso2.

the class DefaultMonetizationImpl method publishMonetizationUsageRecords.

/**
 * Update info about monetization usage publish job
 *
 * @param monetizationUsagePublishInfo
 * @return boolean always return true if there is no exception
 * @throws MonetizationException
 */
@Override
public boolean publishMonetizationUsageRecords(MonetizationUsagePublishInfo monetizationUsagePublishInfo) throws MonetizationException {
    APIAdmin apiAdmin = new APIAdminImpl();
    monetizationUsagePublishInfo.setState(APIConstants.Monetization.COMPLETED);
    monetizationUsagePublishInfo.setStatus(APIConstants.Monetization.SUCCESSFULL);
    DateFormat df = new SimpleDateFormat(APIConstants.Monetization.USAGE_PUBLISH_TIME_FORMAT);
    Date dateobj = new Date();
    // get the time in UTC format
    df.setTimeZone(TimeZone.getTimeZone(APIConstants.Monetization.USAGE_PUBLISH_TIME_ZONE));
    String currentDate = df.format(dateobj);
    long currentTimestamp = apiAdmin.getTimestamp(currentDate);
    monetizationUsagePublishInfo.setLastPublishTime(currentTimestamp);
    try {
        apiAdmin.updateMonetizationUsagePublishInfo(monetizationUsagePublishInfo);
    } catch (APIManagementException e) {
        throw new MonetizationException("Failed to update the monetization usage publish info", e);
    }
    return true;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) MonetizationException(org.wso2.carbon.apimgt.api.MonetizationException) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 5 with Monetization

use of org.wso2.carbon.apimgt.api.model.Monetization in project carbon-apimgt by wso2.

the class MonetizationUsagePublishAgent method run.

@Override
public void run() {
    Monetization monetizationImpl = null;
    APIAdmin apiAdmin = null;
    try {
        apiAdmin = new APIAdminImpl();
        monetizationImpl = apiAdmin.getMonetizationImplClass();
        monetizationUsagePublishInfo.setState(APIConstants.Monetization.RUNNING);
        monetizationUsagePublishInfo.setStatus(APIConstants.Monetization.INPROGRESS);
        DateFormat df = new SimpleDateFormat(APIConstants.Monetization.USAGE_PUBLISH_TIME_FORMAT);
        Date dateobj = new Date();
        df.setTimeZone(TimeZone.getTimeZone(APIConstants.Monetization.USAGE_PUBLISH_TIME_ZONE));
        String currentDate = df.format(dateobj);
        long currentTimestamp = apiAdmin.getTimestamp(currentDate);
        // set the current time as starting time of the job
        monetizationUsagePublishInfo.setStartedTime(currentTimestamp);
        apiAdmin.updateMonetizationUsagePublishInfo(monetizationUsagePublishInfo);
        monetizationImpl.publishMonetizationUsageRecords(monetizationUsagePublishInfo);
    } catch (Exception e) {
        try {
            // update the state and status of the job incase of any execptions
            monetizationUsagePublishInfo.setState(APIConstants.Monetization.COMPLETED);
            monetizationUsagePublishInfo.setStatus(APIConstants.Monetization.FAILED);
            apiAdmin.updateMonetizationUsagePublishInfo(monetizationUsagePublishInfo);
        } catch (APIManagementException ex) {
            String errorMsg = "Failed to update the state of monetization ussge publisher";
            log.error(errorMsg, ex);
        }
        String errorMsg = "Failed to publish monetization usage to billing Engine";
        log.error(errorMsg, e);
    }
}
Also used : Monetization(org.wso2.carbon.apimgt.api.model.Monetization) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)24 JSONObject (org.json.simple.JSONObject)14 Tier (org.wso2.carbon.apimgt.api.model.Tier)14 API (org.wso2.carbon.apimgt.api.model.API)13 ArrayList (java.util.ArrayList)9 ParseException (org.json.simple.parser.ParseException)9 Monetization (org.wso2.carbon.apimgt.api.model.Monetization)9 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)9 JSONParser (org.json.simple.parser.JSONParser)8 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)8 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)8 Gson (com.google.gson.Gson)7 MonetizationException (org.wso2.carbon.apimgt.api.MonetizationException)7 APIProduct (org.wso2.carbon.apimgt.api.model.APIProduct)7 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)7 HashMap (java.util.HashMap)6 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)6 UserStoreException (org.wso2.carbon.user.api.UserStoreException)6 HashSet (java.util.HashSet)5 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)5