Search in sources :

Example 6 with APIPolicy

use of org.wso2.carbon.apimgt.core.models.policy.APIPolicy in project carbon-apimgt by wso2.

the class PolicyDAOImpl method getApiPolicy.

@Override
public APIPolicy getApiPolicy(String policyName) throws APIMgtDAOException {
    try {
        String sqlQuery = "SELECT UUID, NAME, DEFAULT_QUOTA_TYPE, DEFAULT_TIME_UNIT, DEFAULT_UNIT_TIME, " + "DEFAULT_QUOTA, DEFAULT_QUOTA_UNIT, DESCRIPTION, DISPLAY_NAME, IS_DEPLOYED, APPLICABLE_LEVEL " + "from AM_API_POLICY WHERE NAME = ?";
        try (Connection connection = DAOUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
            preparedStatement.setString(1, policyName);
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                if (resultSet.next()) {
                    APIPolicy policy = new APIPolicy(policyName);
                    setCommonPolicyDetails(policy, resultSet);
                    policy.setUserLevel(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_APPLICABLE_LEVEL));
                    policy.setPipelines(getPipelines(policy.getUuid(), connection));
                    return policy;
                } else {
                    // not found
                    String msg = "API Policy not found for name: " + policyName;
                    log.warn(msg);
                    throw new APIMgtDAOException(msg, ExceptionCodes.POLICY_NOT_FOUND);
                }
            }
        }
    } catch (SQLException e) {
        String errorMsg = "Error in retrieving API policy with name: " + policyName;
        log.error(errorMsg, e);
        throw new APIMgtDAOException(errorMsg, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy)

Example 7 with APIPolicy

use of org.wso2.carbon.apimgt.core.models.policy.APIPolicy in project carbon-apimgt by wso2.

the class PolicyDAOImpl method addApiPolicy.

@Override
public void addApiPolicy(APIPolicy policy) throws APIMgtDAOException {
    try (Connection connection = DAOUtil.getConnection()) {
        try {
            connection.setAutoCommit(false);
            addApiPolicy(policy, connection);
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            String errorMessage = "Error in adding API policy, policy name: " + policy.getPolicyName();
            log.error(errorMessage, e);
            throw new APIMgtDAOException(errorMessage, e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        String errorMsg = "Error in obtaining DB connection";
        log.error(errorMsg, e);
        throw new APIMgtDAOException(errorMsg, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 8 with APIPolicy

use of org.wso2.carbon.apimgt.core.models.policy.APIPolicy in project carbon-apimgt by wso2.

the class PolicyDAOImpl method addApiPolicy.

private static void addApiPolicy(APIPolicy policy, Connection connection) throws SQLException, APIMgtDAOException {
    final String query = "INSERT INTO AM_API_POLICY (UUID, NAME, DISPLAY_NAME, DESCRIPTION, " + "DEFAULT_QUOTA_TYPE, DEFAULT_QUOTA, DEFAULT_QUOTA_UNIT, DEFAULT_UNIT_TIME," + " DEFAULT_TIME_UNIT, APPLICABLE_LEVEL, IS_DEPLOYED) " + "VALUES (?,?,?,?,?,?,?,?,?,?,?)";
    Limit limit = policy.getDefaultQuotaPolicy().getLimit();
    try (PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setString(1, policy.getUuid());
        statement.setString(2, policy.getPolicyName());
        statement.setString(3, policy.getDisplayName());
        statement.setString(4, policy.getDescription());
        statement.setString(5, policy.getDefaultQuotaPolicy().getType());
        setDefaultThrottlePolicyDetailsPreparedStmt(limit, statement);
        statement.setLong(8, policy.getDefaultQuotaPolicy().getLimit().getUnitTime());
        statement.setString(9, policy.getDefaultQuotaPolicy().getLimit().getTimeUnit());
        statement.setString(10, API_TIER_LEVEL);
        statement.setBoolean(11, policy.isDeployed());
        statement.execute();
        if (policy.getPipelines() != null) {
            addAPIPipeline(connection, policy.getPipelines(), policy.getUuid());
        }
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Limit(org.wso2.carbon.apimgt.core.models.policy.Limit) RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)

Example 9 with APIPolicy

use of org.wso2.carbon.apimgt.core.models.policy.APIPolicy in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImpl method addApiPolicy.

@Override
public String addApiPolicy(APIPolicy policy) throws APIManagementException {
    try {
        String policyUuid = policy.getUuid();
        if (policyUuid == null) {
            if (log.isDebugEnabled()) {
                log.debug("Policy id is null, hence generating a new UUID for the policy with name: " + policy.getPolicyName());
            }
            policyUuid = UUID.randomUUID().toString();
            policy.setUuid(policyUuid);
        }
        policyDAO.addApiPolicy(policy);
        PolicyValidationData policyValidationData = new PolicyValidationData(policyUuid, policy.getPolicyName(), false);
        apiGateway.addPolicy(policyValidationData);
        return policyUuid;
    } catch (APIMgtDAOException e) {
        String errorMessage = "Couldn't add API policy for uuid: " + policy.getUuid();
        log.error(errorMessage, e);
        throw new APIManagementException(errorMessage, e, e.getErrorHandler());
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) PolicyValidationData(org.wso2.carbon.apimgt.core.models.PolicyValidationData)

Example 10 with APIPolicy

use of org.wso2.carbon.apimgt.core.models.policy.APIPolicy in project carbon-apimgt by wso2.

the class APIThrottlePolicyTemplateBuilder method getThrottlePolicyTemplateForPipelines.

/**
 * Generate policy for api level throttling
 *
 * @return throttle policies for api level
 * @throws APITemplateException throws if generation failure occur
 */
public Map<String, String> getThrottlePolicyTemplateForPipelines() throws APITemplateException {
    if (log.isDebugEnabled()) {
        log.debug("Generating Siddhi App for apiLevel :" + apiPolicy.toString());
    }
    // get velocity template for API policy pipeline and generate the template
    Map<String, String> policyArray = new HashMap<String, String>();
    StringWriter writer;
    VelocityContext context;
    VelocityEngine velocityengine = initVelocityEngine();
    Template template = velocityengine.getTemplate(getTemplatePathForAPI());
    // Generate template for pipeline conditions if pipelines not null
    if (apiPolicy.getPipelines() != null) {
        for (Pipeline pipeline : apiPolicy.getPipelines()) {
            // set values for velocity context
            context = new VelocityContext();
            setConstantContext(context);
            context.put(PIPELINE_ITEM, pipeline);
            context.put(POLICY, apiPolicy);
            context.put(QUOTA_POLICY, pipeline.getQuotaPolicy());
            context.put(PIPELINE, CONDITION + UNDERSCORE + pipeline.getId());
            String conditionString = getPolicyCondition(pipeline.getConditions());
            context.put(CONDITION, AND + conditionString);
            writer = new StringWriter();
            template.merge(context, writer);
            if (log.isDebugEnabled()) {
                log.debug("Generated Siddhi App : " + writer.toString());
            }
            String policyName = PolicyConstants.POLICY_LEVEL_RESOURCE + UNDERSCORE + apiPolicy.getPolicyName() + UNDERSCORE + CONDITION + UNDERSCORE + pipeline.getId();
            policyArray.put(policyName, writer.toString());
        }
    }
    return policyArray;
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) VelocityContext(org.apache.velocity.VelocityContext) Template(org.apache.velocity.Template) Pipeline(org.wso2.carbon.apimgt.core.models.policy.Pipeline)

Aggregations

APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)87 Test (org.testng.annotations.Test)44 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)40 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)39 API (org.wso2.carbon.apimgt.core.models.API)33 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)27 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)22 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)22 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)22 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)21 HashMap (java.util.HashMap)20 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)20 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)19 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)18 HashSet (java.util.HashSet)16 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)15 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)15 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)14 BusinessInformation (org.wso2.carbon.apimgt.core.models.BusinessInformation)13 CorsConfiguration (org.wso2.carbon.apimgt.core.models.CorsConfiguration)13