Search in sources :

Example 1 with OperationPolicyData

use of org.wso2.carbon.apimgt.api.model.OperationPolicyData in project carbon-apimgt by wso2.

the class ApiMgtDAO method cloneOperationPolicy.

/**
 * Clone an operation policy to the API. This method is used to clone policy to a newly created api version.
 * Cloning a common policy to API.
 * Cloning a dependent policy of a product
 * Each of these scenarios, original APIs' policy ID will be recorded as the cloned policy ID.
 *
 * @param apiUUID      UUID of the API
 * @param operationPolicyData
 * @return cloned policyID
 * @throws APIManagementException
 */
public String cloneOperationPolicy(String apiUUID, OperationPolicyData operationPolicyData) throws APIManagementException {
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        try {
            connection.setAutoCommit(false);
            String policyId = addAPISpecificOperationPolicy(connection, apiUUID, null, operationPolicyData, operationPolicyData.getClonedCommonPolicyId());
            connection.commit();
            return policyId;
        } catch (SQLException e) {
            connection.rollback();
            throw e;
        }
    } catch (SQLException e) {
        throw new APIManagementException("Error while cloning Operation policies", e);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 2 with OperationPolicyData

use of org.wso2.carbon.apimgt.api.model.OperationPolicyData in project carbon-apimgt by wso2.

the class ApiMgtDAO method getAPISpecificOperationPolicyByPolicyName.

private OperationPolicyData getAPISpecificOperationPolicyByPolicyName(Connection connection, String policyName, String apiUUID, String revisionUUID, String tenantDomain, boolean isWithPolicyDefinition) throws SQLException {
    String dbQuery = SQLConstants.OperationPolicyConstants.GET_API_SPECIFIC_OPERATION_POLICY_FROM_POLICY_NAME;
    if (revisionUUID != null) {
        dbQuery += " AND AOP.REVISION_UUID = ?";
    } else {
        dbQuery += " AND AOP.REVISION_UUID IS NULL";
    }
    PreparedStatement statement = connection.prepareStatement(dbQuery);
    statement.setString(1, policyName);
    statement.setString(2, tenantDomain);
    statement.setString(3, apiUUID);
    if (revisionUUID != null) {
        statement.setString(4, revisionUUID);
    }
    ResultSet rs = statement.executeQuery();
    OperationPolicyData policyData = null;
    if (rs.next()) {
        policyData = new OperationPolicyData();
        policyData.setOrganization(tenantDomain);
        policyData.setPolicyId(rs.getString("POLICY_UUID"));
        policyData.setApiUUID(rs.getString("API_UUID"));
        policyData.setRevisionUUID(rs.getString("REVISION_UUID"));
        policyData.setMd5Hash(rs.getString("POLICY_MD5"));
        policyData.setClonedCommonPolicyId(rs.getString("CLONED_POLICY_UUID"));
        policyData.setSpecification(populatePolicySpecificationFromRS(rs));
    }
    if (isWithPolicyDefinition && policyData != null) {
        if (isWithPolicyDefinition && policyData != null) {
            populatePolicyDefinitions(connection, policyData.getPolicyId(), policyData);
        }
    }
    return policyData;
}
Also used : OperationPolicyData(org.wso2.carbon.apimgt.api.model.OperationPolicyData) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 3 with OperationPolicyData

use of org.wso2.carbon.apimgt.api.model.OperationPolicyData in project carbon-apimgt by wso2.

the class ApiMgtDAO method addOperationPolicyContent.

/**
 * This method is used to populate AM_OPERATION_POLICY table. This will return the policy ID.
 *
 * @param connection DB connection
 * @param policyData Unique Identifier of API
 * @return UUID of the newly created policy
 * @throws SQLException
 */
private String addOperationPolicyContent(Connection connection, OperationPolicyData policyData) throws SQLException {
    OperationPolicySpecification policySpecification = policyData.getSpecification();
    String dbQuery = SQLConstants.OperationPolicyConstants.ADD_OPERATION_POLICY;
    String policyUUID = UUID.randomUUID().toString();
    PreparedStatement statement = connection.prepareStatement(dbQuery);
    statement.setString(1, policyUUID);
    statement.setString(2, policySpecification.getName());
    statement.setString(3, policySpecification.getDisplayName());
    statement.setString(4, policySpecification.getDescription());
    statement.setString(5, policySpecification.getApplicableFlows().toString());
    statement.setString(6, policySpecification.getSupportedGateways().toString());
    statement.setString(7, policySpecification.getSupportedApiTypes().toString());
    statement.setBinaryStream(8, new ByteArrayInputStream(APIUtil.getPolicyAttributesAsString(policySpecification).getBytes()));
    statement.setString(9, policyData.getOrganization());
    statement.setString(10, policySpecification.getCategory().toString());
    statement.setBoolean(11, policySpecification.isMultipleAllowed());
    statement.setString(12, policyData.getMd5Hash());
    statement.executeUpdate();
    statement.close();
    if (policyData.getSynapsePolicyDefinition() != null) {
        addOperationPolicyDefinition(connection, policyUUID, policyData.getSynapsePolicyDefinition());
    }
    if (policyData.getCcPolicyDefinition() != null) {
        addOperationPolicyDefinition(connection, policyUUID, policyData.getCcPolicyDefinition());
    }
    return policyUUID;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PreparedStatement(java.sql.PreparedStatement) OperationPolicySpecification(org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)

Example 4 with OperationPolicyData

use of org.wso2.carbon.apimgt.api.model.OperationPolicyData in project carbon-apimgt by wso2.

the class ApiMgtDAO method getCommonOperationPolicyByPolicyID.

private OperationPolicyData getCommonOperationPolicyByPolicyID(Connection connection, String policyId, String organization, boolean isWithPolicyDefinition) throws SQLException {
    String dbQuery = SQLConstants.OperationPolicyConstants.GET_COMMON_OPERATION_POLICY_WITH_OUT_DEFINITION_FROM_POLICY_ID;
    PreparedStatement statement = connection.prepareStatement(dbQuery);
    statement.setString(1, policyId);
    statement.setString(2, organization);
    ResultSet rs = statement.executeQuery();
    OperationPolicyData policyData = null;
    if (rs.next()) {
        policyData = new OperationPolicyData();
        policyData.setPolicyId(policyId);
        policyData.setOrganization(organization);
        policyData.setMd5Hash(rs.getString("POLICY_MD5"));
        policyData.setSpecification(populatePolicySpecificationFromRS(rs));
    }
    rs.close();
    statement.close();
    if (isWithPolicyDefinition && policyData != null) {
        populatePolicyDefinitions(connection, policyId, policyData);
    }
    return policyData;
}
Also used : OperationPolicyData(org.wso2.carbon.apimgt.api.model.OperationPolicyData) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 5 with OperationPolicyData

use of org.wso2.carbon.apimgt.api.model.OperationPolicyData in project carbon-apimgt by wso2.

the class ApiMgtDAO method restoreOperationPolicyRevision.

/**
 * This method is used to restore an API specific operation policy revision.
 *
 * @param connection   DB connection
 * @param apiUUID      UUID of the API
 * @param policyId     Original policy's ID that needs to be cloned
 * @param revisionId   The revision number
 * @param organization Organization name
 * @throws SQLException
 * @throws APIManagementException
 */
private String restoreOperationPolicyRevision(Connection connection, String apiUUID, String policyId, int revisionId, String organization) throws SQLException, APIManagementException {
    OperationPolicyData revisionedPolicy = getAPISpecificOperationPolicyByPolicyID(connection, policyId, apiUUID, organization, true);
    String restoredPolicyId = null;
    if (revisionedPolicy != null) {
        // First check whether there exists a API specific policy for same policy name with revision uuid null
        // This is the state where we record the policies applied in the working copy.
        OperationPolicyData apiSpecificPolicy = getAPISpecificOperationPolicyByPolicyName(connection, revisionedPolicy.getSpecification().getName(), revisionedPolicy.getApiUUID(), null, organization, false);
        if (apiSpecificPolicy != null) {
            if (apiSpecificPolicy.getMd5Hash().equals(revisionedPolicy.getMd5Hash())) {
                if (log.isDebugEnabled()) {
                    log.debug("Matching API specific operation policy found for the revisioned policy and " + "MD5 hashes match");
                }
            } else {
                updateAPISpecificOperationPolicyWithClonedPolicyId(connection, apiSpecificPolicy.getPolicyId(), revisionedPolicy);
                if (log.isDebugEnabled()) {
                    log.debug("Even though a matching API specific operation policy found for name," + " MD5 hashes does not match. Policy " + apiSpecificPolicy.getPolicyId() + " has been updated from the revision.");
                }
            }
            restoredPolicyId = apiSpecificPolicy.getPolicyId();
        } else {
            if (revisionedPolicy.isClonedPolicy()) {
                // Check for a common operation policy only if it is a cloned policy.
                OperationPolicyData commonPolicy = getCommonOperationPolicyByPolicyID(connection, revisionedPolicy.getClonedCommonPolicyId(), organization, false);
                if (commonPolicy != null) {
                    if (commonPolicy.getMd5Hash().equals(revisionedPolicy.getMd5Hash())) {
                        if (log.isDebugEnabled()) {
                            log.debug("Matching common operation policy found. MD5 hash match");
                        }
                        // This means the common policy is same with our revision. A clone is created and original
                        // common policy ID is referenced as the ClonedCommonPolicyId
                        restoredPolicyId = addAPISpecificOperationPolicy(connection, apiUUID, null, revisionedPolicy, revisionedPolicy.getClonedCommonPolicyId());
                    } else {
                        // This means the common policy is updated since we created the revision.
                        // we have to create a clone and since policy is different, we can't refer the original common
                        // policy as ClonedCommonPolicyId. This should be a new API specific policy
                        revisionedPolicy.getSpecification().setName(revisionedPolicy.getSpecification().getName() + "_restored-" + revisionId);
                        revisionedPolicy.getSpecification().setDisplayName(revisionedPolicy.getSpecification().getDisplayName() + " Restored from revision " + revisionId);
                        revisionedPolicy.setMd5Hash(APIUtil.getMd5OfOperationPolicy(revisionedPolicy));
                        revisionedPolicy.setRevisionUUID(null);
                        restoredPolicyId = addAPISpecificOperationPolicy(connection, apiUUID, null, revisionedPolicy, null);
                        if (log.isDebugEnabled()) {
                            log.debug("An updated matching common operation policy found. A new API specific operation " + "policy created by the display name " + revisionedPolicy.getSpecification().getName());
                        }
                    }
                } else {
                    // This means this is a clone of a deleted common policy. A new API specific policy will be created.
                    revisionedPolicy.getSpecification().setName(revisionedPolicy.getSpecification().getName() + "_restored-" + revisionId);
                    revisionedPolicy.getSpecification().setDisplayName(revisionedPolicy.getSpecification().getDisplayName() + " Restored from revision " + revisionId);
                    revisionedPolicy.setMd5Hash(APIUtil.getMd5OfOperationPolicy(revisionedPolicy));
                    revisionedPolicy.setRevisionUUID(null);
                    restoredPolicyId = addAPISpecificOperationPolicy(connection, apiUUID, null, revisionedPolicy, null);
                    if (log.isDebugEnabled()) {
                        log.debug("No matching operation policy found. A new API specific operation " + "policy created by the name " + revisionedPolicy.getSpecification().getName());
                    }
                }
            } else {
                // This means this is a completely new policy and we don't have any reference of a previous state in
                // working copy. A new API specific policy will be created.
                revisionedPolicy.setRevisionUUID(null);
                restoredPolicyId = addAPISpecificOperationPolicy(connection, apiUUID, null, revisionedPolicy, null);
                if (log.isDebugEnabled()) {
                    log.debug("No matching operation policy found. A new API specific operation " + "policy created by the name " + revisionedPolicy.getSpecification().getName());
                }
            }
        }
    } else {
        throw new APIManagementException("A revisioned operation policy not found for " + policyId);
    }
    return restoredPolicyId;
}
Also used : OperationPolicyData(org.wso2.carbon.apimgt.api.model.OperationPolicyData) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Aggregations

OperationPolicyData (org.wso2.carbon.apimgt.api.model.OperationPolicyData)25 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)17 PreparedStatement (java.sql.PreparedStatement)11 OperationPolicySpecification (org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)11 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)10 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)10 ResultSet (java.sql.ResultSet)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 APIImportExportException (org.wso2.carbon.apimgt.impl.importexport.APIImportExportException)7 OperationPolicy (org.wso2.carbon.apimgt.api.model.OperationPolicy)6 OperationPolicyDataDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataDTO)6 SdkClientException (com.amazonaws.SdkClientException)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 MalformedURLException (java.net.MalformedURLException)5 URISyntaxException (java.net.URISyntaxException)5 UnknownHostException (java.net.UnknownHostException)5 SQLException (java.sql.SQLException)5 JSONException (org.json.JSONException)5 ParseException (org.json.simple.parser.ParseException)5