Skip to content

Commit

Permalink
Add logic to search policies based on policy type
Browse files Browse the repository at this point in the history
  • Loading branch information
RusJaI committed Dec 15, 2024
1 parent bff9dc9 commit e398fb0
Showing 1 changed file with 37 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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());
}
}
}

Expand Down Expand Up @@ -789,9 +796,10 @@ public static List<OperationPolicy> findOrImportPolicy(List<OperationPolicy> pol
List<OperationPolicy> 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 =
Expand Down Expand Up @@ -825,15 +833,18 @@ public static List<OperationPolicy> findOrImportPolicy(List<OperationPolicy> 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();
Expand All @@ -848,10 +859,13 @@ public static List<OperationPolicy> findOrImportPolicy(List<OperationPolicy> 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
Expand Down

0 comments on commit e398fb0

Please sign in to comment.