Search in sources :

Example 1 with NetScalerImplementNetworkCommand

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;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) InsufficientNetworkCapacityException(com.cloud.exception.InsufficientNetworkCapacityException) Answer(com.cloud.agent.api.Answer) SetStaticNatRulesAnswer(com.cloud.agent.api.routing.SetStaticNatRulesAnswer) HealthCheckLBConfigAnswer(com.cloud.agent.api.routing.HealthCheckLBConfigAnswer) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) NetScalerImplementNetworkCommand(com.cloud.agent.api.NetScalerImplementNetworkCommand) HostVO(com.cloud.host.HostVO)

Aggregations

Answer (com.cloud.agent.api.Answer)1 NetScalerImplementNetworkCommand (com.cloud.agent.api.NetScalerImplementNetworkCommand)1 HealthCheckLBConfigAnswer (com.cloud.agent.api.routing.HealthCheckLBConfigAnswer)1 SetStaticNatRulesAnswer (com.cloud.agent.api.routing.SetStaticNatRulesAnswer)1 DataCenterVO (com.cloud.dc.DataCenterVO)1 InsufficientNetworkCapacityException (com.cloud.exception.InsufficientNetworkCapacityException)1 HostVO (com.cloud.host.HostVO)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1