use of org.wso2.carbon.apimgt.api.model.policy.Pipeline in project carbon-apimgt by wso2.
the class ApiMgtDAO method addPipeline.
/**
* Add throttling policy pipeline to database
*
* @param pipeline condition pipeline
* @param policyID id of the policy to add pipeline
* @param conn database connection. This should be provided inorder to rollback transaction
* @throws SQLException
*/
private void addPipeline(Pipeline pipeline, int policyID, Connection conn) throws SQLException {
PreparedStatement conditionStatement = null;
ResultSet rs = null;
try {
String sqlAddQuery = SQLConstants.ThrottleSQLConstants.INSERT_CONDITION_GROUP_SQL;
List<Condition> conditionList = pipeline.getConditions();
// Add data to the AM_CONDITION table
String dbProductName = conn.getMetaData().getDatabaseProductName();
conditionStatement = conn.prepareStatement(sqlAddQuery, new String[] { DBUtils.getConvertedAutoGeneratedColumnName(dbProductName, "CONDITION_GROUP_ID") });
conditionStatement.setInt(1, policyID);
conditionStatement.setString(2, pipeline.getQuotaPolicy().getType());
if (PolicyConstants.REQUEST_COUNT_TYPE.equals(pipeline.getQuotaPolicy().getType())) {
conditionStatement.setLong(3, ((RequestCountLimit) pipeline.getQuotaPolicy().getLimit()).getRequestCount());
conditionStatement.setString(4, null);
} else if (PolicyConstants.BANDWIDTH_TYPE.equals(pipeline.getQuotaPolicy().getType())) {
BandwidthLimit limit = (BandwidthLimit) pipeline.getQuotaPolicy().getLimit();
conditionStatement.setLong(3, limit.getDataAmount());
conditionStatement.setString(4, limit.getDataUnit());
}
conditionStatement.setLong(5, pipeline.getQuotaPolicy().getLimit().getUnitTime());
conditionStatement.setString(6, pipeline.getQuotaPolicy().getLimit().getTimeUnit());
conditionStatement.setString(7, pipeline.getDescription());
conditionStatement.executeUpdate();
rs = conditionStatement.getGeneratedKeys();
// Add Throttling parameters which have multiple entries
if (rs != null && rs.next()) {
// Get the inserted
int pipelineId = rs.getInt(1);
// CONDITION_GROUP_ID (auto
// incremented value)
pipeline.setId(pipelineId);
for (Condition condition : conditionList) {
if (condition == null) {
continue;
}
String type = condition.getType();
if (PolicyConstants.IP_RANGE_TYPE.equals(type) || PolicyConstants.IP_SPECIFIC_TYPE.equals(type)) {
IPCondition ipCondition = (IPCondition) condition;
addIPCondition(ipCondition, pipelineId, conn);
}
if (PolicyConstants.HEADER_TYPE.equals(type)) {
addHeaderCondition((HeaderCondition) condition, pipelineId, conn);
} else if (PolicyConstants.QUERY_PARAMETER_TYPE.equals(type)) {
addQueryParameterCondition((QueryParameterCondition) condition, pipelineId, conn);
} else if (PolicyConstants.JWT_CLAIMS_TYPE.equals(type)) {
addJWTClaimsCondition((JWTClaimsCondition) condition, pipelineId, conn);
}
}
}
} finally {
APIMgtDBUtil.closeAllConnections(conditionStatement, null, rs);
}
}
use of org.wso2.carbon.apimgt.api.model.policy.Pipeline in project carbon-apimgt by wso2.
the class APIMgtDAOTest method testUpdateAPIPolicy.
@Test
public void testUpdateAPIPolicy() throws APIManagementException {
String policyName = "TestUpdateApiPolicy";
APIPolicy policy = (APIPolicy) getPolicyAPILevelPerUser(policyName);
apiMgtDAO.addAPIPolicy(policy);
policy = apiMgtDAO.getAPIPolicy(policyName, -1234);
policy.setDescription("New Description");
ArrayList<Pipeline> pipelines = new ArrayList<Pipeline>();
Pipeline p = new Pipeline();
QuotaPolicy quotaPolicy = new QuotaPolicy();
quotaPolicy.setType("requestCount");
RequestCountLimit requestCountLimit = new RequestCountLimit();
requestCountLimit.setTimeUnit("min");
requestCountLimit.setUnitTime(50);
requestCountLimit.setRequestCount(1000);
quotaPolicy.setLimit(requestCountLimit);
ArrayList<Condition> conditions = new ArrayList<Condition>();
DateCondition dateCondition = new DateCondition();
dateCondition.setSpecificDate("2016-03-03");
conditions.add(dateCondition);
HeaderCondition headerCondition1 = new HeaderCondition();
headerCondition1.setHeader("User-Agent");
headerCondition1.setValue("Chrome");
conditions.add(headerCondition1);
HeaderCondition headerCondition2 = new HeaderCondition();
headerCondition2.setHeader("Accept-Ranges");
headerCondition2.setValue("bytes");
conditions.add(headerCondition2);
QueryParameterCondition queryParameterCondition1 = new QueryParameterCondition();
queryParameterCondition1.setParameter("test1");
queryParameterCondition1.setValue("testValue1");
conditions.add(queryParameterCondition1);
QueryParameterCondition queryParameterCondition2 = new QueryParameterCondition();
queryParameterCondition2.setParameter("x");
queryParameterCondition2.setValue("abc");
conditions.add(queryParameterCondition2);
JWTClaimsCondition jwtClaimsCondition1 = new JWTClaimsCondition();
jwtClaimsCondition1.setClaimUrl("test_url");
jwtClaimsCondition1.setAttribute("test_attribute");
conditions.add(jwtClaimsCondition1);
p.setQuotaPolicy(quotaPolicy);
p.setConditions(conditions);
pipelines.add(p);
policy.setPipelines(pipelines);
apiMgtDAO.updateAPIPolicy(policy);
APIPolicy apiPolicy = apiMgtDAO.getAPIPolicy(policyName, -1234);
assertNotNull(apiPolicy);
List<Pipeline> pipelineList = apiPolicy.getPipelines();
assertNotNull(pipelineList);
assertEquals(pipelineList.size(), pipelines.size());
}
use of org.wso2.carbon.apimgt.api.model.policy.Pipeline in project carbon-apimgt by wso2.
the class APIMgtDAOTest method getPolicyAPILevelPerUser.
private Policy getPolicyAPILevelPerUser(String policyName) {
APIPolicy policy = new APIPolicy(policyName);
policy.setUserLevel(PolicyConstants.PER_USER);
policy.setDescription("Description");
policy.setTenantId(-1234);
BandwidthLimit defaultLimit = new BandwidthLimit();
defaultLimit.setTimeUnit("min");
defaultLimit.setUnitTime(5);
defaultLimit.setDataAmount(400);
defaultLimit.setDataUnit("MB");
QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
defaultQuotaPolicy.setLimit(defaultLimit);
defaultQuotaPolicy.setType(PolicyConstants.BANDWIDTH_TYPE);
policy.setDefaultQuotaPolicy(defaultQuotaPolicy);
List<Pipeline> pipelines;
QuotaPolicy quotaPolicy;
List<Condition> condition;
BandwidthLimit bandwidthLimit;
RequestCountLimit requestCountLimit;
pipelines = new ArrayList<Pipeline>();
// /////////pipeline item 1 start//////
Pipeline p1 = new Pipeline();
quotaPolicy = new QuotaPolicy();
quotaPolicy.setType(PolicyConstants.BANDWIDTH_TYPE);
bandwidthLimit = new BandwidthLimit();
bandwidthLimit.setTimeUnit("min");
bandwidthLimit.setUnitTime(5);
bandwidthLimit.setDataAmount(100);
bandwidthLimit.setDataUnit("GB");
quotaPolicy.setLimit(bandwidthLimit);
condition = new ArrayList<Condition>();
HTTPVerbCondition verbCond = new HTTPVerbCondition();
verbCond.setHttpVerb("POST");
condition.add(verbCond);
IPCondition ipCondition = new IPCondition(PolicyConstants.IP_SPECIFIC_TYPE);
ipCondition.setSpecificIP("127.0.0.1");
condition.add(ipCondition);
DateRangeCondition dateRangeCondition = new DateRangeCondition();
dateRangeCondition.setStartingDate("2016-01-03");
dateRangeCondition.setEndingDate("2016-01-31");
condition.add(dateRangeCondition);
p1.setQuotaPolicy(quotaPolicy);
p1.setConditions(condition);
pipelines.add(p1);
// /////////pipeline item 1 end//////
// /////////pipeline item 2 start//////
Pipeline p2 = new Pipeline();
quotaPolicy = new QuotaPolicy();
quotaPolicy.setType("requestCount");
requestCountLimit = new RequestCountLimit();
requestCountLimit.setTimeUnit("min");
requestCountLimit.setUnitTime(50);
requestCountLimit.setRequestCount(1000);
quotaPolicy.setLimit(requestCountLimit);
List<Condition> condition2 = new ArrayList<Condition>();
DateCondition dateCondition = new DateCondition();
dateCondition.setSpecificDate("2016-01-02");
condition2.add(dateCondition);
HeaderCondition headerCondition1 = new HeaderCondition();
headerCondition1.setHeader("User-Agent");
headerCondition1.setValue("Firefox");
condition2.add(headerCondition1);
HeaderCondition headerCondition2 = new HeaderCondition();
headerCondition2.setHeader("Accept-Ranges");
headerCondition2.setValue("bytes");
condition2.add(headerCondition2);
QueryParameterCondition queryParameterCondition1 = new QueryParameterCondition();
queryParameterCondition1.setParameter("test1");
queryParameterCondition1.setValue("testValue1");
condition2.add(queryParameterCondition1);
QueryParameterCondition queryParameterCondition2 = new QueryParameterCondition();
queryParameterCondition2.setParameter("test2");
queryParameterCondition2.setValue("testValue2");
condition2.add(queryParameterCondition2);
JWTClaimsCondition jwtClaimsCondition1 = new JWTClaimsCondition();
jwtClaimsCondition1.setClaimUrl("test_url");
jwtClaimsCondition1.setAttribute("test_attribute");
condition2.add(jwtClaimsCondition1);
JWTClaimsCondition jwtClaimsCondition2 = new JWTClaimsCondition();
jwtClaimsCondition2.setClaimUrl("test_url");
jwtClaimsCondition2.setAttribute("test_attribute");
condition2.add(jwtClaimsCondition2);
IPCondition ipRangeCondition = new IPCondition(PolicyConstants.IP_RANGE_TYPE);
ipCondition.setStartingIP("127.0.0.1");
ipCondition.setEndingIP("127.0.0.12");
condition2.add(ipRangeCondition);
p2.setQuotaPolicy(quotaPolicy);
p2.setConditions(condition2);
pipelines.add(p2);
// /////////pipeline item 2 end//////
policy.setPipelines(pipelines);
return policy;
}
use of org.wso2.carbon.apimgt.api.model.policy.Pipeline in project carbon-apimgt by wso2.
the class ApiMgtDAO method getConditions.
/**
* Retrieves list of Conditions for a pipeline specified by <code>pipelineId</code>
*
* @param pipelineId pipeline Id with conditions to retrieve
* @return list of Conditions for a pipeline
* @throws APIManagementException
*/
private ArrayList<Condition> getConditions(int pipelineId) throws APIManagementException {
Connection connection = null;
PreparedStatement conditionsStatement = null;
ResultSet resultSet = null;
ArrayList<Condition> conditions = new ArrayList<Condition>();
String startingIP = null;
String endingIP = null;
String specificIP = null;
boolean invert;
try {
connection = APIMgtDBUtil.getConnection();
conditionsStatement = connection.prepareStatement(SQLConstants.ThrottleSQLConstants.GET_IP_CONDITIONS_SQL);
conditionsStatement.setInt(1, pipelineId);
resultSet = conditionsStatement.executeQuery();
while (resultSet.next()) {
startingIP = resultSet.getString(ThrottlePolicyConstants.COLUMN_STARTING_IP);
endingIP = resultSet.getString(ThrottlePolicyConstants.COLUMN_ENDING_IP);
specificIP = resultSet.getString(ThrottlePolicyConstants.COLUMN_SPECIFIC_IP);
invert = resultSet.getBoolean(ThrottlePolicyConstants.COLUMN_WITHIN_IP_RANGE);
if (specificIP != null && !"".equals(specificIP)) {
IPCondition ipCondition = new IPCondition(PolicyConstants.IP_SPECIFIC_TYPE);
ipCondition.setSpecificIP(specificIP);
ipCondition.setInvertCondition(invert);
conditions.add(ipCondition);
} else if (startingIP != null && !"".equals(startingIP)) {
/*
Assumes availability of starting ip means ip range is enforced.
Therefore availability of ending ip is not checked.
*/
IPCondition ipRangeCondition = new IPCondition(PolicyConstants.IP_RANGE_TYPE);
ipRangeCondition.setStartingIP(startingIP);
ipRangeCondition.setEndingIP(endingIP);
ipRangeCondition.setInvertCondition(invert);
conditions.add(ipRangeCondition);
}
}
setHeaderConditions(pipelineId, conditions);
setQueryParameterConditions(pipelineId, conditions);
setJWTClaimConditions(pipelineId, conditions);
} catch (SQLException e) {
handleException("Failed to get conditions for pipelineId: " + pipelineId, e);
} finally {
APIMgtDBUtil.closeAllConnections(conditionsStatement, connection, resultSet);
}
return conditions;
}
use of org.wso2.carbon.apimgt.api.model.policy.Pipeline in project carbon-apimgt by wso2.
the class ApiMgtDAO method updateAPIPolicy.
/**
* Update a API level throttling policy to database.
* <p>
* If condition group already exists for the policy, that condition Group will be deleted and condition Group will
* be inserted to the database with old POLICY_ID.
* </p>
*
* @param policy policy object defining the throttle policy
* @throws APIManagementException
*/
public APIPolicy updateAPIPolicy(APIPolicy policy) throws APIManagementException {
String updateQuery;
int policyId = 0;
String selectQuery;
if (policy != null) {
if (!StringUtils.isBlank(policy.getPolicyName()) && policy.getTenantId() != -1) {
selectQuery = SQLConstants.ThrottleSQLConstants.GET_API_POLICY_ID_SQL;
updateQuery = SQLConstants.ThrottleSQLConstants.UPDATE_API_POLICY_SQL;
} else if (!StringUtils.isBlank(policy.getUUID())) {
selectQuery = SQLConstants.ThrottleSQLConstants.GET_API_POLICY_ID_BY_UUID_SQL;
updateQuery = ThrottleSQLConstants.UPDATE_API_POLICY_BY_UUID_SQL;
} else {
String errorMsg = "Policy object doesn't contain mandatory parameters. At least UUID or Name,Tenant Id" + " should be provided. Name: " + policy.getPolicyName() + ", Tenant Id: " + policy.getTenantId() + ", UUID: " + policy.getUUID();
log.error(errorMsg);
throw new APIManagementException(errorMsg);
}
} else {
String errorMsg = "Provided Policy to update is null";
log.error(errorMsg);
throw new APIManagementException(errorMsg);
}
try (Connection connection = APIMgtDBUtil.getConnection()) {
connection.setAutoCommit(false);
try (PreparedStatement selectStatement = connection.prepareStatement(selectQuery);
PreparedStatement deleteStatement = connection.prepareStatement(SQLConstants.ThrottleSQLConstants.DELETE_CONDITION_GROUP_SQL);
PreparedStatement updateStatement = connection.prepareStatement(updateQuery)) {
if (selectQuery.equals(SQLConstants.ThrottleSQLConstants.GET_API_POLICY_ID_SQL)) {
selectStatement.setString(1, policy.getPolicyName());
selectStatement.setInt(2, policy.getTenantId());
} else {
selectStatement.setString(1, policy.getUUID());
}
try (ResultSet resultSet = selectStatement.executeQuery()) {
if (resultSet.next()) {
policyId = resultSet.getInt(ThrottlePolicyConstants.COLUMN_POLICY_ID);
}
}
deleteStatement.setInt(1, policyId);
deleteStatement.executeUpdate();
if (!StringUtils.isEmpty(policy.getDisplayName())) {
updateStatement.setString(1, policy.getDisplayName());
} else {
updateStatement.setString(1, policy.getPolicyName());
}
updateStatement.setString(2, policy.getDescription());
updateStatement.setString(3, policy.getDefaultQuotaPolicy().getType());
if (PolicyConstants.REQUEST_COUNT_TYPE.equalsIgnoreCase(policy.getDefaultQuotaPolicy().getType())) {
RequestCountLimit limit = (RequestCountLimit) policy.getDefaultQuotaPolicy().getLimit();
updateStatement.setLong(4, limit.getRequestCount());
updateStatement.setString(5, null);
} else if (PolicyConstants.BANDWIDTH_TYPE.equalsIgnoreCase(policy.getDefaultQuotaPolicy().getType())) {
BandwidthLimit limit = (BandwidthLimit) policy.getDefaultQuotaPolicy().getLimit();
updateStatement.setLong(4, limit.getDataAmount());
updateStatement.setString(5, limit.getDataUnit());
} else if (PolicyConstants.EVENT_COUNT_TYPE.equalsIgnoreCase(policy.getDefaultQuotaPolicy().getType())) {
EventCountLimit limit = (EventCountLimit) policy.getDefaultQuotaPolicy().getLimit();
updateStatement.setLong(4, limit.getEventCount());
updateStatement.setString(5, null);
}
updateStatement.setLong(6, policy.getDefaultQuotaPolicy().getLimit().getUnitTime());
updateStatement.setString(7, policy.getDefaultQuotaPolicy().getLimit().getTimeUnit());
if (!StringUtils.isBlank(policy.getPolicyName()) && policy.getTenantId() != -1) {
updateStatement.setString(8, policy.getPolicyName());
updateStatement.setInt(9, policy.getTenantId());
} else if (!StringUtils.isBlank(policy.getUUID())) {
updateStatement.setString(8, policy.getUUID());
}
int updatedRawCount = updateStatement.executeUpdate();
if (updatedRawCount > 0) {
List<Pipeline> pipelines = policy.getPipelines();
if (pipelines != null) {
for (Pipeline pipeline : pipelines) {
// add each pipeline data to AM_CONDITION_GROUP table
addPipeline(pipeline, policyId, connection);
}
}
}
connection.commit();
} catch (SQLException e) {
try {
connection.rollback();
} catch (SQLException ex) {
// rollback failed. exception will be thrown later for upper exception
log.error("Failed to rollback the add Global Policy: " + policy.toString(), ex);
}
handleException("Failed to update API policy: " + policy.getPolicyName() + '-' + policy.getTenantId(), e);
}
} catch (SQLException e) {
handleException("Failed to update API policy: " + policy.getPolicyName() + '-' + policy.getTenantId(), e);
}
return policy;
}
Aggregations