PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/ovirt-engine-3.0.0_0001/backend/manager/modules/vdsbroker/src/main/java/org/ovirt/engine/core/vdsbroker/vdsbroker/CreateVDSCommand.java

#
Java | 318 lines | 253 code | 38 blank | 27 comment | 50 complexity | 1295dcd7cff7d0769da149301b15f7cd MD5 | raw file
Possible License(s): Apache-2.0
  1. package org.ovirt.engine.core.vdsbroker.vdsbroker;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.Date;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.TimeZone;
  9. import java.util.regex.Matcher;
  10. import java.util.regex.Pattern;
  11. import org.ovirt.engine.core.common.businessentities.DiskImage;
  12. import org.ovirt.engine.core.common.businessentities.DisplayType;
  13. import org.ovirt.engine.core.common.businessentities.VM;
  14. import org.ovirt.engine.core.common.businessentities.VMStatus;
  15. import org.ovirt.engine.core.common.businessentities.VmInterfaceType;
  16. import org.ovirt.engine.core.common.businessentities.VmType;
  17. import org.ovirt.engine.core.common.businessentities.network;
  18. import org.ovirt.engine.core.common.businessentities.network_cluster;
  19. import org.ovirt.engine.core.common.config.Config;
  20. import org.ovirt.engine.core.common.config.ConfigValues;
  21. import org.ovirt.engine.core.common.vdscommands.CreateVmVDSCommandParameters;
  22. import org.ovirt.engine.core.compat.LogCompat;
  23. import org.ovirt.engine.core.compat.LogFactoryCompat;
  24. import org.ovirt.engine.core.compat.StringHelper;
  25. import org.ovirt.engine.core.compat.TimeZoneInfo;
  26. import org.ovirt.engine.core.compat.WindowsJavaTimezoneMapping;
  27. import org.ovirt.engine.core.dal.comparators.DiskImageByBootComparator;
  28. import org.ovirt.engine.core.dal.comparators.DiskImageByDriveMappingComparator;
  29. import org.ovirt.engine.core.dal.dbbroker.DbFacade;
  30. import org.ovirt.engine.core.utils.vmproperties.VmPropertiesUtils;
  31. import org.ovirt.engine.core.vdsbroker.xmlrpc.XmlRpcStruct;
  32. public class CreateVDSCommand<P extends CreateVmVDSCommandParameters> extends VmReturnVdsBrokerCommand<P> {
  33. protected VM mVm;
  34. protected XmlRpcStruct mCreateInfo = new XmlRpcStruct();
  35. public CreateVDSCommand(P parameters) {
  36. super(parameters, parameters.getVm().getvm_guid());
  37. mVm = parameters.getVm();
  38. }
  39. @Override
  40. protected void ExecuteVdsBrokerCommand() {
  41. InitData();
  42. mVmReturn = getBroker().create(mCreateInfo);
  43. LogCommandInfo();
  44. ProceedProxyReturnValue();
  45. VdsBrokerObjectsBuilder.updateVMDynamicData(mVm.getDynamicData(), mVmReturn.mVm);
  46. }
  47. /**
  48. * Logs the command info.
  49. */
  50. private void LogCommandInfo() {
  51. final char EQUAL = '=';
  52. final char SEP = ',';
  53. StringBuilder info = new StringBuilder();
  54. String sep = "";
  55. for (String o : mCreateInfo.getKeys()) {
  56. info.append(sep);
  57. info.append(o);
  58. info.append(EQUAL);
  59. info.append(mCreateInfo.getItem(o));
  60. sep = (new Character(SEP)).toString();
  61. }
  62. log.infoFormat("{0} {1}", getClass().getName(), info.toString());
  63. }
  64. private void InitDesktopSoundDevice(){
  65. final String OS_REGEX = "^.*%1s,([^,]*).*$";
  66. final String DEFAULT_TYPE = "default";
  67. String soundDeviceTypeConfig = Config.<String> GetValue(ConfigValues.DesktopAudioDeviceType,mVm.getvds_group_compatibility_version().toString());
  68. String vmOS = mVm.getos().name();
  69. Pattern regexPattern = Pattern.compile(String.format(OS_REGEX, vmOS));
  70. Matcher regexMatcher = regexPattern.matcher(soundDeviceTypeConfig);
  71. if (regexMatcher.find()) {
  72. mCreateInfo.add(VdsProperties.soundDevice, regexMatcher.group(1));
  73. }
  74. else {
  75. regexPattern = Pattern.compile(String.format(OS_REGEX, DEFAULT_TYPE));
  76. regexMatcher = regexPattern.matcher(soundDeviceTypeConfig);
  77. if (regexMatcher.find()) {
  78. mCreateInfo.add(VdsProperties.soundDevice, regexMatcher.group(1));
  79. }
  80. }
  81. }
  82. private void InitData() {
  83. mCreateInfo.add(VdsProperties.vm_guid, mVm.getvm_guid().toString());
  84. mCreateInfo.add(VdsProperties.vm_name, mVm.getvm_name());
  85. mCreateInfo.add(VdsProperties.mem_size_mb, mVm.getvm_mem_size_mb());
  86. mCreateInfo.add(VdsProperties.num_of_monitors, (new Integer(mVm.getnum_of_monitors())).toString());
  87. mCreateInfo.add(VdsProperties.num_of_cpus, (new Integer(mVm.getnum_of_cpus())).toString());
  88. if (Config.<Boolean> GetValue(ConfigValues.SendSMPOnRunVm)) {
  89. mCreateInfo.add(VdsProperties.cores_per_socket, (new Integer(mVm.getcpu_per_socket())).toString());
  90. }
  91. mCreateInfo.add(VdsProperties.emulatedMachine, Config.<String> GetValue(ConfigValues.EmulatedMachine, mVm
  92. .getvds_group_compatibility_version().toString()));
  93. // send cipher suite and spice secure channels parameters only if ssl
  94. // enabled.
  95. if (Config.<Boolean> GetValue(ConfigValues.SSLEnabled)) {
  96. mCreateInfo.add(VdsProperties.spiceSslCipherSuite, Config.<String> GetValue(ConfigValues.CipherSuite));
  97. mCreateInfo.add(VdsProperties.SpiceSecureChannels,
  98. Config.<String> GetValue(ConfigValues.SpiceSecureChannels));
  99. }
  100. if (!StringHelper.isNullOrEmpty(mVm.getCdPath())) {
  101. mCreateInfo.add(VdsProperties.CDRom, mVm.getCdPath());
  102. }
  103. if (!StringHelper.isNullOrEmpty(mVm.getFloppyPath())) {
  104. mCreateInfo.add(VdsProperties.Floppy, mVm.getFloppyPath());
  105. }
  106. mCreateInfo.add(VdsProperties.kvmEnable, mVm.getkvm_enable().toString().toLowerCase());
  107. mCreateInfo.add(VdsProperties.acpiEnable, mVm.getacpi_enable().toString().toLowerCase());
  108. mCreateInfo.add(VdsProperties.Custom, VmPropertiesUtils.getVMProperties(mVm.getStaticData()));
  109. InitDrivesData();
  110. // netowrk
  111. StringBuilder macs = new StringBuilder();
  112. StringBuilder nics = new StringBuilder();
  113. StringBuilder networks = new StringBuilder();
  114. for (int i = 0; i < mVm.getInterfaces().size(); i++) {
  115. macs.append(mVm.getInterfaces().get(i).getMacAddress());
  116. networks.append(mVm.getInterfaces().get(i).getNetworkName());
  117. VmInterfaceType ifaceType = VmInterfaceType.rtl8139;
  118. if (mVm.getInterfaces().get(i).getType() != null) {
  119. ifaceType = VmInterfaceType.forValue(mVm.getInterfaces().get(i).getType());
  120. }
  121. if (ifaceType == VmInterfaceType.rtl8139_pv) {
  122. Boolean useRtl8139_pv = Config.<Boolean> GetValue(ConfigValues.UseRtl8139_pv,
  123. mVm.getvds_group_compatibility_version().toString());
  124. if (!useRtl8139_pv) {
  125. if (mVm.getHasAgent()) {
  126. nics.append("pv");
  127. } else {
  128. nics.append("rtl8139");
  129. }
  130. } else {
  131. nics.append("rtl8139,pv");
  132. macs.append(",");
  133. macs.append(mVm.getInterfaces().get(i).getMacAddress());
  134. networks.append(",");
  135. networks.append(mVm.getInterfaces().get(i).getNetworkName());
  136. }
  137. } else {
  138. nics.append(ifaceType.toString());
  139. }
  140. if (i < mVm.getInterfaces().size() - 1) {
  141. macs.append(",");
  142. nics.append(",");
  143. networks.append(",");
  144. }
  145. }
  146. // set Display network
  147. List<network_cluster> all = DbFacade.getInstance().getNetworkClusterDAO().getAllForCluster(
  148. mVm.getvds_group_id());
  149. // LINQ 29456
  150. // network_cluster networkCluster = all.FirstOrDefault(n =>
  151. // n.is_display);
  152. network_cluster networkCluster = null;
  153. for (network_cluster tempNetworkCluster : all) {
  154. if (tempNetworkCluster.getis_display()) {
  155. networkCluster = tempNetworkCluster;
  156. break;
  157. }
  158. }
  159. // LINQ 29456
  160. if (networkCluster != null) {
  161. // LINQ 29456
  162. // network net = DbFacade.Instance.getAllNetworks().FirstOrDefault(n
  163. // => n.id == networkCluster.network_id);
  164. network net = null;
  165. List<network> allNetworks = DbFacade.getInstance().getNetworkDAO().getAll();
  166. for (network tempNetwork : allNetworks) {
  167. if (tempNetwork.getId().equals(networkCluster.getnetwork_id())) {
  168. net = tempNetwork;
  169. break;
  170. }
  171. }
  172. // LINQ 29456
  173. if (net != null) {
  174. mCreateInfo.add(VdsProperties.displaynetwork, net.getname());
  175. }
  176. }
  177. if (!StringHelper.isNullOrEmpty(macs.toString().trim())) {
  178. mCreateInfo.add(VdsProperties.mac_addr, macs.toString());
  179. mCreateInfo.add(VdsProperties.nic_type, nics.toString());
  180. mCreateInfo.add(VdsProperties.bridge, networks.toString());
  181. }
  182. mCreateInfo.add(VdsProperties.display, mVm.getdisplay_type().toString()); // vnc,
  183. // qxl
  184. mCreateInfo.add(VdsProperties.vm_type, "kvm"); // "qemu", "kvm"
  185. if (mVm.getRunAndPause()) {
  186. mCreateInfo.add(VdsProperties.launch_paused_param, "true");
  187. }
  188. mCreateInfo.add(VdsProperties.Boot, mVm.getboot_sequence().toString().toLowerCase());
  189. // send vm_dynamic.utc_diff if exist, if not send vm_static.time_zone
  190. if (mVm.getutc_diff() != null) {
  191. mCreateInfo.add(VdsProperties.utc_diff, mVm.getutc_diff().toString());
  192. } else {
  193. // get vm timezone
  194. String timeZone = TimeZoneInfo.Local.getId();
  195. if (!StringHelper.isNullOrEmpty(mVm.gettime_zone())) {
  196. timeZone = mVm.gettime_zone();
  197. }
  198. // convert to java & calculate offset
  199. String javaZoneId = WindowsJavaTimezoneMapping.windowsToJava.get(timeZone);
  200. int offset = 0;
  201. if (javaZoneId != null) {
  202. offset = (TimeZone.getTimeZone(javaZoneId).getOffset(new Date().getTime()) / 1000);
  203. }
  204. mCreateInfo.add(VdsProperties.utc_diff, "" + offset);
  205. }
  206. if (mVm.getvm_type() == VmType.Desktop) {
  207. InitDesktopSoundDevice();
  208. }
  209. if (mVm.getvds_group_cpu_flags_data() != null) {
  210. mCreateInfo.add(VdsProperties.cpuType, mVm.getvds_group_cpu_flags_data());
  211. }
  212. mCreateInfo.add(VdsProperties.niceLevel, (new Integer(mVm.getnice_level())).toString());
  213. if (mVm.getstatus() == VMStatus.Suspended && !StringHelper.isNullOrEmpty(mVm.gethibernation_vol_handle())) {
  214. mCreateInfo.add(VdsProperties.hiberVolHandle, mVm.gethibernation_vol_handle());
  215. }
  216. mCreateInfo.add(VdsProperties.KeyboardLayout, Config.<String> GetValue(ConfigValues.VncKeyboardLayout));
  217. if (mVm.getvm_os().isLinux()) {
  218. mCreateInfo.add(VdsProperties.PitReinjection, "false");
  219. }
  220. if (mVm.getdisplay_type() == DisplayType.vnc) {
  221. mCreateInfo.add(VdsProperties.TabletEnable, "true");
  222. }
  223. // Boot Options
  224. if (!StringHelper.isNullOrEmpty(mVm.getinitrd_url())) {
  225. mCreateInfo.add(VdsProperties.InitrdUrl, mVm.getinitrd_url());
  226. }
  227. if (!StringHelper.isNullOrEmpty(mVm.getkernel_url())) {
  228. mCreateInfo.add(VdsProperties.KernelUrl, mVm.getkernel_url());
  229. if (!StringHelper.isNullOrEmpty(mVm.getkernel_params())) {
  230. mCreateInfo.add(VdsProperties.KernelParams, mVm.getkernel_params());
  231. }
  232. }
  233. mCreateInfo.add(VdsProperties.transparent_huge_pages, mVm.getTransparentHugePages() ? "true" : "false");
  234. }
  235. private void InitDrivesData() {
  236. int[] ideIndexSlots = new int[] { 0, 1, 3 };
  237. Map[] drives = new Map[mVm.getDiskMap().size()];
  238. int ideCount = 0, pciCount = 0;
  239. int i = 0;
  240. // LINQ 29456
  241. // order first by drive numbers and then order by boot for the bootable
  242. // drive to be first (important for IDE to be index 0) !
  243. // foreach (DiskImage disk in mVm.DiskMap.Values.OrderBy(a =>
  244. // int.Parse(a.internal_drive_mapping)).OrderByDescending(a => a.boot))
  245. List<DiskImage> diskImages = new ArrayList<DiskImage>(mVm.getDiskMap().values());
  246. Collections.sort(diskImages, new DiskImageByDriveMappingComparator());
  247. Collections.sort(diskImages, Collections.reverseOrder(new DiskImageByBootComparator()));
  248. for (DiskImage disk : diskImages)
  249. // LINQ 29456
  250. {
  251. Map drive = new HashMap();
  252. drive.put("domainID", disk.getstorage_id().toString());
  253. drive.put("poolID", disk.getstorage_pool_id().toString());
  254. drive.put("volumeID", disk.getId().toString());
  255. drive.put("imageID", disk.getimage_group_id().toString());
  256. drive.put("format", disk.getvolume_format().toString().toLowerCase());
  257. drive.put("propagateErrors", disk.getpropagate_errors().toString().toLowerCase());
  258. switch (disk.getdisk_interface()) {
  259. case IDE:
  260. drive.put("if", "ide");
  261. drive.put("index", String.valueOf(ideIndexSlots[ideCount]));
  262. ideCount++;
  263. break;
  264. case VirtIO:
  265. drive.put("if", "virtio");
  266. drive.put("index", String.valueOf(pciCount));
  267. drive.put("boot", String.valueOf(disk.getboot()).toLowerCase());
  268. pciCount++;
  269. break;
  270. default:
  271. // ISCI not supported
  272. break;
  273. }
  274. drives[i] = drive;
  275. i++;
  276. }
  277. mCreateInfo.add("drives", drives);
  278. }
  279. private static LogCompat log = LogFactoryCompat.getLog(CreateVDSCommand.class);
  280. }