Skip to content

Commit

Permalink
feat: send operation policies in analytics event-approach2
Browse files Browse the repository at this point in the history
  • Loading branch information
BLasan committed Oct 26, 2023
1 parent c7226b8 commit 51b2059
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class URLMapping {
private String authScheme;
private String httpMethod;
private String urlPattern;
private Set<String> scopes = new HashSet<>();
private List<String> scopes = new ArrayList<>();
private Set<OperationPolicy> operationPolicies = new HashSet<>();


Expand Down Expand Up @@ -80,7 +80,7 @@ public void addScope(String scope) {
scopes.add(scope);
}

public Set<String> getScopes() {
public List<String> getScopes() {
return scopes;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5071,7 +5071,6 @@ public API getLightweightAPIByUUID(String uuid, String organization) throws APIM
Map<String, Tier> definedTiers = APIUtil.getTiers(tenantId);
Set<Tier> availableTiers = APIUtil.getAvailableTiers(definedTiers, tiers, api.getId().getApiName());
api.removeAllTiers();

api.setAvailableTiers(availableTiers);
return api;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,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;
try (Connection connection = APIMgtDBUtil.getConnection()) {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
Expand Down Expand Up @@ -1211,10 +1210,6 @@ private void attachURLMappingDetails(Connection connection, String revisionId, A
String urlPattern = resultSet.getString("URL_PATTERN");
String throttlingTier = resultSet.getString("THROTTLING_TIER");
String scopeName = resultSet.getString("SCOPE_NAME");
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) {
urlMapping = new URLMapping();
Expand All @@ -1223,22 +1218,46 @@ private void attachURLMappingDetails(Connection connection, String revisionId, A
urlMapping.setThrottlingPolicy(throttlingTier);
urlMapping.setUrlPattern(urlPattern);
}
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);
}
if (StringUtils.isNotEmpty(scopeName)) {
urlMapping.addScope(scopeName);
}
api.addResource(urlMapping);
}
}
}

// 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 @@ -565,26 +565,17 @@ public class SubscriptionValidationSQLConstants {
public static final String GET_DEFAULT_VERSION_API_SQL = "SELECT PUBLISHED_DEFAULT_API_VERSION FROM " +
"AM_API_DEFAULT_VERSION WHERE API_NAME = ? AND API_PROVIDER = ? AND PUBLISHED_DEFAULT_API_VERSION = ?";

// public static final String GET_URI_TEMPLATES_BY_API_SQL = "SELECT AUM.HTTP_METHOD," +
// "AUM.AUTH_SCHEME,AUM.URL_PATTERN,AUM.THROTTLING_TIER," +
// "AM_API_RESOURCE_SCOPE_MAPPING.SCOPE_NAME, OP.POLICY_NAME, OP.POLICY_VERSION, OPM.DIRECTION, OPM.POLICY_UUID " +
// " FROM AM_API_URL_MAPPING AUM LEFT JOIN AM_API_RESOURCE_SCOPE_MAPPING" +
// " ON AUM.URL_MAPPING_ID=AM_API_RESOURCE_SCOPE_MAPPING.URL_MAPPING_ID " +
// "LEFT JOIN AM_API_OPERATION_POLICY_MAPPING OPM ON AUM.URL_MAPPING_ID = OPM.URL_MAPPING_ID " +
// "LEFT JOIN AM_OPERATION_POLICY OP ON OPM.POLICY_UUID = OP.POLICY_UUID " +
// "LEFT JOIN AM_API_OPERATION_POLICY AOP ON OPM.POLICY_UUID = AOP.POLICY_UUID " +
// "WHERE AM_API_URL_MAPPING.API_ID = ? AND AM_API_URL_MAPPING.REVISION_UUID = ?";

