From 7ac614c6c46fc7430c4e209a77c8c054629d4c0d Mon Sep 17 00:00:00 2001 From: rusirijayodaillesinghe Date: Wed, 2 Oct 2024 02:09:40 +0530 Subject: [PATCH 01/15] Fix incorrectly added policy type --- .../wso2/carbon/apimgt/api/APIProvider.java | 3 + .../apimgt/api/model/OperationPolicy.java | 13 +- .../carbon/apimgt/impl/APIProviderImpl.java | 126 ++++++++++++++++++ .../carbon/apimgt/impl/dao/ApiMgtDAO.java | 9 +- .../importexport/ImportExportConstants.java | 3 + .../carbon/apimgt/impl/utils/APIUtil.java | 10 +- .../src/main/resources/publisher-api.yaml | 2 + .../publisher/v1/dto/OperationPolicyDTO.java | 22 ++- .../v1/common/SynapsePolicyAggregator.java | 2 +- .../v1/common/mappings/ExportUtils.java | 22 ++- .../v1/common/mappings/ImportUtils.java | 7 +- .../mappings/OperationPolicyMappingUtil.java | 2 + .../src/main/resources/publisher-api.yaml | 2 + 13 files changed, 206 insertions(+), 17 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java index fcd13d13c6c4..b797aed8c8ab 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java @@ -1553,6 +1553,9 @@ EnvironmentPropertiesDTO getEnvironmentSpecificAPIProperties(String apiUuid, Str */ String importOperationPolicy(OperationPolicyData operationPolicyData, String organization) throws APIManagementException; + String importOperationPolicyOfGivenType(OperationPolicyData operationPolicyData, String policyType, + String organization) throws APIManagementException; + /** * Add an API specific operation policy diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/OperationPolicy.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/OperationPolicy.java index 6d78f5e7e40f..7fc417294384 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/OperationPolicy.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/OperationPolicy.java @@ -25,6 +25,7 @@ public class OperationPolicy implements Comparable { private String policyName = ""; private String policyVersion = "v1"; + private String policyType = null; private String direction = null; private Map parameters = null; private String policyId = null; @@ -50,6 +51,16 @@ public void setPolicyVersion(String policyVersion) { this.policyVersion = policyVersion; } + public String getPolicyType() { + + return policyType; + } + + public void setPolicyType(String policyType) { + + this.policyType = policyType; + } + public Map getParameters() { return parameters; @@ -98,7 +109,7 @@ public boolean equals(Object o) { if (o instanceof OperationPolicy) { OperationPolicy policyObj = (OperationPolicy) o; return Objects.equals(policyName, policyObj.policyName) && Objects.equals(policyVersion, - policyObj.policyVersion) && Objects.equals(direction, policyObj.direction) && Objects.equals( + policyObj.policyVersion) && Objects.equals(direction, policyObj.direction) && policyType.equals(policyObj.policyType) && Objects.equals( parameters, policyObj.parameters) && Objects.equals(policyId, policyObj.policyId); } return false; 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 038e41cfe828..44d70b9be1fc 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 @@ -135,6 +135,7 @@ import org.wso2.carbon.apimgt.impl.importexport.APIImportExportException; import org.wso2.carbon.apimgt.impl.importexport.ExportFormat; import org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI; +import org.wso2.carbon.apimgt.impl.importexport.ImportExportConstants; import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder; import org.wso2.carbon.apimgt.impl.lifecycle.CheckListItem; import org.wso2.carbon.apimgt.impl.lifecycle.LCManagerFactory; @@ -5299,6 +5300,52 @@ public String getSecuritySchemeOfAPI(String uuid, String organization) throws AP } } + public String getPolicyType(OperationPolicy policy, String apiUUId, String tenantDomain) + throws APIManagementException { + String policyType = null; + if (policy.getPolicyId() == null) { + policyType = ImportExportConstants.POLICY_TYPE_API; + } else { + OperationPolicyData basicPolicyData = + getAPISpecificOperationPolicyByPolicyId(policy.getPolicyId(), + apiUUId, tenantDomain, false); + if (basicPolicyData.getClonedCommonPolicyId() == null) { + policyType = ImportExportConstants.POLICY_TYPE_API; + } else { + policyType = ImportExportConstants.POLICY_TYPE_COMMON; + } + } + return policyType; + } + + public API addPolicyTypeFieldToApi(API api, String tenantDomain) + throws APIManagementException { + + Set uriTemplates = api.getUriTemplates(); + for (URITemplate uriTemplate : uriTemplates) { + List operationPolicies = uriTemplate.getOperationPolicies(); + if (!operationPolicies.isEmpty()) { + for (OperationPolicy operationPolicy : operationPolicies) { + String policyType = getPolicyType(operationPolicy, api.getUuid(), + tenantDomain); + operationPolicy.setPolicyType(policyType); + } + } + } + api.setUriTemplates(uriTemplates); + + if (isAPILevelPolicySupportEnabled) { + List apiPolicies = api.getApiPolicies(); + for (OperationPolicy policy : apiPolicies) { + String policyType = getPolicyType(policy, api.getUuid(), + tenantDomain); + policy.setPolicyType(policyType); + } + api.setApiPolicies(apiPolicies); + } + return api; + } + @Override public boolean isSubscriptionValidationDisabled(String uuid) throws APIManagementException { String status = apiMgtDAO.getSubscriptionValidationStatus(uuid); @@ -5341,6 +5388,7 @@ public API getAPIbyUUID(String uuid, String organization) throws APIManagementEx populateApiInfo(api); populateSubtypeConfiguration(api); populateDefaultVersion(api); + api = addPolicyTypeFieldToApi(api, organization); return api; } else { String msg = "Failed to get API. API artifact corresponding to artifactId " + uuid + " does not exist"; @@ -7115,6 +7163,84 @@ public String importOperationPolicy(OperationPolicyData importedPolicyData, Stri return policyId; } + @Override + public String importOperationPolicyOfGivenType(OperationPolicyData importedPolicyData, String policyType, + String organization) + throws APIManagementException { + + OperationPolicySpecification importedSpec = importedPolicyData.getSpecification(); + OperationPolicyData existingOperationPolicy; + + String policyId = null; + if (policyType == null) { + /*To handle scenarios where api is exported from a previous U2 version. API and Common policies with same name + and same version is not supported there + */ + importOperationPolicy(importedPolicyData, organization); + } else if (policyType.equalsIgnoreCase(ImportExportConstants.POLICY_TYPE_COMMON)) { + existingOperationPolicy = getCommonOperationPolicyByPolicyName(importedSpec.getName(), + importedSpec.getVersion(),organization, false); + + if (existingOperationPolicy != null) { + if (existingOperationPolicy.getMd5Hash().equals(importedPolicyData.getMd5Hash())) { + if (log.isDebugEnabled()) { + log.debug("Matching common policy found for imported policy and Md5 hashes match."); + } + policyId = existingOperationPolicy.getPolicyId(); + } else { + importedSpec.setName(importedSpec.getName() + "_imported"); + importedSpec.setDisplayName(importedSpec.getDisplayName() + " Imported"); + importedPolicyData.setSpecification(importedSpec); + importedPolicyData.setMd5Hash(APIUtil.getMd5OfOperationPolicy(importedPolicyData)); + policyId = addAPISpecificOperationPolicy(importedPolicyData.getApiUUID(), importedPolicyData, + organization); + if (log.isDebugEnabled()) { + log.debug("Even though existing common policy name match with imported policy, " + + "the MD5 hashes does not match in the policy " + existingOperationPolicy.getPolicyId() + + ". A new policy created with ID " + policyId); + } + } + } else { + policyId = addAPISpecificOperationPolicy(importedPolicyData.getApiUUID(), importedPolicyData, + organization); + if (log.isDebugEnabled()) { + log.debug( + "There aren't any existing policies for the imported policy. A new policy created with ID " + + policyId); + } + } + } else { //api level policy by default + existingOperationPolicy = + getAPISpecificOperationPolicyByPolicyName(importedSpec.getName(), importedSpec.getVersion(), + importedPolicyData.getApiUUID(), null, organization, false); + + if (existingOperationPolicy != null) { + if (existingOperationPolicy.getMd5Hash().equals(importedPolicyData.getMd5Hash())) { + if (log.isDebugEnabled()) { + log.debug("Matching API specific policy found for imported policy and MD5 hashes match."); + } + } else { + if (log.isDebugEnabled()) { + log.debug("Even though existing API specific policy name match with imported policy, " + + "the MD5 hashes does not match in the policy " + existingOperationPolicy.getPolicyId() + + ".Therefore updating the existing policy"); + } + updateOperationPolicy(existingOperationPolicy.getPolicyId(), importedPolicyData, organization); + } + policyId = existingOperationPolicy.getPolicyId(); + } else { + policyId = addAPISpecificOperationPolicy(importedPolicyData.getApiUUID(), importedPolicyData, + organization); + if (log.isDebugEnabled()) { + log.debug( + "There aren't any existing policies for the imported policy. A new policy created with ID " + + policyId); + } + } + } + + return policyId; + } @Override public String addAPISpecificOperationPolicy(String apiUUID, OperationPolicyData operationPolicyData, diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java index 47684cdfeaa1..3600023c750d 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java @@ -125,6 +125,7 @@ import org.wso2.carbon.apimgt.impl.dto.WorkflowDTO; import org.wso2.carbon.apimgt.impl.factory.KeyManagerHolder; import org.wso2.carbon.apimgt.impl.factory.SQLConstantManagerFactory; +import org.wso2.carbon.apimgt.impl.importexport.ImportExportConstants; import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder; import org.wso2.carbon.apimgt.impl.utils.APIMgtDBUtil; import org.wso2.carbon.apimgt.impl.utils.APIUtil; @@ -21273,7 +21274,13 @@ public Set getCommonOperationPolicyNames(String organization) throws API while (rs.next()) { String policyName = rs.getString("POLICY_NAME"); String policyVersion = rs.getString("POLICY_VERSION"); - policyNames.add(APIUtil.getOperationPolicyFileName(policyName, policyVersion)); + policyNames.add(APIUtil.getOperationPolicyFileName(policyName, policyVersion, null)); + /*since the only usage of this method is to load the common operation policies from the + specifications and we are keeping only the common policies without appending the string "common" + to the file name, it's not required to append the policyType string + (policyNames.add(APIUtil.getOperationPolicyFileName(policyName, policyVersion, + ImportExportConstants.POLICY_TYPE_COMMON));)here as well. + */ } } } catch (SQLException e) { diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/importexport/ImportExportConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/importexport/ImportExportConstants.java index b2658c493a58..9644b40c9b1c 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/importexport/ImportExportConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/importexport/ImportExportConstants.java @@ -316,4 +316,7 @@ public final class ImportExportConstants { public static final String EXPORT_POLICY_TYPE_JSON = "JSON"; public static final String POLICY_NAME = "name"; + + public static final String POLICY_TYPE_API = "api"; + public static final String POLICY_TYPE_COMMON = "common"; } diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java index bbde9fd047f5..e380448b59c2 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java @@ -168,6 +168,7 @@ import org.wso2.carbon.apimgt.impl.dto.ThrottleProperties; import org.wso2.carbon.apimgt.impl.dto.WorkflowDTO; import org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.DataLoadingException; +import org.wso2.carbon.apimgt.impl.importexport.ImportExportConstants; import org.wso2.carbon.apimgt.impl.internal.APIManagerComponent; import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder; import org.wso2.carbon.apimgt.impl.kmclient.ApacheFeignHttpClient; @@ -10212,7 +10213,7 @@ public static void loadCommonOperationPolicies(String organization) { policyData.setSpecification(policySpec); policyData.setOrganization(organization); String policyFileName = getOperationPolicyFileName(policySpec.getName(), - policySpec.getVersion()); + policySpec.getVersion(), ImportExportConstants.POLICY_TYPE_COMMON); OperationPolicyDefinition synapsePolicyDefinition = getOperationPolicyDefinitionFromFile(policyDefinitionLocation, policyFileName, APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION); @@ -10536,11 +10537,14 @@ public static OperationPolicyData generateOperationPolicyDataObject(String apiUu } - public static String getOperationPolicyFileName(String policyName, String policyVersion) { + public static String getOperationPolicyFileName(String policyName, String policyVersion, String policyType) { if (StringUtils.isEmpty(policyVersion)) { policyVersion = "v1"; } - return policyName + "_" + policyVersion; + if (policyType == null) { + return policyName + "_" + policyVersion; + } + return policyName + "_" + policyVersion + "_" + policyType; } public static String getCustomBackendFileName(String apiUUID, String endpointType) { diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml index 04e1f36411e1..c4c70e012475 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml @@ -13547,6 +13547,8 @@ components: policyVersion: type: string default: v1 + policyType: + type: string policyId: type: string parameters: diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/OperationPolicyDTO.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/OperationPolicyDTO.java index b88a28f2073f..fbdf6c279568 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/OperationPolicyDTO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/OperationPolicyDTO.java @@ -25,6 +25,7 @@ public class OperationPolicyDTO { private String policyName = null; private String policyVersion = "v1"; + private String policyType = null; private String policyId = null; private Map parameters = new HashMap(); @@ -63,6 +64,23 @@ public void setPolicyVersion(String policyVersion) { this.policyVersion = policyVersion; } + /** + **/ + public OperationPolicyDTO policyType(String policyType) { + this.policyType = policyType; + return this; + } + + + @ApiModelProperty(value = "") + @JsonProperty("policyType") + public String getPolicyType() { + return policyType; + } + public void setPolicyType(String policyType) { + this.policyType = policyType; + } + /** **/ public OperationPolicyDTO policyId(String policyId) { @@ -109,13 +127,14 @@ public boolean equals(java.lang.Object o) { OperationPolicyDTO operationPolicy = (OperationPolicyDTO) o; return Objects.equals(policyName, operationPolicy.policyName) && Objects.equals(policyVersion, operationPolicy.policyVersion) && + Objects.equals(policyType, operationPolicy.policyType) && Objects.equals(policyId, operationPolicy.policyId) && Objects.equals(parameters, operationPolicy.parameters); } @Override public int hashCode() { - return Objects.hash(policyName, policyVersion, policyId, parameters); + return Objects.hash(policyName, policyVersion, policyType, policyId, parameters); } @Override @@ -125,6 +144,7 @@ public String toString() { sb.append(" policyName: ").append(toIndentedString(policyName)).append("\n"); sb.append(" policyVersion: ").append(toIndentedString(policyVersion)).append("\n"); + sb.append(" policyType: ").append(toIndentedString(policyType)).append("\n"); sb.append(" policyId: ").append(toIndentedString(policyId)).append("\n"); sb.append(" parameters: ").append(toIndentedString(parameters)).append("\n"); sb.append("}"); diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/SynapsePolicyAggregator.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/SynapsePolicyAggregator.java index c204989287fb..4e75821a214b 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/SynapsePolicyAggregator.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/SynapsePolicyAggregator.java @@ -192,7 +192,7 @@ private static List renderPolicyMapping(List policyList if (flow.equals(policy.getDirection())) { Map policyParameters = policy.getParameters(); String policyFileName = APIUtil.getOperationPolicyFileName(policy.getPolicyName(), - policy.getPolicyVersion()); + policy.getPolicyVersion(), policy.getPolicyType()); OperationPolicySpecification policySpecification = ImportUtils .getOperationPolicySpecificationFromFile(policyDirectory, policyFileName); if (policySpecification.getSupportedGateways() diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ExportUtils.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ExportUtils.java index 9cf094aa3f3b..c626c24e4e39 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ExportUtils.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ExportUtils.java @@ -709,19 +709,25 @@ public static void addOperationPoliciesToArchive(String archivePath, String tena List operationPolicies = uriTemplate.getOperationPolicies(); if (operationPolicies != null && !operationPolicies.isEmpty()) { for (OperationPolicy policy : operationPolicies) { - if (!exportedPolicies.contains(policy.getPolicyName() + "_" + policy.getPolicyVersion())) { - String policyFileName = APIUtil.getOperationPolicyFileName(policy.getPolicyName(), - policy.getPolicyVersion()); + if (!exportedPolicies.contains(policy.getPolicyName() + "_" + policy.getPolicyVersion() + "_" + + policy.getPolicyType())) { if (policy.getPolicyId() != null) { + String policyFileName = APIUtil.getOperationPolicyFileName(policy.getPolicyName(), + policy.getPolicyVersion(), policy.getPolicyType()); + OperationPolicyData policyData = apiProvider.getAPISpecificOperationPolicyByPolicyId(policy.getPolicyId(), currentApiUuid, tenantDomain, true); if (policyData != null) { exportPolicyData(policyFileName, policyData, archivePath, exportFormat); - exportedPolicies.add(policy.getPolicyName() + "_" + policy.getPolicyVersion()); + exportedPolicies.add(policy.getPolicyName() + "_" + policy.getPolicyVersion() + "_" + + policy.getPolicyType()); } } else { // This path is to handle migrated APIs with mediation policies attached + // These are considered as API policies by default + String policyFileName = APIUtil.getOperationPolicyFileName(policy.getPolicyName(), + policy.getPolicyVersion(), ImportExportConstants.POLICY_TYPE_API); if (APIUtil.isSequenceDefined(api.getInSequence()) || APIUtil.isSequenceDefined(api.getOutSequence()) || APIUtil.isSequenceDefined(api.getFaultSequence())) { @@ -736,7 +742,8 @@ public static void addOperationPoliciesToArchive(String archivePath, String tena policy.getDirection(), tenantDomain); if (policyData != null) { exportPolicyData(policyFileName, policyData, archivePath, exportFormat); - exportedPolicies.add(policy.getPolicyName() + "_" + policy.getPolicyVersion()); + exportedPolicies.add(policy.getPolicyName() + "_" + policy.getPolicyVersion() + + "_" + ImportExportConstants.POLICY_TYPE_API); } } } @@ -748,14 +755,15 @@ public static void addOperationPoliciesToArchive(String archivePath, String tena if (api.getApiPolicies() != null && !api.getApiPolicies().isEmpty()) { for (OperationPolicy policy : api.getApiPolicies()) { String policyFileName = APIUtil.getOperationPolicyFileName(policy.getPolicyName(), - policy.getPolicyVersion()); + policy.getPolicyVersion(), policy.getPolicyType()); if (!exportedPolicies.contains(policyFileName)) { OperationPolicyData policyData = apiProvider.getAPISpecificOperationPolicyByPolicyId(policy.getPolicyId(), currentApiUuid, tenantDomain, true); if (policyData != null) { exportPolicyData(policyFileName, policyData, archivePath, exportFormat); - exportedPolicies.add(policy.getPolicyName() + "_" + policy.getPolicyVersion()); + exportedPolicies.add(policy.getPolicyName() + "_" + policy.getPolicyVersion() + "_" + + policy.getPolicyType()); } } } diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java index 85baaa5aa087..817fb5bc110c 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java @@ -636,7 +636,7 @@ public static void validateAppliedPolicy(OperationPolicy appliedPolicy, String policyDirectory = extractedFolderPath + File.separator + ImportExportConstants.POLICIES_DIRECTORY; appliedPolicy.setPolicyId(null); String policyFileName = APIUtil.getOperationPolicyFileName(appliedPolicy.getPolicyName(), - appliedPolicy.getPolicyVersion()); + appliedPolicy.getPolicyVersion(), appliedPolicy.getPolicyType()); OperationPolicySpecification policySpec = null; if (visitedPoliciesMap.containsKey(policyFileName)) { @@ -791,7 +791,7 @@ public static List findOrImportPolicy(List pol boolean policyImported = false; try { String policyFileName = APIUtil.getOperationPolicyFileName(policy.getPolicyName(), - policy.getPolicyVersion()); + policy.getPolicyVersion(), policy.getPolicyType()); String policyID = null; if (!importedPolicies.containsKey(policyFileName)) { OperationPolicySpecification policySpec = @@ -824,7 +824,8 @@ public static List findOrImportPolicy(List pol } operationPolicyData.setMd5Hash( APIUtil.getHashOfOperationPolicy(operationPolicyData)); - policyID = provider.importOperationPolicy(operationPolicyData, tenantDomain); + policyID = provider.importOperationPolicyOfGivenType(operationPolicyData, + policy.getPolicyType(), tenantDomain); importedPolicies.put(policyFileName, policyID); policyImported = true; } else { diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/OperationPolicyMappingUtil.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/OperationPolicyMappingUtil.java index e0ce3142a737..59b04ba3d320 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/OperationPolicyMappingUtil.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/OperationPolicyMappingUtil.java @@ -57,6 +57,7 @@ public static OperationPolicy fromDTOToOperationPolicy(OperationPolicyDTO operat OperationPolicy operationPolicy = new OperationPolicy(); operationPolicy.setPolicyName(operationPolicyDTO.getPolicyName()); operationPolicy.setPolicyVersion(operationPolicyDTO.getPolicyVersion()); + operationPolicy.setPolicyType(operationPolicyDTO.getPolicyType()); operationPolicy.setPolicyId(operationPolicyDTO.getPolicyId()); operationPolicy.setParameters(operationPolicyDTO.getParameters()); return operationPolicy; @@ -67,6 +68,7 @@ public static OperationPolicyDTO fromOperationPolicyToDTO(OperationPolicy operat OperationPolicyDTO dto = new OperationPolicyDTO(); dto.setPolicyName(operationPolicy.getPolicyName()); dto.setPolicyVersion(operationPolicy.getPolicyVersion()); + dto.setPolicyType(operationPolicy.getPolicyType()); dto.setPolicyId(operationPolicy.getPolicyId()); dto.setParameters(operationPolicy.getParameters()); return dto; diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml index 04e1f36411e1..c4c70e012475 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml @@ -13547,6 +13547,8 @@ components: policyVersion: type: string default: v1 + policyType: + type: string policyId: type: string parameters: From f3a09e816398b4eb96ab2570409fb8ebb9557d10 Mon Sep 17 00:00:00 2001 From: rusirijayodaillesinghe Date: Mon, 7 Oct 2024 15:32:02 +0530 Subject: [PATCH 02/15] Fix npe in api creation flow when api policies config is enabled --- .../org/wso2/carbon/apimgt/impl/APIProviderImpl.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) 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 44d70b9be1fc..8daa4cbc7b28 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 @@ -5336,10 +5336,12 @@ public API addPolicyTypeFieldToApi(API api, String tenantDomain) if (isAPILevelPolicySupportEnabled) { List apiPolicies = api.getApiPolicies(); - for (OperationPolicy policy : apiPolicies) { - String policyType = getPolicyType(policy, api.getUuid(), - tenantDomain); - policy.setPolicyType(policyType); + if (apiPolicies != null && !apiPolicies.isEmpty()) { + for (OperationPolicy policy : apiPolicies) { + String policyType = getPolicyType(policy, api.getUuid(), + tenantDomain); + policy.setPolicyType(policyType); + } } api.setApiPolicies(apiPolicies); } From 2c52bb2d755f8403334f71b27d9104a7a45d1f50 Mon Sep 17 00:00:00 2001 From: rusirijayodaillesinghe Date: Tue, 8 Oct 2024 11:09:06 +0530 Subject: [PATCH 03/15] Fix api and common policies with same name and version is attached to an api and is exported from that env but common policy is not in the env where the api is imported --- .../java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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 8daa4cbc7b28..d40845feda23 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 @@ -7203,12 +7203,16 @@ public String importOperationPolicyOfGivenType(OperationPolicyData importedPolic } } } else { + importedSpec.setName(importedSpec.getName() + "_imported"); + importedSpec.setDisplayName(importedSpec.getDisplayName() + " Imported"); + importedPolicyData.setSpecification(importedSpec); + importedPolicyData.setMd5Hash(APIUtil.getMd5OfOperationPolicy(importedPolicyData)); policyId = addAPISpecificOperationPolicy(importedPolicyData.getApiUUID(), importedPolicyData, organization); if (log.isDebugEnabled()) { log.debug( - "There aren't any existing policies for the imported policy. A new policy created with ID " - + policyId); + "There aren't any existing common policy for the imported policy. " + + "A new policy created with ID " + policyId); } } } else { //api level policy by default From f0096c67714c742575add843f3ce2c7689719c3a Mon Sep 17 00:00:00 2001 From: rusirijayodaillesinghe Date: Wed, 9 Oct 2024 11:16:56 +0530 Subject: [PATCH 04/15] Fix file name of common policies which are loaded from repository/resources --- .../main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java index e380448b59c2..6ed5f6e8068c 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java @@ -10212,8 +10212,9 @@ public static void loadCommonOperationPolicies(String organization) { OperationPolicyData policyData = new OperationPolicyData(); policyData.setSpecification(policySpec); policyData.setOrganization(organization); + //since the directory contains common policies only, files are not renamed with type String policyFileName = getOperationPolicyFileName(policySpec.getName(), - policySpec.getVersion(), ImportExportConstants.POLICY_TYPE_COMMON); + policySpec.getVersion(), null); OperationPolicyDefinition synapsePolicyDefinition = getOperationPolicyDefinitionFromFile(policyDefinitionLocation, policyFileName, APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION); From 07c33dc446ca3fa148745c562116a4b68b041a7e Mon Sep 17 00:00:00 2001 From: rusirijayodaillesinghe Date: Fri, 11 Oct 2024 14:01:26 +0530 Subject: [PATCH 05/15] Fix the issue of policies not being attached when importing APIs which are exported from previous U2 levels --- .../main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 d40845feda23..5918848512c9 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 @@ -7178,7 +7178,7 @@ public String importOperationPolicyOfGivenType(OperationPolicyData importedPolic /*To handle scenarios where api is exported from a previous U2 version. API and Common policies with same name and same version is not supported there */ - importOperationPolicy(importedPolicyData, organization); + policyId = importOperationPolicy(importedPolicyData, organization); } else if (policyType.equalsIgnoreCase(ImportExportConstants.POLICY_TYPE_COMMON)) { existingOperationPolicy = getCommonOperationPolicyByPolicyName(importedSpec.getName(), importedSpec.getVersion(),organization, false); From bd4245a65d88b409bf495027349c05e3e4dba406 Mon Sep 17 00:00:00 2001 From: rusirijayodaillesinghe Date: Mon, 14 Oct 2024 21:41:24 +0530 Subject: [PATCH 06/15] Remove api policy enabled check --- .../carbon/apimgt/impl/APIProviderImpl.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) 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 5918848512c9..afff0815acce 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 @@ -5334,17 +5334,16 @@ public API addPolicyTypeFieldToApi(API api, String tenantDomain) } api.setUriTemplates(uriTemplates); - if (isAPILevelPolicySupportEnabled) { - List apiPolicies = api.getApiPolicies(); - if (apiPolicies != null && !apiPolicies.isEmpty()) { - for (OperationPolicy policy : apiPolicies) { - String policyType = getPolicyType(policy, api.getUuid(), - tenantDomain); - policy.setPolicyType(policyType); - } + List apiPolicies = api.getApiPolicies(); + if (apiPolicies != null && !apiPolicies.isEmpty()) { + for (OperationPolicy policy : apiPolicies) { + String policyType = getPolicyType(policy, api.getUuid(), + tenantDomain); + policy.setPolicyType(policyType); } - api.setApiPolicies(apiPolicies); } + api.setApiPolicies(apiPolicies); + return api; } From 1dd747488bf89680d44c48a619e9911627a53070 Mon Sep 17 00:00:00 2001 From: rusirijayodaillesinghe Date: Mon, 16 Dec 2024 00:11:00 +0530 Subject: [PATCH 07/15] Add logic to search policies based on policy type --- .../v1/common/mappings/ImportUtils.java | 60 ++++++++++++------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java index 817fb5bc110c..ebe781485ef0 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java @@ -635,8 +635,9 @@ public static void validateAppliedPolicy(OperationPolicy appliedPolicy, String policyDirectory = extractedFolderPath + File.separator + ImportExportConstants.POLICIES_DIRECTORY; appliedPolicy.setPolicyId(null); + String policyType = appliedPolicy.getPolicyType(); String policyFileName = APIUtil.getOperationPolicyFileName(appliedPolicy.getPolicyName(), - appliedPolicy.getPolicyVersion(), appliedPolicy.getPolicyType()); + appliedPolicy.getPolicyVersion(), policyType); OperationPolicySpecification policySpec = null; if (visitedPoliciesMap.containsKey(policyFileName)) { @@ -650,22 +651,28 @@ public static void validateAppliedPolicy(OperationPolicy appliedPolicy, if (policySpec == null && apiUUID != null) { // if policy is not found in the project, policy can be referenced from an existing policy. - OperationPolicyData policyData = - provider.getAPISpecificOperationPolicyByPolicyName(appliedPolicy.getPolicyName(), - appliedPolicy.getPolicyVersion(), apiUUID, null, tenantDomain, false); - if (policyData != null) { - policySpec = policyData.getSpecification(); - appliedPolicy.setPolicyId(policyData.getPolicyId()); + if (policyType == null || ImportExportConstants.POLICY_TYPE_API.equalsIgnoreCase(policyType)) { + // if policy type is 'api' or not specified, then search API specific operation policies + OperationPolicyData policyData = + provider.getAPISpecificOperationPolicyByPolicyName(appliedPolicy.getPolicyName(), + appliedPolicy.getPolicyVersion(), apiUUID, null, tenantDomain, false); + if (policyData != null) { + policySpec = policyData.getSpecification(); + appliedPolicy.setPolicyId(policyData.getPolicyId()); + } } } if (policySpec == null) { - OperationPolicyData policyData = - provider.getCommonOperationPolicyByPolicyName(appliedPolicy.getPolicyName(), - appliedPolicy.getPolicyVersion(), tenantDomain, false); - if (policyData != null) { - policySpec = policyData.getSpecification(); - appliedPolicy.setPolicyId(policyData.getPolicyId()); + if (policyType == null || ImportExportConstants.POLICY_TYPE_COMMON.equalsIgnoreCase(policyType)) { + // if policy type is 'common' or not specified, then search common operation policies + OperationPolicyData policyData = + provider.getCommonOperationPolicyByPolicyName(appliedPolicy.getPolicyName(), + appliedPolicy.getPolicyVersion(), tenantDomain, false); + if (policyData != null) { + policySpec = policyData.getSpecification(); + appliedPolicy.setPolicyId(policyData.getPolicyId()); + } } } @@ -789,9 +796,10 @@ public static List findOrImportPolicy(List pol List validatedOperationPolicies = new ArrayList<>(); for (OperationPolicy policy : policiesList) { boolean policyImported = false; + String policyType = policy.getPolicyType(); try { String policyFileName = APIUtil.getOperationPolicyFileName(policy.getPolicyName(), - policy.getPolicyVersion(), policy.getPolicyType()); + policy.getPolicyVersion(), policyType); String policyID = null; if (!importedPolicies.containsKey(policyFileName)) { OperationPolicySpecification policySpec = @@ -825,15 +833,18 @@ public static List findOrImportPolicy(List pol operationPolicyData.setMd5Hash( APIUtil.getHashOfOperationPolicy(operationPolicyData)); policyID = provider.importOperationPolicyOfGivenType(operationPolicyData, - policy.getPolicyType(), tenantDomain); + policyType, tenantDomain); importedPolicies.put(policyFileName, policyID); policyImported = true; } else { // Check whether the policy has been referenced - OperationPolicyData policyData = - provider.getAPISpecificOperationPolicyByPolicyName(policy.getPolicyName(), - policy.getPolicyVersion(), api.getUuid(), null, - tenantDomain, false); + OperationPolicyData policyData = null; + if (policyType == null + || ImportExportConstants.POLICY_TYPE_API.equalsIgnoreCase(policyType)) { + policyData = provider.getAPISpecificOperationPolicyByPolicyName(policy.getPolicyName(), + policy.getPolicyVersion(), api.getUuid(), null, + tenantDomain, false); + } if (policyData != null) { OperationPolicySpecification policySpecification = policyData. getSpecification(); @@ -848,10 +859,13 @@ public static List findOrImportPolicy(List pol } } } else { - OperationPolicyData commonPolicyData = - provider.getCommonOperationPolicyByPolicyName(policy.getPolicyName(), - policy.getPolicyVersion(), tenantDomain, - false); + OperationPolicyData commonPolicyData = null; + if (policyType == null || + ImportExportConstants.POLICY_TYPE_COMMON.equalsIgnoreCase(policyType)) { + commonPolicyData = provider.getCommonOperationPolicyByPolicyName(policy.getPolicyName(), + policy.getPolicyVersion(), tenantDomain, + false); + } if (commonPolicyData != null) { log.info(commonPolicyData.getPolicyId()); // A common policy is found for specified policy. This will be validated From 0665ced17f8947b40b2901a76a03897776f85c5c Mon Sep 17 00:00:00 2001 From: rusirijayodaillesinghe Date: Mon, 16 Dec 2024 02:23:02 +0530 Subject: [PATCH 08/15] Fix build errors --- .../java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 afff0815acce..bacff517861b 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 @@ -7192,7 +7192,7 @@ public String importOperationPolicyOfGivenType(OperationPolicyData importedPolic importedSpec.setName(importedSpec.getName() + "_imported"); importedSpec.setDisplayName(importedSpec.getDisplayName() + " Imported"); importedPolicyData.setSpecification(importedSpec); - importedPolicyData.setMd5Hash(APIUtil.getMd5OfOperationPolicy(importedPolicyData)); + importedPolicyData.setMd5Hash(APIUtil.getHashOfOperationPolicy(importedPolicyData)); policyId = addAPISpecificOperationPolicy(importedPolicyData.getApiUUID(), importedPolicyData, organization); if (log.isDebugEnabled()) { @@ -7205,7 +7205,7 @@ public String importOperationPolicyOfGivenType(OperationPolicyData importedPolic importedSpec.setName(importedSpec.getName() + "_imported"); importedSpec.setDisplayName(importedSpec.getDisplayName() + " Imported"); importedPolicyData.setSpecification(importedSpec); - importedPolicyData.setMd5Hash(APIUtil.getMd5OfOperationPolicy(importedPolicyData)); + importedPolicyData.setMd5Hash(APIUtil.getHashOfOperationPolicy(importedPolicyData)); policyId = addAPISpecificOperationPolicy(importedPolicyData.getApiUUID(), importedPolicyData, organization); if (log.isDebugEnabled()) { From 1310d7508811dc60fb0a0456b91305ef35629e20 Mon Sep 17 00:00:00 2001 From: rusirijayodaillesinghe Date: Tue, 17 Dec 2024 18:19:29 +0530 Subject: [PATCH 09/15] refactor policy update function to reduce the amount of db calls --- .../carbon/apimgt/impl/APIProviderImpl.java | 43 ++++++++--------- .../carbon/apimgt/impl/dao/ApiMgtDAO.java | 46 +++++++++++++++++++ .../impl/dao/constants/SQLConstants.java | 16 +++++++ 3 files changed, 80 insertions(+), 25 deletions(-) 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 bacff517861b..eda8d1517fba 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 @@ -5300,34 +5300,18 @@ public String getSecuritySchemeOfAPI(String uuid, String organization) throws AP } } - public String getPolicyType(OperationPolicy policy, String apiUUId, String tenantDomain) - throws APIManagementException { - String policyType = null; - if (policy.getPolicyId() == null) { - policyType = ImportExportConstants.POLICY_TYPE_API; - } else { - OperationPolicyData basicPolicyData = - getAPISpecificOperationPolicyByPolicyId(policy.getPolicyId(), - apiUUId, tenantDomain, false); - if (basicPolicyData.getClonedCommonPolicyId() == null) { - policyType = ImportExportConstants.POLICY_TYPE_API; - } else { - policyType = ImportExportConstants.POLICY_TYPE_COMMON; - } - } - return policyType; - } - - public API addPolicyTypeFieldToApi(API api, String tenantDomain) - throws APIManagementException { + public API addPolicyTypeFieldToApi(API api) throws APIManagementException { + List apiOperationPolicyIds = getClonedAPISpecificOperationPolicyIdsList(api.getUuid()); Set uriTemplates = api.getUriTemplates(); for (URITemplate uriTemplate : uriTemplates) { List operationPolicies = uriTemplate.getOperationPolicies(); if (!operationPolicies.isEmpty()) { for (OperationPolicy operationPolicy : operationPolicies) { - String policyType = getPolicyType(operationPolicy, api.getUuid(), - tenantDomain); + String policyType = ImportExportConstants.POLICY_TYPE_API; + if (apiOperationPolicyIds.contains(operationPolicy.getPolicyId())) { + policyType = ImportExportConstants.POLICY_TYPE_COMMON; + } operationPolicy.setPolicyType(policyType); } } @@ -5337,8 +5321,10 @@ public API addPolicyTypeFieldToApi(API api, String tenantDomain) List apiPolicies = api.getApiPolicies(); if (apiPolicies != null && !apiPolicies.isEmpty()) { for (OperationPolicy policy : apiPolicies) { - String policyType = getPolicyType(policy, api.getUuid(), - tenantDomain); + String policyType = ImportExportConstants.POLICY_TYPE_API; + if (apiOperationPolicyIds.contains(policy.getPolicyId())) { + policyType = ImportExportConstants.POLICY_TYPE_COMMON; + } policy.setPolicyType(policyType); } } @@ -5389,7 +5375,7 @@ public API getAPIbyUUID(String uuid, String organization) throws APIManagementEx populateApiInfo(api); populateSubtypeConfiguration(api); populateDefaultVersion(api); - api = addPolicyTypeFieldToApi(api, organization); + api = addPolicyTypeFieldToApi(api); return api; } else { String msg = "Failed to get API. API artifact corresponding to artifactId " + uuid + " does not exist"; @@ -7300,6 +7286,13 @@ public OperationPolicyData getAPISpecificOperationPolicyByPolicyId(String policy .getAPISpecificOperationPolicyByPolicyID(policyId, apiUUID, organization, isWithPolicyDefinition); } + public List getClonedAPISpecificOperationPolicyIdsList(String apiUUID) + throws APIManagementException { + + return apiMgtDAO + .getClonedAPISpecificOperationPolicyIdsList(apiUUID); + } + @Override public OperationPolicyData getCommonOperationPolicyByPolicyId(String policyId, String organization, boolean isWithPolicyDefinition) diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java index 3600023c750d..067481799d69 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java @@ -20905,6 +20905,52 @@ private OperationPolicyData getAPISpecificOperationPolicyByPolicyID(Connection c return policyData; } + /** + * Get the list of API specific operation policy IDs from AM_API_OPERATION_POLICY table where cloned policy ID is + * non null. This method is intended to get the common operation policy IDs which have been attached to the + * given API. + * + * @param apiUUID UUID of the API + * @param organization Organization name + * @return operation policy + * @throws APIManagementException + */ + public List getClonedAPISpecificOperationPolicyIdsList(String apiUUID) + throws APIManagementException { + + try (Connection connection = APIMgtDBUtil.getConnection()) { + return getClonedAPISpecificOperationPolicyIdsList(connection, apiUUID); + } catch (SQLException e) { + handleException("Failed to get the API specific operation policy IDs from API " + + apiUUID, e); + } + return null; + } + + private List getClonedAPISpecificOperationPolicyIdsList(Connection connection, String apiUUID) + throws SQLException, APIManagementException { + + String dbQuery; + boolean isAPIRevision = checkAPIUUIDIsARevisionUUID(apiUUID) != null; + if (isAPIRevision) { + dbQuery = SQLConstants.OperationPolicyConstants. + GET_REVISION_SPECIFIC_OPERATION_POLICY_LIST_FROM_REVISION_UUID; + } else { + dbQuery = SQLConstants.OperationPolicyConstants.GET_API_SPECIFIC_OPERATION_POLICY_LIST_FROM_API_UUID; + } + List policyIdList = null; + try (PreparedStatement statement = connection.prepareStatement(dbQuery)) { + statement.setString(1, apiUUID); + try (ResultSet rs = statement.executeQuery()) { + if (rs.next()) { + policyIdList = new ArrayList<>(); + policyIdList.add(rs.getString("POLICY_UUID")); + } + } + } + return policyIdList; + } + private List getPolicyDefinitionForPolicyId(Connection connection, String policyId) throws SQLException { diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java index abf53c0ec5d8..703c3664f75d 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java @@ -4284,6 +4284,22 @@ public static class OperationPolicyConstants { " WHERE " + " OP.POLICY_UUID = ? AND OP.ORGANIZATION = ? AND AOP.REVISION_UUID = ?"; + public static final String GET_API_SPECIFIC_OPERATION_POLICY_LIST_FROM_API_UUID = + "SELECT " + + " POLICY_UUID " + + " FROM " + + " AM_API_OPERATION_POLICY " + + " WHERE " + + " CLONED_POLICY_UUID IS NOT NULL AND API_UUID = ?"; + + public static final String GET_REVISION_SPECIFIC_OPERATION_POLICY_LIST_FROM_REVISION_UUID = + "SELECT " + + " POLICY_UUID " + + " FROM " + + " AM_API_OPERATION_POLICY " + + " WHERE " + + " CLONED_POLICY_UUID IS NOT NULL AND REVISION_UUID = ?"; + public static final String GET_COMMON_OPERATION_POLICY_WITH_OUT_DEFINITION_FROM_POLICY_ID = "SELECT " + " OP.POLICY_UUID, OP.POLICY_NAME, OP.POLICY_VERSION, OP.DISPLAY_NAME, OP.POLICY_DESCRIPTION, OP.APPLICABLE_FLOWS, OP.GATEWAY_TYPES, OP.API_TYPES, " + From 18f5f954e62660abf5b8e7861e502a1dd039d157 Mon Sep 17 00:00:00 2001 From: rusirijayodaillesinghe Date: Thu, 17 Oct 2024 17:11:47 +0530 Subject: [PATCH 10/15] refactor policy update function to be compatible with API Products --- .../carbon/apimgt/impl/APIProviderImpl.java | 69 ++++++++++++++++--- .../carbon/apimgt/impl/dao/ApiMgtDAO.java | 13 ++-- .../impl/dao/constants/SQLConstants.java | 8 +-- 3 files changed, 69 insertions(+), 21 deletions(-) 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 eda8d1517fba..f6f1c4468dd8 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 @@ -5300,18 +5300,47 @@ public String getSecuritySchemeOfAPI(String uuid, String organization) throws AP } } + public String getPolicyType(OperationPolicy policy, Map apiOperationPolicyIdToClonedPolicyIdMap) + throws APIManagementException { + String policyType = null; + if (policy.getPolicyId() == null) { + policyType = ImportExportConstants.POLICY_TYPE_API; + } else { + // In an api product resource update scenario, when existing policy has been removed from an api, + // there's no entry attached to api policy id and apiId in AM_API_OPERATION_POLICY table + if (!apiOperationPolicyIdToClonedPolicyIdMap.containsKey(policy.getPolicyId())) { + return null; + } + // check if cloned policy id is null + if (apiOperationPolicyIdToClonedPolicyIdMap.get(policy.getPolicyId()) == null) { + policyType = ImportExportConstants.POLICY_TYPE_API; + } else { + policyType = ImportExportConstants.POLICY_TYPE_COMMON; + } + } + return policyType; + } + + public String getProductPolicyType(OperationPolicy policy, String apiUUID, + Map apiProductOperationPolicyIdToClonedPolicyIdMap) + throws APIManagementException { + + String originatedPolicyId = apiProductOperationPolicyIdToClonedPolicyIdMap.get(policy.getPolicyId()); + Map apiOperationPolicyIdToClonedPolicyIdMap = + getClonedAPISpecificOperationPolicyIdsList(apiUUID); + policy.setPolicyId(originatedPolicyId); + return getPolicyType(policy, apiOperationPolicyIdToClonedPolicyIdMap); + } + public API addPolicyTypeFieldToApi(API api) throws APIManagementException { - List apiOperationPolicyIds = getClonedAPISpecificOperationPolicyIdsList(api.getUuid()); + Map apiOperationPolicyIdToClonedPolicyIdMap = getClonedAPISpecificOperationPolicyIdsList(api.getUuid()); Set uriTemplates = api.getUriTemplates(); for (URITemplate uriTemplate : uriTemplates) { List operationPolicies = uriTemplate.getOperationPolicies(); if (!operationPolicies.isEmpty()) { for (OperationPolicy operationPolicy : operationPolicies) { - String policyType = ImportExportConstants.POLICY_TYPE_API; - if (apiOperationPolicyIds.contains(operationPolicy.getPolicyId())) { - policyType = ImportExportConstants.POLICY_TYPE_COMMON; - } + String policyType = getPolicyType(operationPolicy, apiOperationPolicyIdToClonedPolicyIdMap); operationPolicy.setPolicyType(policyType); } } @@ -5321,10 +5350,7 @@ public API addPolicyTypeFieldToApi(API api) throws APIManagementException { List apiPolicies = api.getApiPolicies(); if (apiPolicies != null && !apiPolicies.isEmpty()) { for (OperationPolicy policy : apiPolicies) { - String policyType = ImportExportConstants.POLICY_TYPE_API; - if (apiOperationPolicyIds.contains(policy.getPolicyId())) { - policyType = ImportExportConstants.POLICY_TYPE_COMMON; - } + String policyType = getPolicyType(policy, apiOperationPolicyIdToClonedPolicyIdMap); policy.setPolicyType(policyType); } } @@ -5333,6 +5359,28 @@ public API addPolicyTypeFieldToApi(API api) throws APIManagementException { return api; } + public APIProduct addPolicyTypeFieldToApiProduct(APIProduct product) throws APIManagementException { + + Map apiProductOperationPolicyIdToClonedPolicyIdMap = + getClonedAPISpecificOperationPolicyIdsList(product.getUuid()); + List productResources = product.getProductResources(); + for (APIProductResource resource : productResources) { + URITemplate uriTemplate = resource.getUriTemplate(); + List operationPolicies = uriTemplate.getOperationPolicies(); + if (!operationPolicies.isEmpty()) { + for (OperationPolicy operationPolicy : operationPolicies) { + String policyType = getProductPolicyType(operationPolicy, resource.getApiId(), + apiProductOperationPolicyIdToClonedPolicyIdMap); + operationPolicy.setPolicyType(policyType); + } + } + uriTemplate.setOperationPolicies(operationPolicies); + resource.setUriTemplate(uriTemplate); + } + product.setProductResources(productResources); + return product; + } + @Override public boolean isSubscriptionValidationDisabled(String uuid) throws APIManagementException { String status = apiMgtDAO.getSubscriptionValidationStatus(uuid); @@ -5555,6 +5603,7 @@ public APIProduct getAPIProductbyUUID(String uuid, String organization) throws A if (migrationEnabled == null) { populateDefaultVersion(product); } + product = addPolicyTypeFieldToApiProduct(product); return product; } else { String msg = "Failed to get API Product. API Product artifact corresponding to artifactId " + uuid @@ -7286,7 +7335,7 @@ public OperationPolicyData getAPISpecificOperationPolicyByPolicyId(String policy .getAPISpecificOperationPolicyByPolicyID(policyId, apiUUID, organization, isWithPolicyDefinition); } - public List getClonedAPISpecificOperationPolicyIdsList(String apiUUID) + public Map getClonedAPISpecificOperationPolicyIdsList(String apiUUID) throws APIManagementException { return apiMgtDAO diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java index 067481799d69..6479473a69e2 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java @@ -20911,11 +20911,10 @@ private OperationPolicyData getAPISpecificOperationPolicyByPolicyID(Connection c * given API. * * @param apiUUID UUID of the API - * @param organization Organization name * @return operation policy * @throws APIManagementException */ - public List getClonedAPISpecificOperationPolicyIdsList(String apiUUID) + public Map getClonedAPISpecificOperationPolicyIdsList(String apiUUID) throws APIManagementException { try (Connection connection = APIMgtDBUtil.getConnection()) { @@ -20927,7 +20926,7 @@ public List getClonedAPISpecificOperationPolicyIdsList(String apiUUID) return null; } - private List getClonedAPISpecificOperationPolicyIdsList(Connection connection, String apiUUID) + private Map getClonedAPISpecificOperationPolicyIdsList(Connection connection, String apiUUID) throws SQLException, APIManagementException { String dbQuery; @@ -20938,17 +20937,17 @@ private List getClonedAPISpecificOperationPolicyIdsList(Connection conne } else { dbQuery = SQLConstants.OperationPolicyConstants.GET_API_SPECIFIC_OPERATION_POLICY_LIST_FROM_API_UUID; } - List policyIdList = null; + Map policyMap = null; try (PreparedStatement statement = connection.prepareStatement(dbQuery)) { statement.setString(1, apiUUID); try (ResultSet rs = statement.executeQuery()) { if (rs.next()) { - policyIdList = new ArrayList<>(); - policyIdList.add(rs.getString("POLICY_UUID")); + policyMap = new HashMap<>(); + policyMap.put(rs.getString("POLICY_UUID"), rs.getString("CLONED_POLICY_UUID")); } } } - return policyIdList; + return policyMap; } private List getPolicyDefinitionForPolicyId(Connection connection, String policyId) diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java index 703c3664f75d..8da4a19c7687 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java @@ -4286,19 +4286,19 @@ public static class OperationPolicyConstants { public static final String GET_API_SPECIFIC_OPERATION_POLICY_LIST_FROM_API_UUID = "SELECT " + - " POLICY_UUID " + + " POLICY_UUID, CLONED_POLICY_UUID " + " FROM " + " AM_API_OPERATION_POLICY " + " WHERE " + - " CLONED_POLICY_UUID IS NOT NULL AND API_UUID = ?"; + " API_UUID = ?"; public static final String GET_REVISION_SPECIFIC_OPERATION_POLICY_LIST_FROM_REVISION_UUID = "SELECT " + - " POLICY_UUID " + + " POLICY_UUID, CLONED_POLICY_UUID " + " FROM " + " AM_API_OPERATION_POLICY " + " WHERE " + - " CLONED_POLICY_UUID IS NOT NULL AND REVISION_UUID = ?"; + " REVISION_UUID = ?"; public static final String GET_COMMON_OPERATION_POLICY_WITH_OUT_DEFINITION_FROM_POLICY_ID = "SELECT " + From 3e8deab1070c3122f44f9e0524d5e4372fb3101e Mon Sep 17 00:00:00 2001 From: rusirijayodaillesinghe Date: Fri, 20 Dec 2024 10:25:30 +0530 Subject: [PATCH 11/15] Refactor method names and bug fixes --- .../wso2/carbon/apimgt/impl/APIProviderImpl.java | 2 +- .../wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java | 16 +++++++--------- .../apimgt/impl/dao/constants/SQLConstants.java | 4 ++-- 3 files changed, 10 insertions(+), 12 deletions(-) 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 f6f1c4468dd8..c471a02b8f3d 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 @@ -7339,7 +7339,7 @@ public Map getClonedAPISpecificOperationPolicyIdsList(String api throws APIManagementException { return apiMgtDAO - .getClonedAPISpecificOperationPolicyIdsList(apiUUID); + .getClonedIdsMappedApiSpecificOperationPolicies(apiUUID); } @Override diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java index 6479473a69e2..b96fcb58d2eb 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java @@ -125,7 +125,6 @@ import org.wso2.carbon.apimgt.impl.dto.WorkflowDTO; import org.wso2.carbon.apimgt.impl.factory.KeyManagerHolder; import org.wso2.carbon.apimgt.impl.factory.SQLConstantManagerFactory; -import org.wso2.carbon.apimgt.impl.importexport.ImportExportConstants; import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder; import org.wso2.carbon.apimgt.impl.utils.APIMgtDBUtil; import org.wso2.carbon.apimgt.impl.utils.APIUtil; @@ -20914,11 +20913,11 @@ private OperationPolicyData getAPISpecificOperationPolicyByPolicyID(Connection c * @return operation policy * @throws APIManagementException */ - public Map getClonedAPISpecificOperationPolicyIdsList(String apiUUID) + public Map getClonedIdsMappedApiSpecificOperationPolicies(String apiUUID) throws APIManagementException { try (Connection connection = APIMgtDBUtil.getConnection()) { - return getClonedAPISpecificOperationPolicyIdsList(connection, apiUUID); + return getClonedIdsMappedApiSpecificOperationPolicies(connection, apiUUID); } catch (SQLException e) { handleException("Failed to get the API specific operation policy IDs from API " + apiUUID, e); @@ -20926,23 +20925,22 @@ public Map getClonedAPISpecificOperationPolicyIdsList(String api return null; } - private Map getClonedAPISpecificOperationPolicyIdsList(Connection connection, String apiUUID) + private Map getClonedIdsMappedApiSpecificOperationPolicies(Connection connection, String apiUUID) throws SQLException, APIManagementException { String dbQuery; boolean isAPIRevision = checkAPIUUIDIsARevisionUUID(apiUUID) != null; if (isAPIRevision) { dbQuery = SQLConstants.OperationPolicyConstants. - GET_REVISION_SPECIFIC_OPERATION_POLICY_LIST_FROM_REVISION_UUID; + GET_REVISION_SPECIFIC_OPERATION_POLICY_IDS_FROM_REVISION_UUID; } else { - dbQuery = SQLConstants.OperationPolicyConstants.GET_API_SPECIFIC_OPERATION_POLICY_LIST_FROM_API_UUID; + dbQuery = SQLConstants.OperationPolicyConstants.GET_API_SPECIFIC_OPERATION_POLICY_IDS_FROM_API_UUID; } - Map policyMap = null; + Map policyMap = new HashMap<>(); try (PreparedStatement statement = connection.prepareStatement(dbQuery)) { statement.setString(1, apiUUID); try (ResultSet rs = statement.executeQuery()) { - if (rs.next()) { - policyMap = new HashMap<>(); + while (rs.next()) { policyMap.put(rs.getString("POLICY_UUID"), rs.getString("CLONED_POLICY_UUID")); } } diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java index 8da4a19c7687..9d9dd8ecaff1 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java @@ -4284,7 +4284,7 @@ public static class OperationPolicyConstants { " WHERE " + " OP.POLICY_UUID = ? AND OP.ORGANIZATION = ? AND AOP.REVISION_UUID = ?"; - public static final String GET_API_SPECIFIC_OPERATION_POLICY_LIST_FROM_API_UUID = + public static final String GET_API_SPECIFIC_OPERATION_POLICY_IDS_FROM_API_UUID = "SELECT " + " POLICY_UUID, CLONED_POLICY_UUID " + " FROM " + @@ -4292,7 +4292,7 @@ public static class OperationPolicyConstants { " WHERE " + " API_UUID = ?"; - public static final String GET_REVISION_SPECIFIC_OPERATION_POLICY_LIST_FROM_REVISION_UUID = + public static final String GET_REVISION_SPECIFIC_OPERATION_POLICY_IDS_FROM_REVISION_UUID = "SELECT " + " POLICY_UUID, CLONED_POLICY_UUID " + " FROM " + From a3e381d34388fa01ca4bed893ea9d1e2d4c953fc Mon Sep 17 00:00:00 2001 From: rusirijayodaillesinghe Date: Fri, 20 Dec 2024 16:23:55 +0530 Subject: [PATCH 12/15] Allow creating an API level policy while having a common policy with same name and version and is attached to the API already --- .../wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java index 9d9dd8ecaff1..38dcdc1b7d19 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java @@ -4310,6 +4310,8 @@ public static class OperationPolicyConstants { " OP.POLICY_UUID = ? AND OP.ORGANIZATION = ?"; + // CLONED_POLICY_UUID IS NULL was added to the query to allow creating an API level policy while having a common policy with same name and version + // and is attached to the API already public static final String GET_API_SPECIFIC_OPERATION_POLICY_FROM_POLICY_NAME = "SELECT " + " OP.POLICY_UUID, OP.POLICY_NAME, OP.POLICY_VERSION, OP.DISPLAY_NAME, OP.POLICY_DESCRIPTION, OP.APPLICABLE_FLOWS, OP.GATEWAY_TYPES, OP.API_TYPES, " + @@ -4318,7 +4320,7 @@ public static class OperationPolicyConstants { " FROM " + " AM_OPERATION_POLICY OP INNER JOIN AM_API_OPERATION_POLICY AOP ON OP.POLICY_UUID = AOP.POLICY_UUID " + " WHERE " + - " OP.POLICY_NAME = ? AND OP.POLICY_VERSION = ? AND OP.ORGANIZATION = ? AND AOP.API_UUID = ? "; + " OP.POLICY_NAME = ? AND OP.POLICY_VERSION = ? AND OP.ORGANIZATION = ? AND AOP.API_UUID = ? AND AOP.CLONED_POLICY_UUID IS NULL "; public static final String GET_COMMON_OPERATION_POLICY_FROM_POLICY_NAME = "SELECT " + From ba84e7e7e1742278bec096ce70f01d9b3e0d0121 Mon Sep 17 00:00:00 2001 From: rusirijayodaillesinghe Date: Fri, 3 Jan 2025 15:43:28 +0530 Subject: [PATCH 13/15] Address review comments --- .../wso2/carbon/apimgt/api/APIProvider.java | 18 ++++++++-- .../apimgt/api/model/OperationPolicy.java | 3 +- .../carbon/apimgt/impl/APIProviderImpl.java | 33 ++++++++++--------- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java index b797aed8c8ab..69157976272c 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java @@ -1543,8 +1543,10 @@ EnvironmentPropertiesDTO getEnvironmentSpecificAPIProperties(String apiUuid, Str void setOperationPoliciesToURITemplates(String apiId, Set uriTemplates) throws APIManagementException; /** - * Import an operation policy from the API CTL project. This will either create a new API specific policy, - * update existing API specific policy or return the policyID of existing policy if policy content is not changed. + * Import an operation policy from the API CTL project which is exported from a product version prior to the update + * level which introduced to have an API and a Common policy with identical names and versions. + * This will either create a new API specific policy, update existing API specific policy or return the + * policyID of existing policy if policy content is not changed. * * @param operationPolicyData Operation Policy Data * @param organization Organization name @@ -1553,6 +1555,18 @@ EnvironmentPropertiesDTO getEnvironmentSpecificAPIProperties(String apiUuid, Str */ String importOperationPolicy(OperationPolicyData operationPolicyData, String organization) throws APIManagementException; + + /** + * Import an operation policy of a given policy type, from the API CTL project. + * This will either create a new API specific policy, update existing API specific policy or return the + * policyID of existing policy if policy content is not changed. + * + * @param operationPolicyData Operation Policy Data + * @param organization Organization name + * @param policyType Policy Type + * @return UUID of the imported operation policy + * @throws APIManagementException + */ String importOperationPolicyOfGivenType(OperationPolicyData operationPolicyData, String policyType, String organization) throws APIManagementException; diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/OperationPolicy.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/OperationPolicy.java index 7fc417294384..56c49000dd28 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/OperationPolicy.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/OperationPolicy.java @@ -109,7 +109,8 @@ public boolean equals(Object o) { if (o instanceof OperationPolicy) { OperationPolicy policyObj = (OperationPolicy) o; return Objects.equals(policyName, policyObj.policyName) && Objects.equals(policyVersion, - policyObj.policyVersion) && Objects.equals(direction, policyObj.direction) && policyType.equals(policyObj.policyType) && Objects.equals( + policyObj.policyVersion) && Objects.equals(direction, policyObj.direction) + && policyType.equals(policyObj.policyType) && Objects.equals( parameters, policyObj.parameters) && Objects.equals(policyId, policyObj.policyId); } return false; 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 c471a02b8f3d..4fdac0c5947c 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 @@ -5300,6 +5300,13 @@ public String getSecuritySchemeOfAPI(String uuid, String organization) throws AP } } + /** + * + * @param policy + * @param apiOperationPolicyIdToClonedPolicyIdMap + * @return + * @throws APIManagementException + */ public String getPolicyType(OperationPolicy policy, Map apiOperationPolicyIdToClonedPolicyIdMap) throws APIManagementException { String policyType = null; @@ -5332,7 +5339,7 @@ public String getProductPolicyType(OperationPolicy policy, String apiUUID, return getPolicyType(policy, apiOperationPolicyIdToClonedPolicyIdMap); } - public API addPolicyTypeFieldToApi(API api) throws APIManagementException { + public void populatePolicyTypeInAPI(API api) throws APIManagementException { Map apiOperationPolicyIdToClonedPolicyIdMap = getClonedAPISpecificOperationPolicyIdsList(api.getUuid()); Set uriTemplates = api.getUriTemplates(); @@ -5355,11 +5362,9 @@ public API addPolicyTypeFieldToApi(API api) throws APIManagementException { } } api.setApiPolicies(apiPolicies); - - return api; } - public APIProduct addPolicyTypeFieldToApiProduct(APIProduct product) throws APIManagementException { + public void populatePolicyTypeInApiProduct(APIProduct product) throws APIManagementException { Map apiProductOperationPolicyIdToClonedPolicyIdMap = getClonedAPISpecificOperationPolicyIdsList(product.getUuid()); @@ -5378,7 +5383,6 @@ public APIProduct addPolicyTypeFieldToApiProduct(APIProduct product) throws APIM resource.setUriTemplate(uriTemplate); } product.setProductResources(productResources); - return product; } @Override @@ -5423,7 +5427,7 @@ public API getAPIbyUUID(String uuid, String organization) throws APIManagementEx populateApiInfo(api); populateSubtypeConfiguration(api); populateDefaultVersion(api); - api = addPolicyTypeFieldToApi(api); + populatePolicyTypeInAPI(api); return api; } else { String msg = "Failed to get API. API artifact corresponding to artifactId " + uuid + " does not exist"; @@ -5603,7 +5607,7 @@ public APIProduct getAPIProductbyUUID(String uuid, String organization) throws A if (migrationEnabled == null) { populateDefaultVersion(product); } - product = addPolicyTypeFieldToApiProduct(product); + populatePolicyTypeInApiProduct(product); return product; } else { String msg = "Failed to get API Product. API Product artifact corresponding to artifactId " + uuid @@ -7137,7 +7141,7 @@ public void setOperationPoliciesToURITemplates(String apiId, Set ur * If there aren't any existing policies, a new API specific policy will be created. * * @param importedPolicyData Imported policy - * @param organization Organization name + * @param organization Organization name * @return corrosponding policy ID for imported data * @throws APIManagementException if failed to delete APIRevision */ @@ -7199,23 +7203,23 @@ public String importOperationPolicy(OperationPolicyData importedPolicyData, Stri return policyId; } + @Override public String importOperationPolicyOfGivenType(OperationPolicyData importedPolicyData, String policyType, - String organization) - throws APIManagementException { + String organization) throws APIManagementException { OperationPolicySpecification importedSpec = importedPolicyData.getSpecification(); OperationPolicyData existingOperationPolicy; String policyId = null; if (policyType == null) { - /*To handle scenarios where api is exported from a previous U2 version. API and Common policies with same name - and same version is not supported there + /*To handle scenarios where api is exported from a previous U2 version. API and Common policies with + same name and same version is not supported there */ policyId = importOperationPolicy(importedPolicyData, organization); } else if (policyType.equalsIgnoreCase(ImportExportConstants.POLICY_TYPE_COMMON)) { existingOperationPolicy = getCommonOperationPolicyByPolicyName(importedSpec.getName(), - importedSpec.getVersion(),organization, false); + importedSpec.getVersion(), organization, false); if (existingOperationPolicy != null) { if (existingOperationPolicy.getMd5Hash().equals(importedPolicyData.getMd5Hash())) { @@ -7244,8 +7248,7 @@ public String importOperationPolicyOfGivenType(OperationPolicyData importedPolic policyId = addAPISpecificOperationPolicy(importedPolicyData.getApiUUID(), importedPolicyData, organization); if (log.isDebugEnabled()) { - log.debug( - "There aren't any existing common policy for the imported policy. " + + log.debug("There is no common policy currently available for the imported policy. " + "A new policy created with ID " + policyId); } } From 237f3d867be332714375a21ed85f03a1ced441ae Mon Sep 17 00:00:00 2001 From: rusirijayodaillesinghe Date: Thu, 23 Jan 2025 13:45:01 +0530 Subject: [PATCH 14/15] Remove previously added logic to handle api product update flow --- .../java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java | 5 ----- 1 file changed, 5 deletions(-) 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 4fdac0c5947c..c5ee0a4c5609 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 @@ -5313,11 +5313,6 @@ public String getPolicyType(OperationPolicy policy, Map apiOpera if (policy.getPolicyId() == null) { policyType = ImportExportConstants.POLICY_TYPE_API; } else { - // In an api product resource update scenario, when existing policy has been removed from an api, - // there's no entry attached to api policy id and apiId in AM_API_OPERATION_POLICY table - if (!apiOperationPolicyIdToClonedPolicyIdMap.containsKey(policy.getPolicyId())) { - return null; - } // check if cloned policy id is null if (apiOperationPolicyIdToClonedPolicyIdMap.get(policy.getPolicyId()) == null) { policyType = ImportExportConstants.POLICY_TYPE_API; From a5c721ba4378bc862873d0a88cbe39305ef404ff Mon Sep 17 00:00:00 2001 From: rusirijayodaillesinghe Date: Mon, 27 Jan 2025 11:41:26 +0530 Subject: [PATCH 15/15] Address review comments --- .../java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) 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 c5ee0a4c5609..23f810d0faac 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 @@ -5309,18 +5309,16 @@ public String getSecuritySchemeOfAPI(String uuid, String organization) throws AP */ public String getPolicyType(OperationPolicy policy, Map apiOperationPolicyIdToClonedPolicyIdMap) throws APIManagementException { - String policyType = null; if (policy.getPolicyId() == null) { - policyType = ImportExportConstants.POLICY_TYPE_API; + return ImportExportConstants.POLICY_TYPE_API; } else { // check if cloned policy id is null if (apiOperationPolicyIdToClonedPolicyIdMap.get(policy.getPolicyId()) == null) { - policyType = ImportExportConstants.POLICY_TYPE_API; + return ImportExportConstants.POLICY_TYPE_API; } else { - policyType = ImportExportConstants.POLICY_TYPE_COMMON; + return ImportExportConstants.POLICY_TYPE_COMMON; } } - return policyType; } public String getProductPolicyType(OperationPolicy policy, String apiUUID,