use of com.cloud.network.dao.ExternalLoadBalancerDeviceVO in project cloudstack by apache.
the class ExternalLoadBalancerDeviceManagerImpl method manageGuestNetworkWithExternalLoadBalancer.
@Override
public boolean manageGuestNetworkWithExternalLoadBalancer(boolean add, Network guestConfig) throws ResourceUnavailableException, InsufficientCapacityException {
if (guestConfig.getTrafficType() != TrafficType.Guest) {
s_logger.trace("External load balancer can only be used for guest networks.");
return false;
}
long zoneId = guestConfig.getDataCenterId();
DataCenterVO zone = _dcDao.findById(zoneId);
HostVO externalLoadBalancer = null;
if (add) {
ExternalLoadBalancerDeviceVO lbDeviceVO = null;
// on restart network, device could have been allocated already, skip allocation if a device is assigned
lbDeviceVO = getExternalLoadBalancerForNetwork(guestConfig);
if (lbDeviceVO == null) {
// allocate a load balancer device for the network
lbDeviceVO = allocateLoadBalancerForNetwork(guestConfig);
if (lbDeviceVO == null) {
String msg = "failed to alloacate a external load balancer for the network " + guestConfig.getId();
s_logger.error(msg);
throw new InsufficientNetworkCapacityException(msg, DataCenter.class, guestConfig.getDataCenterId());
}
}
externalLoadBalancer = _hostDao.findById(lbDeviceVO.getHostId());
s_logger.debug("Allocated external load balancer device:" + lbDeviceVO.getId() + " for the network: " + guestConfig.getId());
} else {
// find the load balancer device allocated for the network
ExternalLoadBalancerDeviceVO lbDeviceVO = getExternalLoadBalancerForNetwork(guestConfig);
if (lbDeviceVO == null) {
s_logger.warn("Network shutdwon requested on external load balancer element, which did not implement the network." + " Either network implement failed half way through or already network shutdown is completed. So just returning.");
return true;
}
externalLoadBalancer = _hostDao.findById(lbDeviceVO.getHostId());
assert (externalLoadBalancer != null) : "There is no device assigned to this network how did shutdown network ended up here??";
}
// Send a command to the external load balancer to implement or shutdown the guest network
String guestVlanTag = BroadcastDomainType.getValue(guestConfig.getBroadcastUri());
String selfIp = null;
String guestVlanNetmask = NetUtils.cidr2Netmask(guestConfig.getCidr());
Integer networkRate = _networkModel.getNetworkRate(guestConfig.getId(), null);
if (add) {
// on restart network, network could have already been implemented. If already implemented then return
Nic selfipNic = getPlaceholderNic(guestConfig);
if (selfipNic != null) {
return true;
}
// Acquire a self-ip address from the guest network IP address range
selfIp = _ipAddrMgr.acquireGuestIpAddress(guestConfig, null);
if (selfIp == null) {
String msg = "failed to acquire guest IP address so not implementing the network on the external load balancer ";
s_logger.error(msg);
throw new InsufficientNetworkCapacityException(msg, Network.class, guestConfig.getId());
}
} else {
// get the self-ip used by the load balancer
Nic selfipNic = getPlaceholderNic(guestConfig);
if (selfipNic == null) {
s_logger.warn("Network shutdwon requested on external load balancer element, which did not implement the network." + " Either network implement failed half way through or already network shutdown is completed. So just returning.");
return true;
}
selfIp = selfipNic.getIPv4Address();
}
// It's a hack, using isOneToOneNat field for indicate if it's inline or not
boolean inline = _networkMgr.isNetworkInlineMode(guestConfig);
IpAddressTO ip = new IpAddressTO(guestConfig.getAccountId(), null, add, false, true, guestVlanTag, selfIp, guestVlanNetmask, null, networkRate, inline);
IpAddressTO[] ips = new IpAddressTO[1];
ips[0] = ip;
IpAssocCommand cmd = new IpAssocCommand(ips);
Answer answer = _agentMgr.easySend(externalLoadBalancer.getId(), cmd);
if (answer == null || !answer.getResult()) {
String action = add ? "implement" : "shutdown";
String answerDetails = (answer != null) ? answer.getDetails() : null;
answerDetails = (answerDetails != null) ? " due to " + answerDetails : "";
String msg = "External load balancer was unable to " + action + " the guest network on the external load balancer in zone " + zone.getName() + answerDetails;
s_logger.error(msg);
throw new ResourceUnavailableException(msg, Network.class, guestConfig.getId());
}
if (add) {
// Insert a new NIC for this guest network to reserve the self IP
_networkMgr.savePlaceholderNic(guestConfig, selfIp, null, null);
} else {
// release the self-ip obtained from guest network
Nic selfipNic = getPlaceholderNic(guestConfig);
_nicDao.remove(selfipNic.getId());
// release the load balancer allocated for the network
boolean releasedLB = freeLoadBalancerForNetwork(guestConfig);
if (!releasedLB) {
String msg = "Failed to release the external load balancer used for the network: " + guestConfig.getId();
s_logger.error(msg);
}
}
if (s_logger.isDebugEnabled()) {
Account account = _accountDao.findByIdIncludingRemoved(guestConfig.getAccountId());
String action = add ? "implemented" : "shut down";
s_logger.debug("External load balancer has " + action + " the guest network for account " + account.getAccountName() + "(id = " + account.getAccountId() + ") with VLAN tag " + guestVlanTag);
}
return true;
}
use of com.cloud.network.dao.ExternalLoadBalancerDeviceVO in project cloudstack by apache.
the class ExternalDeviceUsageManagerImpl method getExternalLoadBalancerForNetwork.
private ExternalLoadBalancerDeviceVO getExternalLoadBalancerForNetwork(Network network) {
NetworkExternalLoadBalancerVO lbDeviceForNetwork = _networkExternalLBDao.findByNetworkId(network.getId());
if (lbDeviceForNetwork != null) {
long lbDeviceId = lbDeviceForNetwork.getExternalLBDeviceId();
ExternalLoadBalancerDeviceVO lbDeviceVo = _externalLoadBalancerDeviceDao.findById(lbDeviceId);
assert (lbDeviceVo != null);
return lbDeviceVo;
}
return null;
}
use of com.cloud.network.dao.ExternalLoadBalancerDeviceVO in project cloudstack by apache.
the class AddF5LoadBalancerCmd method execute.
// ///////////////////////////////////////////////////
// ///////////// API Implementation///////////////////
// ///////////////////////////////////////////////////
@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException {
try {
ExternalLoadBalancerDeviceVO lbDeviceVO = _f5DeviceManagerService.addF5LoadBalancer(this);
if (lbDeviceVO != null) {
F5LoadBalancerResponse response = _f5DeviceManagerService.createF5LoadBalancerResponse(lbDeviceVO);
response.setObjectName("f5loadbalancer");
response.setResponseName(getCommandName());
this.setResponseObject(response);
} else {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add F5 Big IP load balancer due to internal error.");
}
} catch (InvalidParameterValueException invalidParamExcp) {
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, invalidParamExcp.getMessage());
} catch (CloudRuntimeException runtimeExcp) {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, runtimeExcp.getMessage());
}
}
use of com.cloud.network.dao.ExternalLoadBalancerDeviceVO in project cloudstack by apache.
the class ListF5LoadBalancersCmd method execute.
// ///////////////////////////////////////////////////
// ///////////// API Implementation///////////////////
// ///////////////////////////////////////////////////
@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException {
try {
List<ExternalLoadBalancerDeviceVO> lbDevices = _f5DeviceManagerService.listF5LoadBalancers(this);
ListResponse<F5LoadBalancerResponse> response = new ListResponse<F5LoadBalancerResponse>();
List<F5LoadBalancerResponse> lbDevicesResponse = new ArrayList<F5LoadBalancerResponse>();
if (lbDevices != null && !lbDevices.isEmpty()) {
for (ExternalLoadBalancerDeviceVO lbDeviceVO : lbDevices) {
F5LoadBalancerResponse lbdeviceResponse = _f5DeviceManagerService.createF5LoadBalancerResponse(lbDeviceVO);
lbDevicesResponse.add(lbdeviceResponse);
}
}
response.setResponses(lbDevicesResponse);
response.setResponseName(getCommandName());
this.setResponseObject(response);
} catch (InvalidParameterValueException invalidParamExcp) {
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, invalidParamExcp.getMessage());
} catch (CloudRuntimeException runtimeExcp) {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, runtimeExcp.getMessage());
}
}
use of com.cloud.network.dao.ExternalLoadBalancerDeviceVO in project cloudstack by apache.
the class F5ExternalLoadBalancerElement method listNetworks.
@Override
public List<? extends Network> listNetworks(ListF5LoadBalancerNetworksCmd cmd) {
Long lbDeviceId = cmd.getLoadBalancerDeviceId();
List<NetworkVO> networks = new ArrayList<NetworkVO>();
ExternalLoadBalancerDeviceVO lbDeviceVo = _lbDeviceDao.findById(lbDeviceId);
if (lbDeviceVo == null || !lbDeviceVo.getDeviceName().equalsIgnoreCase(NetworkDevice.F5BigIpLoadBalancer.getName())) {
throw new InvalidParameterValueException("Could not find F5 load balancer device with ID " + lbDeviceId);
}
List<NetworkExternalLoadBalancerVO> networkLbMaps = _networkLBDao.listByLoadBalancerDeviceId(lbDeviceId);
if (networkLbMaps != null && !networkLbMaps.isEmpty()) {
for (NetworkExternalLoadBalancerVO networkLbMap : networkLbMaps) {
NetworkVO network = _networkDao.findById(networkLbMap.getNetworkId());
networks.add(network);
}
}
return networks;
}
Aggregations