Search in sources :

Example 11 with Usage

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

the class ApiMgtDAO method getAllAPIProductUsageByProvider.

/**
 * @param providerName Name of the provider
 * @return UserApplicationAPIUsage of given provider
 * @throws org.wso2.carbon.apimgt.api.APIManagementException if failed to get
 *                                                           UserApplicationAPIUsage for given provider
 */
public UserApplicationAPIUsage[] getAllAPIProductUsageByProvider(String providerName) throws APIManagementException {
    try (Connection connection = APIMgtDBUtil.getConnection();
        PreparedStatement ps = connection.prepareStatement(SQLConstants.GET_APP_API_USAGE_BY_PROVIDER_SQL)) {
        ps.setString(1, APIUtil.replaceEmailDomainBack(providerName));
        try (ResultSet result = ps.executeQuery()) {
            Map<String, UserApplicationAPIUsage> userApplicationUsages = new TreeMap<String, UserApplicationAPIUsage>();
            while (result.next()) {
                int subId = result.getInt("SUBSCRIPTION_ID");
                String userId = result.getString("USER_ID");
                String application = result.getString("APPNAME");
                int appId = result.getInt("APPLICATION_ID");
                String subStatus = result.getString("SUB_STATUS");
                String subsCreateState = result.getString("SUBS_CREATE_STATE");
                String key = userId + "::" + application;
                UserApplicationAPIUsage usage = userApplicationUsages.get(key);
                if (usage == null) {
                    usage = new UserApplicationAPIUsage();
                    usage.setUserId(userId);
                    usage.setApplicationName(application);
                    usage.setAppId(appId);
                    userApplicationUsages.put(key, usage);
                }
                APIProductIdentifier apiProductId = new APIProductIdentifier(result.getString("API_PROVIDER"), result.getString("API_NAME"), result.getString("API_VERSION"));
                SubscribedAPI apiSubscription = new SubscribedAPI(new Subscriber(userId), apiProductId);
                apiSubscription.setSubStatus(subStatus);
                apiSubscription.setSubCreatedStatus(subsCreateState);
                apiSubscription.setUUID(result.getString("SUB_UUID"));
                apiSubscription.setTier(new Tier(result.getString("SUB_TIER_ID")));
                Application applicationObj = new Application(result.getString("APP_UUID"));
                apiSubscription.setApplication(applicationObj);
                usage.addApiSubscriptions(apiSubscription);
            }
            return userApplicationUsages.values().toArray(new UserApplicationAPIUsage[userApplicationUsages.size()]);
        }
    } catch (SQLException e) {
        handleException("Failed to find API Product Usage for :" + providerName, e);
    }
    return new UserApplicationAPIUsage[] {};
}
Also used : UserApplicationAPIUsage(org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage) Tier(org.wso2.carbon.apimgt.api.model.Tier) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) TreeMap(java.util.TreeMap) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) ResultSet(java.sql.ResultSet) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) Application(org.wso2.carbon.apimgt.api.model.Application)

Example 12 with Usage

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

the class ApiMgtDAO method getAllAPIUsageByProviderAndApiId.

/**
 * @param uuid API uuid
 * @param organization Organization of the API
 * @return UserApplicationAPIUsage of given provider
 * @throws org.wso2.carbon.apimgt.api.APIManagementException if failed to get
 *                                                           UserApplicationAPIUsage for given provider
 */
