From 587c32b5019cf088bd20f208a2f4bff2a95ad870 Mon Sep 17 00:00:00 2001 From: BLasan Date: Fri, 27 Oct 2023 20:11:01 +0530 Subject: [PATCH] add: API Policies to the API object in gateway --- .../apimgt/api/model/subscription/API.java | 14 +++++++ .../common/analytics/publishers/dto/API.java | 9 +++++ .../SynapseAnalyticsDataProvider.java | 11 ++++++ .../impl/dao/SubscriptionValidationDAO.java | 39 +++++++++++++------ .../SubscriptionValidationSQLConstants.java | 27 +++++++++---- .../apimgt/internal/service/dto/APIDTO.java | 23 ++++++++++- .../utils/SubscriptionValidationDataUtil.java | 21 ++++++++++ .../src/main/resources/api.yaml | 4 ++ .../apimgt/keymgt/model/entity/API.java | 11 ++++++ 9 files changed, 139 insertions(+), 20 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/subscription/API.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/subscription/API.java index 169108248163..826ee0eaa36b 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/subscription/API.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/subscription/API.java @@ -18,8 +18,13 @@ package org.wso2.carbon.apimgt.api.model.subscription; +import org.wso2.carbon.apimgt.api.model.OperationPolicy; +import org.wso2.carbon.apimgt.api.model.policy.APIPolicy; + import java.util.HashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Set; /** * Entity for keeping API related information. @@ -39,6 +44,7 @@ public class API implements CacheableEntity { private String status; private String revision; private String organization; + private Set apiPolicies = new HashSet<>(); public String getRevision() { @@ -199,4 +205,12 @@ public String getOrganization() { public void setOrganization(String organization) { this.organization = organization; } + + public void setApiPolicy(OperationPolicy apiPolicy) { + this.apiPolicies.add(apiPolicy); + } + + public Set getApiPolicies() { + return apiPolicies; + } } diff --git a/components/apimgt/org.wso2.carbon.apimgt.common.analytics/src/main/java/org/wso2/carbon/apimgt/common/analytics/publishers/dto/API.java b/components/apimgt/org.wso2.carbon.apimgt.common.analytics/src/main/java/org/wso2/carbon/apimgt/common/analytics/publishers/dto/API.java index bc1e4b41b045..3c14d18ebded 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.common.analytics/src/main/java/org/wso2/carbon/apimgt/common/analytics/publishers/dto/API.java +++ b/components/apimgt/org.wso2.carbon.apimgt.common.analytics/src/main/java/org/wso2/carbon/apimgt/common/analytics/publishers/dto/API.java @@ -31,6 +31,7 @@ public class API { private String apiCreator; private String apiCreatorTenantDomain; private List uriTemplates = new ArrayList<>(); + private List apiPolicies = new ArrayList<>(); public String getApiId() { return apiId; @@ -87,4 +88,12 @@ public List getUriTemplates() { public void setUriTemplates(List uriTemplates) { this.uriTemplates = uriTemplates; } + + public void setApiPolicies(List apiPolicies) { + this.apiPolicies = apiPolicies; + } + + public List getApiPolicies() { + return apiPolicies; + } } diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java index 1a3a65928684..6d4233b57bf6 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java @@ -181,7 +181,18 @@ public API getApi() throws DataNotFoundException { uriTemplates.add(uriTemplateObj); } } + List apiPolicyList = new ArrayList<>(); + for(OperationPolicy apiPolicy: apiObj.getApiPolicies()){ + org.wso2.carbon.apimgt.common.analytics.publishers.dto.OperationPolicy operationPolicyObj = new org.wso2.carbon.apimgt.common.analytics.publishers.dto.OperationPolicy(); + operationPolicyObj.setPolicyVersion(apiPolicy.getPolicyVersion()); + operationPolicyObj.setPolicyName(apiPolicy.getPolicyName()); + operationPolicyObj.setPolicyId(apiPolicy.getPolicyId()); + operationPolicyObj.setDirection(apiPolicy.getDirection()); + operationPolicyObj.setOrder(apiPolicy.getOrder()); + apiPolicyList.add(operationPolicyObj); + } api.setUriTemplates(uriTemplates); + api.setApiPolicies(apiPolicyList); } return api; } 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 b79c7b717801..c5623ad67843 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 @@ -1226,13 +1226,14 @@ private void attachURLMappingDetails(Connection connection, String revisionId, A } } - // Attach the relevant operation policies to the resources. - attachOperationPolicies(connection, revisionId, api); + // Attach the relevant operation policies and api to the resources and to the API. + attachPolicies(connection, revisionId, api); + } - private void attachOperationPolicies(Connection connection, String revisionId, API api) throws SQLException { + private void attachPolicies(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(1, api.getApiUUID()); preparedStatement.setString(2, revisionId); try(ResultSet resultSet = preparedStatement.executeQuery()){ while (resultSet.next()){ @@ -1240,20 +1241,34 @@ private void attachOperationPolicies(Connection connection, String revisionId, A 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); + String operationPolicyDirection = resultSet.getString("OPERATION_POLICY_DIRECTION"); + String apiPolicyDirection = resultSet.getString("API_POLICY_DIRECTION"); + String operationPolicyID = resultSet.getString("OPERATION_POLICY_UUID"); + String apiPolicyUUID = resultSet.getString("API_POLICY_UUID"); + URLMapping urlMapping = null; + if(StringUtils.isNotEmpty(httpMethod) && StringUtils.isNotEmpty(urlPattern)){ + urlMapping = api.getResource(urlPattern, httpMethod); + } if(urlMapping!=null){ - if (StringUtils.isNotEmpty(policyID) && StringUtils.isNotEmpty(policyName) - && StringUtils.isNotEmpty(policyVersion) && StringUtils.isNotEmpty(direction)) { + if (StringUtils.isNotEmpty(operationPolicyID) && StringUtils.isNotEmpty(policyName) + && StringUtils.isNotEmpty(policyVersion) && StringUtils.isNotEmpty(operationPolicyDirection)) { OperationPolicy operationPolicy = new OperationPolicy(); - operationPolicy.setPolicyId(policyID); + operationPolicy.setPolicyId(operationPolicyID); operationPolicy.setPolicyName(policyName); operationPolicy.setPolicyVersion(policyVersion); - operationPolicy.setDirection(direction); + operationPolicy.setDirection(operationPolicyDirection); urlMapping.setOperationPolicies(operationPolicy); + api.addResource(urlMapping); } - api.addResource(urlMapping); + } + if(StringUtils.isNotEmpty(apiPolicyUUID) && StringUtils.isNotEmpty(policyName) + && StringUtils.isNotEmpty(policyVersion) && StringUtils.isNotEmpty(apiPolicyDirection)){ + OperationPolicy apiPolicy = new OperationPolicy(); + apiPolicy.setPolicyId(apiPolicyUUID); + apiPolicy.setPolicyName(policyName); + apiPolicy.setPolicyVersion(policyVersion); + apiPolicy.setDirection(apiPolicyDirection); + api.setApiPolicy(apiPolicy); } } } 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 e44177d64818..8c06a5f7df8f 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 @@ -570,13 +570,26 @@ 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_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_OPERATION_POLICIES_PER_URI_BY_API_SQL = + "SELECT OP.POLICY_NAME, OP.POLICY_VERSION,APM.DIRECTION AS API_POLICY_DIRECTION, APM.POLICY_UUID AS API_POLICY_UUID, OPM.POLICY_UUID AS OPERATION_POLICY_UUID, OPM.DIRECTION AS OPERATION_POLICY_DIRECTION, AUM.HTTP_METHOD, AUM.URL_PATTERN " + + "FROM AM_API_OPERATION_POLICY AS AOP " + + "LEFT JOIN AM_API_OPERATION_POLICY_MAPPING OPM ON AOP.POLICY_UUID = OPM.POLICY_UUID " + + "LEFT JOIN AM_API_URL_MAPPING AUM ON AUM.URL_MAPPING_ID = OPM.URL_MAPPING_ID " + + "INNER JOIN AM_OPERATION_POLICY OP ON OP.POLICY_UUID = AOP.POLICY_UUID " + + "LEFT JOIN AM_API_POLICY_MAPPING as APM ON APM.POLICY_UUID = AOP.POLICY_UUID " + + "WHERE AOP.API_UUID = ? AND AOP.REVISION_UUID = ?"; + +// public static final String GET_API_POLICIES = "SELECT AUM.HTTP_METHOD, AUM.URL_PATTERN, " +// + "OP.POLICY_NAME, OP.POLICY_VERSION, OPM.DIRECTION, OPM.POLICY_UUID " +// + "FROM AM_API_POLICY_MAPPING AS APM INNER JOIN AM_API_OPERATION_POLICY OPM ON APM.POLICY_UUID = OPM.POLICY_UUID " +// + "WHERE APM.API_ID = ? AND APM.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" + ".API_TYPE,AM_API.STATUS,AM_REVISION.REVISION_UUID AS REVISION_UUID,AM_DEPLOYMENT_REVISION_MAPPING.NAME " + diff --git a/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/gen/java/org/wso2/carbon/apimgt/internal/service/dto/APIDTO.java b/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/gen/java/org/wso2/carbon/apimgt/internal/service/dto/APIDTO.java index 28f5672acaee..02f196a41056 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/gen/java/org/wso2/carbon/apimgt/internal/service/dto/APIDTO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/gen/java/org/wso2/carbon/apimgt/internal/service/dto/APIDTO.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonCreator; import java.util.ArrayList; import java.util.List; +import org.wso2.carbon.apimgt.internal.service.dto.OperationPolicyDTO; import org.wso2.carbon.apimgt.internal.service.dto.URLMappingDTO; import javax.validation.constraints.*; @@ -30,6 +31,7 @@ public class APIDTO { private String status = null; private String organization = null; private Boolean isDefaultVersion = null; + private List apiPolicies = new ArrayList<>(); private List urlMappings = new ArrayList<>(); /** @@ -229,6 +231,23 @@ public void setIsDefaultVersion(Boolean isDefaultVersion) { this.isDefaultVersion = isDefaultVersion; } + /** + **/ + public APIDTO apiPolicies(List apiPolicies) { + this.apiPolicies = apiPolicies; + return this; + } + + + @ApiModelProperty(value = "") + @JsonProperty("apiPolicies") + public List getApiPolicies() { + return apiPolicies; + } + public void setApiPolicies(List apiPolicies) { + this.apiPolicies = apiPolicies; + } + /** **/ public APIDTO urlMappings(List urlMappings) { @@ -267,12 +286,13 @@ public boolean equals(java.lang.Object o) { Objects.equals(status, API.status) && Objects.equals(organization, API.organization) && Objects.equals(isDefaultVersion, API.isDefaultVersion) && + Objects.equals(apiPolicies, API.apiPolicies) && Objects.equals(urlMappings, API.urlMappings); } @Override public int hashCode() { - return Objects.hash(uuid, apiId, provider, name, version, context, policy, apiType, status, organization, isDefaultVersion, urlMappings); + return Objects.hash(uuid, apiId, provider, name, version, context, policy, apiType, status, organization, isDefaultVersion, apiPolicies, urlMappings); } @Override @@ -291,6 +311,7 @@ public String toString() { sb.append(" status: ").append(toIndentedString(status)).append("\n"); sb.append(" organization: ").append(toIndentedString(organization)).append("\n"); sb.append(" isDefaultVersion: ").append(toIndentedString(isDefaultVersion)).append("\n"); + sb.append(" apiPolicies: ").append(toIndentedString(apiPolicies)).append("\n"); sb.append(" urlMappings: ").append(toIndentedString(urlMappings)).append("\n"); sb.append("}"); return sb.toString(); 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 333bb1fb47ae..5be0fc40f86e 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 @@ -115,6 +115,16 @@ private static APIDTO fromAPItoDTO(API model) { urlMappingDTO.setOperationPolicies(operationPolicyDTOList); urlMappingsDTO.add(urlMappingDTO); } + List apiPolicies = new ArrayList<>(); + for(OperationPolicy apiPolicy: model.getApiPolicies()) { + OperationPolicyDTO operationPolicyDTO = new OperationPolicyDTO(); + operationPolicyDTO.setPolicyId(apiPolicy.getPolicyId()); + operationPolicyDTO.setPolicyName(apiPolicy.getPolicyName()); + operationPolicyDTO.setPolicyVersion(apiPolicy.getPolicyVersion()); + operationPolicyDTO.setDirection(apiPolicy.getDirection()); + operationPolicyDTO.setOrder(apiPolicy.getOrder()); + } + apidto.setApiPolicies(apiPolicies); apidto.setUrlMappings(urlMappingsDTO); } return apidto; @@ -158,6 +168,17 @@ public static APIListDTO fromAPIToAPIListDTO(API model) { urlMappingDTO.setOperationPolicies(operationPolicyDTOList); urlMappingsDTO.add(urlMappingDTO); } + List apiPolicies = new ArrayList<>(); + for(OperationPolicy apiPolicy: model.getApiPolicies()) { + OperationPolicyDTO operationPolicyDTO = new OperationPolicyDTO(); + operationPolicyDTO.setPolicyId(apiPolicy.getPolicyId()); + operationPolicyDTO.setPolicyName(apiPolicy.getPolicyName()); + operationPolicyDTO.setPolicyVersion(apiPolicy.getPolicyVersion()); + operationPolicyDTO.setDirection(apiPolicy.getDirection()); + operationPolicyDTO.setOrder(apiPolicy.getOrder()); + apiPolicies.add(operationPolicyDTO); + } + apidto.setApiPolicies(apiPolicies); apidto.setUrlMappings(urlMappingsDTO); apiListdto.setCount(1); apiListdto.getList().add(apidto); diff --git a/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/main/resources/api.yaml b/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/main/resources/api.yaml index c993565bacd3..96c16de650ba 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/main/resources/api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/main/resources/api.yaml @@ -863,6 +863,10 @@ definitions: type: boolean description: Whether this is the default version of the API. example: false + apiPolicies: + type: array + items: + $ref: '#/definitions/OperationPolicy' urlMappings: type: array items: 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 11f0678a7339..0d091fece3bf 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,6 +18,7 @@ package org.wso2.carbon.apimgt.keymgt.model.entity; +import org.wso2.carbon.apimgt.api.model.OperationPolicy; import org.wso2.carbon.apimgt.api.model.subscription.CacheableEntity; import org.wso2.carbon.apimgt.api.model.subscription.URLMapping; @@ -41,6 +42,7 @@ public class API implements CacheableEntity { private String organization; private boolean deployed = false; private boolean isDefaultVersion = false; + private List apiPolicies = new ArrayList<>(); public API() { } @@ -205,6 +207,7 @@ public String toString() { ", status='" + status + '\'' + ", isDefaultVersion=" + isDefaultVersion + ", urlMappings=" + urlMappings + + ", apiPolicies=" + apiPolicies + '}'; } @@ -295,4 +298,12 @@ public boolean isDeployed() { public void setDeployed(boolean deployed) { this.deployed = deployed; } + + public void setApiPolicies(List apiPolicies) { + this.apiPolicies = apiPolicies; + } + + public List getApiPolicies() { + return apiPolicies; + } }