1717
1818package com .cloud .hypervisor .vmware .mo ;
1919
20+ import java .lang .reflect .InvocationTargetException ;
2021import java .util .ArrayList ;
2122import java .util .Arrays ;
2223import java .util .List ;
@@ -59,7 +60,7 @@ public DatacenterMO(VmwareContext context, String morType, String morValue) {
5960 super (context , morType , morValue );
6061 }
6162
62- public DatacenterMO (VmwareContext context , String dcName ) throws Exception {
63+ public DatacenterMO (VmwareContext context , String dcName ) throws InvalidPropertyFaultMsg , RuntimeFaultFaultMsg {
6364 super (context , null );
6465
6566 _mor = _context .getVimClient ().getDecendentMoRef (_context .getRootFolder (), "Datacenter" , dcName );
@@ -70,24 +71,7 @@ public DatacenterMO(VmwareContext context, String dcName) throws Exception {
7071
7172 @ Override
7273 public String getName () throws Exception {
73- return (String )_context .getVimClient ().getDynamicProperty (_mor , "name" );
74- }
75-
76- public void registerTemplate (ManagedObjectReference morHost , String datastoreName , String templateName , String templateFileName ) throws Exception {
77-
78- ManagedObjectReference morFolder = (ManagedObjectReference )_context .getVimClient ().getDynamicProperty (_mor , "vmFolder" );
79- assert (morFolder != null );
80-
81- ManagedObjectReference morTask =
82- _context .getService ()
83- .registerVMTask (morFolder , String .format ("[%s] %s/%s" , datastoreName , templateName , templateFileName ), templateName , true , null , morHost );
84-
85- boolean result = _context .getVimClient ().waitForTask (morTask );
86- if (!result ) {
87- throw new Exception ("Unable to register template due to " + TaskMO .getTaskFailureInfo (_context , morTask ));
88- } else {
89- _context .waitForTaskProgressDone (morTask );
90- }
74+ return _context .getVimClient ().getDynamicProperty (_mor , "name" );
9175 }
9276
9377 public VirtualMachineMO findVm (String vmName ) throws Exception {
@@ -105,10 +89,10 @@ public List<VirtualMachineMO> findVmByNameAndLabel(String vmLabel) throws Except
10589 int key = cfmMo .getCustomFieldKey ("VirtualMachine" , CustomFieldConstants .CLOUD_UUID );
10690 assert (key != 0 );
10791
108- List <VirtualMachineMO > list = new ArrayList <VirtualMachineMO >();
92+ List <VirtualMachineMO > list = new ArrayList <>();
10993
11094 List <ObjectContent > ocs = getVmPropertiesOnDatacenterVmFolder (new String [] {"name" , String .format ("value[%d]" , key )});
111- if (ocs != null && ocs . size () > 0 ) {
95+ if (CollectionUtils . isNotEmpty ( ocs ) ) {
11296 for (ObjectContent oc : ocs ) {
11397 List <DynamicProperty > props = oc .getPropSet ();
11498 if (props != null ) {
@@ -141,7 +125,7 @@ public VirtualMachineMO checkIfVmAlreadyExistsInVcenter(String vmNameOnVcenter,
141125 }
142126
143127 List <ObjectContent > ocs = getVmPropertiesOnDatacenterVmFolder (new String [] {"name" , String .format ("value[%d]" , key )});
144- if (ocs != null && ocs . size () > 0 ) {
128+ if (CollectionUtils . isNotEmpty ( ocs ) ) {
145129 for (ObjectContent oc : ocs ) {
146130 List <DynamicProperty > props = oc .getPropSet ();
147131 if (props != null ) {
@@ -166,7 +150,7 @@ public VirtualMachineMO checkIfVmAlreadyExistsInVcenter(String vmNameOnVcenter,
166150 return null ;
167151 }
168152
169- public Pair <String , List <UnmanagedInstanceTO >> getVmsOnDatacenter (Integer maxObjects , String token ) throws Exception {
153+ public Pair <String , List <UnmanagedInstanceTO >> getVmsOnDatacenter (Integer maxObjects , String token ) throws InvalidPropertyFaultMsg , RuntimeFaultFaultMsg , InvocationTargetException , NoSuchMethodException , IllegalAccessException {
170154 List <UnmanagedInstanceTO > vms = new ArrayList <>();
171155 Pair <String , List <ObjectContent >> objectContents = getVmPropertiesOnDatacenterVmFolder (new String [] {"name" }, maxObjects , token );
172156 Pair <String , List <UnmanagedInstanceTO >> retval = new Pair <>(objectContents .first (), vms );
@@ -219,20 +203,6 @@ public ManagedObjectReference findDatastore(String name) throws Exception {
219203 return null ;
220204 }
221205
222- public ManagedObjectReference listDatastore (String name ) throws Exception {
223- assert (name != null );
224-
225- List <ObjectContent > ocs = getDatastorePropertiesOnDatacenter (new String [] {"name" });
226- if (ocs != null ) {
227- for (ObjectContent oc : ocs ) {
228- if (oc .getPropSet ().get (0 ).getVal ().toString ().equals (name )) {
229- return oc .getObj ();
230- }
231- }
232- }
233- return null ;
234- }
235-
236206 public ManagedObjectReference findHost (String name ) throws Exception {
237207 List <ObjectContent > ocs = getHostPropertiesOnDatacenterHostFolder (new String [] {"name" });
238208
@@ -247,7 +217,7 @@ public ManagedObjectReference findHost(String name) throws Exception {
247217 }
248218
249219 public ManagedObjectReference getVmFolder () throws Exception {
250- return ( ManagedObjectReference ) _context .getVimClient ().getDynamicProperty (_mor , "vmFolder" );
220+ return _context .getVimClient ().getDynamicProperty (_mor , "vmFolder" );
251221 }
252222
253223 public List <ObjectContent > getHostPropertiesOnDatacenterHostFolder (String [] propertyPaths ) throws Exception {
@@ -284,7 +254,7 @@ public List<ObjectContent> getHostPropertiesOnDatacenterHostFolder(String[] prop
284254 PropertyFilterSpec pfSpec = new PropertyFilterSpec ();
285255 pfSpec .getPropSet ().add (pSpec );
286256 pfSpec .getObjectSet ().add (oSpec );
287- List <PropertyFilterSpec > pfSpecArr = new ArrayList <PropertyFilterSpec >();
257+ List <PropertyFilterSpec > pfSpecArr = new ArrayList <>();
288258 pfSpecArr .add (pfSpec );
289259
290260 return _context .getService ().retrieveProperties (_context .getPropertyCollector (), pfSpecArr );
@@ -310,7 +280,7 @@ public List<ObjectContent> getDatastorePropertiesOnDatacenter(String[] propertyP
310280 PropertyFilterSpec pfSpec = new PropertyFilterSpec ();
311281 pfSpec .getPropSet ().add (pSpec );
312282 pfSpec .getObjectSet ().add (oSpec );
313- List <PropertyFilterSpec > pfSpecArr = new ArrayList <PropertyFilterSpec >();
283+ List <PropertyFilterSpec > pfSpecArr = new ArrayList <>();
314284 pfSpecArr .add (pfSpec );
315285
316286 return _context .getService ().retrieveProperties (_context .getPropertyCollector (), pfSpecArr );
@@ -327,7 +297,8 @@ public List<ObjectContent> getVmPropertiesOnDatacenterVmFolder(String[] property
327297 * @param maxObjects the number of objects to retrieve
328298 * @param tokenForPriorQuery restart the query or continue a previous query
329299 * @return The propertyPaths requested for the objects of type "VirtualMachine" in a list are found/returned by the DC
330- * @throws Exception generic {code}Exception{code} as thrown by Vmware.
300+ * @throws InvalidPropertyFaultMsg property does not exist as thrown by Vmware.
301+ * @throws RuntimeFaultFaultMsg generic vmware runtime exception
331302 */
332303 public Pair <String , List <ObjectContent >> getVmPropertiesOnDatacenterVmFolder (String [] propertyPaths , Integer maxObjects , String tokenForPriorQuery ) throws InvalidPropertyFaultMsg , RuntimeFaultFaultMsg {
333304 if (StringUtils .isNotBlank (tokenForPriorQuery )) {
@@ -410,18 +381,18 @@ public static Pair<DatacenterMO, String> getOwnerDatacenter(VmwareContext contex
410381 PropertyFilterSpec pfSpec = new PropertyFilterSpec ();
411382 pfSpec .getPropSet ().add (pSpec );
412383 pfSpec .getObjectSet ().add (oSpec );
413- List <PropertyFilterSpec > pfSpecArr = new ArrayList <PropertyFilterSpec >();
384+ List <PropertyFilterSpec > pfSpecArr = new ArrayList <>();
414385 pfSpecArr .add (pfSpec );
415386
416387 List <ObjectContent > ocs = context .getService ().retrieveProperties (context .getPropertyCollector (), pfSpecArr );
417388
418- assert (ocs != null && ocs . size () > 0 );
389+ assert (CollectionUtils . isNotEmpty ( ocs ) );
419390 assert (ocs .get (0 ).getObj () != null );
420391 assert (ocs .get (0 ).getPropSet ().get (0 ) != null );
421392 assert (ocs .get (0 ).getPropSet ().get (0 ).getVal () != null );
422393
423394 String dcName = ocs .get (0 ).getPropSet ().get (0 ).getVal ().toString ();
424- return new Pair <DatacenterMO , String >(new DatacenterMO (context , ocs .get (0 ).getObj ()), dcName );
395+ return new Pair <>(new DatacenterMO (context , ocs .get (0 ).getObj ()), dcName );
425396 }
426397
427398 public ManagedObjectReference getDvPortGroupMor (String dvPortGroupName ) throws Exception {
@@ -442,7 +413,7 @@ public ManagedObjectReference getDvPortGroupMor(String dvPortGroupName) throws E
442413 PropertyFilterSpec pfSpec = new PropertyFilterSpec ();
443414 pfSpec .getPropSet ().add (pSpec );
444415 pfSpec .getObjectSet ().add (oSpec );
445- List <PropertyFilterSpec > pfSpecArr = new ArrayList <PropertyFilterSpec >();
416+ List <PropertyFilterSpec > pfSpecArr = new ArrayList <>();
446417 pfSpecArr .add (pfSpec );
447418
448419 List <ObjectContent > ocs = _context .getService ().retrieveProperties (_context .getPropertyCollector (), pfSpecArr );
@@ -463,9 +434,7 @@ public ManagedObjectReference getDvPortGroupMor(String dvPortGroupName) throws E
463434
464435 public boolean hasDvPortGroup (String dvPortGroupName ) throws Exception {
465436 ManagedObjectReference morNetwork = getDvPortGroupMor (dvPortGroupName );
466- if (morNetwork != null )
467- return true ;
468- return false ;
437+ return morNetwork != null ;
469438 }
470439
471440 public DVPortgroupConfigInfo getDvPortGroupSpec (String dvPortGroupName ) throws Exception {
@@ -489,7 +458,7 @@ public DVPortgroupConfigInfo getDvPortGroupSpec(String dvPortGroupName) throws E
489458 PropertyFilterSpec pfSpec = new PropertyFilterSpec ();
490459 pfSpec .getPropSet ().add (pSpec );
491460 pfSpec .getObjectSet ().add (oSpec );
492- List <PropertyFilterSpec > pfSpecArr = new ArrayList <PropertyFilterSpec >();
461+ List <PropertyFilterSpec > pfSpecArr = new ArrayList <>();
493462 pfSpecArr .add (pfSpec );
494463
495464 List <ObjectContent > ocs = _context .getService ().retrieveProperties (_context .getPropertyCollector (), pfSpecArr );
@@ -506,7 +475,7 @@ public DVPortgroupConfigInfo getDvPortGroupSpec(String dvPortGroupName) throws E
506475 nameProperty = prop .getVal ().toString ();
507476 }
508477 }
509- if (nameProperty .equalsIgnoreCase (dvPortGroupName )) {
478+ if (nameProperty != null && nameProperty .equalsIgnoreCase (dvPortGroupName )) {
510479 return configSpec ;
511480 }
512481 }
@@ -536,7 +505,7 @@ public ManagedObjectReference getDvSwitchMor(ManagedObjectReference dvPortGroupM
536505 PropertyFilterSpec pfSpec = new PropertyFilterSpec ();
537506 pfSpec .getPropSet ().add (pSpec );
538507 pfSpec .getObjectSet ().add (oSpec );
539- List <PropertyFilterSpec > pfSpecArr = new ArrayList <PropertyFilterSpec >();
508+ List <PropertyFilterSpec > pfSpecArr = new ArrayList <>();
540509 pfSpecArr .add (pfSpec );
541510
542511 List <ObjectContent > ocs = _context .getService ().retrieveProperties (_context .getPropertyCollector (), pfSpecArr );
@@ -564,7 +533,7 @@ public ManagedObjectReference getDvSwitchMor(ManagedObjectReference dvPortGroupM
564533
565534 public String getDvSwitchUuid (ManagedObjectReference dvSwitchMor ) throws Exception {
566535 assert (dvSwitchMor != null );
567- return ( String ) _context .getVimClient ().getDynamicProperty (dvSwitchMor , "uuid" );
536+ return _context .getVimClient ().getDynamicProperty (dvSwitchMor , "uuid" );
568537 }
569538
570539 public VirtualEthernetCardDistributedVirtualPortBackingInfo getDvPortBackingInfo (Pair <ManagedObjectReference , String > networkInfo ) throws Exception {
@@ -582,8 +551,8 @@ public VirtualEthernetCardDistributedVirtualPortBackingInfo getDvPortBackingInfo
582551 }
583552
584553 public ManagedObjectReference getDvSwitchMor (String dvSwitchName ) throws Exception {
585- ManagedObjectReference dvSwitchMor = null ;
586- ManagedObjectReference networkFolderMor = null ;
554+ ManagedObjectReference dvSwitchMor ;
555+ ManagedObjectReference networkFolderMor ;
587556 networkFolderMor = _context .getVimClient ().getMoRefProp (_mor , "networkFolder" );
588557 dvSwitchMor = _context .getVimClient ().getDecendentMoRef (networkFolderMor , "VmwareDistributedVirtualSwitch" , dvSwitchName );
589558 return dvSwitchMor ;
@@ -595,7 +564,6 @@ public boolean ensureCustomFieldDef(String fieldName) throws Exception {
595564 }
596565
597566 public DatacenterConfigInfo getDatacenterConfigInfo () throws Exception {
598- DatacenterConfigInfo configInfo = (DatacenterConfigInfo )_context .getVimClient ().getDynamicProperty (_mor , "configuration" );
599- return configInfo ;
567+ return _context .getVimClient ().getDynamicProperty (_mor , "configuration" );
600568 }
601569}
0 commit comments