summaryrefslogtreecommitdiffstats
path: root/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/virtualization/LibvirtVirtualMachine.java
blob: 32b99d66cad43ab7e74e687ac3df9c9815842295 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package org.openslx.runvirt.virtualization;

import org.libvirt.Domain;
import org.libvirt.LibvirtException;

/**
 * Representation of a Libvirt virtual machine.
 * 
 * @author Manuel Bentele
 * @version 1.0
 */
public class LibvirtVirtualMachine
{
	/**
	 * Libvirt virtualization configuration of the virtual machine.
	 */
	private Domain domain;

	/**
	 * Creates a new Libvirt virtual machine specified by a virtualization configuration.
	 * 
	 * @param vm Libvirt virtualization configuration to specify the Libvirt virtual machine.
	 */
	LibvirtVirtualMachine( Domain vm )
	{
		this.domain = vm;
	}

	/**
	 * Returns the Libvirt virtualization configuration of the Libvirt virtual machine.
	 * 
	 * @return Libvirt virtualization configuration of the Libvirt virtual machine.
	 */
	public Domain getLibvirtDomain()
	{
		return this.domain;
	}

	/**
	 * Checks if the Libvirt virtual machine is running.
	 * 
	 * @return state of the Libvirt virtual machine whether it is running or not.
	 * @throws LibvirtVirtualMachineException failed to check if Libvirt machine is running or not.
	 */
	public boolean isRunning() throws LibvirtVirtualMachineException
	{
		int state = 0;

		try {
			state = this.domain.isActive();
		} catch ( LibvirtException e ) {
			throw new LibvirtVirtualMachineException( e.getLocalizedMessage() );
		}

		return ( state == 0 ) ? false : true;
	}

	/**
	 * Starts the Libvirt virtual machine.
	 * 
	 * @throws LibvirtVirtualMachineException failed to start the Libvirt virtual machine.
	 */
	public void start() throws LibvirtVirtualMachineException
	{
		if ( !this.isRunning() ) {
			try {
				this.domain.create();
			} catch ( LibvirtException e ) {
				throw new LibvirtVirtualMachineException( e.getLocalizedMessage() );
			}
		}
	}

	/**
	 * Stops the Libvirt virtual machine.
	 * 
	 * @throws LibvirtVirtualMachineException failed to stop the Libvirt virtual machine.
	 */
	public void stop() throws LibvirtVirtualMachineException
	{
		if ( this.isRunning() ) {
			try {
				this.domain.shutdown();
			} catch ( LibvirtException e ) {
				throw new LibvirtVirtualMachineException( e.getLocalizedMessage() );
			}
		}
	}

	/**
	 * Suspends the Libvirt virtual machine.
	 * 
	 * @throws LibvirtVirtualMachineException failed to suspend the Libvirt virtual machine.
	 */
	public void suspend() throws LibvirtVirtualMachineException
	{
		try {
			this.domain.suspend();
		} catch ( LibvirtException e ) {
			throw new LibvirtVirtualMachineException( e.getLocalizedMessage() );
		}
	}

	/**
	 * Resumes the Libvirt virtual machine.
	 * 
	 * @throws LibvirtVirtualMachineException faild to resume the Libvirt virtual machine.
	 */
	public void resume() throws LibvirtVirtualMachineException
	{
		try {
			this.domain.resume();
		} catch ( LibvirtException e ) {
			throw new LibvirtVirtualMachineException( e.getLocalizedMessage() );
		}
	}

	/**
	 * Reboot the Libvirt virtual machine.
	 * 
	 * @throws LibvirtVirtualMachineException failed to reboot the Libvirt virtual machine.
	 */
	public void reboot() throws LibvirtVirtualMachineException
	{
		try {
			this.domain.reboot( 0 );
		} catch ( LibvirtException e ) {
			throw new LibvirtVirtualMachineException( e.getLocalizedMessage() );
		}
	}
}