Search in sources :

Example 31 with NicTO

use of com.cloud.legacymodel.to.NicTO in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testStartCommandIsolationEc2.

@Test
public void testStartCommandIsolationEc2() {
    final VirtualMachineTO vmSpec = Mockito.mock(VirtualMachineTO.class);
    final Host host = Mockito.mock(Host.class);
    final boolean executeInSequence = false;
    final StartCommand command = new StartCommand(vmSpec, host, executeInSequence);
    final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final Connect conn = Mockito.mock(Connect.class);
    final LibvirtVmDef vmDef = Mockito.mock(LibvirtVmDef.class);
    final VirtualRoutingResource virtRouterResource = Mockito.mock(VirtualRoutingResource.class);
    final NicTO nic = Mockito.mock(NicTO.class);
    final NicTO[] nics = new NicTO[] { nic };
    final int[] vms = new int[0];
    final String vmName = "Test";
    final String controlIp = "127.0.0.1";
    when(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
    when(vmSpec.getNics()).thenReturn(nics);
    when(vmSpec.getType()).thenReturn(VirtualMachineType.DomainRouter);
    when(vmSpec.getName()).thenReturn(vmName);
    when(this.libvirtComputingResource.createVmFromSpec(vmSpec)).thenReturn(vmDef);
    when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    try {
        when(libvirtUtilitiesHelper.getConnectionByType(vmDef.getHvsType())).thenReturn(conn);
        when(conn.listDomains()).thenReturn(vms);
        doNothing().when(this.libvirtComputingResource).createVbd(conn, vmSpec, vmName, vmDef);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    } catch (final InternalErrorException e) {
        fail(e.getMessage());
    } catch (final URISyntaxException e) {
        fail(e.getMessage());
    }
    when(storagePoolMgr.connectPhysicalDisksViaVmSpec(vmSpec)).thenReturn(true);
    try {
        doNothing().when(this.libvirtComputingResource).createVifs(vmSpec, vmDef);
        when(this.libvirtComputingResource.startVm(conn, vmName, vmDef.toString())).thenReturn("SUCCESS");
        when(nic.getIsolationUri()).thenReturn(new URI("ec2://test"));
        when(vmSpec.getBootArgs()).thenReturn("ls -lart");
        when(this.libvirtComputingResource.passCmdLine(vmName, vmSpec.getBootArgs())).thenReturn(true);
        when(nic.getIp()).thenReturn(controlIp);
        when(nic.getType()).thenReturn(TrafficType.Control);
        when(this.libvirtComputingResource.getVirtRouterResource()).thenReturn(virtRouterResource);
        when(virtRouterResource.connect(controlIp, 30, 5000)).thenReturn(true);
    } catch (final InternalErrorException e) {
        fail(e.getMessage());
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    } catch (final URISyntaxException 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)).getStoragePoolMgr();
    verify(this.libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
    try {
        verify(libvirtUtilitiesHelper, times(1)).getConnectionByType(vmDef.getHvsType());
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
}
Also used : LibvirtVmDef(com.cloud.agent.resource.kvm.xml.LibvirtVmDef) LibvirtException(org.libvirt.LibvirtException) StartCommand(com.cloud.legacymodel.communication.command.StartCommand) Connect(org.libvirt.Connect) VirtualRoutingResource(com.cloud.common.virtualnetwork.VirtualRoutingResource) Host(com.cloud.legacymodel.dc.Host) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) VirtualMachineTO(com.cloud.legacymodel.to.VirtualMachineTO) LibvirtUtilitiesHelper(com.cloud.agent.resource.kvm.wrapper.LibvirtUtilitiesHelper) 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) KvmStoragePoolManager(com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager) NicTO(com.cloud.legacymodel.to.NicTO) Test(org.junit.Test)

Example 32 with NicTO

use of com.cloud.legacymodel.to.NicTO in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testStartCommandLibvirtException.

