summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/util/vm/VmMetaData.java
blob: 7e67fe6f0634c2a264b937063564a6e296e04518 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package org.openslx.util.vm;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;

import javax.swing.JOptionPane;

import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.OperatingSystem;
import org.openslx.bwlp.thrift.iface.Virtualizer;

/**
 * Describes a configured virtual machine. This class is parsed from a machine description, like a
 * *.vmx for VMware machines.
 */
public abstract class VmMetaData
{
	private static final Logger LOGGER = Logger.getLogger( VmMetaData.class );
	/*
	 * Helper types
	 */

	public static enum DriveBusType
	{
		SCSI,
		IDE,
		SATA;
	}

	public static class HardDisk
	{
		public final String chipsetDriver;
		public final DriveBusType bus;
		public final String diskImage;

		public HardDisk( String chipsetDriver, DriveBusType bus, String diskImage )
		{
			this.chipsetDriver = chipsetDriver;
			this.bus = bus;
			this.diskImage = diskImage;
		}
	}

	/*
	 * Members
	 */

	protected final List<HardDisk> hdds = new ArrayList<>();

	private final List<OperatingSystem> osList;

	private OperatingSystem os = null;

	protected String displayName = null;
	/*
	 * Guettas
	 */

	/**
	 * Get operating system of this VM.
	 */
	public OperatingSystem getOs()
	{
		return os;
	}

	/**
	 * Get all hard disks of this VM.
	 */
	public List<HardDisk> getHdds()
	{
		return Collections.unmodifiableList( hdds );
	}

	/**
	 * Get display name of VM.
	 */
	public String getDisplayName()
	{
		return displayName;
	}

	/**
	 * This method should return a minimal representation of the input meta data. The representation
	 * is platform dependent, and should be stripped of all non-essential configuration, such as
	 * CD/DVD/FLoppy drives, serial or parallel ports, shared folders, or anything else that could be
	 * considered sensible information (absolute paths containing the local user's name).
	 */
	public abstract byte[] getFilteredDefinitionArray();

	public final ByteBuffer getFilteredDefinition()
	{
		return ByteBuffer.wrap( getFilteredDefinitionArray() );
	}

	/*
	 * Methods
	 */

	public VmMetaData( List<OperatingSystem> osList )
	{
		this.osList = osList;
	}

	/**
	 * Called from subclass to set the OS. If the OS cannot be determined from the given parameters,
	 * it will not be set.
	 * 
	 * @param virtId virtualizer, eg "vmware" for VMware
	 * @param virtOsId the os identifier used by the virtualizer, eg. windows7-64 for 64bit Windows 7
	 *           on VMware
	 */
	protected final void setOs( String virtId, String virtOsId )
	{
		OperatingSystem lazyMatch = null;
		boolean x = false;
		for ( OperatingSystem os : osList ) {
			if ( os.getVirtualizerOsId() == null )
				continue;
			for ( Entry<String, String> entry : os.getVirtualizerOsId().entrySet() ) {
				if ( !entry.getValue().equals( virtOsId ) )
					continue;
				if ( entry.getKey().equals( virtId ) ) {
					this.os = os;
					x = true;
					return;
				} else {
					lazyMatch = os;
				}
			}
		}
		
		/*if(!x) {
			//LOGGER.debug( "os with " + virtId + " " + virtOsId + " is not in list");
		}*/
		this.os = lazyMatch;
	}

	public abstract Virtualizer getVirtualizer();
	
	public abstract void enableUsb(boolean enabled);
	
	/**
	 * Apply config options that are desired when locally editing a VM.
	 * for vmware, this disables automatic DPI scaling of the guest.
	 */
	public abstract void applySettingsForLocalEdit();
	
	// meta object needed when reading vm from file
	public static VmMetaData getInstance(List<OperatingSystem> osList, File file) throws IOException {

		VmMetaData meta = null;
		try {
			meta = new VmwareMetaData(osList, file);
		} catch ( UnsupportedVirtualizerFormatException e ) {
			LOGGER.debug( "datei war nicht .vmx; versuche mit VBox");
			try {
				meta = new VboxMetaData(osList, file);
			}
			catch (UnsupportedVirtualizerFormatException ex) {
				LOGGER.debug( "datei war nicht .vbox; unterbrochen!" , ex);
				// TODO ok so or throw new IOException?
			}
		}
		if (meta != null) {
			return meta;
		}
		return null;
	}
	
	// meta object needed when reading from configarray
	public static VmMetaData getInstance(List<OperatingSystem> osList, byte[] vmContent, int length) throws IOException {
		
		VmMetaData meta = null;
		try {
			meta = new VmwareMetaData( osList, vmContent, length );
		} catch (UnsupportedVirtualizerFormatException e) {
			LOGGER.debug( "machine Description entspricht nicht vmx format; versuche mit VBox" );
			try {
				meta = new VboxMetaData( osList, vmContent, length );
			} catch (UnsupportedVirtualizerFormatException ex) {
				LOGGER.debug( "machine Description entspricht nicht vbox format ); unterbrochen!", ex);
			}
			if (meta != null) {
				return meta;
			}
			return null;
		}
		
		return null;
	}

	public abstract boolean addHddTemplate( String diskImagePath, String hddMode, String redoDir );

	public abstract boolean addDefaultNat();

	public abstract void setOs( String vendorOsId );

	public abstract boolean addDisplayName( String name );

	public abstract boolean addRam( int mem );

	public abstract void addFloppy( int index, String image, boolean readOnly );

	public abstract boolean addCdrom( String image );
}