use of com.cloud.network.dao.LoadBalancerVO in project cloudstack by apache.
the class LoadBalancingRulesManagerImpl method applyLoadBalancerConfig.
@Override
public boolean applyLoadBalancerConfig(long lbRuleId) throws ResourceUnavailableException {
LoadBalancerVO lb = _lbDao.findById(lbRuleId);
List<LoadBalancerVO> lbs;
if (isRollBackAllowedForProvider(lb)) {
// this is for Netscalar type of devices. if their is failure the db
// entries will be rollbacked.
lbs = Arrays.asList(lb);
} else {
boolean onlyRulesInTransitionState = true;
for (LoadBalancingServiceProvider lbElement : _lbProviders) {
Provider provider = lbElement.getProvider();
boolean isLbProvider = _networkModel.isProviderSupportServiceInNetwork(lb.getNetworkId(), Service.Lb, provider);
if (!isLbProvider) {
continue;
}
onlyRulesInTransitionState = lbElement.handlesOnlyRulesInTransitionState();
break;
}
// get all rules in transition state
if (onlyRulesInTransitionState) {
lbs = _lbDao.listInTransitionStateByNetworkIdAndScheme(lb.getNetworkId(), lb.getScheme());
} else {
lbs = _lbDao.listByNetworkIdAndScheme(lb.getNetworkId(), lb.getScheme());
}
}
return applyLoadBalancerRules(lbs, true);
}
use of com.cloud.network.dao.LoadBalancerVO in project cloudstack by apache.
the class LoadBalancingRulesManagerImpl method deleteLBStickinessPolicy.
@Override
@ActionEvent(eventType = EventTypes.EVENT_LB_STICKINESSPOLICY_DELETE, eventDescription = "revoking LB Stickiness policy ", async = true)
public boolean deleteLBStickinessPolicy(long stickinessPolicyId, boolean apply) {
boolean success = true;
CallContext caller = CallContext.current();
LBStickinessPolicyVO stickinessPolicy = _lb2stickinesspoliciesDao.findById(stickinessPolicyId);
if (stickinessPolicy == null) {
throw new InvalidParameterException("Invalid Stickiness policy id value: " + stickinessPolicyId);
}
LoadBalancerVO loadBalancer = _lbDao.findById(Long.valueOf(stickinessPolicy.getLoadBalancerId()));
if (loadBalancer == null) {
throw new InvalidParameterException("Invalid Load balancer : " + stickinessPolicy.getLoadBalancerId() + " for Stickiness policy id: " + stickinessPolicyId);
}
long loadBalancerId = loadBalancer.getId();
FirewallRule.State backupState = loadBalancer.getState();
_accountMgr.checkAccess(caller.getCallingAccount(), null, true, loadBalancer);
if (apply) {
if (loadBalancer.getState() == FirewallRule.State.Active) {
loadBalancer.setState(FirewallRule.State.Add);
_lbDao.persist(loadBalancer);
}
boolean backupStickyState = stickinessPolicy.isRevoke();
stickinessPolicy.setRevoke(true);
_lb2stickinesspoliciesDao.persist(stickinessPolicy);
s_logger.debug("Set load balancer rule for revoke: rule id " + loadBalancerId + ", stickinesspolicyID " + stickinessPolicyId);
try {
if (!applyLoadBalancerConfig(loadBalancerId)) {
s_logger.warn("Failed to remove load balancer rule id " + loadBalancerId + " for stickinesspolicyID " + stickinessPolicyId);
throw new CloudRuntimeException("Failed to remove load balancer rule id " + loadBalancerId + " for stickinesspolicyID " + stickinessPolicyId);
}
} catch (ResourceUnavailableException e) {
if (isRollBackAllowedForProvider(loadBalancer)) {
stickinessPolicy.setRevoke(backupStickyState);
_lb2stickinesspoliciesDao.persist(stickinessPolicy);
loadBalancer.setState(backupState);
_lbDao.persist(loadBalancer);
s_logger.debug("LB Rollback rule id: " + loadBalancer.getId() + " while deleting sticky policy: " + stickinessPolicyId);
}
s_logger.warn("Unable to apply the load balancer config because resource is unavaliable.", e);
success = false;
}
} else {
_lb2stickinesspoliciesDao.expunge(stickinessPolicyId);
}
return success;
}
use of com.cloud.network.dao.LoadBalancerVO in project cloudstack by apache.
the class LoadBalancingRulesManagerImpl method createLBStickinessPolicy.
@SuppressWarnings("rawtypes")
@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_LB_STICKINESSPOLICY_CREATE, eventDescription = "create lb stickinesspolicy to load balancer", create = true)
public StickinessPolicy createLBStickinessPolicy(CreateLBStickinessPolicyCmd cmd) throws NetworkRuleConflictException {
CallContext caller = CallContext.current();
/* Validation : check corresponding load balancer rule exist */
LoadBalancerVO loadBalancer = _lbDao.findById(cmd.getLbRuleId());
if (loadBalancer == null) {
throw new InvalidParameterValueException("Failed: LB rule id: " + cmd.getLbRuleId() + " not present ");
}
_accountMgr.checkAccess(caller.getCallingAccount(), null, true, loadBalancer);
if (loadBalancer.getState() == FirewallRule.State.Revoke) {
throw new InvalidParameterValueException("Failed: LB rule id: " + cmd.getLbRuleId() + " is in deleting state: ");
}
/* Generic validations */
if (!genericValidator(cmd)) {
throw new InvalidParameterValueException("Failed to create Stickiness policy: Validation Failed " + cmd.getLbRuleId());
}
/*
* Specific validations using network element validator for specific
* validations
*/
LBStickinessPolicyVO lbpolicy = new LBStickinessPolicyVO(loadBalancer.getId(), cmd.getLBStickinessPolicyName(), cmd.getStickinessMethodName(), cmd.getparamList(), cmd.getDescription());
List<LbStickinessPolicy> policyList = new ArrayList<LbStickinessPolicy>();
policyList.add(new LbStickinessPolicy(cmd.getStickinessMethodName(), lbpolicy.getParams()));
Ip sourceIp = getSourceIp(loadBalancer);
LoadBalancingRule lbRule = new LoadBalancingRule(loadBalancer, getExistingDestinations(lbpolicy.getId()), policyList, null, sourceIp, null, loadBalancer.getLbProtocol());
if (!validateLbRule(lbRule)) {
throw new InvalidParameterValueException("Failed to create Stickiness policy: Validation Failed " + cmd.getLbRuleId());
}
/* Finally Insert into DB */
LBStickinessPolicyVO policy = new LBStickinessPolicyVO(loadBalancer.getId(), cmd.getLBStickinessPolicyName(), cmd.getStickinessMethodName(), cmd.getparamList(), cmd.getDescription());
Boolean forDisplay = cmd.getDisplay();
if (forDisplay != null) {
policy.setDisplay(forDisplay);
}
policy = _lb2stickinesspoliciesDao.persist(policy);
return policy;
}
use of com.cloud.network.dao.LoadBalancerVO in project cloudstack by apache.
the class LoadBalancingRulesManagerImpl method applyLBStickinessPolicy.
@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_LB_STICKINESSPOLICY_CREATE, eventDescription = "Apply Stickinesspolicy to load balancer ", async = true)
public boolean applyLBStickinessPolicy(CreateLBStickinessPolicyCmd cmd) {
boolean success = true;
FirewallRule.State backupState = null;
long oldStickinessPolicyId = 0;
LoadBalancerVO loadBalancer = _lbDao.findById(cmd.getLbRuleId());
if (loadBalancer == null) {
throw new InvalidParameterException("Invalid Load balancer Id:" + cmd.getLbRuleId());
}
_accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true, loadBalancer);
List<LBStickinessPolicyVO> stickinessPolicies = _lb2stickinesspoliciesDao.listByLoadBalancerId(cmd.getLbRuleId(), false);
for (LBStickinessPolicyVO stickinessPolicy : stickinessPolicies) {
if (stickinessPolicy.getId() == cmd.getEntityId()) {
backupState = loadBalancer.getState();
loadBalancer.setState(FirewallRule.State.Add);
_lbDao.persist(loadBalancer);
} else {
oldStickinessPolicyId = stickinessPolicy.getId();
stickinessPolicy.setRevoke(true);
_lb2stickinesspoliciesDao.persist(stickinessPolicy);
}
}
try {
applyLoadBalancerConfig(cmd.getLbRuleId());
} catch (ResourceUnavailableException e) {
s_logger.warn("Unable to apply Stickiness policy to the lb rule: " + cmd.getLbRuleId() + " because resource is unavaliable:", e);
if (isRollBackAllowedForProvider(loadBalancer)) {
loadBalancer.setState(backupState);
_lbDao.persist(loadBalancer);
deleteLBStickinessPolicy(cmd.getEntityId(), false);
s_logger.debug("LB Rollback rule id: " + loadBalancer.getId() + " lb state rolback while creating sticky policy");
} else {
deleteLBStickinessPolicy(cmd.getEntityId(), false);
if (oldStickinessPolicyId != 0) {
LBStickinessPolicyVO stickinessPolicy = _lb2stickinesspoliciesDao.findById(oldStickinessPolicyId);
stickinessPolicy.setRevoke(false);
_lb2stickinesspoliciesDao.persist(stickinessPolicy);
try {
if (backupState.equals(FirewallRule.State.Active))
applyLoadBalancerConfig(cmd.getLbRuleId());
} catch (ResourceUnavailableException e1) {
s_logger.info("[ignored] applying load balancer config.", e1);
} finally {
loadBalancer.setState(backupState);
_lbDao.persist(loadBalancer);
}
}
}
success = false;
}
return success;
}
use of com.cloud.network.dao.LoadBalancerVO in project cloudstack by apache.
the class GlobalLoadBalancingRulesServiceImpl method removeFromGlobalLoadBalancerRule.
@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_REMOVE_FROM_GLOBAL_LOAD_BALANCER_RULE, eventDescription = "Removing a load balancer rule to be part of global load balancer rule")
public boolean removeFromGlobalLoadBalancerRule(RemoveFromGlobalLoadBalancerRuleCmd removeFromGslbCmd) {
CallContext ctx = CallContext.current();
Account caller = ctx.getCallingAccount();
final long gslbRuleId = removeFromGslbCmd.getGlobalLoadBalancerRuleId();
final GlobalLoadBalancerRuleVO gslbRule = _gslbRuleDao.findById(gslbRuleId);
if (gslbRule == null) {
throw new InvalidParameterValueException("Invalid global load balancer rule id: " + gslbRuleId);
}
_accountMgr.checkAccess(caller, SecurityChecker.AccessType.OperateEntry, true, gslbRule);
if (gslbRule.getState() == GlobalLoadBalancerRule.State.Revoke) {
throw new InvalidParameterValueException("global load balancer rule id: " + gslbRuleId + " is already in revoked state");
}
final List<Long> lbRuleIdsToremove = removeFromGslbCmd.getLoadBalancerRulesIds();
if (lbRuleIdsToremove == null || lbRuleIdsToremove.isEmpty()) {
throw new InvalidParameterValueException("empty list of load balancer rule Ids specified to be un-assigned" + " to global load balancer rule");
}
// get the active list of LB rule id's that are assigned currently to GSLB rule and corresponding zone id's
List<Long> oldLbRuleIds = new ArrayList<Long>();
List<Long> oldZones = new ArrayList<Long>();
List<GlobalLoadBalancerLbRuleMapVO> gslbLbMapVos = _gslbLbMapDao.listByGslbRuleId(gslbRuleId);
if (gslbLbMapVos == null) {
throw new InvalidParameterValueException(" There are no load balancer rules that are assigned to global " + " load balancer rule id: " + gslbRule.getUuid() + " that are available for deletion");
}
for (Long lbRuleId : lbRuleIdsToremove) {
LoadBalancerVO loadBalancer = _lbDao.findById(lbRuleId);
if (loadBalancer == null) {
throw new InvalidParameterValueException("Specified load balancer rule ID does not exist.");
}
_accountMgr.checkAccess(caller, null, true, loadBalancer);
}
for (GlobalLoadBalancerLbRuleMapVO gslbLbMapVo : gslbLbMapVos) {
LoadBalancerVO loadBalancer = _lbDao.findById(gslbLbMapVo.getLoadBalancerId());
Network network = _networkDao.findById(loadBalancer.getNetworkId());
oldLbRuleIds.add(gslbLbMapVo.getLoadBalancerId());
oldZones.add(network.getDataCenterId());
}
for (Long lbRuleId : lbRuleIdsToremove) {
LoadBalancerVO loadBalancer = _lbDao.findById(lbRuleId);
if (oldLbRuleIds != null && !oldLbRuleIds.contains(loadBalancer.getId())) {
throw new InvalidParameterValueException("Load balancer ID " + loadBalancer.getUuid() + " is not assigned" + " to global load balancer rule: " + gslbRule.getUuid());
}
}
Transaction.execute(new TransactionCallbackNoReturn() {
@Override
public void doInTransactionWithoutResult(TransactionStatus status) {
// update the mapping of gslb rule to Lb rule, to revoke state
for (Long lbRuleId : lbRuleIdsToremove) {
GlobalLoadBalancerLbRuleMapVO removeGslbLbMap = _gslbLbMapDao.findByGslbRuleIdAndLbRuleId(gslbRuleId, lbRuleId);
removeGslbLbMap.setRevoke(true);
_gslbLbMapDao.update(removeGslbLbMap.getId(), removeGslbLbMap);
}
// mark the gslb rule state as add
if (gslbRule.getState() == GlobalLoadBalancerRule.State.Staged) {
gslbRule.setState(GlobalLoadBalancerRule.State.Add);
_gslbRuleDao.update(gslbRule.getId(), gslbRule);
}
}
});
boolean success = false;
try {
s_logger.debug("Attempting to configure global load balancer rule configuration on the gslb service providers ");
// apply the gslb rule on to the back end gslb service providers
if (!applyGlobalLoadBalancerRuleConfig(gslbRuleId, false)) {
s_logger.warn("Failed to remove load balancer rules " + lbRuleIdsToremove + " from global load balancer rule id " + gslbRuleId);
CloudRuntimeException ex = new CloudRuntimeException("Failed to remove load balancer rule ids from GSLB rule ");
throw ex;
}
Transaction.execute(new TransactionCallbackNoReturn() {
@Override
public void doInTransactionWithoutResult(TransactionStatus status) {
// remove the mappings of gslb rule to Lb rule that are in revoked state
for (Long lbRuleId : lbRuleIdsToremove) {
GlobalLoadBalancerLbRuleMapVO removeGslbLbMap = _gslbLbMapDao.findByGslbRuleIdAndLbRuleId(gslbRuleId, lbRuleId);
_gslbLbMapDao.remove(removeGslbLbMap.getId());
}
// on success set state back to Active
gslbRule.setState(GlobalLoadBalancerRule.State.Active);
_gslbRuleDao.update(gslbRule.getId(), gslbRule);
}
});
success = true;
} catch (ResourceUnavailableException e) {
throw new CloudRuntimeException("Failed to update removed load balancer details from gloabal load balancer");
}
return success;
}
Aggregations