1919package com .cloud .agent .resource .computing ;
2020
2121import java .util .ArrayList ;
22+ import java .util .Collection ;
23+ import java .util .HashMap ;
2224import java .util .List ;
25+ import java .util .Map ;
2326import java .util .UUID ;
2427
2528public class LibvirtVMDef {
2629 private String _hvsType ;
2730 private String _domName ;
2831 private String _domUUID ;
2932 private String _desc ;
30- private final List < Object > components = new ArrayList < Object >();
33+ private final Map < String , Object > components = new HashMap < String , Object >();
3134
32- public static class guestDef {
35+ public static class GuestDef {
3336 enum guestType {
3437 KVM ,
3538 XEN ,
@@ -104,12 +107,12 @@ public String toString () {
104107 }
105108 }
106109
107- public static class guestResourceDef {
108- private int _mem ;
110+ public static class GuestResourceDef {
111+ private long _mem ;
109112 private int _currentMem = -1 ;
110113 private String _memBacking ;
111114 private int _vcpu = -1 ;
112- public void setMemorySize (int mem ) {
115+ public void setMemorySize (long mem ) {
113116 _mem = mem ;
114117 }
115118 public void setCurrentMem (int currMem ) {
@@ -138,7 +141,7 @@ public String toString(){
138141 }
139142 }
140143
141- public static class featuresDef {
144+ public static class FeaturesDef {
142145 private final List <String > _features = new ArrayList <String >();
143146 public void addFeatures (String feature ) {
144147 _features .add (feature );
@@ -154,11 +157,11 @@ public String toString() {
154157 return feaBuilder .toString ();
155158 }
156159 }
157- public static class termPolicy {
160+ public static class TermPolicy {
158161 private String _reboot ;
159162 private String _powerOff ;
160163 private String _crash ;
161- public termPolicy () {
164+ public TermPolicy () {
162165 _reboot = _powerOff = _crash = "destroy" ;
163166 }
164167 public void setRebootPolicy (String rbPolicy ) {
@@ -180,11 +183,20 @@ public String toString() {
180183 }
181184 }
182185
183- public static class devicesDef {
186+ public static class DevicesDef {
184187 private String _emulator ;
185- private final List <Object > devices = new ArrayList < Object >();
188+ private final Map < String , List <?>> devices = new HashMap < String , List <?> >();
186189 public boolean addDevice (Object device ) {
187- return devices .add (device );
190+ Object dev = devices .get (device .getClass ().toString ());
191+ if (dev == null ) {
192+ List <Object > devs = new ArrayList <Object >();
193+ devs .add (device );
194+ devices .put (device .getClass ().toString (), devs );
195+ } else {
196+ List <Object > devs = (List <Object >)dev ;
197+ devs .add (device );
198+ }
199+ return true ;
188200 }
189201 public void setEmulatorPath (String emulator ) {
190202 _emulator = emulator ;
@@ -196,15 +208,24 @@ public String toString() {
196208 if (_emulator != null ) {
197209 devicesBuilder .append ("<emulator>" + _emulator + "</emulator>\n " );
198210 }
199- for (Object o : devices ) {
200- devicesBuilder .append (o .toString ());
211+
212+ for (List <?> devs : devices .values ()) {
213+ for (Object dev : devs ) {
214+ devicesBuilder .append (dev .toString ());
215+ }
201216 }
202217 devicesBuilder .append ("</devices>\n " );
203218 return devicesBuilder .toString ();
204219 }
220+ public List <DiskDef > getDisks () {
221+ return (List <DiskDef >)devices .get (DiskDef .class .toString ());
222+ }
223+ public List <InterfaceDef > getInterfaces () {
224+ return (List <InterfaceDef >)devices .get (InterfaceDef .class .toString ());
225+ }
205226
206227 }
207- public static class diskDef {
228+ public static class DiskDef {
208229 enum deviceType {
209230 FLOOPY ("floopy" ),
210231 DISK ("disk" ),
@@ -282,6 +303,33 @@ public void defFileBasedDisk(String filePath, String diskLabel, diskBus bus, dis
282303 _bus = bus ;
283304
284305 }
306+ private String getDevLabel (int devId , diskBus bus ) {
307+ char suffix = (char )('a' + devId );
308+ if (bus == diskBus .SCSI ) {
309+ return "sd" + suffix ;
310+ } else if (bus == diskBus .VIRTIO ) {
311+ return "vd" + suffix ;
312+ }
313+ return "hd" + suffix ;
314+ }
315+ public void defFileBasedDisk (String filePath , int devId , diskBus bus , diskFmtType diskFmtType ) {
316+
317+ _diskType = diskType .FILE ;
318+ _deviceType = deviceType .DISK ;
319+ _sourcePath = filePath ;
320+ _diskLabel = getDevLabel (devId , bus );
321+ _diskFmtType = diskFmtType ;
322+ _bus = bus ;
323+
324+ }
325+ public void defISODisk (String volPath ) {
326+ _diskType = diskType .FILE ;
327+ _deviceType = deviceType .CDROM ;
328+ _sourcePath = volPath ;
329+ _diskLabel = "hdc" ;
330+ _diskFmtType = diskFmtType .RAW ;
331+ _bus = diskBus .IDE ;
332+ }
285333 public void defBlockBasedDisk (String diskName , String diskLabel , diskBus bus ) {
286334 _diskType = diskType .BLOCK ;
287335 _deviceType = deviceType .DISK ;
@@ -307,6 +355,19 @@ public String getDiskPath() {
307355 public String getDiskLabel () {
308356 return _diskLabel ;
309357 }
358+ public deviceType getDeviceType () {
359+ return _deviceType ;
360+ }
361+ public void setDiskPath (String volPath ) {
362+ this ._sourcePath = volPath ;
363+ }
364+ public diskBus getBusType () {
365+ return _bus ;
366+ }
367+ public int getDiskSeq () {
368+ char suffix = this ._diskLabel .charAt (this ._diskLabel .length () - 1 );
369+ return suffix - 'a' ;
370+ }
310371 @ Override
311372 public String toString () {
312373 StringBuilder diskBuilder = new StringBuilder ();
@@ -342,7 +403,7 @@ public String toString() {
342403 }
343404 }
344405
345- public static class interfaceDef {
406+ public static class InterfaceDef {
346407 enum guestNetType {
347408 BRIDGE ("bridge" ),
348409 NETWORK ("network" ),
@@ -415,6 +476,7 @@ public String getBrName() {
415476 public guestNetType getNetType () {
416477 return _netType ;
417478 }
479+
418480 @ Override
419481 public String toString () {
420482 StringBuilder netBuilder = new StringBuilder ();
@@ -437,12 +499,12 @@ public String toString() {
437499 return netBuilder .toString ();
438500 }
439501 }
440- public static class consoleDef {
502+ public static class ConsoleDef {
441503 private final String _ttyPath ;
442504 private final String _type ;
443505 private final String _source ;
444506 private short _port = -1 ;
445- public consoleDef (String type , String path , String source , short port ) {
507+ public ConsoleDef (String type , String path , String source , short port ) {
446508 _type = type ;
447509 _ttyPath = path ;
448510 _source = source ;
@@ -467,11 +529,11 @@ public String toString() {
467529 return consoleBuilder .toString ();
468530 }
469531 }
470- public static class serialDef {
532+ public static class SerialDef {
471533 private final String _type ;
472534 private final String _source ;
473535 private short _port = -1 ;
474- public serialDef (String type , String source , short port ) {
536+ public SerialDef (String type , String source , short port ) {
475537 _type = type ;
476538 _source = source ;
477539 _port = port ;
@@ -490,14 +552,14 @@ public String toString() {
490552 return serialBuidler .toString ();
491553 }
492554 }
493- public static class graphicDef {
555+ public static class GraphicDef {
494556 private final String _type ;
495557 private short _port = -2 ;
496558 private boolean _autoPort = false ;
497559 private final String _listenAddr ;
498560 private final String _passwd ;
499561 private final String _keyMap ;
500- public graphicDef (String type , short port , boolean auotPort , String listenAddr , String passwd , String keyMap ) {
562+ public GraphicDef (String type , short port , boolean auotPort , String listenAddr , String passwd , String keyMap ) {
501563 _type = type ;
502564 _port = port ;
503565 _autoPort = auotPort ;
@@ -528,10 +590,10 @@ public String toString() {
528590 return graphicBuilder .toString ();
529591 }
530592 }
531- public static class inputDef {
593+ public static class InputDef {
532594 private final String _type ; /*tablet, mouse*/
533595 private final String _bus ; /*ps2, usb, xen*/
534- public inputDef (String type , String bus ) {
596+ public InputDef (String type , String bus ) {
535597 _type = type ;
536598 _bus = bus ;
537599 }
@@ -558,8 +620,18 @@ public void setDomUUID(String uuid) {
558620 public void setDomDescription (String desc ) {
559621 _desc = desc ;
560622 }
561- public boolean addComp (Object comp ) {
562- return components .add (comp );
623+ public String getGuestOSType () {
624+ return _desc ;
625+ }
626+ public void addComp (Object comp ) {
627+ components .put (comp .getClass ().toString (), comp );
628+ }
629+ public DevicesDef getDevices () {
630+ Object o = components .get (DevicesDef .class .toString ());
631+ if (o != null ) {
632+ return (DevicesDef )o ;
633+ }
634+ return null ;
563635 }
564636 @ Override
565637 public String toString () {
@@ -572,7 +644,7 @@ public String toString() {
572644 if (_desc != null ) {
573645 vmBuilder .append ("<description>" + _desc + "</description>\n " );
574646 }
575- for (Object o : components ) {
647+ for (Object o : components . values () ) {
576648 vmBuilder .append (o .toString ());
577649 }
578650 vmBuilder .append ("</domain>\n " );
@@ -586,63 +658,63 @@ public static void main(String [] args){
586658 vm .setDomainName ("testing" );
587659 vm .setDomUUID (UUID .randomUUID ().toString ());
588660
589- guestDef guest = new guestDef ();
590- guest .setGuestType (guestDef .guestType .KVM );
661+ GuestDef guest = new GuestDef ();
662+ guest .setGuestType (GuestDef .guestType .KVM );
591663 guest .setGuestArch ("x86_64" );
592664 guest .setMachineType ("pc-0.11" );
593- guest .setBootOrder (guestDef .bootOrder .HARDISK );
665+ guest .setBootOrder (GuestDef .bootOrder .HARDISK );
594666 vm .addComp (guest );
595667
596- guestResourceDef grd = new guestResourceDef ();
668+ GuestResourceDef grd = new GuestResourceDef ();
597669 grd .setMemorySize (512 *1024 );
598670 grd .setVcpuNum (1 );
599671 vm .addComp (grd );
600672
601- featuresDef features = new featuresDef ();
673+ FeaturesDef features = new FeaturesDef ();
602674 features .addFeatures ("pae" );
603675 features .addFeatures ("apic" );
604676 features .addFeatures ("acpi" );
605677 vm .addComp (features );
606678
607- termPolicy term = new termPolicy ();
679+ TermPolicy term = new TermPolicy ();
608680 term .setCrashPolicy ("destroy" );
609681 term .setPowerOffPolicy ("destroy" );
610682 term .setRebootPolicy ("destroy" );
611683 vm .addComp (term );
612684
613- devicesDef devices = new devicesDef ();
685+ DevicesDef devices = new DevicesDef ();
614686 devices .setEmulatorPath ("/usr/bin/cloud-qemu-system-x86_64" );
615687
616- diskDef hda = new diskDef ();
617- hda .defFileBasedDisk ("/path/to/hda1" , "hda" , diskDef .diskBus .IDE , diskDef .diskFmtType .QCOW2 );
688+ DiskDef hda = new DiskDef ();
689+ hda .defFileBasedDisk ("/path/to/hda1" , 0 , DiskDef .diskBus .VIRTIO , DiskDef .diskFmtType .QCOW2 );
618690 devices .addDevice (hda );
619691
620- diskDef hdb = new diskDef ();
621- hdb .defFileBasedDisk ("/path/to/hda2" , "hdb" , diskDef .diskBus .IDE , diskDef .diskFmtType .QCOW2 );
692+ DiskDef hdb = new DiskDef ();
693+ hdb .defFileBasedDisk ("/path/to/hda2" , 1 , DiskDef .diskBus .VIRTIO , DiskDef .diskFmtType .QCOW2 );
622694 devices .addDevice (hdb );
623695
624- interfaceDef pubNic = new interfaceDef ();
625- pubNic .defBridgeNet ("cloudbr0" , "vnet1" , "00:16:3e:77:e2:a1" , interfaceDef .nicModel .VIRTIO );
696+ InterfaceDef pubNic = new InterfaceDef ();
697+ pubNic .defBridgeNet ("cloudbr0" , "vnet1" , "00:16:3e:77:e2:a1" , InterfaceDef .nicModel .VIRTIO );
626698 devices .addDevice (pubNic );
627699
628- interfaceDef privNic = new interfaceDef ();
629- privNic .defPrivateNet ("cloud-private" , null , "00:16:3e:77:e2:a2" , interfaceDef .nicModel .VIRTIO );
700+ InterfaceDef privNic = new InterfaceDef ();
701+ privNic .defPrivateNet ("cloud-private" , null , "00:16:3e:77:e2:a2" , InterfaceDef .nicModel .VIRTIO );
630702 devices .addDevice (privNic );
631703
632- interfaceDef vlanNic = new interfaceDef ();
633- vlanNic .defBridgeNet ("vnbr1000" , "tap1" , "00:16:3e:77:e2:a2" , interfaceDef .nicModel .VIRTIO );
704+ InterfaceDef vlanNic = new InterfaceDef ();
705+ vlanNic .defBridgeNet ("vnbr1000" , "tap1" , "00:16:3e:77:e2:a2" , InterfaceDef .nicModel .VIRTIO );
634706 devices .addDevice (vlanNic );
635707
636- serialDef serial = new serialDef ("pty" , null , (short )0 );
708+ SerialDef serial = new SerialDef ("pty" , null , (short )0 );
637709 devices .addDevice (serial );
638710
639- consoleDef console = new consoleDef ("pty" , null , null , (short )0 );
711+ ConsoleDef console = new ConsoleDef ("pty" , null , null , (short )0 );
640712 devices .addDevice (console );
641713
642- graphicDef grap = new graphicDef ("vnc" , (short )0 , true , null , null , null );
714+ GraphicDef grap = new GraphicDef ("vnc" , (short )0 , true , null , null , null );
643715 devices .addDevice (grap );
644716
645- inputDef input = new inputDef ("tablet" , "usb" );
717+ InputDef input = new InputDef ("tablet" , "usb" );
646718 devices .addDevice (input );
647719
648720 vm .addComp (devices );
0 commit comments