Connection conn = getConnection();
String vmName = cmd.getVmName();
try {
Set<VM> vms = VM.getByNameLabel(conn, vmName);
if ( vms == null || vms.isEmpty() ) {
return new PlugNicAnswer(cmd, false, "Can not find VM " + vmName);
}
VM vm = vms.iterator().next();
NicTO nic = cmd.getNic();
VIF vif = getVifByMac(conn, vm, nic.getMac());
if ( vif != null ) {
String msg = " Plug Nic failed due to a VIF with the same mac " + nic.getMac() + " exists";
s_logger.warn(msg);
return new PlugNicAnswer(cmd, false, msg);
}
String deviceId = getLowestAvailableVIFDeviceNum(conn, vm);
nic.setDeviceId(Integer.parseInt(deviceId));
vif = createVif(conn, vmName, vm, nic);
vif.plug(conn);
return new PlugNicAnswer(cmd, true, "success");
} catch (Exception e) {
String msg = " Plug Nic failed due to " + e.toString();
s_logger.warn(msg, e);
return new PlugNicAnswer(cmd, false, msg);
}
}