From 276d88f509631b44d88ede8b298d83e5a2dc65c0 Mon Sep 17 00:00:00 2001 From: BLasan Date: Thu, 26 Oct 2023 11:21:41 +0530 Subject: [PATCH] feat: send operation policies in analytics event-approach2 --- .../api/model/subscription/URLMapping.java | 4 +- .../carbon/apimgt/impl/APIProviderImpl.java | 1 - .../impl/dao/SubscriptionValidationDAO.java | 47 +++++++++++++------ .../SubscriptionValidationSQLConstants.java | 31 +++++------- .../utils/SubscriptionValidationDataUtil.java | 4 +- .../handlers/DefaultKeyValidationHandler.java | 2 +- .../apimgt/keymgt/model/entity/API.java | 6 +-- .../rest/api/gateway/impl/GatewayUtils.java | 2 +- 8 files changed, 51 insertions(+), 46 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/subscription/URLMapping.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/subscription/URLMapping.java index 0ad94795b5f1..5424825b7c43 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/subscription/URLMapping.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/subscription/URLMapping.java @@ -32,7 +32,7 @@ public class URLMapping { private String authScheme; private String httpMethod; private String urlPattern; - private Set scopes = new HashSet<>(); + private List scopes = new ArrayList<>(); private Set operationPolicies = new HashSet<>(); @@ -80,7 +80,7 @@ public void addScope(String scope) { scopes.add(scope); } - public Set getScopes() { + public List getScopes() { return scopes; } diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java index cc2f77dc3140..74bbaa33b8a8 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java @@ -5071,7 +5071,6 @@ public API getLightweightAPIByUUID(String uuid, String organization) throws APIM Map definedTiers = APIUtil.getTiers(tenantId); Set availableTiers = APIUtil.getAvailableTiers(definedTiers, tiers, api.getId().getApiName()); api.removeAllTiers(); - api.setAvailableTiers(availableTiers); return api; } else { diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/SubscriptionValidationDAO.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/SubscriptionValidationDAO.java index c73c7c6d9c74..ee15d8d96395 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/SubscriptionValidationDAO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/SubscriptionValidationDAO.java @@ -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)) { @@ -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(); @@ -1223,15 +1218,6 @@ 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); } @@ -1239,6 +1225,39 @@ private void attachURLMappingDetails(Connection connection, String revisionId, A } } } + + // This way we can reduce the number of iterations + 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) diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SubscriptionValidationSQLConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SubscriptionValidationSQLConstants.java index fa27d4742234..e44177d64818 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SubscriptionValidationSQLConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SubscriptionValidationSQLConstants.java @@ -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" + diff --git a/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/main/java/org/wso2/carbon/apimgt/internal/service/utils/SubscriptionValidationDataUtil.java b/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/main/java/org/wso2/carbon/apimgt/internal/service/utils/SubscriptionValidationDataUtil.java index 86697d461201..333bb1fb47ae 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/main/java/org/wso2/carbon/apimgt/internal/service/utils/SubscriptionValidationDataUtil.java +++ b/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/main/java/org/wso2/carbon/apimgt/internal/service/utils/SubscriptionValidationDataUtil.java @@ -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 operationPolicyDTOList = new ArrayList<>(); for(OperationPolicy operationPolicy: urlMapping.getOperationPolicies()) { OperationPolicyDTO operationPolicyDTO = new OperationPolicyDTO(); @@ -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 operationPolicyDTOList = new ArrayList<>(); for(OperationPolicy operationPolicy: urlMapping.getOperationPolicies()) { OperationPolicyDTO operationPolicyDTO = new OperationPolicyDTO(); diff --git a/components/apimgt/org.wso2.carbon.apimgt.keymgt/src/main/java/org/wso2/carbon/apimgt/keymgt/handlers/DefaultKeyValidationHandler.java b/components/apimgt/org.wso2.carbon.apimgt.keymgt/src/main/java/org/wso2/carbon/apimgt/keymgt/handlers/DefaultKeyValidationHandler.java index b8ea9e9f0e73..c918dc8ea863 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.keymgt/src/main/java/org/wso2/carbon/apimgt/keymgt/handlers/DefaultKeyValidationHandler.java +++ b/components/apimgt/org.wso2.carbon.apimgt.keymgt/src/main/java/org/wso2/carbon/apimgt/keymgt/handlers/DefaultKeyValidationHandler.java @@ -189,7 +189,7 @@ public boolean validateScopes(TokenValidationContext validationContext) throws A scopesValidated = true; continue; } - List mappingScopes = new ArrayList<>(urlMapping.getScopes()); + List mappingScopes = urlMapping.getScopes(); boolean validate = false; for (String scope : mappingScopes) { if (scopesSet.contains(scope)) { diff --git a/components/apimgt/org.wso2.carbon.apimgt.keymgt/src/main/java/org/wso2/carbon/apimgt/keymgt/model/entity/API.java b/components/apimgt/org.wso2.carbon.apimgt.keymgt/src/main/java/org/wso2/carbon/apimgt/keymgt/model/entity/API.java index a60c936e953a..e93e6ce97737 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.keymgt/src/main/java/org/wso2/carbon/apimgt/keymgt/model/entity/API.java +++ b/components/apimgt/org.wso2.carbon.apimgt.keymgt/src/main/java/org/wso2/carbon/apimgt/keymgt/model/entity/API.java @@ -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. @@ -44,7 +40,6 @@ public class API implements CacheableEntity { private String organization; private boolean deployed = false; private boolean isDefaultVersion = false; - private List urlMappings = new ArrayList<>(); public API() { } @@ -103,6 +98,7 @@ public API(String uuid, Integer apiId, String provider, String name, String vers this.deployed = isDeployed; } + private List urlMappings = new ArrayList<>(); public void addResource(URLMapping resource) { urlMappings.add(resource); diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.gateway/src/main/java/org/wso2/carbon/apimgt/rest/api/gateway/impl/GatewayUtils.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.gateway/src/main/java/org/wso2/carbon/apimgt/rest/api/gateway/impl/GatewayUtils.java index 6d012593e6fa..d4404fb67d17 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.gateway/src/main/java/org/wso2/carbon/apimgt/rest/api/gateway/impl/GatewayUtils.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.gateway/src/main/java/org/wso2/carbon/apimgt/rest/api/gateway/impl/GatewayUtils.java @@ -117,7 +117,7 @@ private static List convertUriTemplate(List 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;