forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHostMO.java
More file actions
executable file
·1062 lines (880 loc) · 45.8 KB
/
HostMO.java
File metadata and controls
executable file
·1062 lines (880 loc) · 45.8 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.hypervisor.vmware.mo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
import com.google.gson.Gson;
import com.vmware.vim25.AboutInfo;
import com.vmware.vim25.AlreadyExistsFaultMsg;
import com.vmware.vim25.ClusterDasConfigInfo;
import com.vmware.vim25.ComputeResourceSummary;
import com.vmware.vim25.CustomFieldStringValue;
import com.vmware.vim25.DatastoreSummary;
import com.vmware.vim25.DynamicProperty;
import com.vmware.vim25.HostConfigManager;
import com.vmware.vim25.HostConnectInfo;
import com.vmware.vim25.HostFirewallInfo;
import com.vmware.vim25.HostFirewallRuleset;
import com.vmware.vim25.HostHardwareSummary;
import com.vmware.vim25.HostHyperThreadScheduleInfo;
import com.vmware.vim25.HostIpConfig;
import com.vmware.vim25.HostIpRouteEntry;
import com.vmware.vim25.HostListSummaryQuickStats;
import com.vmware.vim25.HostNetworkInfo;
import com.vmware.vim25.HostNetworkPolicy;
import com.vmware.vim25.HostNetworkSecurityPolicy;
import com.vmware.vim25.HostNetworkTrafficShapingPolicy;
import com.vmware.vim25.HostPortGroup;
import com.vmware.vim25.HostPortGroupSpec;
import com.vmware.vim25.HostRuntimeInfo;
import com.vmware.vim25.HostSystemConnectionState;
import com.vmware.vim25.HostVirtualNic;
import com.vmware.vim25.HostVirtualSwitch;
import com.vmware.vim25.ManagedObjectReference;
import com.vmware.vim25.NasDatastoreInfo;
import com.vmware.vim25.ObjectContent;
import com.vmware.vim25.ObjectSpec;
import com.vmware.vim25.OptionValue;
import com.vmware.vim25.PropertyFilterSpec;
import com.vmware.vim25.PropertySpec;
import com.vmware.vim25.TraversalSpec;
import com.vmware.vim25.VirtualMachineConfigSpec;
import com.vmware.vim25.VirtualNicManagerNetConfig;
import com.cloud.hypervisor.vmware.util.VmwareContext;
import com.cloud.hypervisor.vmware.util.VmwareHelper;
import com.cloud.utils.Pair;
public class HostMO extends BaseMO implements VmwareHypervisorHost {
private static final Logger s_logger = Logger.getLogger(HostMO.class);
Map<String, VirtualMachineMO> _vmCache = new HashMap<String, VirtualMachineMO>();
//Map<String, String> _vmInternalNameMapCache = new HashMap<String, String>();
public HostMO(VmwareContext context, ManagedObjectReference morHost) {
super(context, morHost);
}
public HostMO(VmwareContext context, String morType, String morValue) {
super(context, morType, morValue);
}
public HostHardwareSummary getHostHardwareSummary() throws Exception {
HostConnectInfo hostInfo = _context.getService().queryHostConnectionInfo(_mor);
HostHardwareSummary hardwareSummary = hostInfo.getHost().getHardware();
return hardwareSummary;
}
public HostConfigManager getHostConfigManager() throws Exception {
return (HostConfigManager)_context.getVimClient().getDynamicProperty(_mor, "configManager");
}
public List<VirtualNicManagerNetConfig> getHostVirtualNicManagerNetConfig() throws Exception {
return _context.getVimClient().getDynamicProperty(_mor, "config.virtualNicManagerInfo.netConfig");
}
public List<HostIpRouteEntry> getHostIpRouteEntries() throws Exception {
return _context.getVimClient().getDynamicProperty(_mor, "config.network.routeTableInfo.ipRoute");
}
public HostListSummaryQuickStats getHostQuickStats() throws Exception {
return (HostListSummaryQuickStats)_context.getVimClient().getDynamicProperty(_mor, "summary.quickStats");
}
public HostHyperThreadScheduleInfo getHostHyperThreadInfo() throws Exception {
return (HostHyperThreadScheduleInfo)_context.getVimClient().getDynamicProperty(_mor, "config.hyperThread");
}
public HostNetworkInfo getHostNetworkInfo() throws Exception {
return (HostNetworkInfo)_context.getVimClient().getDynamicProperty(_mor, "config.network");
}
public HostPortGroupSpec getHostPortGroupSpec(String portGroupName) throws Exception {
HostNetworkInfo hostNetInfo = getHostNetworkInfo();
List<HostPortGroup> portGroups = hostNetInfo.getPortgroup();
if (portGroups != null) {
for (HostPortGroup portGroup : portGroups) {
HostPortGroupSpec spec = portGroup.getSpec();
if (spec.getName().equals(portGroupName))
return spec;
}
}
return null;
}
@Override
public String getHyperHostName() throws Exception {
return getName();
}
@Override
public ClusterDasConfigInfo getDasConfig() throws Exception {
ManagedObjectReference morParent = getParentMor();
if (morParent.getType().equals("ClusterComputeResource")) {
ClusterMO clusterMo = new ClusterMO(_context, morParent);
return clusterMo.getDasConfig();
}
return null;
}
@Override
public String getHyperHostDefaultGateway() throws Exception {
List<HostIpRouteEntry> entries = getHostIpRouteEntries();
for (HostIpRouteEntry entry : entries) {
if (entry.getNetwork().equalsIgnoreCase("0.0.0.0"))
return entry.getGateway();
}
throw new Exception("Could not find host default gateway, host is not properly configured?");
}
public HostStorageSystemMO getHostStorageSystemMO() throws Exception {
return new HostStorageSystemMO(_context, (ManagedObjectReference)_context.getVimClient().getDynamicProperty(_mor, "configManager.storageSystem"));
}
public HostDatastoreSystemMO getHostDatastoreSystemMO() throws Exception {
return new HostDatastoreSystemMO(_context, (ManagedObjectReference)_context.getVimClient().getDynamicProperty(_mor, "configManager.datastoreSystem"));
}
public HostDatastoreBrowserMO getHostDatastoreBrowserMO() throws Exception {
return new HostDatastoreBrowserMO(_context, (ManagedObjectReference)_context.getVimClient().getDynamicProperty(_mor, "datastoreBrowser"));
}
private DatastoreMO getHostDatastoreMO(String datastoreName) throws Exception {
ObjectContent[] ocs = getDatastorePropertiesOnHyperHost(new String[] {"name"});
if (ocs != null && ocs.length > 0) {
for (ObjectContent oc : ocs) {
List<DynamicProperty> objProps = oc.getPropSet();
if (objProps != null) {
for (DynamicProperty objProp : objProps) {
if (objProp.getVal().toString().equals(datastoreName))
return new DatastoreMO(_context, oc.getObj());
}
}
}
}
return null;
}
public HostNetworkSystemMO getHostNetworkSystemMO() throws Exception {
HostConfigManager configMgr = getHostConfigManager();
return new HostNetworkSystemMO(_context, configMgr.getNetworkSystem());
}
public HostFirewallSystemMO getHostFirewallSystemMO() throws Exception {
HostConfigManager configMgr = getHostConfigManager();
ManagedObjectReference morFirewall = configMgr.getFirewallSystem();
// only ESX hosts have firewall manager
if (morFirewall != null)
return new HostFirewallSystemMO(_context, morFirewall);
return null;
}
@Override
public ManagedObjectReference getHyperHostDatacenter() throws Exception {
Pair<DatacenterMO, String> dcPair = DatacenterMO.getOwnerDatacenter(getContext(), getMor());
assert (dcPair != null);
return dcPair.first().getMor();
}
@Override
public ManagedObjectReference getHyperHostOwnerResourcePool() throws Exception {
ManagedObjectReference morComputerResource = (ManagedObjectReference)_context.getVimClient().getDynamicProperty(_mor, "parent");
return (ManagedObjectReference)_context.getVimClient().getDynamicProperty(morComputerResource, "resourcePool");
}
@Override
public ManagedObjectReference getHyperHostCluster() throws Exception {
ManagedObjectReference morParent = (ManagedObjectReference)_context.getVimClient().getDynamicProperty(_mor, "parent");
if (morParent.getType().equalsIgnoreCase("ClusterComputeResource")) {
return morParent;
}
assert (false);
throw new Exception("Standalone host is not supported");
}
public ManagedObjectReference[] getHostLocalDatastore() throws Exception {
List<ManagedObjectReference> datastores = _context.getVimClient().getDynamicProperty(_mor, "datastore");
List<ManagedObjectReference> l = new ArrayList<ManagedObjectReference>();
if (datastores != null) {
for (ManagedObjectReference mor : datastores) {
DatastoreSummary summary = (DatastoreSummary)_context.getVimClient().getDynamicProperty(mor, "summary");
if (summary.getType().equalsIgnoreCase("VMFS") && !summary.isMultipleHostAccess())
l.add(mor);
}
}
return l.toArray(new ManagedObjectReference[1]);
}
public HostVirtualSwitch getHostVirtualSwitchByName(String name) throws Exception {
List<HostVirtualSwitch> switches = _context.getVimClient().getDynamicProperty(_mor, "config.network.vswitch");
if (switches != null) {
for (HostVirtualSwitch vswitch : switches) {
if (vswitch.getName().equals(name))
return vswitch;
}
}
return null;
}
public List<HostVirtualSwitch> getHostVirtualSwitch() throws Exception {
return _context.getVimClient().getDynamicProperty(_mor, "config.network.vswitch");
}
public AboutInfo getHostAboutInfo() throws Exception {
return (AboutInfo)_context.getVimClient().getDynamicProperty(_mor, "config.product");
}
public VmwareHostType getHostType() throws Exception {
AboutInfo aboutInfo = getHostAboutInfo();
if ("VMware ESXi".equals(aboutInfo.getName()))
return VmwareHostType.ESXi;
else if ("VMware ESX".equals(aboutInfo.getName()))
return VmwareHostType.ESX;
throw new Exception("Unrecognized VMware host type " + aboutInfo.getName());
}
// default virtual switch is which management network residents on
public HostVirtualSwitch getHostDefaultVirtualSwitch() throws Exception {
String managementPortGroup = getPortGroupNameByNicType(HostVirtualNicType.management);
if (managementPortGroup != null)
return getPortGroupVirtualSwitch(managementPortGroup);
return null;
}
public HostVirtualSwitch getPortGroupVirtualSwitch(String portGroupName) throws Exception {
String vSwitchName = getPortGroupVirtualSwitchName(portGroupName);
if (vSwitchName != null)
return getVirtualSwitchByName(vSwitchName);
return null;
}
public HostVirtualSwitch getVirtualSwitchByName(String vSwitchName) throws Exception {
List<HostVirtualSwitch> vSwitchs = getHostVirtualSwitch();
if (vSwitchs != null) {
for (HostVirtualSwitch vSwitch : vSwitchs) {
if (vSwitch.getName().equals(vSwitchName))
return vSwitch;
}
}
return null;
}
public String getPortGroupVirtualSwitchName(String portGroupName) throws Exception {
HostNetworkInfo hostNetInfo = getHostNetworkInfo();
List<HostPortGroup> portGroups = hostNetInfo.getPortgroup();
if (portGroups != null) {
for (HostPortGroup portGroup : portGroups) {
HostPortGroupSpec spec = portGroup.getSpec();
if (spec.getName().equals(portGroupName))
return spec.getVswitchName();
}
}
return null;
}
public HostPortGroupSpec getPortGroupSpec(String portGroupName) throws Exception {
HostNetworkInfo hostNetInfo = getHostNetworkInfo();
List<HostPortGroup> portGroups = hostNetInfo.getPortgroup();
if (portGroups != null) {
for (HostPortGroup portGroup : portGroups) {
HostPortGroupSpec spec = portGroup.getSpec();
if (spec.getName().equals(portGroupName))
return spec;
}
}
return null;
}
public String getPortGroupNameByNicType(HostVirtualNicType nicType) throws Exception {
assert (nicType != null);
List<VirtualNicManagerNetConfig> netConfigs =
_context.getVimClient().getDynamicProperty(_mor, "config.virtualNicManagerInfo.netConfig");
if (netConfigs != null) {
for (VirtualNicManagerNetConfig netConfig : netConfigs) {
if (netConfig.getNicType().equals(nicType.toString())) {
List<HostVirtualNic> nics = netConfig.getCandidateVnic();
if (nics != null) {
for (HostVirtualNic nic : nics) {
return nic.getPortgroup();
}
}
}
}
}
if (nicType == HostVirtualNicType.management) {
// ESX management network is configured in service console
HostNetworkInfo netInfo = getHostNetworkInfo();
assert (netInfo != null);
List<HostVirtualNic> nics = netInfo.getConsoleVnic();
if (nics != null) {
for (HostVirtualNic nic : nics) {
return nic.getPortgroup();
}
}
}
return null;
}
public boolean hasPortGroup(HostVirtualSwitch vSwitch, String portGroupName) throws Exception {
ManagedObjectReference morNetwork = getNetworkMor(portGroupName);
if (morNetwork != null)
return true;
return false;
}
public void createPortGroup(HostVirtualSwitch vSwitch, String portGroupName, Integer vlanId, HostNetworkSecurityPolicy secPolicy,
HostNetworkTrafficShapingPolicy shapingPolicy) throws Exception {
assert (portGroupName != null);
HostNetworkSystemMO hostNetMo = getHostNetworkSystemMO();
assert (hostNetMo != null);
HostPortGroupSpec spec = new HostPortGroupSpec();
spec.setName(portGroupName);
if (vlanId != null)
spec.setVlanId(vlanId.intValue());
HostNetworkPolicy policy = new HostNetworkPolicy();
if (secPolicy != null)
policy.setSecurity(secPolicy);
policy.setShapingPolicy(shapingPolicy);
spec.setPolicy(policy);
spec.setVswitchName(vSwitch.getName());
hostNetMo.addPortGroup(spec);
}
public void updatePortGroup(HostVirtualSwitch vSwitch, String portGroupName, Integer vlanId, HostNetworkSecurityPolicy secPolicy,
HostNetworkTrafficShapingPolicy shapingPolicy) throws Exception {
assert (portGroupName != null);
HostNetworkSystemMO hostNetMo = getHostNetworkSystemMO();
assert (hostNetMo != null);
HostPortGroupSpec spec = new HostPortGroupSpec();
spec.setName(portGroupName);
if (vlanId != null)
spec.setVlanId(vlanId.intValue());
HostNetworkPolicy policy = new HostNetworkPolicy();
if (secPolicy != null)
policy.setSecurity(secPolicy);
policy.setShapingPolicy(shapingPolicy);
spec.setPolicy(policy);
spec.setVswitchName(vSwitch.getName());
hostNetMo.updatePortGroup(portGroupName, spec);
}
public void deletePortGroup(String portGroupName) throws Exception {
assert (portGroupName != null);
HostNetworkSystemMO hostNetMo = getHostNetworkSystemMO();
assert (hostNetMo != null);
hostNetMo.removePortGroup(portGroupName);
}
public ManagedObjectReference getNetworkMor(String portGroupName) throws Exception {
PropertySpec pSpec = new PropertySpec();
pSpec.setType("Network");
pSpec.getPathSet().add("summary.name");
TraversalSpec host2NetworkTraversal = new TraversalSpec();
host2NetworkTraversal.setType("HostSystem");
host2NetworkTraversal.setPath("network");
host2NetworkTraversal.setName("host2NetworkTraversal");
ObjectSpec oSpec = new ObjectSpec();
oSpec.setObj(_mor);
oSpec.setSkip(Boolean.TRUE);
oSpec.getSelectSet().add(host2NetworkTraversal);
PropertyFilterSpec pfSpec = new PropertyFilterSpec();
pfSpec.getPropSet().add(pSpec);
pfSpec.getObjectSet().add(oSpec);
List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
pfSpecArr.add(pfSpec);
List<ObjectContent> ocs = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);
if (ocs != null) {
for (ObjectContent oc : ocs) {
List<DynamicProperty> props = oc.getPropSet();
if (props != null) {
for (DynamicProperty prop : props) {
if (prop.getVal().equals(portGroupName))
return oc.getObj();
}
}
}
}
return null;
}
public List<ManagedObjectReference> getVmMorsOnNetwork(String portGroupName) throws Exception {
ManagedObjectReference morNetwork = getNetworkMor(portGroupName);
if (morNetwork != null)
return _context.getVimClient().getDynamicProperty(morNetwork, "vm");
return null;
}
public String getHostName() throws Exception {
return (String)_context.getVimClient().getDynamicProperty(_mor, "name");
}
@Override
public synchronized VirtualMachineMO findVmOnHyperHost(String vmName) throws Exception {
if (s_logger.isDebugEnabled())
s_logger.debug("find VM " + vmName + " on host");
VirtualMachineMO vmMo = _vmCache.get(vmName);
if (vmMo != null) {
if (s_logger.isDebugEnabled())
s_logger.debug("VM " + vmName + " found in host cache");
return vmMo;
}
s_logger.info("VM " + vmName + " not found in host cache");
loadVmCache();
return _vmCache.get(vmName);
}
private boolean isUserVMInternalCSName(String vmInternalCSName) {
// CS generated internal names for user VMs are always of the format i-x-y.
String internalCSUserVMNamingPattern = "^[i][-][0-9]+[-][0-9]+[-]";
Pattern p = Pattern.compile(internalCSUserVMNamingPattern);
java.util.regex.Matcher m = p.matcher(vmInternalCSName);
if (m.find()) {
return true;
} else {
return false;
}
}
private void loadVmCache() throws Exception {
if (s_logger.isDebugEnabled())
s_logger.debug("load VM cache on host");
_vmCache.clear();
int key = getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
if (key == 0) {
s_logger.warn("Custom field " + CustomFieldConstants.CLOUD_VM_INTERNAL_NAME + " is not registered ?!");
}
// name is the name of the VM as it appears in vCenter. The CLOUD_VM_INTERNAL_NAME custom
// field value contains the name of the VM as it is maintained internally by cloudstack (i-x-y).
ObjectContent[] ocs = getVmPropertiesOnHyperHost(new String[] {"name", "value[" + key + "]"});
if (ocs != null && ocs.length > 0) {
for (ObjectContent oc : ocs) {
List<DynamicProperty> props = oc.getPropSet();
if (props != null) {
String vmVcenterName = null;
String vmInternalCSName = null;
for (DynamicProperty prop : props) {
if (prop.getName().equals("name")) {
vmVcenterName = prop.getVal().toString();
} else if (prop.getName().startsWith("value[")) {
if (prop.getVal() != null)
vmInternalCSName = ((CustomFieldStringValue)prop.getVal()).getValue();
}
}
String vmName = null;
if (vmInternalCSName != null && isUserVMInternalCSName(vmInternalCSName)) {
vmName = vmInternalCSName;
} else {
vmName = vmVcenterName;
}
if (s_logger.isTraceEnabled())
s_logger.trace("put " + vmName + " into host cache");
_vmCache.put(vmName, new VirtualMachineMO(_context, oc.getObj()));
}
}
}
}
@Override
public VirtualMachineMO findVmOnPeerHyperHost(String name) throws Exception {
ManagedObjectReference morParent = getParentMor();
if (morParent.getType().equals("ClusterComputeResource")) {
ClusterMO clusterMo = new ClusterMO(_context, morParent);
return clusterMo.findVmOnHyperHost(name);
} else {
// we don't support standalone host, all hosts have to be managed by
// a cluster within vCenter
assert (false);
return null;
}
}
@Override
public boolean createVm(VirtualMachineConfigSpec vmSpec) throws Exception {
assert (vmSpec != null);
DatacenterMO dcMo = new DatacenterMO(_context, getHyperHostDatacenter());
ManagedObjectReference morPool = getHyperHostOwnerResourcePool();
ManagedObjectReference morTask = _context.getService().createVMTask(dcMo.getVmFolder(), vmSpec, morPool, _mor);
boolean result = _context.getVimClient().waitForTask(morTask);
if (result) {
_context.waitForTaskProgressDone(morTask);
return true;
} else {
s_logger.error("VMware createVM_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask));
}
return false;
}
public HashMap<String, Integer> getVmVncPortsOnHost() throws Exception {
int key = getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
if (key == 0) {
s_logger.warn("Custom field " + CustomFieldConstants.CLOUD_VM_INTERNAL_NAME + " is not registered ?!");
}
ObjectContent[] ocs = getVmPropertiesOnHyperHost(new String[] {"name", "config.extraConfig[\"RemoteDisplay.vnc.port\"]", "value[" + key + "]"});
HashMap<String, Integer> portInfo = new HashMap<String, Integer>();
if (ocs != null && ocs.length > 0) {
for (ObjectContent oc : ocs) {
List<DynamicProperty> objProps = oc.getPropSet();
if (objProps != null) {
String vmName = null;
String value = null;
String vmInternalCSName = null;
for (DynamicProperty objProp : objProps) {
if (objProp.getName().equals("name")) {
vmName = (String)objProp.getVal();
} else if (objProp.getName().startsWith("value[")) {
if (objProp.getVal() != null)
vmInternalCSName = ((CustomFieldStringValue)objProp.getVal()).getValue();
} else {
OptionValue optValue = (OptionValue)objProp.getVal();
value = (String)optValue.getValue();
}
}
if (vmInternalCSName != null && isUserVMInternalCSName(vmInternalCSName))
vmName = vmInternalCSName;
if (vmName != null && value != null) {
portInfo.put(vmName, Integer.parseInt(value));
}
}
}
}
return portInfo;
}
@Override
public ObjectContent[] getVmPropertiesOnHyperHost(String[] propertyPaths) throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - retrieveProperties() for VM properties. target MOR: " + _mor.getValue() + ", properties: " +
new Gson().toJson(propertyPaths));
PropertySpec pSpec = new PropertySpec();
pSpec.setType("VirtualMachine");
pSpec.getPathSet().addAll(Arrays.asList(propertyPaths));
TraversalSpec host2VmTraversal = new TraversalSpec();
host2VmTraversal.setType("HostSystem");
host2VmTraversal.setPath("vm");
host2VmTraversal.setName("host2VmTraversal");
ObjectSpec oSpec = new ObjectSpec();
oSpec.setObj(_mor);
oSpec.setSkip(Boolean.TRUE);
oSpec.getSelectSet().add(host2VmTraversal);
PropertyFilterSpec pfSpec = new PropertyFilterSpec();
pfSpec.getPropSet().add(pSpec);
pfSpec.getObjectSet().add(oSpec);
List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
pfSpecArr.add(pfSpec);
List<ObjectContent> properties = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - retrieveProperties() done");
return properties.toArray(new ObjectContent[properties.size()]);
}
@Override
public ObjectContent[] getDatastorePropertiesOnHyperHost(String[] propertyPaths) throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - retrieveProperties() on Datastore properties. target MOR: " + _mor.getValue() + ", properties: " +
new Gson().toJson(propertyPaths));
PropertySpec pSpec = new PropertySpec();
pSpec.setType("Datastore");
pSpec.getPathSet().addAll(Arrays.asList(propertyPaths));
TraversalSpec host2DatastoreTraversal = new TraversalSpec();
host2DatastoreTraversal.setType("HostSystem");
host2DatastoreTraversal.setPath("datastore");
host2DatastoreTraversal.setName("host2DatastoreTraversal");
ObjectSpec oSpec = new ObjectSpec();
oSpec.setObj(_mor);
oSpec.setSkip(Boolean.TRUE);
oSpec.getSelectSet().add(host2DatastoreTraversal);
PropertyFilterSpec pfSpec = new PropertyFilterSpec();
pfSpec.getPropSet().add(pSpec);
pfSpec.getObjectSet().add(oSpec);
List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
pfSpecArr.add(pfSpec);
List<ObjectContent> properties = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - retrieveProperties() done");
return properties.toArray(new ObjectContent[properties.size()]);
}
public List<Pair<ManagedObjectReference, String>> getDatastoreMountsOnHost() throws Exception {
List<Pair<ManagedObjectReference, String>> mounts = new ArrayList<Pair<ManagedObjectReference, String>>();
ObjectContent[] ocs = getDatastorePropertiesOnHyperHost(new String[] {String.format("host[\"%s\"].mountInfo.path", _mor.getValue())});
if (ocs != null) {
for (ObjectContent oc : ocs) {
Pair<ManagedObjectReference, String> mount = new Pair<ManagedObjectReference, String>(oc.getObj(), oc.getPropSet().get(0).getVal().toString());
mounts.add(mount);
}
}
return mounts;
}
public List<Pair<ManagedObjectReference, String>> getLocalDatastoreOnHost() throws Exception {
List<Pair<ManagedObjectReference, String>> dsList = new ArrayList<Pair<ManagedObjectReference, String>>();
ObjectContent[] ocs = getDatastorePropertiesOnHyperHost(new String[] {"name", "summary"});
if (ocs != null) {
for (ObjectContent oc : ocs) {
DatastoreSummary dsSummary = (DatastoreSummary)VmwareHelper.getPropValue(oc, "summary");
if (dsSummary.isMultipleHostAccess() == false && dsSummary.isAccessible() && dsSummary.getType().equalsIgnoreCase("vmfs")) {
ManagedObjectReference morDs = oc.getObj();
String name = (String)VmwareHelper.getPropValue(oc, "name");
if (!name.startsWith("-iqn.") && !name.startsWith("_iqn.")) {
dsList.add(new Pair<ManagedObjectReference, String>(morDs, name));
}
}
}
}
return dsList;
}
public void importVmFromOVF(String ovfFilePath, String vmName, String datastoreName, String diskOption) throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - importVmFromOVF(). target MOR: " + _mor.getValue() + ", ovfFilePath: " + ovfFilePath + ", vmName: " + vmName +
",datastoreName: " + datastoreName + ", diskOption: " + diskOption);
DatastoreMO dsMo = getHostDatastoreMO(datastoreName);
if (dsMo == null)
throw new Exception("Invalid datastore name: " + datastoreName);
importVmFromOVF(ovfFilePath, vmName, dsMo, diskOption);
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - importVmFromOVF() done");
}
@Override
public void importVmFromOVF(String ovfFilePath, String vmName, DatastoreMO dsMo, String diskOption) throws Exception {
ManagedObjectReference morRp = getHyperHostOwnerResourcePool();
assert (morRp != null);
HypervisorHostHelper.importVmFromOVF(this, ovfFilePath, vmName, dsMo, diskOption, morRp, _mor);
}
@Override
public boolean createBlankVm(String vmName, String vmInternalCSName, int cpuCount, int cpuSpeedMHz, int cpuReservedMHz, boolean limitCpuUse, int memoryMB,
int memoryReserveMB, String guestOsIdentifier, ManagedObjectReference morDs, boolean snapshotDirToParent) throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - createBlankVm(). target MOR: " + _mor.getValue() + ", vmName: " + vmName + ", cpuCount: " + cpuCount + ", cpuSpeedMhz: " +
cpuSpeedMHz + ", cpuReservedMHz: " + cpuReservedMHz + ", limitCpu: " + limitCpuUse + ", memoryMB: " + memoryMB + ", guestOS: " + guestOsIdentifier +
", datastore: " + morDs.getValue() + ", snapshotDirToParent: " + snapshotDirToParent);
boolean result =
HypervisorHostHelper.createBlankVm(this, vmName, vmInternalCSName, cpuCount, cpuSpeedMHz, cpuReservedMHz, limitCpuUse, memoryMB, memoryReserveMB,
guestOsIdentifier, morDs, snapshotDirToParent);
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - createBlankVm() done");
return result;
}
public ManagedObjectReference getExistingDataStoreOnHost(boolean vmfsDatastore, String hostAddress, int hostPort, String path, String uuid,
HostDatastoreSystemMO hostDatastoreSystemMo) {
// First retrieve the list of Datastores on the host.
List<ManagedObjectReference> morArray;
try {
morArray = hostDatastoreSystemMo.getDatastores();
} catch (Exception e) {
s_logger.info("Failed to retrieve list of Managed Object References");
return null;
}
// Next, get all the NAS datastores from this array of datastores.
if (morArray.size() > 0) {
int i;
for (i = 0; i < morArray.size(); i++) {
NasDatastoreInfo nasDS;
try {
nasDS = hostDatastoreSystemMo.getNasDatastoreInfo(morArray.get(i));
if (nasDS != null) {
//DatastoreInfo info = (DatastoreInfo)_context.getServiceUtil().getDynamicProperty(morDatastore, "info");
if (nasDS.getNas().getRemoteHost().equalsIgnoreCase(hostAddress) && nasDS.getNas().getRemotePath().equalsIgnoreCase(path)) {
return morArray.get(i);
}
}
} catch (Exception e) {
s_logger.info("Encountered exception when retrieving nas datastore info");
return null;
}
}
}
return null;
}
@Override
public ManagedObjectReference mountDatastore(boolean vmfsDatastore, String poolHostAddress, int poolHostPort, String poolPath, String poolUuid) throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - mountDatastore(). target MOR: " + _mor.getValue() + ", vmfs: " + vmfsDatastore + ", poolHost: " + poolHostAddress +
", poolHostPort: " + poolHostPort + ", poolPath: " + poolPath + ", poolUuid: " + poolUuid);
HostDatastoreSystemMO hostDatastoreSystemMo = getHostDatastoreSystemMO();
ManagedObjectReference morDatastore = hostDatastoreSystemMo.findDatastore(poolUuid);
if (morDatastore == null) {
if (!vmfsDatastore) {
try {
morDatastore = hostDatastoreSystemMo.createNfsDatastore(poolHostAddress, poolHostPort, poolPath, poolUuid);
} catch (AlreadyExistsFaultMsg e) {
s_logger.info("Creation of NFS datastore on vCenter failed since datastore already exists." +
" Details: vCenter API trace - mountDatastore(). target MOR: " + _mor.getValue() + ", vmfs: " + vmfsDatastore + ", poolHost: " + poolHostAddress +
", poolHostPort: " + poolHostPort + ", poolPath: " + poolPath + ", poolUuid: " + poolUuid);
// Retrieve the morDatastore and return it.
return (getExistingDataStoreOnHost(vmfsDatastore, poolHostAddress, poolHostPort, poolPath, poolUuid, hostDatastoreSystemMo));
} catch (Exception e) {
s_logger.info("Creation of NFS datastore on vCenter failed. " + " Details: vCenter API trace - mountDatastore(). target MOR: " + _mor.getValue() +
", vmfs: " + vmfsDatastore + ", poolHost: " + poolHostAddress + ", poolHostPort: " + poolHostPort + ", poolPath: " + poolPath + ", poolUuid: " +
poolUuid + ". Exception mesg: " + e.getMessage());
throw new Exception("Creation of NFS datastore on vCenter failed.");
}
if (morDatastore == null) {
String msg = "Unable to create NFS datastore. host: " + poolHostAddress + ", port: " + poolHostPort + ", path: " + poolPath + ", uuid: " + poolUuid;
s_logger.error(msg);
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - mountDatastore() done(failed)");
throw new Exception(msg);
}
} else {
morDatastore = _context.getDatastoreMorByPath(poolPath);
if (morDatastore == null) {
String msg = "Unable to create VMFS datastore. host: " + poolHostAddress + ", port: " + poolHostPort + ", path: " + poolPath + ", uuid: " + poolUuid;
s_logger.error(msg);
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - mountDatastore() done(failed)");
throw new Exception(msg);
}
DatastoreMO dsMo = new DatastoreMO(_context, morDatastore);
dsMo.setCustomFieldValue(CustomFieldConstants.CLOUD_UUID, poolUuid);
}
}
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - mountDatastore() done(successfully)");
return morDatastore;
}
@Override
public void unmountDatastore(String uuid) throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - unmountDatastore(). target MOR: " + _mor.getValue() + ", uuid: " + uuid);
HostDatastoreSystemMO hostDatastoreSystemMo = getHostDatastoreSystemMO();
if (!hostDatastoreSystemMo.deleteDatastore(uuid)) {
String msg = "Unable to unmount datastore. uuid: " + uuid;
s_logger.error(msg);
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - unmountDatastore() done(failed)");
throw new Exception(msg);
}
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - unmountDatastore() done");
}
@Override
public ManagedObjectReference findDatastore(String poolUuid) throws Exception {
HostDatastoreSystemMO hostDsMo = getHostDatastoreSystemMO();
return hostDsMo.findDatastore(poolUuid);
}
@Override
public ManagedObjectReference findDatastoreByExportPath(String exportPath) throws Exception {
HostDatastoreSystemMO datastoreSystemMo = getHostDatastoreSystemMO();
return datastoreSystemMo.findDatastoreByExportPath(exportPath);
}
@Override
public ManagedObjectReference findDatastoreByName(String datastoreName) throws Exception {
HostDatastoreSystemMO hostDsMo = getHostDatastoreSystemMO();
return hostDsMo.findDatastoreByName(datastoreName);
}
@Override
public ManagedObjectReference findMigrationTarget(VirtualMachineMO vmMo) throws Exception {
return _mor;
}
@Override
public VmwareHypervisorHostResourceSummary getHyperHostResourceSummary() throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - getHyperHostResourceSummary(). target MOR: " + _mor.getValue());
VmwareHypervisorHostResourceSummary summary = new VmwareHypervisorHostResourceSummary();
HostHardwareSummary hardwareSummary = getHostHardwareSummary();
// TODO: not sure how hyper-thread is counted in VMware resource pool
summary.setCpuCount(hardwareSummary.getNumCpuThreads());
summary.setMemoryBytes(hardwareSummary.getMemorySize());
summary.setCpuSpeed(hardwareSummary.getCpuMhz());
summary.setCpuSockets((int)hardwareSummary.getNumCpuPkgs());
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - getHyperHostResourceSummary() done");
return summary;
}
@Override
public VmwareHypervisorHostNetworkSummary getHyperHostNetworkSummary(String managementPortGroup) throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - getHyperHostNetworkSummary(). target MOR: " + _mor.getValue() + ", mgmtPortgroup: " + managementPortGroup);
VmwareHypervisorHostNetworkSummary summary = new VmwareHypervisorHostNetworkSummary();
if (getHostType() == VmwareHostType.ESXi) {
List<VirtualNicManagerNetConfig> netConfigs =
_context.getVimClient().getDynamicProperty(_mor, "config.virtualNicManagerInfo.netConfig");
assert (netConfigs != null);
for (VirtualNicManagerNetConfig netConfig : netConfigs) {
if (netConfig.getNicType().equals("management")) {
for (HostVirtualNic nic : netConfig.getCandidateVnic()) {
if (nic.getPortgroup().equals(managementPortGroup)) {
summary.setHostIp(nic.getSpec().getIp().getIpAddress());
summary.setHostNetmask(nic.getSpec().getIp().getSubnetMask());
summary.setHostMacAddress(nic.getSpec().getMac());
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - getHyperHostNetworkSummary() done(successfully)");
return summary;
}
}
}
}
} else {
// try with ESX path
List<HostVirtualNic> hostVNics = _context.getVimClient().getDynamicProperty(_mor, "config.network.consoleVnic");
if (hostVNics != null) {
for (HostVirtualNic vnic : hostVNics) {
if (vnic.getPortgroup().equals(managementPortGroup)) {
summary.setHostIp(vnic.getSpec().getIp().getIpAddress());
summary.setHostNetmask(vnic.getSpec().getIp().getSubnetMask());
summary.setHostMacAddress(vnic.getSpec().getMac());
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - getHyperHostNetworkSummary() done(successfully)");
return summary;
}
}
}
}
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - getHyperHostNetworkSummary() done(failed)");
throw new Exception("Uanble to find management port group " + managementPortGroup);
}
@Override
public ComputeResourceSummary getHyperHostHardwareSummary() throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - getHyperHostHardwareSummary(). target MOR: " + _mor.getValue());
//
// This is to adopt the model when using Cluster as a big host while ComputeResourceSummary is used
// directly from VMware resource pool
//
// When we break cluster hosts into individual hosts used in our resource allocator,
// we will have to populate ComputeResourceSummary by ourselves here
//
HostHardwareSummary hardwareSummary = getHostHardwareSummary();
ComputeResourceSummary resourceSummary = new ComputeResourceSummary();
// TODO: not sure how hyper-threading is counted in VMware
resourceSummary.setNumCpuCores(hardwareSummary.getNumCpuCores());
// Note: memory here is in Byte unit
resourceSummary.setTotalMemory(hardwareSummary.getMemorySize());
// Total CPU is based on (# of cores) x Mhz
int totalCpu = hardwareSummary.getCpuMhz() * hardwareSummary.getNumCpuCores();
resourceSummary.setTotalCpu(totalCpu);
HostListSummaryQuickStats stats = getHostQuickStats();
if (stats.getOverallCpuUsage() == null || stats.getOverallMemoryUsage() == null)
throw new Exception("Unable to get valid overal CPU/Memory usage data, host may be disconnected");
resourceSummary.setEffectiveCpu(totalCpu - stats.getOverallCpuUsage());
// Note effective memory is in MB unit
resourceSummary.setEffectiveMemory(hardwareSummary.getMemorySize() / (1024 * 1024) - stats.getOverallMemoryUsage());
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - getHyperHostHardwareSummary() done");
return resourceSummary;
}
@Override
public boolean isHyperHostConnected() throws Exception {
HostRuntimeInfo runtimeInfo = (HostRuntimeInfo)_context.getVimClient().getDynamicProperty(_mor, "runtime");
return runtimeInfo.getConnectionState() == HostSystemConnectionState.CONNECTED;
}
public boolean revertToSnapshot(ManagedObjectReference morSnapshot) throws Exception {