Search in sources :

Example 1 with VirtualMachineType

use of com.cloud.model.enumeration.VirtualMachineType in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testPlugNicCommandMatchMack.

@Test
public void testPlugNicCommandMatchMack() {
    final NicTO nic = Mockito.mock(NicTO.class);
    final String instanceName = "Test";
    final VirtualMachineType vmtype = VirtualMachineType.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 List<InterfaceDef> nics = new ArrayList<>();
    final InterfaceDef intDef = Mockito.mock(InterfaceDef.class);
    nics.add(intDef);
    when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    when(this.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:00");
    try {
        when(libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName())).thenReturn(conn);
        when(this.libvirtComputingResource.getDomain(conn, instanceName)).thenReturn(vm);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
    assertTrue(answer.getResult());
    verify(this.libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
    try {
        verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(command.getVmName());
        verify(this.libvirtComputingResource, times(1)).getDomain(conn, instanceName);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ArrayList(java.util.ArrayList) LibvirtUtilitiesHelper(com.cloud.agent.resource.kvm.wrapper.LibvirtUtilitiesHelper) InterfaceDef(com.cloud.agent.resource.kvm.xml.LibvirtVmDef.InterfaceDef) Answer(com.cloud.legacymodel.communication.answer.Answer) CheckRouterAnswer(com.cloud.legacymodel.communication.answer.CheckRouterAnswer) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) LibvirtRequestWrapper(com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper) VirtualMachineType(com.cloud.model.enumeration.VirtualMachineType) Domain(org.libvirt.Domain) PlugNicCommand(com.cloud.legacymodel.communication.command.PlugNicCommand) UnPlugNicCommand(com.cloud.legacymodel.communication.command.UnPlugNicCommand) NicTO(com.cloud.legacymodel.to.NicTO) Test(org.junit.Test)

Example 2 with VirtualMachineType

use of com.cloud.model.enumeration.VirtualMachineType in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testPlugNicCommandInternalError.

@Test
public void testPlugNicCommandInternalError() {
    final NicTO nic = Mockito.mock(NicTO.class);
    final String instanceName = "Test";
    final VirtualMachineType vmtype = VirtualMachineType.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<>();
    final InterfaceDef intDef = Mockito.mock(InterfaceDef.class);
    nics.add(intDef);
    when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    when(this.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(this.libvirtComputingResource.getDomain(conn, instanceName)).thenReturn(vm);
        when(this.libvirtComputingResource.getVifDriver(nic.getType())).thenReturn(vifDriver);
        when(vifDriver.plug(nic, "Default - VirtIO capable OS (64-bit)", "")).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, this.libvirtComputingResource);
    assertFalse(answer.getResult());
    verify(this.libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
    try {
        verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(command.getVmName());
        verify(this.libvirtComputingResource, times(1)).getDomain(conn, instanceName);
        verify(this.libvirtComputingResource, times(1)).getVifDriver(nic.getType());
        verify(vifDriver, times(1)).plug(nic, "Default - VirtIO capable OS (64-bit)", "");
    } 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.legacymodel.exceptions.InternalErrorException) VifDriver(com.cloud.agent.resource.kvm.vif.VifDriver) LibvirtUtilitiesHelper(com.cloud.agent.resource.kvm.wrapper.LibvirtUtilitiesHelper) InterfaceDef(com.cloud.agent.resource.kvm.xml.LibvirtVmDef.InterfaceDef) Answer(com.cloud.legacymodel.communication.answer.Answer) CheckRouterAnswer(com.cloud.legacymodel.communication.answer.CheckRouterAnswer) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) LibvirtRequestWrapper(com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper) VirtualMachineType(com.cloud.model.enumeration.VirtualMachineType) Domain(org.libvirt.Domain) PlugNicCommand(com.cloud.legacymodel.communication.command.PlugNicCommand) UnPlugNicCommand(com.cloud.legacymodel.communication.command.UnPlugNicCommand) NicTO(com.cloud.legacymodel.to.NicTO) Test(org.junit.Test)

Example 3 with VirtualMachineType

use of com.cloud.model.enumeration.VirtualMachineType in project cosmic by MissionCriticalCloud.

the class GuestNetworkGuru method allocate.

@Override
public NicProfile allocate(final Network network, NicProfile nic, final VirtualMachineProfile vm) throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException {
    assert network.getTrafficType() == TrafficType.Guest : "Look at my name!  Why are you calling" + " me when the traffic type is : " + network.getTrafficType();
    if (nic == null) {
        nic = new NicProfile(ReservationStrategy.Start, null, null, null, null);
    }
    final Zone zone = zoneRepository.findById(network.getDataCenterId()).orElse(null);
    if (nic.getIPv4Address() == null && !GuestType.Sync.equals(network.getGuestType())) {
        nic.setBroadcastUri(network.getBroadcastUri());
        nic.setIsolationUri(network.getBroadcastUri());
        nic.setIPv4Gateway(network.getGateway());
        String guestIp;
        if (network.getSpecifyIpRanges()) {
            _ipAddrMgr.allocateDirectIp(nic, zone, vm, network, nic.getRequestedIPv4(), null);
        } else {
            final VirtualMachineType vmtype = vm.getVirtualMachine().getType();
            switch(vmtype) {
                case User:
                    guestIp = assignGuestOrGatewayIp(network, nic, vm, zone);
                    break;
                case DomainRouter:
                    if (_networkModel.isProviderSupportServiceInNetwork(network.getId(), Service.Gateway, Provider.VPCVirtualRouter)) {
                        // Networks that support the Gateway service acquire the gateway ip on their nic
                        guestIp = network.getGateway();
                    } else {
                        // In other cases, acquire an ip address from the DHCP range (take lowest possible)
                        guestIp = _ipAddrMgr.acquireGuestIpAddressForRouter(network, nic.getRequestedIPv4());
                    }
                    break;
                default:
                    // Backwards compatibility
                    guestIp = _ipAddrMgr.acquireGuestIpAddress(network, nic.getRequestedIPv4());
                    break;
            }
            if (guestIp == null) {
                throw new InsufficientVirtualNetworkCapacityException("Unable to acquire Guest IP" + " address for network " + network, DataCenter.class, zone.getId());
            }
            nic.setIPv4Address(guestIp);
            nic.setIPv4Netmask(NetUtils.cidr2Netmask(network.getCidr()));
            if (network.getDns1() != null && network.getDns1().equals("")) {
                nic.setIPv4Dns1(null);
            } else {
                nic.setIPv4Dns1(network.getDns1());
            }
            if (network.getDns2() != null && network.getDns2().equals("")) {
                nic.setIPv4Dns2(null);
            } else {
                nic.setIPv4Dns2(network.getDns2());
            }
            nic.setFormat(IpAddressFormat.Ip4);
        }
    }
    nic.setReservationStrategy(ReservationStrategy.Start);
    if (nic.getMacAddress() == null) {
        nic.setMacAddress(_networkModel.getNextAvailableMacAddressInNetwork(network.getId()));
        if (nic.getMacAddress() == null) {
            throw new InsufficientAddressCapacityException("Unable to allocate more mac addresses", Network.class, network.getId());
        }
    }
    return nic;
}
Also used : Zone(com.cloud.db.model.Zone) InsufficientVirtualNetworkCapacityException(com.cloud.legacymodel.exceptions.InsufficientVirtualNetworkCapacityException) InsufficientAddressCapacityException(com.cloud.legacymodel.exceptions.InsufficientAddressCapacityException) VirtualMachineType(com.cloud.model.enumeration.VirtualMachineType) NicProfile(com.cloud.vm.NicProfile)

Example 4 with VirtualMachineType

use of com.cloud.model.enumeration.VirtualMachineType in project cosmic by MissionCriticalCloud.

the class UserVmDaoImpl method getVmsDetailByNames.

@Override
public List<Pair<Pair<String, VirtualMachineType>, Pair<Long, String>>> getVmsDetailByNames(final Set<String> vmNames, final String detail) {
    final TransactionLegacy txn = TransactionLegacy.currentTxn();
    final List<Pair<Pair<String, VirtualMachineType>, Pair<Long, String>>> vmsDetailByNames = new ArrayList<>();
    try (PreparedStatement pstmt = txn.prepareStatement(VMS_DETAIL_BY_NAME + getQueryBatchAppender(vmNames.size()))) {
        pstmt.setString(1, detail);
        int i = 2;
        for (final String name : vmNames) {
            pstmt.setString(i, name);
            i++;
        }
        try (ResultSet rs = pstmt.executeQuery()) {
            while (rs.next()) {
                vmsDetailByNames.add(new Pair<>(new Pair<>(rs.getString("vm_instance.instance_name"), VirtualMachineType.valueOf(rs.getString("vm_type"))), new Pair<>(rs.getLong("vm_instance.id"), rs.getString("user_vm_details.value"))));
            }
        }
    } catch (final SQLException e) {
        s_logger.error("GetVmsDetailsByNames: Exception in sql: " + e.getMessage());
        throw new CloudRuntimeException("GetVmsDetailsByNames: Exception: " + e.getMessage());
    }
    return vmsDetailByNames;
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) TransactionLegacy(com.cloud.utils.db.TransactionLegacy) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) ResultSet(java.sql.ResultSet) VirtualMachineType(com.cloud.model.enumeration.VirtualMachineType) Pair(com.cloud.legacymodel.utils.Pair)

Example 5 with VirtualMachineType

use of com.cloud.model.enumeration.VirtualMachineType in project cosmic by MissionCriticalCloud.

the class RecreatableFencer method fenceOff.

@Override
public Boolean fenceOff(final VirtualMachine vm, final Host host) {
    final VirtualMachineType type = vm.getType();
    if (type != VirtualMachineType.ConsoleProxy && type != VirtualMachineType.DomainRouter && type != VirtualMachineType.SecondaryStorageVm) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Don't know how to fence off " + type);
        }
        return null;
    }
    final List<VolumeVO> vols = _volsDao.findByInstance(vm.getId());
    for (final VolumeVO vol : vols) {
        if (!vol.isRecreatable()) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Unable to fence off volumes that are not recreatable: " + vol);
            }
            return null;
        }
        if (vol.getPoolType().isShared()) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Unable to fence off volumes that are shared: " + vol);
            }
            return null;
        }
    }
    return true;
}
Also used : VolumeVO(com.cloud.storage.VolumeVO) VirtualMachineType(com.cloud.model.enumeration.VirtualMachineType)

