Skip to content

Commit

Permalink
feat: send operation policies in analytics event
Browse files Browse the repository at this point in the history
  • Loading branch information
BLasan committed Jan 4, 2024
1 parent a41bbca commit d3305b7
Show file tree
Hide file tree
Showing 12 changed files with 468 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass())
return false;
OperationPolicy policyObj = (OperationPolicy) o;
return policyId == policyObj.policyId && policyName == policyObj.policyName && policyVersion == policyObj.policyVersion
&& direction.equals(policyObj.direction) && parameters.equals(policyObj.parameters);
return Objects.equals(policyName, policyObj.policyName) && Objects.equals(policyVersion,
policyObj.policyVersion) && Objects.equals(direction, policyObj.direction) && Objects.equals(
parameters, policyObj.parameters) && Objects.equals(policyId, policyObj.policyId);
}

@Override
Expand All @@ -113,4 +114,16 @@ public int compareTo(OperationPolicy o) {

return this.order - o.getOrder();
}

@Override
public String toString() {

return "operationPolicies {" +
", policyName ='" + policyName + '\'' +
", policyVersion ='" + policyVersion + '\'' +
", direction ='" + direction + '\'' +
", order ='" + order + '\'' +
", policyId ='" + policyId + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@

package org.wso2.carbon.apimgt.api.model.subscription;

import org.wso2.carbon.apimgt.api.model.OperationPolicy;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

public class URLMapping {

Expand All @@ -29,6 +33,7 @@ public class URLMapping {
private String httpMethod;
private String urlPattern;
private List<String> scopes = new ArrayList<>();
private Set<OperationPolicy> operationPolicies = new HashSet<>();


public String getHttpMethod() {
Expand Down Expand Up @@ -79,6 +84,13 @@ public List<String> getScopes() {
return scopes;
}

public void setOperationPolicies(OperationPolicy operationPolicy){
this.operationPolicies.add(operationPolicy);
}
public Set<OperationPolicy> getOperationPolicies(){
return operationPolicies;
}

@Override
public boolean equals(Object o) {

Expand All @@ -105,6 +117,7 @@ public String toString() {
", authScheme ='" + authScheme + '\'' +
", httpMethod ='" + httpMethod + '\'' +
", urlPattern ='" + urlPattern + '\'' +
", operationPolicies ='" + operationPolicies + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

package org.wso2.carbon.apimgt.common.analytics.publishers.dto;

import java.util.ArrayList;
import java.util.List;

/**
* Api attribute in analytics event.
*/
Expand All @@ -27,6 +30,7 @@ public class API {
private String apiVersion;
private String apiCreator;
private String apiCreatorTenantDomain;
private List<URITemplate> uriTemplates = new ArrayList<>();

public String getApiId() {
return apiId;
Expand Down Expand Up @@ -75,4 +79,12 @@ public String getApiCreatorTenantDomain() {
public void setApiCreatorTenantDomain(String apiCreatorTenantDomain) {
this.apiCreatorTenantDomain = apiCreatorTenantDomain;
}

public List<URITemplate> getUriTemplates() {
return uriTemplates;
}

public void setUriTemplates(List<URITemplate> uriTemplates) {
this.uriTemplates = uriTemplates;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.apimgt.common.analytics.publishers.dto;

/**
* OperationPolicy object in analytics event.
*/
public class OperationPolicy {
private String policyId;
private String policyName;
private String policyVersion;
private String direction;
private int order;

public String getPolicyId() {
return policyId;
}

public void setPolicyId(String policyId) {
this.policyId = policyId;
}

public String getPolicyName() {
return policyName;
}

public void setPolicyName(String policyName) {
this.policyName = policyName;
}

public String getPolicyVersion() {
return policyVersion;
}

public void setPolicyVersion(String policyVersion) {
this.policyVersion = policyVersion;
}

public String getDirection() {
return direction;
}

public void setDirection(String direction) {
this.direction = direction;
}

public int getOrder() {
return order;
}

public void setOrder(int order) {
this.order = order;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.wso2.carbon.apimgt.common.analytics.publishers.dto;

import java.util.ArrayList;
import java.util.List;

/**
* URITemplate attribute in analytics event.
*/
public class URITemplate {
private String uriTemplate;
private String resourceURI;
private String resourceSandboxURI;
private String httpVerb;

private String authScheme;
private List<OperationPolicy> operationPolicies = new ArrayList<>();

public void setOperationPolicies(List<OperationPolicy> operationPolicies) {
this.operationPolicies = operationPolicies;
}

public List<OperationPolicy> getOperationPolicies() {
return operationPolicies;
}

public void setHttpVerb(String httpVerb) {
this.httpVerb = httpVerb;
}

public String getHttpVerb() {
return httpVerb;
}

public void setUriTemplate(String uriTemplate) {
this.uriTemplate = uriTemplate;
}

public String getUriTemplate() {
return uriTemplate;
}

public void setResourceURI(String resourceURI) {
this.resourceURI = resourceURI;
}

public String getResourceURI() {
return resourceURI;
}

public void setResourceSandboxURI(String resourceSandboxURI) {
this.resourceSandboxURI = resourceSandboxURI;
}

public String getResourceSandboxURI() {
return resourceSandboxURI;
}

public void setAuthScheme(String authScheme) {
this.authScheme = authScheme;
}

public String getAuthScheme() {
return authScheme;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.rest.RESTConstants;
import org.apache.synapse.transport.passthru.util.RelayUtils;
import org.wso2.carbon.apimgt.api.model.OperationPolicy;
import org.wso2.carbon.apimgt.api.model.subscription.URLMapping;
import org.wso2.carbon.apimgt.common.analytics.collectors.AnalyticsCustomDataProvider;
import org.wso2.carbon.apimgt.common.analytics.collectors.AnalyticsDataProvider;
import org.wso2.carbon.apimgt.common.analytics.exceptions.DataNotFoundException;
Expand All @@ -38,6 +40,7 @@
import org.wso2.carbon.apimgt.common.analytics.publishers.dto.MetaInfo;
import org.wso2.carbon.apimgt.common.analytics.publishers.dto.Operation;
import org.wso2.carbon.apimgt.common.analytics.publishers.dto.Target;
import org.wso2.carbon.apimgt.common.analytics.publishers.dto.URITemplate;
import org.wso2.carbon.apimgt.common.analytics.publishers.dto.enums.EventCategory;
import org.wso2.carbon.apimgt.common.analytics.publishers.dto.enums.FaultCategory;
import org.wso2.carbon.apimgt.common.analytics.publishers.dto.enums.FaultSubCategory;
Expand All @@ -58,9 +61,13 @@
import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS;
import static org.wso2.carbon.apimgt.gateway.handlers.analytics.Constants.UNKNOWN_VALUE;
Expand Down Expand Up @@ -163,6 +170,30 @@ public API getApi() throws DataNotFoundException {
api.setApiVersion(apiObj.getApiVersion());
api.setApiCreator(apiObj.getApiProvider());
api.setApiCreatorTenantDomain(MultitenantUtils.getTenantDomain(api.getApiCreator()));
List<URITemplate> uriTemplates = new ArrayList<>();
for (URLMapping uriTemplate : apiObj.getUrlMappings()) {
org.wso2.carbon.apimgt.common.analytics.publishers.dto.URITemplate uriTemplateObj = new org.wso2.carbon.apimgt.common.analytics.publishers.dto.URITemplate();
if (uriTemplate.getHttpMethod() != null && uriTemplate.getHttpMethod()
.equals(messageContext.getProperty("api.ut.HTTP_METHOD")) && uriTemplate.getUrlPattern() != null
&& uriTemplate.getUrlPattern().equals(messageContext.getProperty("api.ut.resource"))) {
uriTemplateObj.setResourceURI(uriTemplate.getUrlPattern());
uriTemplateObj.setHttpVerb(uriTemplate.getHttpMethod());
uriTemplateObj.setAuthScheme(uriTemplate.getAuthScheme());
List<org.wso2.carbon.apimgt.common.analytics.publishers.dto.OperationPolicy> operationPolicies = new ArrayList<>();
for (OperationPolicy operationPolicy : uriTemplate.getOperationPolicies()) {
org.wso2.carbon.apimgt.common.analytics.publishers.dto.OperationPolicy operationPolicyObj = new org.wso2.carbon.apimgt.common.analytics.publishers.dto.OperationPolicy();
operationPolicyObj.setPolicyVersion(operationPolicy.getPolicyVersion());
operationPolicyObj.setPolicyName(operationPolicy.getPolicyName());
operationPolicyObj.setPolicyId(operationPolicy.getPolicyId());
operationPolicyObj.setDirection(operationPolicy.getDirection());
operationPolicyObj.setOrder(operationPolicy.getOrder());
operationPolicies.add(operationPolicyObj);
}
uriTemplateObj.setOperationPolicies(operationPolicies);
uriTemplates.add(uriTemplateObj);
}
}
api.setUriTemplates(uriTemplates);
}
return api;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.wso2.carbon.apimgt.api.APIManagementException;
import org.wso2.carbon.apimgt.api.dto.ConditionDTO;
import org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO;
import org.wso2.carbon.apimgt.api.model.OperationPolicy;
import org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit;
import org.wso2.carbon.apimgt.api.model.policy.EventCountLimit;
import org.wso2.carbon.apimgt.api.model.policy.PolicyConstants;
Expand Down Expand Up @@ -1173,7 +1174,6 @@ private void attachURlMappingDetailsOfApiProduct(Connection connection, API api)
}

public API getAPIByContextAndVersion(String context, String version, String deployment, boolean isExpand) {

String sql = SubscriptionValidationSQLConstants.GET_API_BY_CONTEXT_AND_VERSION_SQL;
String contextWhenContextTemplateIsNull = context;

Expand Down Expand Up @@ -1260,6 +1260,39 @@ private void attachURLMappingDetails(Connection connection, String revisionId, A
}
}
}

// Attach the relevant operation policies to the resources.
attachOperationPolicies(connection, revisionId, api);
}

private void attachOperationPolicies(Connection connection, String revisionId, API api) throws SQLException {
try(PreparedStatement preparedStatement = connection.prepareStatement(SubscriptionValidationSQLConstants.GET_OPERATION_POLICIES_PER_URI_BY_API_SQL)){
preparedStatement.setInt(1, api.getApiId());
preparedStatement.setString(2, revisionId);
try(ResultSet resultSet = preparedStatement.executeQuery()){
while (resultSet.next()){
String httpMethod = resultSet.getString("HTTP_METHOD");
String urlPattern = resultSet.getString("URL_PATTERN");
String policyName = resultSet.getString("POLICY_NAME");
String policyVersion = resultSet.getString("POLICY_VERSION");
String direction = resultSet.getString("DIRECTION");
String policyID = resultSet.getString("POLICY_UUID");
URLMapping urlMapping = api.getResource(urlPattern, httpMethod);
if(urlMapping!=null){
if (StringUtils.isNotEmpty(policyID) && StringUtils.isNotEmpty(policyName)
&& StringUtils.isNotEmpty(policyVersion) && StringUtils.isNotEmpty(direction)) {
OperationPolicy operationPolicy = new OperationPolicy();
operationPolicy.setPolicyId(policyID);
operationPolicy.setPolicyName(policyName);
operationPolicy.setPolicyVersion(policyVersion);
operationPolicy.setDirection(direction);
urlMapping.setOperationPolicies(operationPolicy);
}
api.addResource(urlMapping);
}
}
}
}
}

private boolean isAPIDefaultVersion(Connection connection, String provider, String name, String version)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,12 @@ public class SubscriptionValidationSQLConstants {
"AM_API_RESOURCE_SCOPE_MAPPING.SCOPE_NAME FROM AM_API_URL_MAPPING LEFT JOIN AM_API_RESOURCE_SCOPE_MAPPING" +
" ON AM_API_URL_MAPPING.URL_MAPPING_ID=AM_API_RESOURCE_SCOPE_MAPPING.URL_MAPPING_ID WHERE " +
"AM_API_URL_MAPPING.API_ID = ? AND AM_API_URL_MAPPING.REVISION_UUID = ?";
public static final String GET_OPERATION_POLICIES_PER_URI_BY_API_SQL = "SELECT AUM.HTTP_METHOD, AUM.URL_PATTERN, "
+ "OP.POLICY_NAME, OP.POLICY_VERSION, OPM.DIRECTION, OPM.POLICY_UUID "
+ "FROM AM_API_URL_MAPPING AS AUM INNER JOIN AM_API_OPERATION_POLICY_MAPPING OPM ON AUM.URL_MAPPING_ID = OPM.URL_MAPPING_ID "
+ "INNER JOIN AM_OPERATION_POLICY OP ON OPM.POLICY_UUID = OP.POLICY_UUID "
+ "INNER JOIN AM_API_OPERATION_POLICY AOP ON OPM.POLICY_UUID = AOP.POLICY_UUID "
+ "WHERE AUM.API_ID = ? AND AUM.REVISION_UUID = ?";

public static final String GET_ALL_APIS_BY_ORGANIZATION_AND_DEPLOYMENT_SQL = "SELECT AM_API.API_PROVIDER,AM_API" +
".API_NAME,AM_API.CONTEXT, AM_API.CONTEXT_TEMPLATE, AM_API.API_UUID,AM_API.API_ID,AM_API.API_TIER,AM_API.API_VERSION,AM_API" +
Expand Down
Loading

0 comments on commit d3305b7

Please sign in to comment.