Skip to content

Commit

Permalink
refactor policy update function to reduce the amount of db calls
Browse files Browse the repository at this point in the history
  • Loading branch information
RusJaI committed Dec 17, 2024
1 parent a24e576 commit 4d0dae3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5281,34 +5281,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<String> apiOperationPolicyIds = 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 = getPolicyType(operationPolicy, api.getUuid(),
tenantDomain);
String policyType = ImportExportConstants.POLICY_TYPE_API;
if (apiOperationPolicyIds.contains(operationPolicy.getPolicyId())) {
policyType = ImportExportConstants.POLICY_TYPE_COMMON;
}
operationPolicy.setPolicyType(policyType);
}
}
Expand All @@ -5318,8 +5302,10 @@ public API addPolicyTypeFieldToApi(API api, String tenantDomain)
List<OperationPolicy> 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);
}
}
Expand Down Expand Up @@ -5370,7 +5356,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";
Expand Down Expand Up @@ -7277,6 +7263,13 @@ public OperationPolicyData getAPISpecificOperationPolicyByPolicyId(String policy
.getAPISpecificOperationPolicyByPolicyID(policyId, apiUUID, organization, isWithPolicyDefinition);
}

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

return apiMgtDAO
.getClonedAPISpecificOperationPolicyIdsList(apiUUID);
}

@Override
public OperationPolicyData getCommonOperationPolicyByPolicyId(String policyId, String organization,
boolean isWithPolicyDefinition)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20829,6 +20829,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<String> 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<String> 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<String> 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<OperationPolicyDefinition> getPolicyDefinitionForPolicyId(Connection connection, String policyId)
throws SQLException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4274,6 +4274,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, " +
Expand Down

0 comments on commit 4d0dae3

Please sign in to comment.