use of com.cloud.agent.api.NetScalerImplementNetworkCommand in project cloudstack by apache.
the class NetscalerElement method manageGuestNetworkWithNetscalerControlCenter.
public boolean manageGuestNetworkWithNetscalerControlCenter(boolean add, Network guestConfig, NetworkOffering offering) throws ResourceUnavailableException, InsufficientCapacityException, ConfigurationException {
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 netscalerControlCenter = null;
if (add) {
HostVO lbDeviceVO = null;
// on restart network, device could have been allocated already,
// skip allocation if a device is assigned
lbDeviceVO = getNetScalerControlCenterForNetwork(guestConfig);
if (lbDeviceVO == null) {
// allocate a load balancer device for the network
lbDeviceVO = allocateNCCResourceForNetwork(guestConfig);
if (lbDeviceVO == null) {
String msg = "failed to allocate Netscaler ControlCenter Resource for the zone in the network " + guestConfig.getId();
s_logger.error(msg);
throw new InsufficientNetworkCapacityException(msg, DataCenter.class, guestConfig.getDataCenterId());
}
}
netscalerControlCenter = _hostDao.findById(lbDeviceVO.getId());
s_logger.debug("Allocated Netscaler Control Center device:" + lbDeviceVO.getId() + " for the network: " + guestConfig.getId());
} else {
// find the load balancer device allocated for the network
HostVO lbDeviceVO = null;
// on restart network, device could have been allocated already, skip allocation if a device is assigned
lbDeviceVO = getNetScalerControlCenterForNetwork(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;
}
netscalerControlCenter = _hostDao.findById(lbDeviceVO.getId());
assert (netscalerControlCenter != null) : "There is no device assigned to this network how did shutdown network ended up here??";
}
JSONObject networkDetails = new JSONObject();
JSONObject networkPayload = new JSONObject();
String selfIp = null;
try {
networkDetails.put("id", guestConfig.getId());
networkDetails.put("vlan", guestConfig.getBroadcastUri());
networkDetails.put("cidr", guestConfig.getCidr());
networkDetails.put("gateway", guestConfig.getGateway());
networkDetails.put("servicepackage_id", offering.getServicePackage());
networkDetails.put("zone_id", zone.getUuid());
networkDetails.put("account_id", guestConfig.getAccountId());
networkDetails.put("add", Boolean.toString(add));
selfIp = _ipAddrMgr.acquireGuestIpAddress(guestConfig, null);
if (selfIp == null) {
String msg = "failed to acquire guest IP address so not implementing the network on the NetscalerControlCenter";
s_logger.error(msg);
throw new InsufficientNetworkCapacityException(msg, Network.class, guestConfig.getId());
}
networkDetails.put("snip", selfIp);
// TODO region is hardcoded make it dynamic
networkDetails.put("region_id", 1);
networkDetails.put("name", guestConfig.getName());
networkPayload.put("network", networkDetails);
} catch (JSONException e) {
e.printStackTrace();
}
NetScalerImplementNetworkCommand cmd = new NetScalerImplementNetworkCommand(zone.getUuid(), netscalerControlCenter.getId(), networkPayload.toString());
Answer answer = _agentMgr.easySend(netscalerControlCenter.getId(), cmd);
if (add) {
// TODO After getting the answer check with the job id and do poll on the job and then save the selfip or acquired guest ip to the Nics table
if (answer != null) {
if (answer.getResult() == true) {
if (add) {
// Insert a new NIC for this guest network to reserve the self IP
_networkService.savePlaceholderNic(guestConfig, selfIp, null, null);
}
} else {
return false;
}
}
} else {
if (answer != null) {
if (answer.getResult() == true) {
return true;
} else {
return false;
}
}
return false;
}
return true;
}
Aggregations