Aggregations

VirtualMachineType (com.cloud.model.enumeration.VirtualMachineType)9 ArrayList (java.util.ArrayList)5 LibvirtRequestWrapper (com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper)4 LibvirtUtilitiesHelper (com.cloud.agent.resource.kvm.wrapper.LibvirtUtilitiesHelper)4 Answer (com.cloud.legacymodel.communication.answer.Answer)4 AttachAnswer (com.cloud.legacymodel.communication.answer.AttachAnswer)4 CheckRouterAnswer (com.cloud.legacymodel.communication.answer.CheckRouterAnswer)4 PlugNicCommand (com.cloud.legacymodel.communication.command.PlugNicCommand)4 UnPlugNicCommand (com.cloud.legacymodel.communication.command.UnPlugNicCommand)4 NicTO (com.cloud.legacymodel.to.NicTO)4 Test (org.junit.Test)4 LibvirtException (org.libvirt.LibvirtException)4 InterfaceDef (com.cloud.agent.resource.kvm.xml.LibvirtVmDef.InterfaceDef)3 Connect (org.libvirt.Connect)3 Domain (org.libvirt.Domain)3 VifDriver (com.cloud.agent.resource.kvm.vif.VifDriver)2 InternalErrorException (com.cloud.legacymodel.exceptions.InternalErrorException)2 Pair (com.cloud.legacymodel.utils.Pair)2 VolumeVO (com.cloud.storage.VolumeVO)2 Zone (com.cloud.db.model.Zone)1