Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extend-monetization-phase1 #12733

Draft
wants to merge 25 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions components/apimgt/org.wso2.carbon.apimgt.api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
<groupId>org.wso2.orbit.graphQL</groupId>
<artifactId>graphQL</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.apimgt</groupId>
<artifactId>org.wso2.carbon.apimgt.common.analytics</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.wso2.carbon.apimgt.api.model;

import org.wso2.carbon.apimgt.common.analytics.exceptions.AnalyticsException;

public interface AnalyticsforMonetization {
/**
* Gets Usage Data from Analytics Provider
*
* @param monetizationUsagePublishInfo monetization publish info
* @return usage data from analytics provider
* @throws AnalyticsException if the action failed
*/
Object getUsageData(MonetizationUsagePublishInfo monetizationUsagePublishInfo) throws AnalyticsException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package org.wso2.carbon.apimgt.api.model;

import java.util.HashMap;

public class MonetizationUsageInfo {

Long currentTimestamp;
String apiUuid;
String apiName;
String apiVersion;
String tenantDomain;
String applicationName;
String applicationOwner;
HashMap<String, Object> customAttributes;
Long requestCount;

public MonetizationUsageInfo(Long currentTimestamp, String apiUuid, String apiName, String apiVersion, String tenantDomain, String applicationName, String applicationOwner, HashMap<String, Object> customAttributes, Long requestCount) {
this.currentTimestamp = currentTimestamp;
this.apiUuid = apiUuid;
this.apiName = apiName;
this.apiVersion = apiVersion;
this.tenantDomain = tenantDomain;
this.applicationName = applicationName;
this.applicationOwner = applicationOwner;
this.customAttributes = customAttributes;
this.requestCount = requestCount;
}

public Long getCurrentTimestamp() {
return currentTimestamp;
}

public void setCurrentTimestamp(Long currentTimestamp) {
this.currentTimestamp = currentTimestamp;
}

public String getApiUuid() {
return apiUuid;
}

public void setApiUuid(String apiUuid) {
this.apiUuid = apiUuid;
}

public String getApiName() {
return apiName;
}

public void setApiName(String apiName) {
this.apiName = apiName;
}

public String getApiVersion() {
return apiVersion;
}

public void setApiVersion(String apiVersion) {
this.apiVersion = apiVersion;
}

public String getTenantDomain() {
return tenantDomain;
}

public void setTenantDomain(String tenantDomain) {
this.tenantDomain = tenantDomain;
}

public String getApplicationName() {
return applicationName;
}

public void setApplicationName(String applicationName) {
this.applicationName = applicationName;
}

public String getApplicationOwner() {
return applicationOwner;
}

public void setApplicationOwner(String applicationOwner) {
this.applicationOwner = applicationOwner;
}

public HashMap<String, Object> getCustomAttributes() {
return customAttributes;
}

public void setCustomAttributes(HashMap<String, Object> customAttributes) {
this.customAttributes = customAttributes;
}

public Long getRequestCount() {
return requestCount;
}

public void setRequestCount(Long requestCount) {
this.requestCount = requestCount;
}
}
4 changes: 0 additions & 4 deletions components/apimgt/org.wso2.carbon.apimgt.impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.wso2.carbon.apimgt</groupId>
<artifactId>org.wso2.carbon.apimgt.api</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.apimgt</groupId>
<artifactId>org.wso2.carbon.apimgt.keymgt.client</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,9 @@ private void readChildElements(OMElement serverConfig,
OMElement enablePolicy = element.getFirstChildWithName(new QName("PolicyEnabled"));
analyticsProps.put("policyEnabled", enablePolicy.getText());

OMElement analyticsImplementation = element.getFirstChildWithName(new QName("AnalyticsImpl"));
analyticsProps.put("analyticsImpl", analyticsImplementation.getText());

analyticsProperties = analyticsProps;
} else if ("PersistenceConfigs".equals(localName)) {
OMElement properties = element.getFirstChildWithName(new QName("Properties"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.wso2.carbon.apimgt.impl.analytics;

import org.wso2.carbon.apimgt.api.model.AnalyticsforMonetization;
import org.wso2.carbon.apimgt.api.model.MonetizationUsagePublishInfo;
import org.wso2.carbon.apimgt.common.analytics.exceptions.AnalyticsException;

public class DefaultAnalyticsImpl implements AnalyticsforMonetization {
@Override
public Object getUsageData(MonetizationUsagePublishInfo monetizationUsagePublishInfo) throws AnalyticsException {
return new Object();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.wso2.carbon.apimgt.impl.monetization;

import org.wso2.carbon.apimgt.api.MonetizationException;
import org.wso2.carbon.apimgt.api.model.AnalyticsforMonetization;
import org.wso2.carbon.apimgt.api.model.Monetization;
import org.wso2.carbon.apimgt.api.model.MonetizationUsagePublishInfo;
import org.wso2.carbon.apimgt.common.analytics.exceptions.AnalyticsException;
import org.wso2.carbon.apimgt.impl.APIManagerConfiguration;
import org.wso2.carbon.apimgt.impl.utils.APIUtil;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Constructor;

import java.util.Map;

/**
* Abstract class for monetization, implements Monetization interface
*/

public abstract class AbstractMonetization implements Monetization {

private AnalyticsforMonetization analyticsClass;
private APIManagerConfiguration configuration = new APIManagerConfiguration();
public static final String ANALYTICS_IMPL = "analyticsImpl";

@Override
public boolean publishMonetizationUsageRecords(MonetizationUsagePublishInfo monetizationUsagePublishInfo) throws MonetizationException {
Object usageData = monetizationAnalyticsDataProvider(monetizationUsagePublishInfo);
return publishUsageData(usageData, monetizationUsagePublishInfo);
}

/**
* Publish the usageData to the billing engine
*
* @return true if the job is successful, and false otherwise
* @throws MonetizationException if failed to publish usageData
*/
public abstract boolean publishUsageData(Object usageData, MonetizationUsagePublishInfo monetizationUsagePublishInfo) throws MonetizationException;

/**
* Gets Usage Data from Analytics Provider
*
* @param monetizationUsagePublishInfo monetization publish info
* @return usage data from analytics provider
* @throws AnalyticsException if the action failed
*/
public Object monetizationAnalyticsDataProvider(MonetizationUsagePublishInfo monetizationUsagePublishInfo) throws MonetizationException{
Map<String,String> configs = configuration.getAnalyticsProperties();
String className;
if (configs.containsKey(ANALYTICS_IMPL) && !configs.get(ANALYTICS_IMPL).isEmpty()) {
className = configs.get(ANALYTICS_IMPL);
} else {
className = "org.wso2.am.analytics.retriever.choreo.ChoreoAnalyticsforMonetizationImpl";
}
String message;
try {
Class<?> callingClass = APIUtil.getClassForName(className);
Constructor<?> cons = callingClass.getConstructors()[0];
analyticsClass = (AnalyticsforMonetization) cons.newInstance();;
return analyticsClass.getUsageData(monetizationUsagePublishInfo);
} catch (ClassNotFoundException e) {
message = "The specified class was not found";
throw new MonetizationException(message, e);
} catch (InvocationTargetException | AnalyticsException e) {
message = "Error fetching usage data";
throw new MonetizationException(message, e);
} catch (InstantiationException e) {
message = "Error creating class instance";
throw new MonetizationException(message, e);
} catch (IllegalAccessException e) {
message = "Error getting class access";
throw new MonetizationException(message, e);
} catch (ClassCastException e) {
message = "Error getting child class";
throw new MonetizationException(message, e);
} //handling of possible exceptions
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@

<PolicyEnabled>{{apim.analytics.enablePolicy}}</PolicyEnabled>

<AnalyticsImpl>{{apim.analytics.analytics_impl}}</AnalyticsImpl>

<Type>{{apim.analytics.type}}</Type>

<AuthToken>{{apim.analytics.auth_token}}</AuthToken>
Expand Down Expand Up @@ -1755,4 +1757,4 @@
<Hashing>
<HashingAlgorithm>{{apim.hashing.hashing_algorithm}}</HashingAlgorithm>
</Hashing>
</APIManager>
</APIManager>
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@

<AuthToken></AuthToken>

<AnalyticsImpl></AnalyticsImpl>

<!-- Event publisher implementation -->
<!-- <ReporterClass></ReporterClass> -->

Expand Down