diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java index fcd13d13c6c4..b797aed8c8ab 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java @@ -1553,6 +1553,9 @@ EnvironmentPropertiesDTO getEnvironmentSpecificAPIProperties(String apiUuid, Str */ String importOperationPolicy(OperationPolicyData operationPolicyData, String organization) throws APIManagementException; + String importOperationPolicyOfGivenType(OperationPolicyData operationPolicyData, String policyType, + String organization) throws APIManagementException; + /** * Add an API specific operation policy diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/OperationPolicy.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/OperationPolicy.java index 6d78f5e7e40f..7fc417294384 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/OperationPolicy.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/OperationPolicy.java @@ -25,6 +25,7 @@ public class OperationPolicy implements Comparable { private String policyName = ""; private String policyVersion = "v1"; + private String policyType = null; private String direction = null; private Map parameters = null; private String policyId = null; @@ -50,6 +51,16 @@ public void setPolicyVersion(String policyVersion) { this.policyVersion = policyVersion; } + public String getPolicyType() { + + return policyType; + } + + public void setPolicyType(String policyType) { + + this.policyType = policyType; + } + public Map getParameters() { return parameters; @@ -98,7 +109,7 @@ public boolean equals(Object o) { if (o instanceof OperationPolicy) { OperationPolicy policyObj = (OperationPolicy) o; return Objects.equals(policyName, policyObj.policyName) && Objects.equals(policyVersion, - policyObj.policyVersion) && Objects.equals(direction, policyObj.direction) && Objects.equals( + policyObj.policyVersion) && Objects.equals(direction, policyObj.direction) && policyType.equals(policyObj.policyType) && Objects.equals( parameters, policyObj.parameters) && Objects.equals(policyId, policyObj.policyId); } return false; diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java index a06a898cf5c6..90e35b7e5020 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java @@ -135,6 +135,7 @@ import org.wso2.carbon.apimgt.impl.importexport.APIImportExportException; import org.wso2.carbon.apimgt.impl.importexport.ExportFormat; import org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI; +import org.wso2.carbon.apimgt.impl.importexport.ImportExportConstants; import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder; import org.wso2.carbon.apimgt.impl.lifecycle.CheckListItem; import org.wso2.carbon.apimgt.impl.lifecycle.LCManagerFactory; @@ -5280,6 +5281,87 @@ public String getSecuritySchemeOfAPI(String uuid, String organization) throws AP } } + public String getPolicyType(OperationPolicy policy, Map 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 apiProductOperationPolicyIdToClonedPolicyIdMap) + throws APIManagementException { + + String originatedPolicyId = apiProductOperationPolicyIdToClonedPolicyIdMap.get(policy.getPolicyId()); + Map apiOperationPolicyIdToClonedPolicyIdMap = + getClonedAPISpecificOperationPolicyIdsList(apiUUID); + policy.setPolicyId(originatedPolicyId); + return getPolicyType(policy, apiOperationPolicyIdToClonedPolicyIdMap); + } + + public API addPolicyTypeFieldToApi(API api) throws APIManagementException { + + Map apiOperationPolicyIdToClonedPolicyIdMap = getClonedAPISpecificOperationPolicyIdsList(api.getUuid()); + Set uriTemplates = api.getUriTemplates(); + for (URITemplate uriTemplate : uriTemplates) { + List operationPolicies = uriTemplate.getOperationPolicies(); + if (!operationPolicies.isEmpty()) { + for (OperationPolicy operationPolicy : operationPolicies) { + String policyType = getPolicyType(operationPolicy, apiOperationPolicyIdToClonedPolicyIdMap); + operationPolicy.setPolicyType(policyType); + } + } + } + api.setUriTemplates(uriTemplates); + + List apiPolicies = api.getApiPolicies(); + if (apiPolicies != null && !apiPolicies.isEmpty()) { + for (OperationPolicy policy : apiPolicies) { + String policyType = getPolicyType(policy, apiOperationPolicyIdToClonedPolicyIdMap); + policy.setPolicyType(policyType); + } + } + api.setApiPolicies(apiPolicies); + + return api; + } + + public APIProduct addPolicyTypeFieldToApiProduct(APIProduct product) throws APIManagementException { + + Map apiProductOperationPolicyIdToClonedPolicyIdMap = + getClonedAPISpecificOperationPolicyIdsList(product.getUuid()); + List productResources = product.getProductResources(); + for (APIProductResource resource : productResources) { + URITemplate uriTemplate = resource.getUriTemplate(); + List 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); @@ -5322,6 +5404,7 @@ public API getAPIbyUUID(String uuid, String organization) throws APIManagementEx populateApiInfo(api); populateSubtypeConfiguration(api); populateDefaultVersion(api); + api = addPolicyTypeFieldToApi(api); return api; } else { String msg = "Failed to get API. API artifact corresponding to artifactId " + uuid + " does not exist"; @@ -5501,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 @@ -7092,6 +7176,88 @@ public String importOperationPolicy(OperationPolicyData importedPolicyData, Stri return policyId; } + @Override + public String importOperationPolicyOfGivenType(OperationPolicyData importedPolicyData, String policyType, + String organization) + throws APIManagementException { + + OperationPolicySpecification importedSpec = importedPolicyData.getSpecification(); + OperationPolicyData existingOperationPolicy; + + String policyId = null; + if (policyType == null) { + /*To handle scenarios where api is exported from a previous U2 version. API and Common policies with same name + and same version is not supported there + */ + policyId = importOperationPolicy(importedPolicyData, organization); + } else if (policyType.equalsIgnoreCase(ImportExportConstants.POLICY_TYPE_COMMON)) { + existingOperationPolicy = getCommonOperationPolicyByPolicyName(importedSpec.getName(), + importedSpec.getVersion(),organization, false); + + if (existingOperationPolicy != null) { + if (existingOperationPolicy.getMd5Hash().equals(importedPolicyData.getMd5Hash())) { + if (log.isDebugEnabled()) { + log.debug("Matching common policy found for imported policy and Md5 hashes match."); + } + policyId = existingOperationPolicy.getPolicyId(); + } else { + importedSpec.setName(importedSpec.getName() + "_imported"); + importedSpec.setDisplayName(importedSpec.getDisplayName() + " Imported"); + importedPolicyData.setSpecification(importedSpec); + importedPolicyData.setMd5Hash(APIUtil.getHashOfOperationPolicy(importedPolicyData)); + policyId = addAPISpecificOperationPolicy(importedPolicyData.getApiUUID(), importedPolicyData, + organization); + if (log.isDebugEnabled()) { + log.debug("Even though existing common policy name match with imported policy, " + + "the MD5 hashes does not match in the policy " + existingOperationPolicy.getPolicyId() + + ". A new policy created with ID " + policyId); + } + } + } else { + importedSpec.setName(importedSpec.getName() + "_imported"); + importedSpec.setDisplayName(importedSpec.getDisplayName() + " Imported"); + importedPolicyData.setSpecification(importedSpec); + importedPolicyData.setMd5Hash(APIUtil.getHashOfOperationPolicy(importedPolicyData)); + policyId = addAPISpecificOperationPolicy(importedPolicyData.getApiUUID(), importedPolicyData, + organization); + if (log.isDebugEnabled()) { + log.debug( + "There aren't any existing common policy for the imported policy. " + + "A new policy created with ID " + policyId); + } + } + } else { //api level policy by default + existingOperationPolicy = + getAPISpecificOperationPolicyByPolicyName(importedSpec.getName(), importedSpec.getVersion(), + importedPolicyData.getApiUUID(), null, organization, false); + + if (existingOperationPolicy != null) { + if (existingOperationPolicy.getMd5Hash().equals(importedPolicyData.getMd5Hash())) { + if (log.isDebugEnabled()) { + log.debug("Matching API specific policy found for imported policy and MD5 hashes match."); + } + } else { + if (log.isDebugEnabled()) { + log.debug("Even though existing API specific policy name match with imported policy, " + + "the MD5 hashes does not match in the policy " + existingOperationPolicy.getPolicyId() + + ".Therefore updating the existing policy"); + } + updateOperationPolicy(existingOperationPolicy.getPolicyId(), importedPolicyData, organization); + } + policyId = existingOperationPolicy.getPolicyId(); + } else { + policyId = addAPISpecificOperationPolicy(importedPolicyData.getApiUUID(), importedPolicyData, + organization); + if (log.isDebugEnabled()) { + log.debug( + "There aren't any existing policies for the imported policy. A new policy created with ID " + + policyId); + } + } + } + + return policyId; + } @Override public String addAPISpecificOperationPolicy(String apiUUID, OperationPolicyData operationPolicyData, @@ -7146,6 +7312,13 @@ public OperationPolicyData getAPISpecificOperationPolicyByPolicyId(String policy .getAPISpecificOperationPolicyByPolicyID(policyId, apiUUID, organization, isWithPolicyDefinition); } + public Map getClonedAPISpecificOperationPolicyIdsList(String apiUUID) + throws APIManagementException { + + return apiMgtDAO + .getClonedIdsMappedApiSpecificOperationPolicies(apiUUID); + } + @Override public OperationPolicyData getCommonOperationPolicyByPolicyId(String policyId, String organization, boolean isWithPolicyDefinition) diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java index 8964e338e4b6..2ce75fc5bed0 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java @@ -20828,6 +20828,50 @@ 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 + * @return operation policy + * @throws APIManagementException + */ + public Map getClonedIdsMappedApiSpecificOperationPolicies(String apiUUID) + throws APIManagementException { + + try (Connection connection = APIMgtDBUtil.getConnection()) { + return getClonedIdsMappedApiSpecificOperationPolicies(connection, apiUUID); + } catch (SQLException e) { + handleException("Failed to get the API specific operation policy IDs from API " + + apiUUID, e); + } + return null; + } + + private Map getClonedIdsMappedApiSpecificOperationPolicies(Connection connection, String apiUUID) + throws SQLException, APIManagementException { + + String dbQuery; + boolean isAPIRevision = checkAPIUUIDIsARevisionUUID(apiUUID) != null; + if (isAPIRevision) { + dbQuery = SQLConstants.OperationPolicyConstants. + GET_REVISION_SPECIFIC_OPERATION_POLICY_IDS_FROM_REVISION_UUID; + } else { + dbQuery = SQLConstants.OperationPolicyConstants.GET_API_SPECIFIC_OPERATION_POLICY_IDS_FROM_API_UUID; + } + Map policyMap = new HashMap<>(); + try (PreparedStatement statement = connection.prepareStatement(dbQuery)) { + statement.setString(1, apiUUID); + try (ResultSet rs = statement.executeQuery()) { + while (rs.next()) { + policyMap.put(rs.getString("POLICY_UUID"), rs.getString("CLONED_POLICY_UUID")); + } + } + } + return policyMap; + } + private List getPolicyDefinitionForPolicyId(Connection connection, String policyId) throws SQLException { @@ -21197,7 +21241,13 @@ public Set getCommonOperationPolicyNames(String organization) throws API while (rs.next()) { String policyName = rs.getString("POLICY_NAME"); String policyVersion = rs.getString("POLICY_VERSION"); - policyNames.add(APIUtil.getOperationPolicyFileName(policyName, policyVersion)); + policyNames.add(APIUtil.getOperationPolicyFileName(policyName, policyVersion, null)); + /*since the only usage of this method is to load the common operation policies from the + specifications and we are keeping only the common policies without appending the string "common" + to the file name, it's not required to append the policyType string + (policyNames.add(APIUtil.getOperationPolicyFileName(policyName, policyVersion, + ImportExportConstants.POLICY_TYPE_COMMON));)here as well. + */ } } } catch (SQLException e) { diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java index bdad6d440205..c237c2e2cb8b 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java @@ -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_IDS_FROM_API_UUID = + "SELECT " + + " POLICY_UUID, CLONED_POLICY_UUID " + + " FROM " + + " AM_API_OPERATION_POLICY " + + " WHERE " + + " API_UUID = ?"; + + public static final String GET_REVISION_SPECIFIC_OPERATION_POLICY_IDS_FROM_REVISION_UUID = + "SELECT " + + " POLICY_UUID, CLONED_POLICY_UUID " + + " FROM " + + " AM_API_OPERATION_POLICY " + + " WHERE " + + " 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, " + @@ -4284,6 +4300,8 @@ public static class OperationPolicyConstants { " OP.POLICY_UUID = ? AND OP.ORGANIZATION = ?"; + // CLONED_POLICY_UUID IS NULL was added to the query to allow creating an API level policy while having a common policy with same name and version + // and is attached to the API already public static final String GET_API_SPECIFIC_OPERATION_POLICY_FROM_POLICY_NAME = "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, " + @@ -4292,7 +4310,7 @@ public static class OperationPolicyConstants { " FROM " + " AM_OPERATION_POLICY OP INNER JOIN AM_API_OPERATION_POLICY AOP ON OP.POLICY_UUID = AOP.POLICY_UUID " + " WHERE " + - " OP.POLICY_NAME = ? AND OP.POLICY_VERSION = ? AND OP.ORGANIZATION = ? AND AOP.API_UUID = ? "; + " OP.POLICY_NAME = ? AND OP.POLICY_VERSION = ? AND OP.ORGANIZATION = ? AND AOP.API_UUID = ? AND AOP.CLONED_POLICY_UUID IS NULL "; public static final String GET_COMMON_OPERATION_POLICY_FROM_POLICY_NAME = "SELECT " + diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/importexport/ImportExportConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/importexport/ImportExportConstants.java index 6aca3a4a3696..681a7bced6bd 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/importexport/ImportExportConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/importexport/ImportExportConstants.java @@ -316,4 +316,7 @@ public final class ImportExportConstants { public static final String EXPORT_POLICY_TYPE_JSON = "JSON"; public static final String POLICY_NAME = "name"; + + public static final String POLICY_TYPE_API = "api"; + public static final String POLICY_TYPE_COMMON = "common"; } diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java index fde4b5f08aea..9be847d7cd21 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java @@ -166,6 +166,7 @@ import org.wso2.carbon.apimgt.impl.dto.ThrottleProperties; import org.wso2.carbon.apimgt.impl.dto.WorkflowDTO; import org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.DataLoadingException; +import org.wso2.carbon.apimgt.impl.importexport.ImportExportConstants; import org.wso2.carbon.apimgt.impl.internal.APIManagerComponent; import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder; import org.wso2.carbon.apimgt.impl.kmclient.ApacheFeignHttpClient; @@ -10088,8 +10089,9 @@ public static void loadCommonOperationPolicies(String organization) { OperationPolicyData policyData = new OperationPolicyData(); policyData.setSpecification(policySpec); policyData.setOrganization(organization); + //since the directory contains common policies only, files are not renamed with type String policyFileName = getOperationPolicyFileName(policySpec.getName(), - policySpec.getVersion()); + policySpec.getVersion(), null); OperationPolicyDefinition synapsePolicyDefinition = getOperationPolicyDefinitionFromFile(policyDefinitionLocation, policyFileName, APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION); @@ -10413,11 +10415,14 @@ public static OperationPolicyData generateOperationPolicyDataObject(String apiUu } - public static String getOperationPolicyFileName(String policyName, String policyVersion) { + public static String getOperationPolicyFileName(String policyName, String policyVersion, String policyType) { if (StringUtils.isEmpty(policyVersion)) { policyVersion = "v1"; } - return policyName + "_" + policyVersion; + if (policyType == null) { + return policyName + "_" + policyVersion; + } + return policyName + "_" + policyVersion + "_" + policyType; } public static String getCustomBackendFileName(String apiUUID, String endpointType) { diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml index 919a6d051230..c3d8c678daa4 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml @@ -13533,6 +13533,8 @@ components: policyVersion: type: string default: v1 + policyType: + type: string policyId: type: string parameters: diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/OperationPolicyDTO.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/OperationPolicyDTO.java index b88a28f2073f..fbdf6c279568 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/OperationPolicyDTO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/gen/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/dto/OperationPolicyDTO.java @@ -25,6 +25,7 @@ public class OperationPolicyDTO { private String policyName = null; private String policyVersion = "v1"; + private String policyType = null; private String policyId = null; private Map parameters = new HashMap(); @@ -63,6 +64,23 @@ public void setPolicyVersion(String policyVersion) { this.policyVersion = policyVersion; } + /** + **/ + public OperationPolicyDTO policyType(String policyType) { + this.policyType = policyType; + return this; + } + + + @ApiModelProperty(value = "") + @JsonProperty("policyType") + public String getPolicyType() { + return policyType; + } + public void setPolicyType(String policyType) { + this.policyType = policyType; + } + /** **/ public OperationPolicyDTO policyId(String policyId) { @@ -109,13 +127,14 @@ public boolean equals(java.lang.Object o) { OperationPolicyDTO operationPolicy = (OperationPolicyDTO) o; return Objects.equals(policyName, operationPolicy.policyName) && Objects.equals(policyVersion, operationPolicy.policyVersion) && + Objects.equals(policyType, operationPolicy.policyType) && Objects.equals(policyId, operationPolicy.policyId) && Objects.equals(parameters, operationPolicy.parameters); } @Override public int hashCode() { - return Objects.hash(policyName, policyVersion, policyId, parameters); + return Objects.hash(policyName, policyVersion, policyType, policyId, parameters); } @Override @@ -125,6 +144,7 @@ public String toString() { sb.append(" policyName: ").append(toIndentedString(policyName)).append("\n"); sb.append(" policyVersion: ").append(toIndentedString(policyVersion)).append("\n"); + sb.append(" policyType: ").append(toIndentedString(policyType)).append("\n"); sb.append(" policyId: ").append(toIndentedString(policyId)).append("\n"); sb.append(" parameters: ").append(toIndentedString(parameters)).append("\n"); sb.append("}"); 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/SynapsePolicyAggregator.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/SynapsePolicyAggregator.java index c204989287fb..4e75821a214b 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/SynapsePolicyAggregator.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/SynapsePolicyAggregator.java @@ -192,7 +192,7 @@ private static List renderPolicyMapping(List policyList if (flow.equals(policy.getDirection())) { Map policyParameters = policy.getParameters(); String policyFileName = APIUtil.getOperationPolicyFileName(policy.getPolicyName(), - policy.getPolicyVersion()); + policy.getPolicyVersion(), policy.getPolicyType()); OperationPolicySpecification policySpecification = ImportUtils .getOperationPolicySpecificationFromFile(policyDirectory, policyFileName); if (policySpecification.getSupportedGateways() 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/ExportUtils.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/ExportUtils.java index 9cf094aa3f3b..c626c24e4e39 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/ExportUtils.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/ExportUtils.java @@ -709,19 +709,25 @@ public static void addOperationPoliciesToArchive(String archivePath, String tena List operationPolicies = uriTemplate.getOperationPolicies(); if (operationPolicies != null && !operationPolicies.isEmpty()) { for (OperationPolicy policy : operationPolicies) { - if (!exportedPolicies.contains(policy.getPolicyName() + "_" + policy.getPolicyVersion())) { - String policyFileName = APIUtil.getOperationPolicyFileName(policy.getPolicyName(), - policy.getPolicyVersion()); + if (!exportedPolicies.contains(policy.getPolicyName() + "_" + policy.getPolicyVersion() + "_" + + policy.getPolicyType())) { if (policy.getPolicyId() != null) { + String policyFileName = APIUtil.getOperationPolicyFileName(policy.getPolicyName(), + policy.getPolicyVersion(), policy.getPolicyType()); + OperationPolicyData policyData = apiProvider.getAPISpecificOperationPolicyByPolicyId(policy.getPolicyId(), currentApiUuid, tenantDomain, true); if (policyData != null) { exportPolicyData(policyFileName, policyData, archivePath, exportFormat); - exportedPolicies.add(policy.getPolicyName() + "_" + policy.getPolicyVersion()); + exportedPolicies.add(policy.getPolicyName() + "_" + policy.getPolicyVersion() + "_" + + policy.getPolicyType()); } } else { // This path is to handle migrated APIs with mediation policies attached + // These are considered as API policies by default + String policyFileName = APIUtil.getOperationPolicyFileName(policy.getPolicyName(), + policy.getPolicyVersion(), ImportExportConstants.POLICY_TYPE_API); if (APIUtil.isSequenceDefined(api.getInSequence()) || APIUtil.isSequenceDefined(api.getOutSequence()) || APIUtil.isSequenceDefined(api.getFaultSequence())) { @@ -736,7 +742,8 @@ public static void addOperationPoliciesToArchive(String archivePath, String tena policy.getDirection(), tenantDomain); if (policyData != null) { exportPolicyData(policyFileName, policyData, archivePath, exportFormat); - exportedPolicies.add(policy.getPolicyName() + "_" + policy.getPolicyVersion()); + exportedPolicies.add(policy.getPolicyName() + "_" + policy.getPolicyVersion() + + "_" + ImportExportConstants.POLICY_TYPE_API); } } } @@ -748,14 +755,15 @@ public static void addOperationPoliciesToArchive(String archivePath, String tena if (api.getApiPolicies() != null && !api.getApiPolicies().isEmpty()) { for (OperationPolicy policy : api.getApiPolicies()) { String policyFileName = APIUtil.getOperationPolicyFileName(policy.getPolicyName(), - policy.getPolicyVersion()); + policy.getPolicyVersion(), policy.getPolicyType()); if (!exportedPolicies.contains(policyFileName)) { OperationPolicyData policyData = apiProvider.getAPISpecificOperationPolicyByPolicyId(policy.getPolicyId(), currentApiUuid, tenantDomain, true); if (policyData != null) { exportPolicyData(policyFileName, policyData, archivePath, exportFormat); - exportedPolicies.add(policy.getPolicyName() + "_" + policy.getPolicyVersion()); + exportedPolicies.add(policy.getPolicyName() + "_" + policy.getPolicyVersion() + "_" + + policy.getPolicyType()); } } } 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 85baaa5aa087..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.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.getPolicyVersion(), policyType); String policyID = null; if (!importedPolicies.containsKey(policyFileName)) { OperationPolicySpecification policySpec = @@ -824,15 +832,19 @@ public static List findOrImportPolicy(List pol } operationPolicyData.setMd5Hash( APIUtil.getHashOfOperationPolicy(operationPolicyData)); - policyID = provider.importOperationPolicy(operationPolicyData, tenantDomain); + policyID = provider.importOperationPolicyOfGivenType(operationPolicyData, + 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(); @@ -847,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 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/OperationPolicyMappingUtil.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/OperationPolicyMappingUtil.java index e0ce3142a737..59b04ba3d320 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/OperationPolicyMappingUtil.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/OperationPolicyMappingUtil.java @@ -57,6 +57,7 @@ public static OperationPolicy fromDTOToOperationPolicy(OperationPolicyDTO operat OperationPolicy operationPolicy = new OperationPolicy(); operationPolicy.setPolicyName(operationPolicyDTO.getPolicyName()); operationPolicy.setPolicyVersion(operationPolicyDTO.getPolicyVersion()); + operationPolicy.setPolicyType(operationPolicyDTO.getPolicyType()); operationPolicy.setPolicyId(operationPolicyDTO.getPolicyId()); operationPolicy.setParameters(operationPolicyDTO.getParameters()); return operationPolicy; @@ -67,6 +68,7 @@ public static OperationPolicyDTO fromOperationPolicyToDTO(OperationPolicy operat OperationPolicyDTO dto = new OperationPolicyDTO(); dto.setPolicyName(operationPolicy.getPolicyName()); dto.setPolicyVersion(operationPolicy.getPolicyVersion()); + dto.setPolicyType(operationPolicy.getPolicyType()); dto.setPolicyId(operationPolicy.getPolicyId()); dto.setParameters(operationPolicy.getParameters()); return dto; diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml index 919a6d051230..c3d8c678daa4 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/resources/publisher-api.yaml @@ -13533,6 +13533,8 @@ components: policyVersion: type: string default: v1 + policyType: + type: string policyId: type: string parameters: