use of com.cloud.agent.api.to.NicTO in project cloudstack by apache.
the class MockVmManagerImpl method startVM.
public String startVM(final String vmName, final NicTO[] nics, final int cpuHz, final long ramSize, final String bootArgs, final String hostGuid) {
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
MockHost host = null;
MockVm vm = null;
try {
txn.start();
host = _mockHostDao.findByGuid(hostGuid);
if (host == null) {
return "can't find host";
}
vm = _mockVmDao.findByVmName(vmName);
txn.commit();
} catch (final Exception ex) {
txn.rollback();
throw new CloudRuntimeException("Unable to start VM " + vmName, ex);
} finally {
txn.close();
txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
txn.close();
}
if (vm == null) {
final int vncPort = 0;
if (vncPort < 0) {
return "Unable to allocate VNC port";
}
vm = new MockVMVO();
vm.setCpu(cpuHz);
vm.setMemory(ramSize);
vm.setPowerState(PowerState.PowerOn);
vm.setName(vmName);
vm.setVncPort(vncPort);
vm.setHostId(host.getId());
vm.setBootargs(bootArgs);
if (vmName.startsWith("s-")) {
vm.setType("SecondaryStorageVm");
} else if (vmName.startsWith("v-")) {
vm.setType("ConsoleProxy");
} else if (vmName.startsWith("r-")) {
vm.setType("DomainRouter");
} else if (vmName.startsWith("i-")) {
vm.setType("User");
}
txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
try {
txn.start();
vm = _mockVmDao.persist((MockVMVO) vm);
txn.commit();
} catch (final Exception ex) {
txn.rollback();
throw new CloudRuntimeException("unable to save vm to db " + vm.getName(), ex);
} finally {
txn.close();
txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
txn.close();
}
} else {
if (vm.getPowerState() == PowerState.PowerOff) {
vm.setPowerState(PowerState.PowerOn);
txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
try {
txn.start();
_mockVmDao.update(vm.getId(), (MockVMVO) vm);
txn.commit();
} catch (final Exception ex) {
txn.rollback();
throw new CloudRuntimeException("unable to update vm " + vm.getName(), ex);
} finally {
txn.close();
txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
txn.close();
}
}
}
if (vm.getPowerState() == PowerState.PowerOn && vmName.startsWith("s-")) {
String prvIp = null;
String prvMac = null;
String prvNetMask = null;
for (final NicTO nic : nics) {
if (nic.getType() == TrafficType.Management) {
prvIp = nic.getIp();
prvMac = nic.getMac();
prvNetMask = nic.getNetmask();
}
}
long dcId = 0;
long podId = 0;
String name = null;
String vmType = null;
String url = null;
final String[] args = bootArgs.trim().split(" ");
for (final String arg : args) {
final String[] params = arg.split("=");
if (params.length < 1) {
continue;
}
if (params[0].equalsIgnoreCase("zone")) {
dcId = Long.parseLong(params[1]);
} else if (params[0].equalsIgnoreCase("name")) {
name = params[1];
} else if (params[0].equalsIgnoreCase("type")) {
vmType = params[1];
} else if (params[0].equalsIgnoreCase("url")) {
url = params[1];
} else if (params[0].equalsIgnoreCase("pod")) {
podId = Long.parseLong(params[1]);
}
}
_mockAgentMgr.handleSystemVMStart(vm.getId(), prvIp, prvMac, prvNetMask, dcId, podId, name, vmType, url);
}
return null;
}
use of com.cloud.agent.api.to.NicTO in project cloudstack by apache.
the class NotAValidCommand method testUnPlugNicCommand.
@Test
public void testUnPlugNicCommand() {
final NicTO nicTO = Mockito.mock(NicTO.class);
final Connection conn = Mockito.mock(Connection.class);
final UnPlugNicCommand unplugNic = new UnPlugNicCommand(nicTO, "Test");
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
assertNotNull(wrapper);
when(citrixResourceBase.getConnection()).thenReturn(conn);
final Answer answer = wrapper.execute(unplugNic, citrixResourceBase);
verify(citrixResourceBase, times(1)).getConnection();
assertFalse(answer.getResult());
}
use of com.cloud.agent.api.to.NicTO in project cloudstack by apache.
the class LibvirtComputingResourceTest method testPlugNicCommandInternalError.
@SuppressWarnings("unchecked")
@Test
public void testPlugNicCommandInternalError() {
final NicTO nic = Mockito.mock(NicTO.class);
final String instanceName = "Test";
final Type vmtype = Type.DomainRouter;
final PlugNicCommand command = new PlugNicCommand(nic, instanceName, vmtype);
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
final Connect conn = Mockito.mock(Connect.class);
final Domain vm = Mockito.mock(Domain.class);
final VifDriver vifDriver = Mockito.mock(VifDriver.class);
final List<InterfaceDef> nics = new ArrayList<InterfaceDef>();
final InterfaceDef intDef = Mockito.mock(InterfaceDef.class);
nics.add(intDef);
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
when(libvirtComputingResource.getInterfaces(conn, command.getVmName())).thenReturn(nics);
when(intDef.getDevName()).thenReturn("eth0");
when(intDef.getBrName()).thenReturn("br0");
when(intDef.getMacAddress()).thenReturn("00:00:00:00");
when(nic.getMac()).thenReturn("00:00:00:01");
try {
when(libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName())).thenReturn(conn);
when(libvirtComputingResource.getDomain(conn, instanceName)).thenReturn(vm);
when(libvirtComputingResource.getVifDriver(nic.getType())).thenReturn(vifDriver);
when(vifDriver.plug(nic, "Other PV", "")).thenThrow(InternalErrorException.class);
} catch (final LibvirtException e) {
fail(e.getMessage());
} catch (final InternalErrorException e) {
fail(e.getMessage());
}
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, libvirtComputingResource);
assertFalse(answer.getResult());
verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
try {
verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(command.getVmName());
verify(libvirtComputingResource, times(1)).getDomain(conn, instanceName);
verify(libvirtComputingResource, times(1)).getVifDriver(nic.getType());
verify(vifDriver, times(1)).plug(nic, "Other PV", "");
} catch (final LibvirtException e) {
fail(e.getMessage());
} catch (final InternalErrorException e) {
fail(e.getMessage());
}
}
use of com.cloud.agent.api.to.NicTO 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);
}
use of com.cloud.agent.api.to.NicTO in project cloudstack by apache.
the class NetworkOrchestrator method toNicTO.
protected NicTO toNicTO(final NicVO nic, final NicProfile profile, final NetworkVO config) {
final NicTO to = new NicTO();
to.setDeviceId(nic.getDeviceId());
to.setBroadcastType(config.getBroadcastDomainType());
to.setType(config.getTrafficType());
to.setIp(nic.getIPv4Address());
to.setNetmask(nic.getIPv4Netmask());
to.setMac(nic.getMacAddress());
to.setDns1(profile.getIPv4Dns1());
to.setDns2(profile.getIPv4Dns2());
if (nic.getIPv4Gateway() != null) {
to.setGateway(nic.getIPv4Gateway());
} else {
to.setGateway(config.getGateway());
}
if (nic.getVmType() != VirtualMachine.Type.User) {
to.setPxeDisable(true);
}
to.setDefaultNic(nic.isDefaultNic());
to.setBroadcastUri(nic.getBroadcastUri());
to.setIsolationuri(nic.getIsolationUri());
if (profile != null) {
to.setDns1(profile.getIPv4Dns1());
to.setDns2(profile.getIPv4Dns2());
}
final Integer networkRate = _networkModel.getNetworkRate(config.getId(), null);
to.setNetworkRateMbps(networkRate);
to.setUuid(config.getUuid());
return to;
}
Aggregations