Search in sources :

Example 6 with InterfaceDef

use of com.cloud.agent.resource.kvm.xml.LibvirtVmDef.InterfaceDef in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testUnPlugNicCommandNoNics.

@Test
public void testUnPlugNicCommandNoNics() {
    final NicTO nic = Mockito.mock(NicTO.class);
    final String instanceName = "Test";
    final UnPlugNicCommand command = new UnPlugNicCommand(nic, instanceName);
    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 VifDriver vifDriver = Mockito.mock(VifDriver.class);
    final List<VifDriver> drivers = new ArrayList<>();
    drivers.add(vifDriver);
    when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    when(this.libvirtComputingResource.getInterfaces(conn, command.getVmName())).thenReturn(nics);
    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) VifDriver(com.cloud.agent.resource.kvm.vif.VifDriver) UnPlugNicCommand(com.cloud.legacymodel.communication.command.UnPlugNicCommand) 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) Domain(org.libvirt.Domain) NicTO(com.cloud.legacymodel.to.NicTO) Test(org.junit.Test)

Example 7 with InterfaceDef

use of com.cloud.agent.resource.kvm.xml.LibvirtVmDef.InterfaceDef 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 8 with InterfaceDef

use of com.cloud.agent.resource.kvm.xml.LibvirtVmDef.InterfaceDef 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 9 with InterfaceDef

use of com.cloud.agent.resource.kvm.xml.LibvirtVmDef.InterfaceDef in project cosmic by MissionCriticalCloud.

the class OvsVifDriver method plug.

@Override
public InterfaceDef plug(final NicTO nic, final String guestOsType, final String nicAdapter) throws InternalErrorException, LibvirtException {
    this.logger.debug("plugging nic=" + nic);
    final LibvirtVmDef.InterfaceDef intf = new LibvirtVmDef.InterfaceDef();
    intf.setVirtualPortType("openvswitch");
    String vlanId = null;
    String logicalSwitchUuid = null;
    if (nic.getBroadcastType() == BroadcastDomainType.Vlan) {
        vlanId = BroadcastDomainType.getValue(nic.getBroadcastUri());
    } else if (nic.getBroadcastType() == BroadcastDomainType.Lswitch) {
        logicalSwitchUuid = BroadcastDomainType.getValue(nic.getBroadcastUri());
    } else if (nic.getBroadcastType() == BroadcastDomainType.Pvlan) {
        // TODO consider moving some of this functionality from NetUtils to Networks....
        vlanId = NetUtils.getPrimaryPvlanFromUri(nic.getBroadcastUri());
    }
    final String trafficLabel = nic.getName();
    if (nic.getType() == TrafficType.Guest) {
        final Integer networkRateKBps = nic.getNetworkRateMbps() != null && nic.getNetworkRateMbps().intValue() != -1 ? nic.getNetworkRateMbps().intValue() * 128 : 0;
        if ((nic.getBroadcastType() == BroadcastDomainType.Vlan || nic.getBroadcastType() == BroadcastDomainType.Pvlan) && !vlanId.equalsIgnoreCase("untagged")) {
            if (trafficLabel != null && !trafficLabel.isEmpty()) {
                this.logger.debug("creating a vlan dev and bridge for guest traffic per traffic label " + trafficLabel);
                intf.defBridgeNet(this.pifs.get(trafficLabel), null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter), networkRateKBps);
                intf.setVlanTag(Integer.parseInt(vlanId));
            } else {
                intf.defBridgeNet(this.pifs.get("private"), null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter), networkRateKBps);
                intf.setVlanTag(Integer.parseInt(vlanId));
            }
        } else if (nic.getBroadcastType() == BroadcastDomainType.Lswitch) {
            this.logger.debug("nic " + nic + " needs to be connected to LogicalSwitch " + logicalSwitchUuid);
            intf.setVirtualPortInterfaceId(nic.getUuid());
            final String brName = trafficLabel != null && !trafficLabel.isEmpty() ? this.pifs.get(trafficLabel) : this.pifs.get("private");
            intf.defBridgeNet(brName, null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter), networkRateKBps);
        } else if (nic.getBroadcastType() == BroadcastDomainType.Vswitch) {
            final String vnetId = BroadcastDomainType.getValue(nic.getBroadcastUri());
            final String brName = "OVSTunnel" + vnetId;
            this.logger.debug("nic " + nic + " needs to be connected to LogicalSwitch " + brName);
            intf.defBridgeNet(brName, null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter), networkRateKBps);
        } else if (nic.getBroadcastType() == BroadcastDomainType.Vsp) {
            intf.setVirtualPortInterfaceId(nic.getUuid());
            final String brName = trafficLabel != null && !trafficLabel.isEmpty() ? this.pifs.get(trafficLabel) : this.pifs.get("private");
            intf.defBridgeNet(brName, null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter), networkRateKBps);
        } else {
            intf.defBridgeNet(this.bridges.get("guest"), null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter), networkRateKBps);
        }
    } else if (nic.getType() == TrafficType.Control) {
        /* Make sure the network is still there */
        createControlNetwork(this.bridges.get("linklocal"));
        intf.defBridgeNet(this.bridges.get("linklocal"), null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter));
    } else if (nic.getType() == TrafficType.Public) {
        final Integer networkRateKBps = nic.getNetworkRateMbps() != null && nic.getNetworkRateMbps().intValue() != -1 ? nic.getNetworkRateMbps().intValue() * 128 : 0;
        if (nic.getBroadcastType() == BroadcastDomainType.Vlan && !vlanId.equalsIgnoreCase("untagged")) {
            if (trafficLabel != null && !trafficLabel.isEmpty()) {
                this.logger.debug("creating a vlan dev and bridge for public traffic per traffic label " + trafficLabel);
                intf.defBridgeNet(this.pifs.get(trafficLabel), null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter), networkRateKBps);
                intf.setVlanTag(Integer.parseInt(vlanId));
            } else {
                intf.defBridgeNet(this.pifs.get("public"), null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter), networkRateKBps);
                intf.setVlanTag(Integer.parseInt(vlanId));
            }
        } else {
            intf.defBridgeNet(this.bridges.get("public"), null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter), networkRateKBps);
        }
    } else if (nic.getType() == TrafficType.Management) {
        intf.defBridgeNet(this.bridges.get("private"), null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter));
    } else if (nic.getType() == TrafficType.Storage) {
        final String storageBrName = nic.getName() == null ? this.bridges.get("private") : nic.getName();
        intf.defBridgeNet(storageBrName, null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter));
    }
    return intf;
}
Also used : LibvirtVmDef(com.cloud.agent.resource.kvm.xml.LibvirtVmDef) InterfaceDef(com.cloud.agent.resource.kvm.xml.LibvirtVmDef.InterfaceDef) InterfaceDef(com.cloud.agent.resource.kvm.xml.LibvirtVmDef.InterfaceDef)

