Search in sources :

Example 76 with NicTO

use of com.cloud.agent.api.to.NicTO in project cloudstack by apache.

the class LibvirtPrepareForMigrationCommandWrapper method execute.

@Override
public Answer execute(final PrepareForMigrationCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final VirtualMachineTO vm = command.getVirtualMachine();
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Preparing host for migrating " + vm);
    }
    final NicTO[] nics = vm.getNics();
    boolean skipDisconnect = false;
    final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vm.getName());
        for (final NicTO nic : nics) {
            libvirtComputingResource.getVifDriver(nic.getType()).plug(nic, null, "");
        }
        /* setup disks, e.g for iso */
        final DiskTO[] volumes = vm.getDisks();
        for (final DiskTO volume : volumes) {
            if (volume.getType() == Volume.Type.ISO) {
                libvirtComputingResource.getVolumePath(conn, volume);
            }
        }
        skipDisconnect = true;
        if (!storagePoolMgr.connectPhysicalDisksViaVmSpec(vm)) {
            return new PrepareForMigrationAnswer(command, "failed to connect physical disks to host");
        }
        return new PrepareForMigrationAnswer(command);
    } catch (final LibvirtException e) {
        return new PrepareForMigrationAnswer(command, e.toString());
    } catch (final InternalErrorException e) {
        return new PrepareForMigrationAnswer(command, e.toString());
    } catch (final URISyntaxException e) {
        return new PrepareForMigrationAnswer(command, e.toString());
    } finally {
        if (!skipDisconnect) {
            storagePoolMgr.disconnectPhysicalDisksViaVmSpec(vm);
        }
    }
}
Also used : KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) InternalErrorException(com.cloud.exception.InternalErrorException) URISyntaxException(java.net.URISyntaxException) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) PrepareForMigrationAnswer(com.cloud.agent.api.PrepareForMigrationAnswer) NicTO(com.cloud.agent.api.to.NicTO) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 77 with NicTO

use of com.cloud.agent.api.to.NicTO in project cloudstack by apache.

the class CitrixResourceBase method prepareNetworkElementCommand.

protected ExecutionResult prepareNetworkElementCommand(final SetNetworkACLCommand cmd) {
    final Connection conn = getConnection();
    final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
    try {
        final VM router = getVM(conn, routerName);
        final NicTO nic = cmd.getNic();
        if (nic != null) {
            final VIF vif = getVifByMac(conn, router, nic.getMac());
            if (vif == null) {
                final String msg = "Prepare SetNetworkACL failed due to VIF is null for : " + nic.getMac() + " with routername: " + routerName;
                s_logger.error(msg);
                return new ExecutionResult(false, msg);
            }
            nic.setDeviceId(Integer.parseInt(vif.getDevice(conn)));
        } else {
            final String msg = "Prepare SetNetworkACL failed due to nic is null for : " + routerName;
            s_logger.error(msg);
            return new ExecutionResult(false, msg);
        }
    } catch (final Exception e) {
        final String msg = "Prepare SetNetworkACL failed due to " + e.toString();
        s_logger.error(msg, e);
        return new ExecutionResult(false, msg);
    }
    return new ExecutionResult(true, null);
}
Also used : VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM) Connection(com.xensource.xenapi.Connection) URLConnection(java.net.URLConnection) ExecutionResult(com.cloud.utils.ExecutionResult) 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) NicTO(com.cloud.agent.api.to.NicTO)

Example 78 with NicTO

use of com.cloud.agent.api.to.NicTO in project cloudstack by apache.

the class CitrixResourceBase method prepareNetworkElementCommand.

protected ExecutionResult prepareNetworkElementCommand(final IpAssocCommand cmd) {
    final Connection conn = getConnection();
    final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
    final String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
    try {
        final IpAddressTO[] ips = cmd.getIpAddresses();
        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());
            final 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;
            if (ip.isAdd() && correctVif == null) {
                addVif = true;
            }
            if (addVif) {
                // Add a new VIF to DomR
                final 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, routerName, router, null, nic);
                correctVif.plug(conn);
                // Add iptables rule for network usage
                networkUsage(conn, routerIp, "addVif", "eth" + correctVif.getDevice(conn));
            }
            if (ip.isAdd() && correctVif == null) {
                throw new InternalErrorException("Failed to find DomR VIF to associate/disassociate IP with.");
            }
            if (correctVif != null) {
                ip.setNicDevId(Integer.valueOf(correctVif.getDevice(conn)));
                ip.setNewNic(addVif);
            }
        }
    } catch (final InternalErrorException e) {
        s_logger.error("Ip Assoc failure on applying one ip due to exception:  ", e);
        return new ExecutionResult(false, e.getMessage());
    } catch (final 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) InternalErrorException(com.cloud.exception.InternalErrorException) 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 79 with NicTO

