From e398fb04d06f44554ce203c7503a22f18f769b63 Mon Sep 17 00:00:00 2001 From: rusirijayodaillesinghe Date: Mon, 16 Dec 2024 00:11:00 +0530 Subject: [PATCH] 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