View Javadoc

1   /*
2    * JBoss, Home of Professional Open Source.
3    * Copyright 2013, Red Hat Middleware LLC, and individual contributors
4    * as indicated by the @author tags. See the copyright.txt file in the
5    * distribution for a full listing of individual contributors.
6    *
7    * This is free software; you can redistribute it and/or modify it
8    * under the terms of the GNU Lesser General Public License as
9    * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */
22  
23  package org.redhat.vmtruckloader.vmware;
24  
25  import org.redhat.vmtruckloader.service.MachineSpecification;
26  
27  import com.vmware.vim25.Description;
28  import com.vmware.vim25.VirtualDeviceConfigSpec;
29  import com.vmware.vim25.VirtualDeviceConfigSpecOperation;
30  import com.vmware.vim25.VirtualEthernetCard;
31  import com.vmware.vim25.VirtualEthernetCardNetworkBackingInfo;
32  import com.vmware.vim25.VirtualMachineConfigSpec;
33  import com.vmware.vim25.VirtualVmxnet3;
34  import com.vmware.vim25.mo.ServiceInstance;
35  
36  /**
37   * 
38   * @author Romain Pelisse - romain@redhat.com
39   *
40   */
41  public class VMWareMachineSpecsUtils {
42  
43  	private final static long GIGABYTE = 1024L;
44  	
45  	private final static String NETWORK_INTERFACE_NAME = "Network Adapter 1";
46  	private final static String NETWORK_ADDRESS_TYPE = "generated";
47  
48  	public static VirtualMachineConfigSpec buildVmSpec(ServiceInstance serviceInstance, MachineSpecification spec) {
49  		VirtualMachineConfigSpec vmSpec = new VirtualMachineConfigSpec();
50  		vmSpec.setName(spec.getHostname());
51  		vmSpec.setNumCPUs(spec.getNbCpu());
52  		vmSpec.setMemoryMB(spec.getVRAM() * GIGABYTE);
53  		vmSpec.setAnnotation(spec.getRole());
54  
55  		final String netName = spec.getVLAN();
56  		VirtualDeviceConfigSpec ethernet = createNicSpec(netName,VirtualDeviceConfigSpecOperation.add);
57  		
58  		vmSpec.setDeviceChange(new VirtualDeviceConfigSpec[] { ethernet,null });
59  		return vmSpec;
60  	}
61  	
62  	private static VirtualDeviceConfigSpec createNicSpec(String netName, VirtualDeviceConfigSpecOperation operation) {
63  		final String nicName = NETWORK_INTERFACE_NAME;
64  		VirtualDeviceConfigSpec nicSpec = new VirtualDeviceConfigSpec();
65  		nicSpec.setOperation(operation);
66  
67  		VirtualEthernetCard nic = new VirtualVmxnet3();
68  		VirtualEthernetCardNetworkBackingInfo nicBacking = new VirtualEthernetCardNetworkBackingInfo();
69  		nicBacking.setDeviceName(netName);
70  
71  		Description info = new Description();
72  		info.setLabel(nicName);
73  		info.setSummary(netName);
74  		nic.setDeviceInfo(info);
75  
76  		// type: "generated", "manual", "assigned" by VC
77  		nic.setAddressType(NETWORK_ADDRESS_TYPE);
78  		nic.setBacking(nicBacking);
79  		nic.setKey(0);	
80  
81  		nicSpec.setDevice(nic);
82  		return nicSpec;
83  	}
84  
85  	
86  	public  static VirtualMachineConfigSpec buildVmSpecAndSetCpusAndMemory(ServiceInstance serviceInstance, MachineSpecification spec) {
87  		VirtualMachineConfigSpec vmSpec = new VirtualMachineConfigSpec();
88  		vmSpec.setName(spec.getVmName());
89  						
90  		if ( spec.getNbCpu() != 0 )
91  			vmSpec.setNumCPUs(spec.getNbCpu());
92  		if ( spec.getVRAM() != 0 )
93  			vmSpec.setMemoryMB(Long.valueOf(spec.getVRAM() * 1024));
94  		return vmSpec;
95  	}
96  
97  
98  	public  static VirtualMachineConfigSpec updateVmSpecAndSetCpusAndMemory(ServiceInstance serviceInstance, MachineSpecification spec, VirtualMachineConfigSpec vmSpec) {
99  		vmSpec.setName(spec.getVmName());
100 						
101 		if ( spec.getNbCpu() != 0 )
102 			vmSpec.setNumCPUs(spec.getNbCpu());
103 		if ( spec.getVRAM() != 0 )
104 			vmSpec.setMemoryMB(Long.valueOf(spec.getVRAM() * 1024));
105 		return vmSpec;
106 	}
107 }