use of com.cloud.agent.api.to.IpAddressTO in project cloudstack by apache.
the class VmwareResource method prepareNetworkElementCommand.
private ExecutionResult prepareNetworkElementCommand(IpAssocCommand cmd) {
VmwareContext context = getServiceContext();
try {
VmwareHypervisorHost hyperHost = getHyperHost(context);
IpAddressTO[] ips = cmd.getIpAddresses();
String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
String controlIp = VmwareResource.getRouterSshControlIp(cmd);
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(routerName);
// the check and will try to find it within cluster
if (vmMo == null) {
if (hyperHost instanceof HostMO) {
ClusterMO clusterMo = new ClusterMO(hyperHost.getContext(), ((HostMO) hyperHost).getParentMor());
vmMo = clusterMo.findVmOnHyperHost(routerName);
}
}
if (vmMo == null) {
String msg = "Router " + routerName + " no longer exists to execute IPAssoc command";
s_logger.error(msg);
throw new Exception(msg);
}
for (IpAddressTO ip : ips) {
/**
* TODO support other networks
*/
URI broadcastUri = BroadcastDomainType.fromString(ip.getBroadcastUri());
if (BroadcastDomainType.getSchemeValue(broadcastUri) != BroadcastDomainType.Vlan) {
throw new InternalErrorException("Unable to assign a public IP to a VIF on network " + ip.getBroadcastUri());
}
String vlanId = BroadcastDomainType.getValue(broadcastUri);
String publicNeworkName = HypervisorHostHelper.getPublicNetworkNamePrefix(vlanId);
Pair<Integer, VirtualDevice> publicNicInfo = vmMo.getNicDeviceIndex(publicNeworkName);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Find public NIC index, public network name: " + publicNeworkName + ", index: " + publicNicInfo.first());
}
boolean addVif = false;
if (ip.isAdd() && publicNicInfo.first().intValue() == -1) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Plug new NIC to associate" + controlIp + " to " + ip.getPublicIp());
}
addVif = true;
}
if (addVif) {
plugPublicNic(vmMo, vlanId, ip.getVifMacAddress());
publicNicInfo = vmMo.getNicDeviceIndex(publicNeworkName);
if (publicNicInfo.first().intValue() >= 0) {
networkUsage(controlIp, "addVif", "eth" + publicNicInfo.first());
}
}
if (publicNicInfo.first().intValue() < 0) {
String msg = "Failed to find DomR VIF to associate/disassociate IP with.";
s_logger.error(msg);
throw new InternalErrorException(msg);
}
ip.setNicDevId(publicNicInfo.first().intValue());
ip.setNewNic(addVif);
}
} catch (Throwable e) {
s_logger.error("Unexpected exception: " + e.toString() + " will shortcut rest of IPAssoc commands", e);
return new ExecutionResult(false, e.toString());
}
return new ExecutionResult(true, null);
}
use of com.cloud.agent.api.to.IpAddressTO in project cloudstack by apache.
the class VmwareResource method prepareNetworkElementCommand.
private ExecutionResult prepareNetworkElementCommand(IpAssocVpcCommand cmd) {
String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
String routerIp = getRouterSshControlIp(cmd);
try {
IpAddressTO[] ips = cmd.getIpAddresses();
for (IpAddressTO ip : ips) {
int ethDeviceNum = findRouterEthDeviceIndex(routerName, routerIp, ip.getVifMacAddress());
if (ethDeviceNum < 0) {
if (ip.isAdd()) {
throw new InternalErrorException("Failed to find DomR VIF to associate/disassociate IP with.");
} else {
s_logger.debug("VIF to deassociate IP with does not exist, return success");
continue;
}
}
ip.setNicDevId(ethDeviceNum);
}
} catch (Exception e) {
s_logger.error("Prepare Ip Assoc failure on applying one ip due to exception: ", e);
return new ExecutionResult(false, e.toString());
}
return new ExecutionResult(true, null);
}
use of com.cloud.agent.api.to.IpAddressTO in project cloudstack by apache.
the class ConfigHelperTest method generateIpAssocVpcCommand.
protected IpAssocVpcCommand generateIpAssocVpcCommand() {
final List<IpAddressTO> ips = new ArrayList<IpAddressTO>();
IpAddressTO ip1 = new IpAddressTO(1, "64.1.1.10", true, true, true, "vlan://64", "64.1.1.1", "255.255.255.0", "01:23:45:67:89:AB", 1000, false);
IpAddressTO ip2 = new IpAddressTO(2, "64.1.1.11", false, false, true, "vlan://64", "64.1.1.1", "255.255.255.0", "01:23:45:67:89:AB", 1000, false);
IpAddressTO ip3 = new IpAddressTO(3, "65.1.1.11", true, false, false, "vlan://65", "65.1.1.1", "255.255.255.0", "11:23:45:67:89:AB", 1000, false);
ip1.setTrafficType(TrafficType.Public);
ip2.setTrafficType(TrafficType.Public);
ip3.setTrafficType(TrafficType.Public);
ips.add(ip1);
ips.add(ip2);
ips.add(ip3);
final IpAddressTO[] ipArray = ips.toArray(new IpAddressTO[ips.size()]);
final IpAssocVpcCommand cmd = new IpAssocVpcCommand(ipArray);
cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, ROUTERNAME);
// AnswersCount is clearly wrong as it doesn't know enough to tell
assertEquals(6, cmd.getAnswersCount());
return cmd;
}
use of com.cloud.agent.api.to.IpAddressTO in project cloudstack by apache.
the class VirtualRoutingResourceTest method generateIpAssocCommand.
protected IpAssocCommand generateIpAssocCommand() {
final List<IpAddressTO> ips = new ArrayList<>();
ips.add(new IpAddressTO(1, "64.1.1.10", true, true, true, "vlan://64", "64.1.1.1", "255.255.255.0", "01:23:45:67:89:AB", 1000, false));
ips.add(new IpAddressTO(2, "64.1.1.11", false, false, false, "vlan://64", "64.1.1.1", "255.255.255.0", "01:23:45:67:89:AB", 1000, false));
ips.add(new IpAddressTO(3, "65.1.1.11", true, false, false, "vlan://65", "65.1.1.1", "255.255.255.0", "11:23:45:67:89:AB", 1000, false));
final IpAddressTO[] ipArray = ips.toArray(new IpAddressTO[ips.size()]);
final IpAssocCommand cmd = new IpAssocCommand(ipArray);
cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, ROUTERNAME);
assertEquals(cmd.getAnswersCount(), 3);
return cmd;
}
use of com.cloud.agent.api.to.IpAddressTO in project cloudstack by apache.
the class CitrixResourceBase method cleanupNetworkElementCommand.
protected ExecutionResult cleanupNetworkElementCommand(final IpAssocCommand cmd) {
final Connection conn = getConnection();
final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
final String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
final String lastIp = cmd.getAccessDetail(NetworkElementCommand.NETWORK_PUB_LAST_IP);
try {
final IpAddressTO[] ips = cmd.getIpAddresses();
final int ipsCount = ips.length;
for (final IpAddressTO ip : ips) {
final VM router = getVM(conn, routerName);
final NicTO nic = new NicTO();
nic.setMac(ip.getVifMacAddress());
nic.setType(ip.getTrafficType());
if (ip.getBroadcastUri() == null) {
nic.setBroadcastType(BroadcastDomainType.Native);
} else {
final URI uri = BroadcastDomainType.fromString(ip.getBroadcastUri());
nic.setBroadcastType(BroadcastDomainType.getSchemeValue(uri));
nic.setBroadcastUri(uri);
}
nic.setDeviceId(0);
nic.setNetworkRateMbps(ip.getNetworkRate());
nic.setName(ip.getNetworkName());
Network network = getNetwork(conn, nic);
// If we are disassociating the last IP address in the VLAN, we
// need
// to remove a VIF
boolean removeVif = false;
// remove the nic
if (org.apache.commons.lang.StringUtils.equalsIgnoreCase(lastIp, "true") && !ip.isAdd()) {
final VIF correctVif = getCorrectVif(conn, router, network);
// in isolated network eth2 is the default public interface. We don't want to delete it.
if (correctVif != null && !correctVif.getDevice(conn).equals("2")) {
removeVif = true;
}
}
if (removeVif) {
// Determine the correct VIF on DomR to
// associate/disassociate the
// IP address with
final VIF correctVif = getCorrectVif(conn, router, network);
if (correctVif != null) {
network = correctVif.getNetwork(conn);
// Mark this vif to be removed from network usage
networkUsage(conn, routerIp, "deleteVif", "eth" + correctVif.getDevice(conn));
// Remove the VIF from DomR
correctVif.unplug(conn);
correctVif.destroy(conn);
// Disable the VLAN network if necessary
disableVlanNetwork(conn, network);
}
}
}
} catch (final Exception e) {
s_logger.debug("Ip Assoc failure on applying one ip due to exception: ", e);
return new ExecutionResult(false, e.getMessage());
}
return new ExecutionResult(true, null);
}
Aggregations