Skip to content

Commit

Permalink
refactor policy update function to be compatible with API Products
Browse files Browse the repository at this point in the history
  • Loading branch information
RusJaI committed Dec 19, 2024
1 parent 4b316c3 commit 61f95e2
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5281,18 +5281,47 @@ public String getSecuritySchemeOfAPI(String uuid, String organization) throws AP
}
}

public String getPolicyType(OperationPolicy policy, Map<String, String> 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<String, String> apiProductOperationPolicyIdToClonedPolicyIdMap)
throws APIManagementException {

String originatedPolicyId = apiProductOperationPolicyIdToClonedPolicyIdMap.get(policy.getPolicyId());
Map<String, String> apiOperationPolicyIdToClonedPolicyIdMap =
getClonedAPISpecificOperationPolicyIdsList(apiUUID);
policy.setPolicyId(originatedPolicyId);
return getPolicyType(policy, apiOperationPolicyIdToClonedPolicyIdMap);
}

public API addPolicyTypeFieldToApi(API api) throws APIManagementException {

List<String> apiOperationPolicyIds = getClonedAPISpecificOperationPolicyIdsList(api.getUuid());
Map<String, String> apiOperationPolicyIdToClonedPolicyIdMap = getClonedAPISpecificOperationPolicyIdsList(api.getUuid());
Set<URITemplate> uriTemplates = api.getUriTemplates();
for (URITemplate uriTemplate : uriTemplates) {
List<OperationPolicy> 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);
}
}
Expand All @@ -5302,10 +5331,7 @@ public API addPolicyTypeFieldToApi(API api) throws APIManagementException {
List<OperationPolicy> 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);
}
}
Expand All @@ -5314,6 +5340,28 @@ public API addPolicyTypeFieldToApi(API api) throws APIManagementException {
return api;
}

public APIProduct addPolicyTypeFieldToApiProduct(APIProduct product) throws APIManagementException {

Map<String, String> apiProductOperationPolicyIdToClonedPolicyIdMap =
getClonedAPISpecificOperationPolicyIdsList(product.getUuid());
List<APIProductResource> productResources = product.getProductResources();
for (APIProductResource resource : productResources) {
URITemplate uriTemplate = resource.getUriTemplate();
List<OperationPolicy> 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);
Expand Down Expand Up @@ -5536,6 +5584,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
Expand Down Expand Up @@ -7263,7 +7312,7 @@ public OperationPolicyData getAPISpecificOperationPolicyByPolicyId(String policy
.getAPISpecificOperationPolicyByPolicyID(policyId, apiUUID, organization, isWithPolicyDefinition);
}

public List<String> getClonedAPISpecificOperationPolicyIdsList(String apiUUID)
public Map<String, String> getClonedAPISpecificOperationPolicyIdsList(String apiUUID)
throws APIManagementException {

return apiMgtDAO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20835,11 +20835,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<String> getClonedAPISpecificOperationPolicyIdsList(String apiUUID)
public Map<String, String> getClonedAPISpecificOperationPolicyIdsList(String apiUUID)
throws APIManagementException {

try (Connection connection = APIMgtDBUtil.getConnection()) {
Expand All @@ -20851,7 +20850,7 @@ public List<String> getClonedAPISpecificOperationPolicyIdsList(String apiUUID)
return null;
}

private List<String> getClonedAPISpecificOperationPolicyIdsList(Connection connection, String apiUUID)
private Map<String, String> getClonedAPISpecificOperationPolicyIdsList(Connection connection, String apiUUID)
throws SQLException, APIManagementException {

String dbQuery;
Expand All @@ -20862,17 +20861,17 @@ private List<String> getClonedAPISpecificOperationPolicyIdsList(Connection conne
} else {
dbQuery = SQLConstants.OperationPolicyConstants.GET_API_SPECIFIC_OPERATION_POLICY_LIST_FROM_API_UUID;
}
List<String> policyIdList = null;
Map<String, String> 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<OperationPolicyDefinition> getPolicyDefinitionForPolicyId(Connection connection, String policyId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4276,19 +4276,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 " +
Expand Down

0 comments on commit 61f95e2

Please sign in to comment.