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[] {};
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations