Search in sources :

Example 86 with NicTO

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;
}
Also used : MockVMVO(com.cloud.simulator.MockVMVO) TransactionLegacy(com.cloud.utils.db.TransactionLegacy) MockHost(com.cloud.simulator.MockHost) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) MockVm(com.cloud.simulator.MockVm) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NicTO(com.cloud.agent.api.to.NicTO)

Example 87 with NicTO

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());
}
Also used : RebootAnswer(com.cloud.agent.api.RebootAnswer) CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) Connection(com.xensource.xenapi.Connection) NicTO(com.cloud.agent.api.to.NicTO) UnPlugNicCommand(com.cloud.agent.api.UnPlugNicCommand) Test(org.junit.Test)

Example 88 with NicTO

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());
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ArrayList(java.util.ArrayList) InternalErrorException(com.cloud.exception.InternalErrorException) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) TrafficType(com.cloud.network.Networks.TrafficType) Type(com.cloud.vm.VirtualMachine.Type) StoragePoolType(com.cloud.storage.Storage.StoragePoolType) BootloaderType(com.cloud.template.VirtualMachineTemplate.BootloaderType) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) Domain(org.libvirt.Domain) PlugNicCommand(com.cloud.agent.api.PlugNicCommand) UnPlugNicCommand(com.cloud.agent.api.UnPlugNicCommand) NicTO(com.cloud.agent.api.to.NicTO) Test(org.junit.Test)

Example 89 with NicTO

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);
}
Also used : IpAddressTO(com.cloud.agent.api.to.IpAddressTO) Connection(com.xensource.xenapi.Connection) URLConnection(java.net.URLConnection) ExecutionResult(com.cloud.utils.ExecutionResult) URI(java.net.URI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM) Network(com.xensource.xenapi.Network) NicTO(com.cloud.agent.api.to.NicTO)

Example 90 with NicTO

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;
}
Also used : NicTO(com.cloud.agent.api.to.NicTO)

Aggregations

NicTO (com.cloud.agent.api.to.NicTO)99 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)42 Answer (com.cloud.agent.api.Answer)31 Test (org.junit.Test)30 InternalErrorException (com.cloud.exception.InternalErrorException)28 LibvirtException (org.libvirt.LibvirtException)27 ArrayList (java.util.ArrayList)25 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)24 ConfigurationException (javax.naming.ConfigurationException)23 Connect (org.libvirt.Connect)23 URISyntaxException (java.net.URISyntaxException)22 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)21 IOException (java.io.IOException)20 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)19 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)19 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)19 Connection (com.xensource.xenapi.Connection)18 KVMStoragePoolManager (com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager)14 UnPlugNicCommand (com.cloud.agent.api.UnPlugNicCommand)13 URI (java.net.URI)12