for(String authId : bucket.getAuthIds()) {
String authToken = cachingTableAuthIdToAuthToken.get(authId);
if (authToken == null)
throw new IllegalArgumentException("Auth ID [" + authId + "] not found");
Auth auth = cachingTableAuth.get(authToken);
if (auth == null)
throw new IllegalArgumentException("Auth token not found");
}
}
for (QuotaRLBucket bucket : policy.getAuthIds()) {
checkIfAuthAlreadyInPolicy(policy.getId(), bucket.getAuthIds(), bucket.getId());
}
Policy cachedPolicy = null;
if(update) {
cachedPolicy = getPolicyById(policy.getId(), false);
// Remove existing contexts
for (Integer contextId : cachedPolicy.getContextIds()) {
cachingTableContext.remove(contextId);
}
}
policy.getContextIds().clear();
// TODO: Check value of quota, if changed, then reset the created date, otherwise, keep old value
//=> Speaker needs to store the created date
// Store all contexts aside, indexed by an int
for (Context ctx : policy.getContexts()) {
boolean ok = false;
while (!ok) {
int idx = (int)(Math.random() * Integer.MAX_VALUE);
Integer contextId = Integer.valueOf(idx);
if (!cachingTableContext.containsKey(contextId)) {
ctx.setContextId(contextId);
ctx.setCreatedDate(new Date());
cachingTableContext.set(contextId, new ContextWrapper(ctx));
policy.getContextIds().add(contextId);
ok = true;
}
}
}
// Store all contexts aside, indexed by an int
for (QuotaRLBucket bucket : policy.getAuthIds()) {
boolean ok = false;
if ((cachedPolicy != null) && ((bucket.getId() == null) || (bucket.getId().equals("")))) {
// First, try to find the current bucket in the old policy
for (QuotaRLBucket cachedBucket : cachedPolicy.getAuthIds()) {
if (cachedBucket.getId().equals(bucket.getId())) {
// Bucket was already in old policy
bucket.setBucketId(cachedBucket.getBucketId());
cachedPolicy.getAuthIds().remove(cachedBucket);
ok = true;
break;
}
}
}
while (!ok) {
int idx = (int)(Math.random() * Integer.MAX_VALUE);
Integer bucketId = Integer.valueOf(idx);
if (!usedBucketIds.contains(bucketId)) {
bucket.setBucketId(bucketId);
usedBucketIds.add(bucketId);
if ((bucket.getId() == null) || (bucket.getId().equals(""))) {
bucket.setId(bucketId.toString());
}
ok = true;
}
}
}
if (cachedPolicy != null) {
// Now remove all buckets which are not in the new policy
for (QuotaRLBucket cachedBucket : cachedPolicy.getAuthIds()) {
usedBucketIds.remove(cachedBucket.getBucketId());
for (IDataManagerUsedBucketIdsListener listner : this.usedBucketIdslisteners)
{
listner.usedBucketIdsRemoved(cachedBucket.getBucketId());
}
}
}
cachingTablePolicy.set(policy.getId(), policy);
// Now, iterate through all APIs and Auth in this policy and update them
for (String apiId : policy.getApiIds()) {
Api api = getApiById(apiId);
if (api.getPolicyIds().indexOf(policy.getId()) < 0)
{
api.getPolicyIds().add(policy.getId());
cachingTableApi.set(api.getId(), api);
}
}
for (QuotaRLBucket bucket : policy.getAuthIds()) {
for(String authId : bucket.getAuthIds()) {
String authToken = cachingTableAuthIdToAuthToken.get(authId);
if (authToken == null)
throw new IllegalArgumentException("Auth ID [" + authId + "] not found");
Auth auth = cachingTableAuth.get(authToken);
if (auth == null)
throw new IllegalArgumentException("Auth token not found");
//Add policy: add a AuthContext object with PolicyId, LimitId, BucketId (instead of just PolicyId) => LimitId should be crossed with "policyContext" on Auth
boolean found = false;
for (AuthIds authCtx : auth.getPolicyContexts()) {
if (authCtx.getPolicyId().equals(policy.getId())) {
//update Auth with good Policy context and bucket
authCtx.setPolicyContextId(getPolicyContextId(auth, policy));
authCtx.setPolicyBucketId(bucket.getBucketId());
cachingTableAuth.set(authToken, auth);//updating auth
found = true;
break;
}
}
if (!found) {
auth.getPolicyContexts().add(new AuthIds(policy.getId(), bucket.getId(), getPolicyContextId(auth, policy), bucket.getBucketId(), auth.getStatus().isActive()));
cachingTableAuth.set(authToken, auth);
}
}
}
}