Skip to content

Commit

Permalink
add: API Policies to the API object in gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
BLasan committed Oct 27, 2023
1 parent 34f5366 commit 587c32b
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@

package org.wso2.carbon.apimgt.api.model.subscription;

import org.wso2.carbon.apimgt.api.model.OperationPolicy;
import org.wso2.carbon.apimgt.api.model.policy.APIPolicy;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* Entity for keeping API related information.
Expand All @@ -39,6 +44,7 @@ public class API implements CacheableEntity<String> {
private String status;
private String revision;
private String organization;
private Set<OperationPolicy> apiPolicies = new HashSet<>();

public String getRevision() {

Expand Down Expand Up @@ -199,4 +205,12 @@ public String getOrganization() {
public void setOrganization(String organization) {
this.organization = organization;
}

public void setApiPolicy(OperationPolicy apiPolicy) {
this.apiPolicies.add(apiPolicy);
}

public Set<OperationPolicy> getApiPolicies() {
return apiPolicies;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class API {
private String apiCreator;
private String apiCreatorTenantDomain;
private List<URITemplate> uriTemplates = new ArrayList<>();
private List<OperationPolicy> apiPolicies = new ArrayList<>();

public String getApiId() {
return apiId;
Expand Down Expand Up @@ -87,4 +88,12 @@ public List<URITemplate> getUriTemplates() {
public void setUriTemplates(List<URITemplate> uriTemplates) {
this.uriTemplates = uriTemplates;
}

public void setApiPolicies(List<OperationPolicy> apiPolicies) {
this.apiPolicies = apiPolicies;
}

public List<OperationPolicy> getApiPolicies() {
return apiPolicies;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,18 @@ public API getApi() throws DataNotFoundException {
uriTemplates.add(uriTemplateObj);
}
}
List<org.wso2.carbon.apimgt.common.analytics.publishers.dto.OperationPolicy> apiPolicyList = new ArrayList<>();
for(OperationPolicy apiPolicy: apiObj.getApiPolicies()){
org.wso2.carbon.apimgt.common.analytics.publishers.dto.OperationPolicy operationPolicyObj = new org.wso2.carbon.apimgt.common.analytics.publishers.dto.OperationPolicy();
operationPolicyObj.setPolicyVersion(apiPolicy.getPolicyVersion());
operationPolicyObj.setPolicyName(apiPolicy.getPolicyName());
operationPolicyObj.setPolicyId(apiPolicy.getPolicyId());
operationPolicyObj.setDirection(apiPolicy.getDirection());
operationPolicyObj.setOrder(apiPolicy.getOrder());
apiPolicyList.add(operationPolicyObj);
}
api.setUriTemplates(uriTemplates);
api.setApiPolicies(apiPolicyList);
}
return api;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1226,34 +1226,49 @@ private void attachURLMappingDetails(Connection connection, String revisionId, A
}
}

// Attach the relevant operation policies to the resources.
attachOperationPolicies(connection, revisionId, api);
// Attach the relevant operation policies and api to the resources and to the API.
attachPolicies(connection, revisionId, api);

}

private void attachOperationPolicies(Connection connection, String revisionId, API api) throws SQLException {
private void attachPolicies(Connection connection, String revisionId, API api) throws SQLException {
try(PreparedStatement preparedStatement = connection.prepareStatement(SubscriptionValidationSQLConstants.GET_OPERATION_POLICIES_PER_URI_BY_API_SQL)){
preparedStatement.setInt(1, api.getApiId());
preparedStatement.setString(1, api.getApiUUID());
preparedStatement.setString(2, revisionId);
try(ResultSet resultSet = preparedStatement.executeQuery()){
while (resultSet.next()){
String httpMethod = resultSet.getString("HTTP_METHOD");
String urlPattern = resultSet.getString("URL_PATTERN");
String policyName = resultSet.getString("POLICY_NAME");
String policyVersion = resultSet.getString("POLICY_VERSION");
String direction = resultSet.getString("DIRECTION");
String policyID = resultSet.getString("POLICY_UUID");
URLMapping urlMapping = api.getResource(urlPattern, httpMethod);
String operationPolicyDirection = resultSet.getString("OPERATION_POLICY_DIRECTION");
String apiPolicyDirection = resultSet.getString("API_POLICY_DIRECTION");
String operationPolicyID = resultSet.getString("OPERATION_POLICY_UUID");
String apiPolicyUUID = resultSet.getString("API_POLICY_UUID");
URLMapping urlMapping = null;
if(StringUtils.isNotEmpty(httpMethod) && StringUtils.isNotEmpty(urlPattern)){
urlMapping = api.getResource(urlPattern, httpMethod);
}
if(urlMapping!=null){
if (StringUtils.isNotEmpty(policyID) && StringUtils.isNotEmpty(policyName)
&& StringUtils.isNotEmpty(policyVersion) && StringUtils.isNotEmpty(direction)) {
if (StringUtils.isNotEmpty(operationPolicyID) && StringUtils.isNotEmpty(policyName)
&& StringUtils.isNotEmpty(policyVersion) && StringUtils.isNotEmpty(operationPolicyDirection)) {
OperationPolicy operationPolicy = new OperationPolicy();
operationPolicy.setPolicyId(policyID);
operationPolicy.setPolicyId(operationPolicyID);
operationPolicy.setPolicyName(policyName);
operationPolicy.setPolicyVersion(policyVersion);
operationPolicy.setDirection(direction);
operationPolicy.setDirection(operationPolicyDirection);
urlMapping.setOperationPolicies(operationPolicy);
api.addResource(urlMapping);
}
api.addResource(urlMapping);
}
if(StringUtils.isNotEmpty(apiPolicyUUID) && StringUtils.isNotEmpty(policyName)
&& StringUtils.isNotEmpty(policyVersion) && StringUtils.isNotEmpty(apiPolicyDirection)){
OperationPolicy apiPolicy = new OperationPolicy();
apiPolicy.setPolicyId(apiPolicyUUID);
apiPolicy.setPolicyName(policyName);
apiPolicy.setPolicyVersion(policyVersion);
apiPolicy.setDirection(apiPolicyDirection);
api.setApiPolicy(apiPolicy);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,13 +570,26 @@ public class SubscriptionValidationSQLConstants {
"AM_API_RESOURCE_SCOPE_MAPPING.SCOPE_NAME FROM AM_API_URL_MAPPING LEFT JOIN AM_API_RESOURCE_SCOPE_MAPPING" +
" ON AM_API_URL_MAPPING.URL_MAPPING_ID=AM_API_RESOURCE_SCOPE_MAPPING.URL_MAPPING_ID WHERE " +
"AM_API_URL_MAPPING.API_ID = ? AND AM_API_URL_MAPPING.REVISION_UUID = ?";
public static final String GET_OPERATION_POLICIES_PER_URI_BY_API_SQL = "SELECT AUM.HTTP_METHOD, AUM.URL_PATTERN, "
+ "OP.POLICY_NAME, OP.POLICY_VERSION, OPM.DIRECTION, OPM.POLICY_UUID "
+ "FROM AM_API_URL_MAPPING AS AUM INNER JOIN AM_API_OPERATION_POLICY_MAPPING OPM ON AUM.URL_MAPPING_ID = OPM.URL_MAPPING_ID "
+ "INNER JOIN AM_OPERATION_POLICY OP ON OPM.POLICY_UUID = OP.POLICY_UUID "
+ "INNER JOIN AM_API_OPERATION_POLICY AOP ON OPM.POLICY_UUID = AOP.POLICY_UUID "
+ "WHERE AUM.API_ID = ? AND AUM.REVISION_UUID = ?";

// public static final String GET_OPERATION_POLICIES_PER_URI_BY_API_SQL = "SELECT AUM.HTTP_METHOD, AUM.URL_PATTERN, "
// + "OP.POLICY_NAME, OP.POLICY_VERSION, OPM.DIRECTION, OPM.POLICY_UUID "
// + "FROM AM_API_URL_MAPPING AS AUM INNER JOIN AM_API_OPERATION_POLICY_MAPPING OPM ON AUM.URL_MAPPING_ID = OPM.URL_MAPPING_ID "
// + "INNER JOIN AM_OPERATION_POLICY OP ON OPM.POLICY_UUID = OP.POLICY_UUID "
// + "INNER JOIN AM_API_OPERATION_POLICY AOP ON OPM.POLICY_UUID = AOP.POLICY_UUID "
// + "WHERE AUM.API_ID = ? AND AUM.REVISION_UUID = ?";

public static final String GET_OPERATION_POLICIES_PER_URI_BY_API_SQL =
"SELECT OP.POLICY_NAME, OP.POLICY_VERSION,APM.DIRECTION AS API_POLICY_DIRECTION, APM.POLICY_UUID AS API_POLICY_UUID, OPM.POLICY_UUID AS OPERATION_POLICY_UUID, OPM.DIRECTION AS OPERATION_POLICY_DIRECTION, AUM.HTTP_METHOD, AUM.URL_PATTERN "
+ "FROM AM_API_OPERATION_POLICY AS AOP "
+ "LEFT JOIN AM_API_OPERATION_POLICY_MAPPING OPM ON AOP.POLICY_UUID = OPM.POLICY_UUID "
+ "LEFT JOIN AM_API_URL_MAPPING AUM ON AUM.URL_MAPPING_ID = OPM.URL_MAPPING_ID "
+ "INNER JOIN AM_OPERATION_POLICY OP ON OP.POLICY_UUID = AOP.POLICY_UUID "
+ "LEFT JOIN AM_API_POLICY_MAPPING as APM ON APM.POLICY_UUID = AOP.POLICY_UUID "
+ "WHERE AOP.API_UUID = ? AND AOP.REVISION_UUID = ?";

// public static final String GET_API_POLICIES = "SELECT AUM.HTTP_METHOD, AUM.URL_PATTERN, "
// + "OP.POLICY_NAME, OP.POLICY_VERSION, OPM.DIRECTION, OPM.POLICY_UUID "
// + "FROM AM_API_POLICY_MAPPING AS APM INNER JOIN AM_API_OPERATION_POLICY OPM ON APM.POLICY_UUID = OPM.POLICY_UUID "
// + "WHERE APM.API_ID = ? AND APM.REVISION_UUID = ?";
public static final String GET_ALL_APIS_BY_ORGANIZATION_AND_DEPLOYMENT_SQL = "SELECT AM_API.API_PROVIDER,AM_API" +
".API_NAME,AM_API.CONTEXT,AM_API.API_UUID,AM_API.API_ID,AM_API.API_TIER,AM_API.API_VERSION,AM_API" +
".API_TYPE,AM_API.STATUS,AM_REVISION.REVISION_UUID AS REVISION_UUID,AM_DEPLOYMENT_REVISION_MAPPING.NAME " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import java.util.ArrayList;
import java.util.List;
import org.wso2.carbon.apimgt.internal.service.dto.OperationPolicyDTO;
import org.wso2.carbon.apimgt.internal.service.dto.URLMappingDTO;
import javax.validation.constraints.*;

Expand All @@ -30,6 +31,7 @@ public class APIDTO {
private String status = null;
private String organization = null;
private Boolean isDefaultVersion = null;
private List<OperationPolicyDTO> apiPolicies = new ArrayList<>();
private List<URLMappingDTO> urlMappings = new ArrayList<>();

/**
Expand Down Expand Up @@ -229,6 +231,23 @@ public void setIsDefaultVersion(Boolean isDefaultVersion) {
this.isDefaultVersion = isDefaultVersion;
}

/**
**/
public APIDTO apiPolicies(List<OperationPolicyDTO> apiPolicies) {
this.apiPolicies = apiPolicies;
return this;
}


@ApiModelProperty(value = "")
@JsonProperty("apiPolicies")
public List<OperationPolicyDTO> getApiPolicies() {
return apiPolicies;
}
public void setApiPolicies(List<OperationPolicyDTO> apiPolicies) {
this.apiPolicies = apiPolicies;
}

/**
**/
public APIDTO urlMappings(List<URLMappingDTO> urlMappings) {
Expand Down Expand Up @@ -267,12 +286,13 @@ public boolean equals(java.lang.Object o) {
Objects.equals(status, API.status) &&
Objects.equals(organization, API.organization) &&
Objects.equals(isDefaultVersion, API.isDefaultVersion) &&
Objects.equals(apiPolicies, API.apiPolicies) &&
Objects.equals(urlMappings, API.urlMappings);
}

@Override
public int hashCode() {
return Objects.hash(uuid, apiId, provider, name, version, context, policy, apiType, status, organization, isDefaultVersion, urlMappings);
return Objects.hash(uuid, apiId, provider, name, version, context, policy, apiType, status, organization, isDefaultVersion, apiPolicies, urlMappings);
}

@Override
Expand All @@ -291,6 +311,7 @@ public String toString() {
sb.append(" status: ").append(toIndentedString(status)).append("\n");
sb.append(" organization: ").append(toIndentedString(organization)).append("\n");
sb.append(" isDefaultVersion: ").append(toIndentedString(isDefaultVersion)).append("\n");
sb.append(" apiPolicies: ").append(toIndentedString(apiPolicies)).append("\n");
sb.append(" urlMappings: ").append(toIndentedString(urlMappings)).append("\n");
sb.append("}");
return sb.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ private static APIDTO fromAPItoDTO(API model) {
urlMappingDTO.setOperationPolicies(operationPolicyDTOList);
urlMappingsDTO.add(urlMappingDTO);
}
List<OperationPolicyDTO> apiPolicies = new ArrayList<>();
for(OperationPolicy apiPolicy: model.getApiPolicies()) {
OperationPolicyDTO operationPolicyDTO = new OperationPolicyDTO();
operationPolicyDTO.setPolicyId(apiPolicy.getPolicyId());
operationPolicyDTO.setPolicyName(apiPolicy.getPolicyName());
operationPolicyDTO.setPolicyVersion(apiPolicy.getPolicyVersion());
operationPolicyDTO.setDirection(apiPolicy.getDirection());
operationPolicyDTO.setOrder(apiPolicy.getOrder());
}
apidto.setApiPolicies(apiPolicies);
apidto.setUrlMappings(urlMappingsDTO);
}
return apidto;
Expand Down Expand Up @@ -158,6 +168,17 @@ public static APIListDTO fromAPIToAPIListDTO(API model) {
urlMappingDTO.setOperationPolicies(operationPolicyDTOList);
urlMappingsDTO.add(urlMappingDTO);
}
List<OperationPolicyDTO> apiPolicies = new ArrayList<>();
for(OperationPolicy apiPolicy: model.getApiPolicies()) {
OperationPolicyDTO operationPolicyDTO = new OperationPolicyDTO();
operationPolicyDTO.setPolicyId(apiPolicy.getPolicyId());
operationPolicyDTO.setPolicyName(apiPolicy.getPolicyName());
operationPolicyDTO.setPolicyVersion(apiPolicy.getPolicyVersion());
operationPolicyDTO.setDirection(apiPolicy.getDirection());
operationPolicyDTO.setOrder(apiPolicy.getOrder());
apiPolicies.add(operationPolicyDTO);
}
apidto.setApiPolicies(apiPolicies);
apidto.setUrlMappings(urlMappingsDTO);
apiListdto.setCount(1);
apiListdto.getList().add(apidto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,10 @@ definitions:
type: boolean
description: Whether this is the default version of the API.
example: false
apiPolicies:
type: array
items:
$ref: '#/definitions/OperationPolicy'
urlMappings:
type: array
items:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.wso2.carbon.apimgt.keymgt.model.entity;

import org.wso2.carbon.apimgt.api.model.OperationPolicy;
import org.wso2.carbon.apimgt.api.model.subscription.CacheableEntity;
import org.wso2.carbon.apimgt.api.model.subscription.URLMapping;

Expand All @@ -41,6 +42,7 @@ public class API implements CacheableEntity<String> {
private String organization;
private boolean deployed = false;
private boolean isDefaultVersion = false;
private List<OperationPolicy> apiPolicies = new ArrayList<>();

public API() {
}
Expand Down Expand Up @@ -205,6 +207,7 @@ public String toString() {
", status='" + status + '\'' +
", isDefaultVersion=" + isDefaultVersion +
", urlMappings=" + urlMappings +
", apiPolicies=" + apiPolicies +
'}';
}

Expand Down Expand Up @@ -295,4 +298,12 @@ public boolean isDeployed() {
public void setDeployed(boolean deployed) {
this.deployed = deployed;
}

public void setApiPolicies(List<OperationPolicy> apiPolicies) {
this.apiPolicies = apiPolicies;
}

public List<OperationPolicy> getApiPolicies() {
return apiPolicies;
}
}

0 comments on commit 587c32b

Please sign in to comment.