Example 10 with InterfaceDef

use of com.cloud.agent.resource.kvm.xml.LibvirtVmDef.InterfaceDef in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testPvlanSetupCommandDhcpAdd.

@Test
public void testPvlanSetupCommandDhcpAdd() {
    final String op = "add";
    final URI uri = URI.create("http://localhost");
    final String networkTag = "/105";
    final String dhcpName = "dhcp";
    final String dhcpMac = "00:00:00:00";
    final String dhcpIp = "127.0.0.1";
    final PvlanSetupCommand command = PvlanSetupCommand.createDhcpSetup(op, uri, networkTag, dhcpName, dhcpMac, dhcpIp);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final Connect conn = Mockito.mock(Connect.class);
    final String guestBridgeName = "br0";
    when(this.libvirtComputingResource.getGuestBridgeName()).thenReturn(guestBridgeName);
    final int timeout = 0;
    when(this.libvirtComputingResource.getScriptsTimeout()).thenReturn(timeout);
    final String ovsPvlanDhcpHostPath = "/pvlan";
    when(this.libvirtComputingResource.getOvsPvlanDhcpHostPath()).thenReturn(ovsPvlanDhcpHostPath);
    when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    final List<InterfaceDef> ifaces = new ArrayList<>();
    final InterfaceDef nic = Mockito.mock(InterfaceDef.class);
    ifaces.add(nic);
    try {
        when(libvirtUtilitiesHelper.getConnectionByVmName(dhcpName)).thenReturn(conn);
        when(this.libvirtComputingResource.getInterfaces(conn, dhcpName)).thenReturn(ifaces);
    } catch (final LibvirtException 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(dhcpName);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
}
Also used : 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) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ArrayList(java.util.ArrayList) PvlanSetupCommand(com.cloud.legacymodel.communication.command.PvlanSetupCommand) URI(java.net.URI) LibvirtUtilitiesHelper(com.cloud.agent.resource.kvm.wrapper.LibvirtUtilitiesHelper) Test(org.junit.Test)

Aggregations

InterfaceDef (com.cloud.agent.resource.kvm.xml.LibvirtVmDef.InterfaceDef)14 Connect (org.libvirt.Connect)9 Test (org.junit.Test)8 Domain (org.libvirt.Domain)8 LibvirtException (org.libvirt.LibvirtException)8 LibvirtRequestWrapper (com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper)7 LibvirtUtilitiesHelper (com.cloud.agent.resource.kvm.wrapper.LibvirtUtilitiesHelper)7 Answer (com.cloud.legacymodel.communication.answer.Answer)7 AttachAnswer (com.cloud.legacymodel.communication.answer.AttachAnswer)7 CheckRouterAnswer (com.cloud.legacymodel.communication.answer.CheckRouterAnswer)7 ArrayList (java.util.ArrayList)7 UnPlugNicCommand (com.cloud.legacymodel.communication.command.UnPlugNicCommand)5 NicTO (com.cloud.legacymodel.to.NicTO)5 VifDriver (com.cloud.agent.resource.kvm.vif.VifDriver)4 LibvirtDiskDef (com.cloud.agent.resource.kvm.xml.LibvirtDiskDef)4 PlugNicCommand (com.cloud.legacymodel.communication.command.PlugNicCommand)3 InternalErrorException (com.cloud.legacymodel.exceptions.InternalErrorException)3 VirtualMachineType (com.cloud.model.enumeration.VirtualMachineType)3 RngDef (com.cloud.agent.resource.kvm.xml.LibvirtVmDef.RngDef)2 WatchDogDef (com.cloud.agent.resource.kvm.xml.LibvirtVmDef.WatchDogDef)2