@Test
public void testStartCommandLibvirtException() {
    final VirtualMachineTO vmSpec = Mockito.mock(VirtualMachineTO.class);
    final Host host = Mockito.mock(Host.class);
    final boolean executeInSequence = false;
    final StartCommand command = new StartCommand(vmSpec, host, executeInSequence);
    final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final LibvirtVmDef vmDef = Mockito.mock(LibvirtVmDef.class);
    final NicTO nic = Mockito.mock(NicTO.class);
    final NicTO[] nics = new NicTO[] { nic };
    final String vmName = "Test";
    when(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
    when(vmSpec.getNics()).thenReturn(nics);
    when(vmSpec.getType()).thenReturn(VirtualMachineType.DomainRouter);
    when(vmSpec.getName()).thenReturn(vmName);
    when(this.libvirtComputingResource.createVmFromSpec(vmSpec)).thenReturn(vmDef);
    when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    try {
        when(libvirtUtilitiesHelper.getConnectionByType(vmDef.getHvsType())).thenThrow(LibvirtException.class);
    } 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)).getStoragePoolMgr();
    verify(this.libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
    try {
        verify(libvirtUtilitiesHelper, times(1)).getConnectionByType(vmDef.getHvsType());
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
}
Also used : LibvirtVmDef(com.cloud.agent.resource.kvm.xml.LibvirtVmDef) LibvirtException(org.libvirt.LibvirtException) StartCommand(com.cloud.legacymodel.communication.command.StartCommand) Host(com.cloud.legacymodel.dc.Host) VirtualMachineTO(com.cloud.legacymodel.to.VirtualMachineTO) LibvirtUtilitiesHelper(com.cloud.agent.resource.kvm.wrapper.LibvirtUtilitiesHelper) 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) KvmStoragePoolManager(com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager) NicTO(com.cloud.legacymodel.to.NicTO) Test(org.junit.Test)

Example 33 with NicTO

use of com.cloud.legacymodel.to.NicTO in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testStartCommandFailedConnect.