use of com.cloud.agent.api.to.NicTO in project cloudstack by apache.

the class XenServer610WrapperTest method testMigrateWithStorageReceiveCommand.

@Test
public void testMigrateWithStorageReceiveCommand() {
    final String vmName = "small";
    final String uuid = "206b21a7-c6ec-40e2-b5e2-f861b9612f04";
    final Connection conn = Mockito.mock(Connection.class);
    final VirtualMachineTO vmSpec = Mockito.mock(VirtualMachineTO.class);
    final VolumeTO vol1 = Mockito.mock(VolumeTO.class);
    final VolumeTO vol2 = Mockito.mock(VolumeTO.class);
    final StorageFilerTO storage1 = Mockito.mock(StorageFilerTO.class);
    final StorageFilerTO storage2 = Mockito.mock(StorageFilerTO.class);
    final List<Pair<VolumeTO, String>> volumeToFiler = new ArrayList<>();
    volumeToFiler.add(new Pair<>(vol1, storage1.getPath()));
    volumeToFiler.add(new Pair<>(vol2, storage2.getPath()));
    final NicTO nicTO1 = Mockito.mock(NicTO.class);
    final NicTO nicTO2 = Mockito.mock(NicTO.class);
    final NicTO nicTO3 = Mockito.mock(NicTO.class);
    final NicTO[] nicTOs = { nicTO1, nicTO2, nicTO3 };
    final XsLocalNetwork nativeNetworkForTraffic = Mockito.mock(XsLocalNetwork.class);
    final Network network = Mockito.mock(Network.class);
    final XsHost xsHost = Mockito.mock(XsHost.class);
    final Network nw1 = Mockito.mock(Network.class);
    final Network nw2 = Mockito.mock(Network.class);
    final Network nw3 = Mockito.mock(Network.class);
    final SR sr1 = Mockito.mock(SR.class);
    final SR sr2 = Mockito.mock(SR.class);
    final MigrateWithStorageReceiveCommand migrateStorageCommand = new MigrateWithStorageReceiveCommand(vmSpec, volumeToFiler);
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    when(xenServer610Resource.getConnection()).thenReturn(conn);
    when(vmSpec.getName()).thenReturn(vmName);
    when(vmSpec.getNics()).thenReturn(nicTOs);
    when(storage1.getUuid()).thenReturn(uuid);
    when(storage2.getUuid()).thenReturn(uuid);
    when(xenServer610Resource.getStorageRepository(conn, storage1.getUuid())).thenReturn(sr1);
    when(xenServer610Resource.getStorageRepository(conn, storage2.getUuid())).thenReturn(sr2);
    try {
        when(xenServer610Resource.getNetwork(conn, nicTO1)).thenReturn(nw1);
        when(xenServer610Resource.getNetwork(conn, nicTO2)).thenReturn(nw2);
        when(xenServer610Resource.getNetwork(conn, nicTO3)).thenReturn(nw3);
        when(xenServer610Resource.getNativeNetworkForTraffic(conn, TrafficType.Storage, null)).thenReturn(nativeNetworkForTraffic);
        when(nativeNetworkForTraffic.getNetwork()).thenReturn(network);
        when(xenServer610Resource.getHost()).thenReturn(xsHost);
        when(xsHost.getUuid()).thenReturn(uuid);
    } catch (final XenAPIException e) {
        fail(e.getMessage());
    } catch (final XmlRpcException e) {
        fail(e.getMessage());
    }
    final Answer answer = wrapper.execute(migrateStorageCommand, xenServer610Resource);
    verify(xenServer610Resource, times(1)).getConnection();
    try {
        verify(xenServer610Resource, times(1)).getNetwork(conn, nicTO1);
        verify(xenServer610Resource, times(1)).getNetwork(conn, nicTO2);
        verify(xenServer610Resource, times(1)).getNetwork(conn, nicTO3);
        verify(xenServer610Resource, times(1)).getNativeNetworkForTraffic(conn, TrafficType.Storage, null);
        verify(nativeNetworkForTraffic, times(1)).getNetwork();
        verify(xenServer610Resource, times(1)).getHost();
        verify(xsHost, times(1)).getUuid();
    } catch (final XenAPIException e) {
        fail(e.getMessage());
    } catch (final XmlRpcException e) {
        fail(e.getMessage());
    }
    assertFalse(answer.getResult());
}
Also used : XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) Connection(com.xensource.xenapi.Connection) ArrayList(java.util.ArrayList) XenAPIException(com.xensource.xenapi.Types.XenAPIException) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) MigrateWithStorageReceiveCommand(com.cloud.agent.api.MigrateWithStorageReceiveCommand) Answer(com.cloud.agent.api.Answer) VolumeTO(com.cloud.agent.api.to.VolumeTO) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) Network(com.xensource.xenapi.Network) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) XmlRpcException(org.apache.xmlrpc.XmlRpcException) Pair(com.cloud.utils.Pair) NicTO(com.cloud.agent.api.to.NicTO) SR(com.xensource.xenapi.SR) Test(org.junit.Test)

