summaryrefslogtreecommitdiffstats
path: root/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/virtualization/LibvirtHypervisor.java
blob: 345900abe27a9492aa2958a45d540ee57486ed0e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package org.openslx.runvirt.virtualization;

import java.io.Closeable;

import org.libvirt.Connect;
import org.libvirt.LibvirtException;
import org.openslx.libvirt.capabilities.Capabilities;
import org.openslx.libvirt.xml.LibvirtXmlDocumentException;
import org.openslx.libvirt.xml.LibvirtXmlSerializationException;
import org.openslx.libvirt.xml.LibvirtXmlValidationException;

public abstract class LibvirtHypervisor implements Closeable
{
	protected Connect hypervisor = null;

	public LibvirtHypervisor( String connectionUri ) throws LibvirtHypervisorException
	{
		this.connect( connectionUri );
	}

	protected void connect( String connectionUri ) throws LibvirtHypervisorException
	{
		try {
			this.hypervisor = new Connect( connectionUri );
		} catch ( LibvirtException e ) {
			throw new LibvirtHypervisorException( e.getLocalizedMessage() );
		}
	}

	public Capabilities getCapabilites() throws LibvirtHypervisorException
	{
		Capabilities hypervisorCapabilities = null;

		try {
			final String hypervisorCapabilitiesString = this.hypervisor.getCapabilities();
			hypervisorCapabilities = new Capabilities( hypervisorCapabilitiesString );
		} catch ( LibvirtException | LibvirtXmlDocumentException | LibvirtXmlSerializationException
				| LibvirtXmlValidationException e ) {
			throw new LibvirtHypervisorException( e.getLocalizedMessage() );
		}

		return hypervisorCapabilities;
	}

	public int getVersion() throws LibvirtHypervisorException
	{
		int hypervisorVersion = 0;

		try {
			final long hypervisorVersionLong = this.hypervisor.getVersion();
			hypervisorVersion = Long.valueOf( hypervisorVersionLong ).intValue();
		} catch ( LibvirtException e ) {
			throw new LibvirtHypervisorException( e.getLocalizedMessage() );
		}

		return hypervisorVersion;
	}

	public LibvirtVirtualMachine registerVm( org.openslx.libvirt.domain.Domain vmConfiguration )
			throws LibvirtHypervisorException
	{
		final String xmlVmConfiguration = vmConfiguration.toString();
		org.libvirt.Domain libvirtDomain = null;

		try {
			libvirtDomain = this.hypervisor.domainDefineXML( xmlVmConfiguration );
		} catch ( LibvirtException e ) {
			throw new LibvirtHypervisorException( e.getLocalizedMessage() );
		}

		return new LibvirtVirtualMachine( libvirtDomain );
	}

	public void deregisterVm( LibvirtVirtualMachine vm )
			throws LibvirtHypervisorException, LibvirtVirtualMachineException
	{
		// stop virtual machine if machine is running
		if ( vm.isRunning() ) {
			vm.stop();
		}

		// deregister and remove virtual machine from hypervisor
		try {
			vm.getLibvirtDomain().undefine();
		} catch ( LibvirtException e ) {
			throw new LibvirtHypervisorException( e.getLocalizedMessage() );
		}
	}

	@Override
	public void close()
	{
		try {
			this.hypervisor.close();
		} catch ( LibvirtException e ) {
			e.printStackTrace();
		}
	}
}