@Test
public void testStartCommandFailedConnect() {
    final VirtualMachineTO vmSpec = Mockito.mock(VirtualMachineTO.class);
    final Host host = Mockito.mock(Host.class);
    final boolean executeInSequence = false;
    final StartCommand command = new StartCommand(vmSpec, host, executeInSequence);
    final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final Connect conn = Mockito.mock(Connect.class);
    final LibvirtVmDef vmDef = Mockito.mock(LibvirtVmDef.class);
    final NicTO nic = Mockito.mock(NicTO.class);
    final NicTO[] nics = new NicTO[] { nic };
    final String vmName = "Test";
    when(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
    when(vmSpec.getNics()).thenReturn(nics);
    when(vmSpec.getType()).thenReturn(VirtualMachineType.DomainRouter);
    when(vmSpec.getName()).thenReturn(vmName);
    when(this.libvirtComputingResource.createVmFromSpec(vmSpec)).thenReturn(vmDef);
    when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    try {
        when(libvirtUtilitiesHelper.getConnectionByType(vmDef.getHvsType())).thenReturn(conn);
        doNothing().when(this.libvirtComputingResource).createVbd(conn, vmSpec, vmName, vmDef);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    } catch (final InternalErrorException e) {
        fail(e.getMessage());
    } catch (final URISyntaxException e) {
        fail(e.getMessage());
    }
    when(storagePoolMgr.connectPhysicalDisksViaVmSpec(vmSpec)).thenReturn(false);
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
    assertFalse(answer.getResult());
    verify(this.libvirtComputingResource, times(1)).getStoragePoolMgr();
    verify(this.libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
    try {
        verify(libvirtUtilitiesHelper, times(1)).getConnectionByType(vmDef.getHvsType());
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
}
Also used : LibvirtVmDef(com.cloud.agent.resource.kvm.xml.LibvirtVmDef) LibvirtException(org.libvirt.LibvirtException) StartCommand(com.cloud.legacymodel.communication.command.StartCommand) Connect(org.libvirt.Connect) Host(com.cloud.legacymodel.dc.Host) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) URISyntaxException(java.net.URISyntaxException) VirtualMachineTO(com.cloud.legacymodel.to.VirtualMachineTO) LibvirtUtilitiesHelper(com.cloud.agent.resource.kvm.wrapper.LibvirtUtilitiesHelper) 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) KvmStoragePoolManager(com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager) NicTO(com.cloud.legacymodel.to.NicTO) Test(org.junit.Test)

Example 34 with NicTO

use of com.cloud.legacymodel.to.NicTO in project cosmic by MissionCriticalCloud.

the class SetNetworkAclConfigItem method generateConfig.

@Override
public List<ConfigItem> generateConfig(final NetworkElementCommand cmd) {
    final SetNetworkACLCommand command = (SetNetworkACLCommand) cmd;
    final String privateGw = cmd.getAccessDetail(NetworkElementCommand.VPC_PRIVATE_GATEWAY);
    final String[][] rules = generateFwRules(command);
    final String[] aclRules = rules[0];
    final NicTO nic = command.getNic();
    final String netmask = Long.toString(NetUtils.getCidrSize(nic.getNetmask()));
    final List<AclRule> ingressRules = new ArrayList<>();
    final List<AclRule> egressRules = new ArrayList<>();
    for (final String aclRule1 : aclRules) {
        final AclRule aclRule;
        final String[] ruleParts = aclRule1.split(":");
        switch(ruleParts[1].toLowerCase()) {
            case "icmp":
                aclRule = new IcmpAclRule(ruleParts[4], "ACCEPT".equals(ruleParts[5]), Integer.parseInt(ruleParts[2]), Integer.parseInt(ruleParts[3]));
                break;
            case "tcp":
                aclRule = new TcpAclRule(ruleParts[4], "ACCEPT".equals(ruleParts[5]), Integer.parseInt(ruleParts[2]), Integer.parseInt(ruleParts[3]));
                break;
            case "udp":
                aclRule = new UdpAclRule(ruleParts[4], "ACCEPT".equals(ruleParts[5]), Integer.parseInt(ruleParts[2]), Integer.parseInt(ruleParts[3]));
                break;
            case "all":
                aclRule = new AllAclRule(ruleParts[4], "ACCEPT".equals(ruleParts[5]));
                break;
            default:
                // So, let's catch the exception and continue in the loop.
                try {
                    aclRule = new ProtocolAclRule(ruleParts[4], "ACCEPT".equals(ruleParts[5]), Integer.parseInt(ruleParts[1]));
                } catch (final Exception e) {
                    s_logger.warn("Unable to read ACL rule definition, string format is different than expected. Original message => " + e.getMessage());
                    continue;
                }
        }
        if ("Ingress".equals(ruleParts[0])) {
            ingressRules.add(aclRule);
        } else {
            egressRules.add(aclRule);
        }
    }
    final NetworkACL networkACL = new NetworkACL(nic.getMac(), privateGw != null, nic.getIp(), netmask, ingressRules.toArray(new AclRule[ingressRules.size()]), egressRules.toArray(new AclRule[egressRules.size()]));
    return generateConfigItems(networkACL);
}
Also used : AllAclRule(com.cloud.legacymodel.network.rules.AllAclRule) ArrayList(java.util.ArrayList) SetNetworkACLCommand(com.cloud.legacymodel.communication.command.SetNetworkACLCommand) IcmpAclRule(com.cloud.legacymodel.network.rules.IcmpAclRule) NetworkACL(com.cloud.legacymodel.network.rules.NetworkACL) ProtocolAclRule(com.cloud.legacymodel.network.rules.ProtocolAclRule) TcpAclRule(com.cloud.legacymodel.network.rules.TcpAclRule) UdpAclRule(com.cloud.legacymodel.network.rules.UdpAclRule) IcmpAclRule(com.cloud.legacymodel.network.rules.IcmpAclRule) UdpAclRule(com.cloud.legacymodel.network.rules.UdpAclRule) AclRule(com.cloud.legacymodel.network.rules.AclRule) AllAclRule(com.cloud.legacymodel.network.rules.AllAclRule) ProtocolAclRule(com.cloud.legacymodel.network.rules.ProtocolAclRule) TcpAclRule(com.cloud.legacymodel.network.rules.TcpAclRule) NicTO(com.cloud.legacymodel.to.NicTO)

Example 35 with NicTO

use of com.cloud.legacymodel.to.NicTO in project cosmic by MissionCriticalCloud.

the class SetPublicIpAclConfigItem method generateConfig.

@Override
public List<ConfigItem> generateConfig(final NetworkElementCommand cmd) {
    final SetPublicIpACLCommand command = (SetPublicIpACLCommand) cmd;
    final String[][] rules = command.generateFwRules();
    final String[] aclRules = rules[0];
    final NicTO nic = command.getNic();
    final String netmask = Long.toString(NetUtils.getCidrSize(nic.getNetmask()));
    final List<AclRule> ingressRules = new ArrayList<>();
    final List<AclRule> egressRules = new ArrayList<>();
    for (final String aclRule1 : aclRules) {
        final AclRule aclRule;
        final String[] ruleParts = aclRule1.split(":");
        switch(ruleParts[1].toLowerCase()) {
            case "icmp":
                aclRule = new IcmpAclRule(ruleParts[4], "ACCEPT".equals(ruleParts[5]), Integer.parseInt(ruleParts[2]), Integer.parseInt(ruleParts[3]));
                break;
            case "tcp":
                aclRule = new TcpAclRule(ruleParts[4], "ACCEPT".equals(ruleParts[5]), Integer.parseInt(ruleParts[2]), Integer.parseInt(ruleParts[3]));
                break;
            case "udp":
                aclRule = new UdpAclRule(ruleParts[4], "ACCEPT".equals(ruleParts[5]), Integer.parseInt(ruleParts[2]), Integer.parseInt(ruleParts[3]));
                break;
            case "all":
                aclRule = new AllAclRule(ruleParts[4], "ACCEPT".equals(ruleParts[5]));
                break;
            default:
                // So, let's catch the exception and continue in the loop.
                try {
                    aclRule = new ProtocolAclRule(ruleParts[4], "ACCEPT".equals(ruleParts[5]), Integer.parseInt(ruleParts[1]));
                } catch (final Exception e) {
                    s_logger.warn("Unable to read ACL rule definition, string format is different than expected. Original message => " + e.getMessage());
                    continue;
                }
        }
        if ("Ingress".equals(ruleParts[0])) {
            ingressRules.add(aclRule);
        } else {
            egressRules.add(aclRule);
        }
    }
    final PublicIpACL publicIpACL = new PublicIpACL(nic.getMac(), nic.getIp(), netmask, command.getPublicIp(), ingressRules.toArray(new AclRule[ingressRules.size()]), egressRules.toArray(new AclRule[egressRules.size()]));
    return generateConfigItems(publicIpACL);
}
Also used : AllAclRule(com.cloud.legacymodel.network.rules.AllAclRule) ArrayList(java.util.ArrayList) PublicIpACL(com.cloud.legacymodel.network.rules.PublicIpACL) IcmpAclRule(com.cloud.legacymodel.network.rules.IcmpAclRule) ProtocolAclRule(com.cloud.legacymodel.network.rules.ProtocolAclRule) TcpAclRule(com.cloud.legacymodel.network.rules.TcpAclRule) SetPublicIpACLCommand(com.cloud.legacymodel.communication.command.SetPublicIpACLCommand) UdpAclRule(com.cloud.legacymodel.network.rules.UdpAclRule) IcmpAclRule(com.cloud.legacymodel.network.rules.IcmpAclRule) UdpAclRule(com.cloud.legacymodel.network.rules.UdpAclRule) AclRule(com.cloud.legacymodel.network.rules.AclRule) AllAclRule(com.cloud.legacymodel.network.rules.AllAclRule) ProtocolAclRule(com.cloud.legacymodel.network.rules.ProtocolAclRule) TcpAclRule(com.cloud.legacymodel.network.rules.TcpAclRule) NicTO(com.cloud.legacymodel.to.NicTO)

Aggregations

NicTO (com.cloud.legacymodel.to.NicTO)51 VirtualMachineTO (com.cloud.legacymodel.to.VirtualMachineTO)28 Answer (com.cloud.legacymodel.communication.answer.Answer)26 Test (org.junit.Test)25 LibvirtException (org.libvirt.LibvirtException)23 AttachAnswer (com.cloud.legacymodel.communication.answer.AttachAnswer)21 LibvirtRequestWrapper (com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper)19 LibvirtUtilitiesHelper (com.cloud.agent.resource.kvm.wrapper.LibvirtUtilitiesHelper)19 CheckRouterAnswer (com.cloud.legacymodel.communication.answer.CheckRouterAnswer)19 Connect (org.libvirt.Connect)19 ArrayList (java.util.ArrayList)16 KvmStoragePoolManager (com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager)14 Connection (com.xensource.xenapi.Connection)14 InternalErrorException (com.cloud.legacymodel.exceptions.InternalErrorException)11 URISyntaxException (java.net.URISyntaxException)11 VifDriver (com.cloud.agent.resource.kvm.vif.VifDriver)10 LibvirtVmDef (com.cloud.agent.resource.kvm.xml.LibvirtVmDef)10 Host (com.cloud.legacymodel.dc.Host)10 UnPlugNicCommand (com.cloud.legacymodel.communication.command.UnPlugNicCommand)9 HashMap (java.util.HashMap)8