Example 80 with NicTO

use of com.cloud.agent.api.to.NicTO in project cloudstack by apache.

the class XenServer610WrapperTest method testMigrateWithStorageSendCommandNetException.

@Test
public void testMigrateWithStorageSendCommandNetException() {
    final String vmName = "small";
    final String path = "/";
    final Connection conn = Mockito.mock(Connection.class);
    final VirtualMachineTO vmSpec = Mockito.mock(VirtualMachineTO.class);
    final VolumeTO volume1 = Mockito.mock(VolumeTO.class);
    final VolumeTO volume2 = Mockito.mock(VolumeTO.class);
    final SR sr1 = Mockito.mock(SR.class);
    final SR sr2 = Mockito.mock(SR.class);
    final VDI vdi1 = Mockito.mock(VDI.class);
    final VDI vdi2 = Mockito.mock(VDI.class);
    final NicTO nic1 = Mockito.mock(NicTO.class);
    final NicTO nic2 = Mockito.mock(NicTO.class);
    Gson gson = new Gson();
    final List<Pair<VolumeTO, Object>> volumeToSr = new ArrayList<Pair<VolumeTO, Object>>();
    volumeToSr.add(new Pair<VolumeTO, Object>(volume1, sr1));
    volumeToSr.add(new Pair<VolumeTO, Object>(volume2, sr2));
    final List<Pair<NicTO, Object>> nicToNetwork = new ArrayList<Pair<NicTO, Object>>();
    nicToNetwork.add(new Pair<NicTO, Object>(nic1, new String("a")));
    nicToNetwork.add(new Pair<NicTO, Object>(nic2, new String("b")));
    final Map<String, String> token = new HashMap<String, String>();
    final MigrateWithStorageSendCommand migrateStorageCommand = new MigrateWithStorageSendCommand(vmSpec, volumeToSr, nicToNetwork, token);
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    when(xenServer610Resource.getConnection()).thenReturn(conn);
    when(vmSpec.getName()).thenReturn(vmName);
    when(volume1.getPath()).thenReturn(path);
    when(volume2.getPath()).thenReturn(path);
    when(xenServer610Resource.getVDIbyUuid(conn, volume1.getPath())).thenReturn(vdi1);
    when(xenServer610Resource.getVDIbyUuid(conn, volume2.getPath())).thenReturn(vdi2);
    final Answer answer = wrapper.execute(migrateStorageCommand, xenServer610Resource);
    verify(xenServer610Resource, times(1)).getConnection();
    assertFalse(answer.getResult());
}
Also used : HashMap(java.util.HashMap) Connection(com.xensource.xenapi.Connection) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) Answer(com.cloud.agent.api.Answer) VolumeTO(com.cloud.agent.api.to.VolumeTO) VDI(com.xensource.xenapi.VDI) MigrateWithStorageSendCommand(com.cloud.agent.api.MigrateWithStorageSendCommand) SR(com.xensource.xenapi.SR) NicTO(com.cloud.agent.api.to.NicTO) Pair(com.cloud.utils.Pair) Test(org.junit.Test)

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