public UserApplicationAPIUsage[] getAllAPIUsageByProviderAndApiId(String uuid, String organization) throws APIManagementException {
    Connection connection = null;
    PreparedStatement ps = null;
    ResultSet result = null;
    try {
        String sqlQuery = SQLConstants.GET_APP_API_USAGE_BY_UUID_SQL;
        connection = APIMgtDBUtil.getConnection();
        ps = connection.prepareStatement(sqlQuery);
        ps.setString(1, uuid);
        ps.setString(2, organization);
        result = ps.executeQuery();
        Map<String, UserApplicationAPIUsage> userApplicationUsages = new TreeMap<String, UserApplicationAPIUsage>();
        while (result.next()) {
            int subId = result.getInt("SUBSCRIPTION_ID");
            String userId = result.getString("USER_ID");
            String application = result.getString("APPNAME");
            int appId = result.getInt("APPLICATION_ID");
            String subStatus = result.getString("SUB_STATUS");
            String subsCreateState = result.getString("SUBS_CREATE_STATE");
            String key = userId + "::" + application;
            UserApplicationAPIUsage usage = userApplicationUsages.get(key);
            if (usage == null) {
                usage = new UserApplicationAPIUsage();
                usage.setUserId(userId);
                usage.setApplicationName(application);
                usage.setAppId(appId);
                userApplicationUsages.put(key, usage);
            }
            APIIdentifier apiId = new APIIdentifier(result.getString("API_PROVIDER"), result.getString("API_NAME"), result.getString("API_VERSION"));
            SubscribedAPI apiSubscription = new SubscribedAPI(new Subscriber(userId), apiId);
            apiSubscription.setSubStatus(subStatus);
            apiSubscription.setSubCreatedStatus(subsCreateState);
            apiSubscription.setUUID(result.getString("SUB_UUID"));
            apiSubscription.setTier(new Tier(result.getString("SUB_TIER_ID")));
            Application applicationObj = new Application(result.getString("APP_UUID"));
            apiSubscription.setApplication(applicationObj);
            usage.addApiSubscriptions(apiSubscription);
        }
        return userApplicationUsages.values().toArray(new UserApplicationAPIUsage[userApplicationUsages.size()]);
    } catch (SQLException e) {
        handleException("Failed to find API Usage for API with UUID :" + uuid, e);
        return null;
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, connection, result);
    }
}
Also used : UserApplicationAPIUsage(org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage) Tier(org.wso2.carbon.apimgt.api.model.Tier) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) TreeMap(java.util.TreeMap) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) ResultSet(java.sql.ResultSet) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Application(org.wso2.carbon.apimgt.api.model.Application)

Example 13 with Usage

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

the class ApiMgtDAO method getMonetizationUsagePublishInfo.

/**
 * Derives info about monetization usage publish job
 *
 * @return ifno about the monetization usage publish job
 * @throws APIManagementException
 */
public MonetizationUsagePublishInfo getMonetizationUsagePublishInfo() throws APIManagementException {
    Connection conn = null;
    ResultSet rs = null;
    PreparedStatement ps = null;
    try {
        conn = APIMgtDBUtil.getConnection();
        String query = SQLConstants.GET_MONETIZATION_USAGE_PUBLISH_INFO;
        ps = conn.prepareStatement(query);
        rs = ps.executeQuery();
        if (rs.next()) {
            MonetizationUsagePublishInfo monetizationUsagePublishInfo = new MonetizationUsagePublishInfo();
            monetizationUsagePublishInfo.setId(rs.getString("ID"));
            monetizationUsagePublishInfo.setState(rs.getString("STATE"));
            monetizationUsagePublishInfo.setStatus(rs.getString("STATUS"));
            monetizationUsagePublishInfo.setStartedTime(rs.getLong("STARTED_TIME"));
            monetizationUsagePublishInfo.setLastPublishTime(rs.getLong("PUBLISHED_TIME"));
            return monetizationUsagePublishInfo;
        }
    } catch (SQLException e) {
        handleException("Error while retrieving Monetization Usage Publish Info: ", e);
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, conn, rs);
    }
    return null;
}
Also used : MonetizationUsagePublishInfo(org.wso2.carbon.apimgt.api.model.MonetizationUsagePublishInfo) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 14 with Usage

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

the class SolaceBrokerDeployer method undeployWhenRetire.

/**
 * Undeploy API artifact from provided environment in the external gateway when Api is retired
 *
 * @param api         API to be undeployed from the external gateway
 * @param environment Environment needed to be undeployed API from the external gateway
 * @throws DeployerException if error occurs when undeploying APIs from the external gateway
 */