public static final String GET_URI_TEMPLATES_BY_API_SQL = "SELECT AUM.HTTP_METHOD," +
"AUM.AUTH_SCHEME,AUM.URL_PATTERN,AUM.THROTTLING_TIER," +
"AM_API_RESOURCE_SCOPE_MAPPING.SCOPE_NAME, OP.POLICY_NAME, OP.POLICY_VERSION, " +
"OPM.DIRECTION, OPM.POLICY_UUID " +
"FROM AM_API_URL_MAPPING AS AUM LEFT JOIN AM_API_RESOURCE_SCOPE_MAPPING " +
"ON AUM.URL_MAPPING_ID=AM_API_RESOURCE_SCOPE_MAPPING.URL_MAPPING_ID " +
"LEFT JOIN AM_API_OPERATION_POLICY_MAPPING OPM ON AUM.URL_MAPPING_ID = OPM.URL_MAPPING_ID " +
"LEFT JOIN AM_OPERATION_POLICY OP ON OPM.POLICY_UUID = OP.POLICY_UUID " +
"LEFT 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_URI_TEMPLATES_BY_API_SQL = "SELECT AM_API_URL_MAPPING.HTTP_METHOD," +
"AM_API_URL_MAPPING.AUTH_SCHEME,AM_API_URL_MAPPING.URL_PATTERN,AM_API_URL_MAPPING.THROTTLING_TIER," +
"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.API_UUID,AM_API.API_ID,AM_API.API_TIER,AM_API.API_VERSION,AM_API" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private static APIDTO fromAPItoDTO(API model) {
urlMappingDTO.setHttpMethod(urlMapping.getHttpMethod());
urlMappingDTO.setThrottlingPolicy(urlMapping.getThrottlingPolicy());
urlMappingDTO.setUrlPattern(urlMapping.getUrlPattern());
urlMappingDTO.setScopes(new ArrayList<>(urlMapping.getScopes()));
urlMappingDTO.setScopes(urlMapping.getScopes());
List<OperationPolicyDTO> operationPolicyDTOList = new ArrayList<>();
for(OperationPolicy operationPolicy: urlMapping.getOperationPolicies()) {
OperationPolicyDTO operationPolicyDTO = new OperationPolicyDTO();
Expand Down Expand Up @@ -144,7 +144,7 @@ public static APIListDTO fromAPIToAPIListDTO(API model) {
urlMappingDTO.setHttpMethod(urlMapping.getHttpMethod());
urlMappingDTO.setThrottlingPolicy(urlMapping.getThrottlingPolicy());
urlMappingDTO.setUrlPattern(urlMapping.getUrlPattern());
urlMappingDTO.setScopes(new ArrayList<>(urlMapping.getScopes()));
urlMappingDTO.setScopes(urlMapping.getScopes());
List<OperationPolicyDTO> operationPolicyDTOList = new ArrayList<>();
for(OperationPolicy operationPolicy: urlMapping.getOperationPolicies()) {
OperationPolicyDTO operationPolicyDTO = new OperationPolicyDTO();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public boolean validateScopes(TokenValidationContext validationContext) throws A
scopesValidated = true;
continue;
}
List<String> mappingScopes = new ArrayList<>(urlMapping.getScopes());
List<String> mappingScopes = urlMapping.getScopes();
boolean validate = false;
for (String scope : mappingScopes) {
if (scopesSet.contains(scope)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@

package org.wso2.carbon.apimgt.keymgt.model.entity;

import org.wso2.carbon.apimgt.api.model.URITemplate;
import org.wso2.carbon.apimgt.api.model.subscription.CacheableEntity;
import org.wso2.carbon.apimgt.api.model.subscription.URLMapping;

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

/**
* Entity for keeping API related information.
Expand All @@ -44,7 +40,6 @@ public class API implements CacheableEntity<String> {
private String organization;
private boolean deployed = false;
private boolean isDefaultVersion = false;
private List<URLMapping> urlMappings = new ArrayList<>();

public API() {
}
Expand Down Expand Up @@ -103,6 +98,7 @@ public API(String uuid, Integer apiId, String provider, String name, String vers
this.deployed = isDeployed;
}

private List<URLMapping> urlMappings = new ArrayList<>();
public void addResource(URLMapping resource) {

urlMappings.add(resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private static List<URLMappingDTO> convertUriTemplate(List<URLMapping> resources
URLMappingDTO urlMappingDTO =
new URLMappingDTO().urlPattern(resource.getUrlPattern()).authScheme(resource.getAuthScheme())
.httpMethod(resource.getHttpMethod()).throttlingPolicy(resource.getThrottlingPolicy())
.scopes(new ArrayList<>(resource.getScopes()));
.scopes(resource.getScopes());
urlMappingDTOList.add(urlMappingDTO);
}
return urlMappingDTOList;
Expand Down

0 comments on commit 51b2059

Please sign in to comment.