* @param cmd
* @return
*/
private SetupGuestNetworkAnswer execute(SetupGuestNetworkCommand cmd) {
Connection conn = getConnection();
NicTO nic = cmd.getNic();
String domrIP = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
String domrGIP = cmd.getAccessDetail(NetworkElementCommand.ROUTER_GUEST_IP);
String domrName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
String gw = cmd.getAccessDetail(NetworkElementCommand.GUEST_NETWORK_GATEWAY);
String cidr = Long.toString(NetUtils.getCidrSize(nic.getNetmask()));;
String domainName = cmd.getNetworkDomain();
String dns = cmd.getDefaultDns1();
if (dns == null || dns.isEmpty()) {
dns = cmd.getDefaultDns2();
} else {
String dns2= cmd.getDefaultDns2();
if ( dns2 != null && !dns2.isEmpty()) {
dns += "," + dns2;
}
}
try {
Set<VM> vms = VM.getByNameLabel(conn, domrName);
if ( vms == null || vms.isEmpty() ) {
return new SetupGuestNetworkAnswer(cmd, false, "Can not find VM " + domrName);
}
VM vm = vms.iterator().next();
String mac = nic.getMac();
VIF domrVif = null;
for ( VIF vif : vm.getVIFs(conn)) {
String lmac = vif.getMAC(conn);
if ( lmac.equals(mac) ) {
domrVif = vif;
break;
}
}
if ( domrVif == null ) {
return new SetupGuestNetworkAnswer(cmd, false, "Can not find vif with mac " + mac + " for VM " + domrName);
}
String args = "vpc_guestnw.sh " + domrIP + (cmd.isAdd()?" -C":" -D");
String dev = "eth" + domrVif.getDevice(conn);
args += " -d " + dev;
args += " -i " + domrGIP;
args += " -g " + gw;
args += " -m " + cidr;
args += " -n " + NetUtils.getSubNet(domrGIP, nic.getNetmask());
if ( dns != null && !dns.isEmpty() ) {
args += " -s " + dns;
}
if ( domainName != null && !domainName.isEmpty() ) {
args += " -e " + domainName;