use of com.cloud.agent.api.routing.IpAssocVpcCommand in project cloudstack by apache.
the class CommandSetupHelper method createRedundantAssociateIPCommands.
public void createRedundantAssociateIPCommands(final VirtualRouter router, final List<? extends PublicIpAddress> ips, final Commands cmds, final String ipAssocCommand, final boolean isVPC) {
// Ensure that in multiple vlans case we first send all ip addresses of
// vlan1, then all ip addresses of vlan2, etc..
final Map<String, ArrayList<PublicIpAddress>> vlanIpMap = new HashMap<String, ArrayList<PublicIpAddress>>();
for (final PublicIpAddress ipAddress : ips) {
final String vlanTag = ipAddress.getVlanTag();
ArrayList<PublicIpAddress> ipList = vlanIpMap.get(vlanTag);
if (ipList == null) {
ipList = new ArrayList<PublicIpAddress>();
}
// the state
if (ipAddress.isSourceNat() && ipAddress.getState() == IpAddress.State.Releasing) {
ipAddress.setState(IpAddress.State.Allocated);
}
ipList.add(ipAddress);
vlanIpMap.put(vlanTag, ipList);
}
final List<NicVO> nics = _nicDao.listByVmId(router.getId());
String baseMac = null;
Map<String, String> vlanMacAddress = new HashMap<String, String>();
;
Long guestNetworkId = null;
for (final NicVO nic : nics) {
final NetworkVO nw = _networkDao.findById(nic.getNetworkId());
if (nw.getTrafficType() == TrafficType.Public) {
if (baseMac == null) {
baseMac = nic.getMacAddress();
}
final String vlanTag = BroadcastDomainType.getValue(nic.getBroadcastUri());
vlanMacAddress.put(vlanTag, nic.getMacAddress());
} else if (nw.getTrafficType() == TrafficType.Guest && guestNetworkId == null) {
guestNetworkId = nw.getId();
}
}
Map<String, Boolean> vlanLastIpMap = getVlanLastIpMap(router.getVpcId(), guestNetworkId);
for (final Map.Entry<String, ArrayList<PublicIpAddress>> vlanAndIp : vlanIpMap.entrySet()) {
final String vlanTagKey = vlanAndIp.getKey();
final List<PublicIpAddress> ipAddrList = vlanAndIp.getValue();
// Source nat ip address should always be sent first
Collections.sort(ipAddrList, new Comparator<PublicIpAddress>() {
@Override
public int compare(final PublicIpAddress o1, final PublicIpAddress o2) {
final boolean s1 = o1.isSourceNat();
final boolean s2 = o2.isSourceNat();
return s1 ^ s2 ? s1 ^ true ? 1 : -1 : 0;
}
});
// Get network rate - required for IpAssoc
final Integer networkRate = _networkModel.getNetworkRate(ipAddrList.get(0).getNetworkId(), router.getId());
final Network network = _networkModel.getNetwork(ipAddrList.get(0).getNetworkId());
final IpAddressTO[] ipsToSend = new IpAddressTO[ipAddrList.size()];
int i = 0;
boolean firstIP = true;
for (final PublicIpAddress ipAddr : ipAddrList) {
final boolean add = ipAddr.getState() == IpAddress.State.Releasing ? false : true;
boolean sourceNat = ipAddr.isSourceNat();
/* enable sourceNAT for the first ip of the public interface */
if (firstIP) {
sourceNat = true;
}
final String vlanId = ipAddr.getVlanTag();
final String vlanGateway = ipAddr.getGateway();
final String vlanNetmask = ipAddr.getNetmask();
String vifMacAddress = null;
final String vlanTag = BroadcastDomainType.getValue(BroadcastDomainType.fromString(ipAddr.getVlanTag()));
if (vlanMacAddress.containsKey(vlanTag)) {
vifMacAddress = vlanMacAddress.get(vlanTag);
} else {
if (ipAddr.getVlanId() != 0) {
vifMacAddress = NetUtils.generateMacOnIncrease(baseMac, ipAddr.getVlanId());
} else {
vifMacAddress = ipAddr.getMacAddress();
}
vlanMacAddress.put(vlanTag, vifMacAddress);
}
final IpAddressTO ip = new IpAddressTO(ipAddr.getAccountId(), ipAddr.getAddress().addr(), add, firstIP, sourceNat, vlanId, vlanGateway, vlanNetmask, vifMacAddress, networkRate, ipAddr.isOneToOneNat());
setIpAddressNetworkParams(ip, network, router);
if (router.getHypervisorType() == Hypervisor.HypervisorType.VMware) {
Map<String, String> details = new HashMap<>();
String defaultSystemVmNicAdapterType = _configDao.getValue(Config.VmwareSystemVmNicDeviceType.key());
if (defaultSystemVmNicAdapterType == null) {
defaultSystemVmNicAdapterType = Config.VmwareSystemVmNicDeviceType.getDefaultValue();
}
details.put(VmDetailConstants.NIC_ADAPTER, defaultSystemVmNicAdapterType);
ip.setDetails(details);
}
ipsToSend[i++] = ip;
/*
* send the firstIP = true for the first Add, this is to create
* primary on interface
*/
if (!firstIP || add) {
firstIP = false;
}
}
final IpAssocCommand cmd;
if (router.getVpcId() != null) {
cmd = new IpAssocVpcCommand(ipsToSend);
} else {
cmd = new IpAssocCommand(ipsToSend);
}
cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, _routerControlHelper.getRouterControlIp(router.getId()));
cmd.setAccessDetail(NetworkElementCommand.ROUTER_GUEST_IP, _routerControlHelper.getRouterIpInNetwork(ipAddrList.get(0).getNetworkId(), router.getId()));
cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName());
final DataCenterVO dcVo = _dcDao.findById(router.getDataCenterId());
cmd.setAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE, dcVo.getNetworkType().toString());
setAccessDetailNetworkLastPublicIp(vlanLastIpMap, vlanTagKey, cmd);
cmds.addCommand(ipAssocCommand, cmd);
}
}
use of com.cloud.agent.api.routing.IpAssocVpcCommand in project cloudstack by apache.
the class LibvirtNetworkElementCommandWrapperTest method testPrepareIpAssocVpcCommand.
@Test
public void testPrepareIpAssocVpcCommand() throws LibvirtException {
IpAddressTO ip = new IpAddressTO(1, "171.31.1.3", true, false, false, "vlan://untagged", "172.31.1.1", "255.255.0.0", "02:00:7c:98:00:02", 0, true);
ip.setTrafficType(Networks.TrafficType.Public);
IpAddressTO[] ips = new IpAddressTO[] { ip };
final IpAssocVpcCommand command = new IpAssocVpcCommand(ips);
command.setAccessDetail(NetworkElementCommand.ROUTER_IP, "127.0.0.1");
ExecutionResult result = res.prepareCommand(command);
assertEquals(1, ips[0].getNicDevId().intValue());
}
use of com.cloud.agent.api.routing.IpAssocVpcCommand in project cloudstack by apache.
the class Ovm3VirtualRoutingResourceTest method prepareVpcCommandFailHeavierTest.
@Test
public void prepareVpcCommandFailHeavierTest() throws ConfigurationException {
prepare();
con.removeMethodResponse("list_vms");
IpAssocVpcCommand vpc = generateIpAssocVpcCommand(xen.getVmNicMac().replace("0", "F"));
results.basicBooleanTest(hypervisor.executeRequest(vpc).getResult(), false);
}
use of com.cloud.agent.api.routing.IpAssocVpcCommand in project cloudstack by apache.
the class Ovm3VirtualRoutingResourceTest method prepareVpcCommandTest.
@Test
public void prepareVpcCommandTest() throws ConfigurationException {
prepare();
IpAssocVpcCommand vpc = generateIpAssocVpcCommand(xen.getVmNicMac());
results.basicBooleanTest(hypervisor.executeRequest(vpc).getResult());
}
use of com.cloud.agent.api.routing.IpAssocVpcCommand in project cloudstack by apache.
the class Ovm3VirtualRoutingResourceTest method generateIpAssocVpcCommand.
private IpAssocVpcCommand generateIpAssocVpcCommand(String mac) {
IpAssocVpcCommand cmd = new IpAssocVpcCommand(getIp(mac));
cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, xen.getVmName());
cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, routerip);
// wrong as it doesn't know enough to tell
return cmd;
}
Aggregations