boolean sourceNat, String vlanId, String vlanGateway, String vlanNetmask, String vifMacAddress, Integer networkRate, TrafficType trafficType, String name) throws InternalErrorException {
try {
VM router = getVM(conn, vmName);
NicTO nic = new NicTO();
nic.setMac(vifMacAddress);
nic.setType(trafficType);
if (vlanId == null) {
nic.setBroadcastType(BroadcastDomainType.Native);
} else {
nic.setBroadcastType(BroadcastDomainType.Vlan);
nic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(vlanId));
}
nic.setDeviceId(0);
nic.setNetworkRateMbps(networkRate);
nic.setName(name);
Network network = getNetwork(conn, nic);
// Determine the correct VIF on DomR to associate/disassociate the
// IP address with
VIF correctVif = getCorrectVif(conn, router, network);
// If we are associating an IP address and DomR doesn't have a VIF
// for the specified vlan ID, we need to add a VIF
// If we are disassociating the last IP address in the VLAN, we need
// to remove a VIF
boolean addVif = false;
boolean removeVif = false;
if (add && correctVif == null) {
addVif = true;
}
if (!add && correctVif == null) {
return; // it is a disassociateIp and it has already happened
}
if (addVif) {
// Add a new VIF to DomR
String vifDeviceNum = getLowestAvailableVIFDeviceNum(conn, router);
if (vifDeviceNum == null) {
throw new InternalErrorException("There were no more available slots for a new VIF on router: " + router.getNameLabel(conn));
}
nic.setDeviceId(Integer.parseInt(vifDeviceNum));
correctVif = createVif(conn, vmName, router, nic);
correctVif.plug(conn);
// Add iptables rule for network usage
networkUsage(conn, privateIpAddress, "addVif", "eth" + correctVif.getDevice(conn));