public boolean undeployWhenRetire(API api, Environment environment) throws DeployerException {
    Application application;
    APIProvider apiProvider;
    // Remove subscription
    try {
        apiProvider = APIManagerFactory.getInstance().getAPIProvider(CarbonContext.getThreadLocalCarbonContext().getUsername());
        List<SubscribedAPI> apiUsages = apiProvider.getAPIUsageByAPIId(api.getUuid(), api.getOrganization());
        for (SubscribedAPI usage : apiUsages) {
            application = usage.getApplication();
            // Check whether the subscription is belongs to an API deployed in Solace
            if (SolaceConstants.SOLACE_ENVIRONMENT.equals(api.getGatewayVendor())) {
                SolaceNotifierUtils.unsubscribeAPIProductFromSolaceApplication(api, application);
            }
        }
    } catch (APIManagementException e) {
        throw new DeployerException("Error occurred when removing subscriptions of the API " + api.getUuid() + "to be retired", e);
    }
    // undeploy API from Solace
    boolean deletedFromSolace = undeploy(api.getId().getName(), api.getId().getVersion(), api.getContext(), environment);
    if (!deletedFromSolace) {
        throw new DeployerException("Error while deleting API product of API " + api.getUuid() + "from Solace " + "broker");
    }
    return true;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) DeployerException(org.wso2.carbon.apimgt.impl.deployer.exceptions.DeployerException) Application(org.wso2.carbon.apimgt.api.model.Application) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 15 with Usage

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

the class APIProviderImpl method getAPIUsageByAPIId.

/**
 * Returns usage details of a particular API
 *
 * @param uuid API uuid
 * @param organization identifier of the organization
 * @return UserApplicationAPIUsages for given provider
 * @throws org.wso2.carbon.apimgt.api.APIManagementException If failed to get UserApplicationAPIUsage
 */
@Override
public List<SubscribedAPI> getAPIUsageByAPIId(String uuid, String organization) throws APIManagementException {
    APIIdentifier identifier = apiMgtDAO.getAPIIdentifierFromUUID(uuid);
    List<SubscribedAPI> subscribedAPIs = new ArrayList<>();
    if (identifier != null) {
        APIIdentifier apiIdEmailReplaced = new APIIdentifier(APIUtil.replaceEmailDomain(identifier.getProviderName()), identifier.getApiName(), identifier.getVersion());
        UserApplicationAPIUsage[] allApiResult = apiMgtDAO.getAllAPIUsageByProviderAndApiId(uuid, organization);
        for (UserApplicationAPIUsage usage : allApiResult) {
            for (SubscribedAPI apiSubscription : usage.getApiSubscriptions()) {
                APIIdentifier subsApiId = apiSubscription.getApiId();
                APIIdentifier subsApiIdEmailReplaced = new APIIdentifier(APIUtil.replaceEmailDomain(subsApiId.getProviderName()), subsApiId.getApiName(), subsApiId.getVersion());
                if (subsApiIdEmailReplaced.equals(apiIdEmailReplaced)) {
                    subscribedAPIs.add(apiSubscription);
                }
            }
        }
    }
    return subscribedAPIs;
}
Also used : UserApplicationAPIUsage(org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage) ArrayList(java.util.ArrayList) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)9 Connection (java.sql.Connection)7 PreparedStatement (java.sql.PreparedStatement)7 ResultSet (java.sql.ResultSet)7 SQLException (java.sql.SQLException)7 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)7 ArrayList (java.util.ArrayList)5 UserApplicationAPIUsage (org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage)5 APIAdmin (org.wso2.carbon.apimgt.api.APIAdmin)4 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)4 Application (org.wso2.carbon.apimgt.api.model.Application)4 APIAdminImpl (org.wso2.carbon.apimgt.impl.APIAdminImpl)4 DateFormat (java.text.DateFormat)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Date (java.util.Date)3 TreeMap (java.util.TreeMap)3 MonetizationException (org.wso2.carbon.apimgt.api.MonetizationException)3 API (org.wso2.carbon.apimgt.api.model.API)3 Monetization (org.wso2.carbon.apimgt.api.model.Monetization)3 MonetizationUsagePublishInfo (org.wso2.carbon.apimgt.api.model.MonetizationUsagePublishInfo)3