return lb;
}
<<<<<<< HEAD
public List getExpiredLbs() {
Calendar threeMonthsAgo = Calendar.getInstance();
threeMonthsAgo.add(Calendar.DATE, -90);
String queryStr = "select l from LoadBalancer l where l.status = :status and l.updated <= :threeMonthsAgo";
Query query = entityManager.createQuery(queryStr);
query.setParameter("threeMonthsAgo", threeMonthsAgo);
query.setParameter("status", LoadBalancerStatus.DELETED);
List results = query.getResultList();
return results;
}
public void removeExpiredLb(int lbId) {
LoadBalancer lb = entityManager.find(LoadBalancer.class, lbId);
entityManager.remove(lb);
}
/*public AccessList getNetworkItemByAccountIdLoadBalancerIdNetworkItemId(Integer aid, Integer lid, Integer nid) throws EntityNotFoundException {
List al = null;
String qStr = "SELECT a from AccessList a "
+ " WHERE a.loadbalancer.id = :lid "
+ " and a.loadbalancer.accountId = :aid "
+ " and a.id = :nid";
// Don't put up with the possibility of bogus querys
if (lid == null || aid == null || nid == null) {
throw new EntityNotFoundException("Null parameter Query rejected");
}
Query q = entityManager.createQuery(qStr);
q.setParameter("aid", aid);
q.setParameter("lid", lid);
q.setParameter("nid", nid);
q.setMaxResults(1);
al = q.getResultList();
if (al.size() != 1) {
throw new EntityNotFoundException("Node not nound");
}
return al.get(0);
}*/
public Integer getNumNonDeletedLoadBalancersForAccount(Integer accountId) {
Query query = entityManager.createNativeQuery(
"select count(account_id) from loadbalancer where status != 'DELETED' and account_id = :accountId").setParameter("accountId", accountId);
return ((BigInteger) query.getSingleResult()).intValue();
}
/*public List getAllProtocols() {
List protocolObjects;
protocolObjects = entityManager.createQuery(
"from LoadBalancerProtocolObject where enabled = True").getResultList();
if (protocolObjects.isEmpty()) {
protocolObjects = new ArrayList();
}
return protocolObjects;
}
public List getAllAlgorithms() {
List algorithmObjects;
algorithmObjects = entityManager.createQuery(
"from LoadBalancerAlgorithmObject lba where lba.enabled = True").getResultList();
if (algorithmObjects.isEmpty()) {
algorithmObjects = new ArrayList();
}
return algorithmObjects;
}*/
public ConnectionThrottle getConnectionLimitsbyAccountIdLoadBalancerId(Integer accountId,
Integer loadbalancerId) throws EntityNotFoundException, DeletedStatusException {
LoadBalancer lb = getByIdAndAccountId(loadbalancerId, accountId);
if (lb.getStatus().equals(LoadBalancerStatus.DELETED)) {
throw new DeletedStatusException("The loadbalancer is marked as deleted.");
}
if (lb.getConnectionThrottle() == null) {
return new ConnectionThrottle();
}
return lb.getConnectionThrottle();
}
/*public RateLimit getRateLimitByLoadBalancerId(
Integer loadbalancerId) throws EntityNotFoundException, DeletedStatusException {
LoadBalancer lb = getById(loadbalancerId);
if (lb.getStatus().equals(LoadBalancerStatus.DELETED)) {
throw new DeletedStatusException("The loadbalancer is marked as deleted.");
}
if (lb.getRateLimit() == null) {
throw new EntityNotFoundException("No rate limit exists");
}
return lb.getRateLimit();
}
public SessionPersistence getSessionPersistenceByAccountIdLoadBalancerId(Integer accountId,
Integer loadbalancerId) throws EntityNotFoundException, DeletedStatusException, BadRequestException {
LoadBalancer lb = getByIdAndAccountId(loadbalancerId, accountId);
if (lb.getStatus().equals(LoadBalancerStatus.DELETED)) {
throw new DeletedStatusException("The loadbalancer is marked as deleted.");
}
if (lb.getSessionPersistence() == null) {
throw new EntityNotFoundException("No session persistence exists");
}
return lb.getSessionPersistence();
}
*/
/*public LoadBalancer enableSessionPersistenceByIdAndAccountId(Integer id,
Integer accountId) throws EntityNotFoundException, BadRequestException {
LoadBalancer lb;
lb = getById(id);
if (!lb.getAccountId().equals(accountId)) {
throw new EntityNotFoundException(String.format("Load balancer not found"));
}
if (!lb.getProtocol().equals(LoadBalancerProtocol.HTTP)) {
throw new BadRequestException(
"Bad Request: The requirements were not met for this request, please verify with spec and try again.");
}
return lb;
}*/
public boolean testAndSetStatus(Integer accountId, Integer loadbalancerId, LoadBalancerStatus statusToChangeTo, boolean allowConcurrentModifications) throws EntityNotFoundException, UnprocessableEntityException {
String qStr = "from LoadBalancer lb where lb.accountId=:aid and lb.id=:lid";
List lbList;
Query q = entityManager.createQuery(qStr).setLockMode(LockModeType.PESSIMISTIC_WRITE).
setParameter("aid", accountId).
setParameter("lid", loadbalancerId);
lbList = q.getResultList();
if (lbList.size() < 1) {
throw new EntityNotFoundException();
}
LoadBalancer lb = lbList.get(0);
if (lb.getStatus().equals(DELETED)) throw new UnprocessableEntityException(Constants.LoadBalancerDeleted);
final boolean isActive = lb.getStatus().equals(LoadBalancerStatus.ACTIVE);
final boolean isPendingOrActive = lb.getStatus().equals(LoadBalancerStatus.PENDING_UPDATE) || isActive;
if(allowConcurrentModifications ? isPendingOrActive : isActive) {
lb.setStatus(statusToChangeTo);
lb.setUpdated(Calendar.getInstance());
entityManager.merge(lb);
return true;
}
return false;
}
/* public List getUsageByAccountIdandLbId(Integer accountId, Integer loadBalancerId, Calendar startTime, Calendar endTime) throws EntityNotFoundException, DeletedStatusException {
// TODO: Find more efficient way of making sure loadbalancer exists
getByIdAndAccountId(loadBalancerId, accountId); // Make sure loadbalancer exists
return getUsageByLbId(loadBalancerId, startTime, endTime);
}*/
public void setStatus(Integer accountId,Integer loadbalancerId,LoadBalancerStatus status) throws EntityNotFoundException{
String qStr = "from LoadBalancer lb where lb.accountId=:aid and lb.id=:lid";
List lbList;
Query q = entityManager.createQuery(qStr).setLockMode(LockModeType.PESSIMISTIC_WRITE).
setParameter("aid", accountId).
setParameter("lid", loadbalancerId);
lbList = q.getResultList();
if (lbList.size() < 1) {
throw new EntityNotFoundException();
}
lbList.get(0).setStatus(status);
entityManager.merge(lbList.get(0));
}
public boolean testAndSetStatusPending(Integer accountId, Integer loadbalancerId) throws EntityNotFoundException {
String qStr = "from LoadBalancer lb where lb.accountId=:aid and lb.id=:lid";
List lbList;
Query q = entityManager.createQuery(qStr).setLockMode(LockModeType.PESSIMISTIC_WRITE).
setParameter("aid", accountId).
setParameter("lid", loadbalancerId);
lbList = q.getResultList();
if (lbList.size() < 1) {
throw new EntityNotFoundException();
}
if (!lbList.get(0).getStatus().equals(LoadBalancerStatus.ACTIVE)) {
return false;
}
lbList.get(0).setStatus(LoadBalancerStatus.PENDING_UPDATE);
lbList.get(0).setUpdated(Calendar.getInstance());
entityManager.merge(lbList.get(0));
return true;
}
public List getNonDeletedByAccountId(Integer accountId) {
List lbs = new ArrayList();
String qStr = "select lb from LoadBalancer lb where status !='DELETED' and lb.accountId = :accountId";
Query q = entityManager.createQuery(qStr).setParameter("accountId", accountId);
lbs = q.getResultList();
return lbs;
}
/**
* This method is optimized performance and only pulls the information that is required for displaying the loadbalancers list, instead
* of eager-loading the childrens.
*/
/*public List getNonDeletedByAccountId(Integer accountId, Integer... p) {
List lbs = new ArrayList();
LoadBalancerStatus lbStatus = LoadBalancerStatus.DELETED;
Query query;
String qStr = "SELECT lb.id, lb.accountId, lb.name, lb.algorithm, lb.protocol, lb.port, lb.status, lb.created, lb.updated FROM LoadBalancer lb WHERE lb.accountId = :accountId and lb.status != :status";
query = entityManager.createQuery(qStr).setParameter("accountId", accountId).setParameter("status", lbStatus);
if (p.length >= 2) {
Integer offset = p[0];
Integer limit = p[1];
Integer changesSince = p[2];
Integer marker = p[3];
if (offset == null) {
offset = 0;
}
if (limit == null) {
limit = 100;
}
if (marker != null) {
if (lbStatus != null) {
query = entityManager.createQuery(
cq.setLimit(limit);
"SELECT lb.id, lb.accountId, lb.name, lb.algorithm, lb.protocol, lb.port, lb.status, lb.created, lb.updated FROM LoadBalancer lb WHERE lb.accountId = :accountId and lb.id >= :lbId and lb.status != :status").setParameter("accountId", accountId).setParameter("lbId", marker).setParameter("status", lbStatus);
}
}
query = query.setFirstResult(offset).setMaxResults(limit);
}
List |