-
Notifications
You must be signed in to change notification settings - Fork 12
/
VCenterDB.java
1006 lines (883 loc) · 37.9 KB
/
VCenterDB.java
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
/**
* Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
*/
package net.juniper.contrail.vcenter;
import java.io.*;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.URL;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Hashtable;
import java.util.Map;
import java.util.SortedMap;
import java.util.Scanner;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.io.FileNotFoundException;
import com.vmware.vim25.InvalidProperty;
import com.vmware.vim25.RuntimeFault;
import com.vmware.vim25.VirtualMachineToolsRunningStatus;
import org.apache.log4j.Logger;
import com.vmware.vim25.DVPortSetting;
import com.vmware.vim25.DVPortgroupConfigInfo;
import com.vmware.vim25.GuestInfo;
import com.vmware.vim25.GuestNicInfo;
import com.vmware.vim25.IpPool;
import com.vmware.vim25.NetIpConfigInfo;
import com.vmware.vim25.NetIpConfigInfoIpAddress;
import com.vmware.vim25.VMwareDVSPortSetting;
import com.vmware.vim25.VirtualDevice;
import com.vmware.vim25.VirtualDeviceBackingInfo;
import com.vmware.vim25.VirtualEthernetCard;
import com.vmware.vim25.VirtualEthernetCardDistributedVirtualPortBackingInfo;
import com.vmware.vim25.VirtualMachineConfigInfo;
import com.vmware.vim25.VmwareDistributedVirtualSwitchPvlanSpec;
import com.vmware.vim25.VmwareDistributedVirtualSwitchVlanIdSpec;
import com.vmware.vim25.VmwareDistributedVirtualSwitchVlanSpec;
import com.vmware.vim25.DistributedVirtualSwitchKeyedOpaqueBlob;
import com.vmware.vim25.VMwareDVSConfigInfo;
import com.vmware.vim25.VMwareDVSPvlanMapEntry;
import com.vmware.vim25.mo.Datacenter;
import com.vmware.vim25.mo.DistributedVirtualPortgroup;
import com.vmware.vim25.mo.Folder;
import com.vmware.vim25.mo.HostSystem;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.IpPoolManager;
import com.vmware.vim25.mo.ManagedEntity;
import com.vmware.vim25.mo.Network;
import com.vmware.vim25.mo.util.PropertyCollectorUtil;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.VirtualMachine;
import com.vmware.vim25.mo.VmwareDistributedVirtualSwitch;
import com.vmware.vim25.mo.ComputeResource;
public class VCenterDB {
private static final Logger s_logger =
Logger.getLogger(VCenterDB.class);
private static final String esxiToVRouterIpMapFile = "/etc/contrail/ESXiToVRouterIp.map";
static final int VCENTER_READ_TIMEOUT = 30000; //30 sec
static final int VCENTER_WAIT_FOR_UPDATES_TIMEOUT = 120; // 120 seconds
protected final String contrailDvSwitchName;
public final String contrailDataCenterName;
private final String vcenterUrl;
private final String vcenterUsername;
private final String vcenterPassword;
private final String contrailIpFabricPgName;
final Mode mode;
private volatile ServiceInstance serviceInstance;
private volatile Folder rootFolder;
private volatile InventoryNavigator inventoryNavigator;
private volatile IpPoolManager ipPoolManager;
protected volatile Datacenter contrailDC;
protected volatile VmwareDistributedVirtualSwitch contrailDVS;
private volatile ConcurrentMap<String, Datacenter> datacenters;
private volatile ConcurrentMap<String, VmwareDistributedVirtualSwitch> dvswitches;
public volatile Map<String, String> esxiToVRouterIpMap;
public static volatile Map<String, Boolean> vRouterActiveMap;
public VCenterDB(String vcenterUrl, String vcenterUsername,
String vcenterPassword, String contrailDcName,
String contrailDvsName, String ipFabricPgName, Mode mode) {
this.vcenterUrl = vcenterUrl;
this.vcenterUsername = vcenterUsername;
this.vcenterPassword = vcenterPassword;
this.contrailDataCenterName = contrailDcName;
this.contrailDvSwitchName = contrailDvsName;
this.contrailIpFabricPgName = ipFabricPgName;
this.mode = mode;
s_logger.info("VCenterDB(" + contrailDvsName + ", " + ipFabricPgName + ")");
vRouterActiveMap = new HashMap<String, Boolean>();
datacenters = new ConcurrentHashMap<String, Datacenter>();
dvswitches = new ConcurrentHashMap<String, VmwareDistributedVirtualSwitch>();
// Create ESXi host to vRouterVM Ip address map
buildEsxiToVRouterIpMap();
}
private boolean initData() {
rootFolder = null;
rootFolder = serviceInstance.getRootFolder();
if (rootFolder == null) {
s_logger.error("Failed to get rootfolder for vCenter ");
return false;
}
s_logger.info("Got rootfolder for vCenter ");
inventoryNavigator = null;
inventoryNavigator = new InventoryNavigator(rootFolder);
if (inventoryNavigator == null) {
s_logger.error("Failed to get InventoryNavigator for vCenter ");
return false;
}
s_logger.info("Got InventoryNavigator for vCenter ");
ipPoolManager = null;
ipPoolManager = serviceInstance.getIpPoolManager();
if (ipPoolManager == null) {
s_logger.error("Failed to get ipPoolManager for vCenter ");
return false;
}
s_logger.info("Got ipPoolManager for vCenter ");
// Search contrailDc
contrailDC = null;
try {
contrailDC = (Datacenter) inventoryNavigator.searchManagedEntity(
"Datacenter", contrailDataCenterName);
} catch (InvalidProperty e) {
return false;
} catch (RuntimeFault e) {
return false;
} catch (RemoteException e) {
return false;
}
if (contrailDC == null) {
s_logger.error("Failed to find " + contrailDataCenterName
+ " DC on vCenter ");
return false;
}
s_logger.info("Found " + contrailDataCenterName + " DC on vCenter ");
datacenters.put(contrailDataCenterName, contrailDC);
// Search contrailDvSwitch
contrailDVS = null;
try {
contrailDVS = (VmwareDistributedVirtualSwitch)
inventoryNavigator.searchManagedEntity(
"VmwareDistributedVirtualSwitch",
contrailDvSwitchName);
} catch (InvalidProperty e) {
return false;
} catch (RuntimeFault e) {
return false;
} catch (RemoteException e) {
return false;
}
if (contrailDVS == null) {
s_logger.error("Failed to find " + contrailDvSwitchName +
" DVSwitch on vCenter");
return false;
}
s_logger.info("Found " + contrailDvSwitchName + " DVSwitch on vCenter ");
dvswitches.put(contrailDvSwitchName, contrailDVS);
// All well on vCenter front.
return true;
}
public boolean connect() {
while(!createServiceInstance()) {
try{
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
return false;
}
}
while(!initData() ) {
try{
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
return false;
}
}
return true;
}
private boolean createServiceInstance() {
try {
serviceInstance = null;
s_logger.info("Trying to Connect to vCenter Server : " + "("
+ vcenterUrl + "," + vcenterUsername + ")");
serviceInstance = new ServiceInstance(new URL(vcenterUrl),
vcenterUsername, vcenterPassword, true);
if (serviceInstance == null) {
s_logger.error("Failed to connect to vCenter Server : " + "("
+ vcenterUrl + "," + vcenterUsername + ","
+ vcenterPassword + ")" + "Retrying after 5 secs");
return false;
}
s_logger.info("Connected to vCenter Server : " + "("
+ vcenterUrl + "," + vcenterUsername + ","
+ vcenterPassword + ")");
return true;
} catch (MalformedURLException e) {
s_logger.info("Re-Connect unsuccessful: " + e.getMessage());
return false;
} catch (RemoteException e) {
s_logger.info("Re-Connect unsuccessful: " + e.getMessage());
return false;
} catch (Exception e) {
s_logger.error("Error while connecting to vcenter: " + e.getMessage());
e.printStackTrace();
return false;
}
}
public ServiceInstance getServiceInstance() {
return serviceInstance;
}
private boolean buildEsxiToVRouterIpMap() {
esxiToVRouterIpMap = new HashMap<String, String>();
try {
File file = new File("/etc/contrail/ESXiToVRouterIp.map");
Scanner input = new Scanner(file);
while(input.hasNext()) {
String nextLine = input.nextLine();
String[] part = nextLine.split(":");
s_logger.info(" ESXi IP Address:" + part[0] + " vRouter-IP-Address: " + part[1]);
esxiToVRouterIpMap.put(part[0], part[1]);
vRouterActiveMap.put(part[1], true);
}
input.close();
} catch (FileNotFoundException e) {
s_logger.error("file not found :" + esxiToVRouterIpMapFile);
return false;
}
return true;
}
public Map<String, String> getEsxiToVRouterIpMap() {
return esxiToVRouterIpMap;
}
public void setReadTimeout(int milliSecs) {
//Set read timeout on connection to vcenter server
serviceInstance.getServerConnection()
.getVimService()
.getWsc().setReadTimeout(milliSecs);
}
public boolean isConnected() {
return (serviceInstance != null);
}
public IpPoolManager getIpPoolManager() {
return ipPoolManager;
}
public void setIpPoolManager(IpPoolManager _ipPoolManager) {
ipPoolManager = _ipPoolManager;
}
public InventoryNavigator getInventoryNavigator() {
return inventoryNavigator;
}
public void setInventoryNavigator(InventoryNavigator _inventoryNavigator) {
inventoryNavigator = _inventoryNavigator;
}
public Datacenter getDatacenter() {
return contrailDC;
}
public void setDatacenter(Datacenter _dc) {
contrailDC = _dc;
}
protected static String getVirtualMachineMacAddress(
VirtualMachineConfigInfo vmConfigInfo,
DistributedVirtualPortgroup portGroup) {
VirtualDevice devices[] = vmConfigInfo.getHardware().getDevice();
for (VirtualDevice device : devices) {
// Assuming only one interface
if (device instanceof VirtualEthernetCard) {
VirtualDeviceBackingInfo backingInfo =
device.getBacking();
if (backingInfo == null)
continue;
// Is it backed by the distributed virtual port group?
if (backingInfo instanceof
VirtualEthernetCardDistributedVirtualPortBackingInfo) {
VirtualEthernetCardDistributedVirtualPortBackingInfo
dvpBackingInfo =
(VirtualEthernetCardDistributedVirtualPortBackingInfo)
backingInfo;
if ((dvpBackingInfo.getPort() == null) ||
(dvpBackingInfo.getPort().getPortgroupKey() == null))
continue;
if (dvpBackingInfo.getPort().getPortgroupKey().
equals(portGroup.getKey())) {
String vmMac = ((VirtualEthernetCard) device).
getMacAddress();
return vmMac;
}
}
}
}
s_logger.error("dvPg: " + portGroup.getName() + " vmConfig: " +
vmConfigInfo + " MAC Address NOT found");
return null;
}
protected String getVRouterVMIpFabricAddress(String hostName,
HostSystem host, String vmNamePrefix)
throws Exception {
// Find if vRouter Ip Fabric mapping exists..
String vRouterIpAddress = esxiToVRouterIpMap.get(hostName);
if (host.getRuntime().isInMaintenanceMode())
vRouterActiveMap.put(vRouterIpAddress, false);
if (vRouterIpAddress != null) {
return vRouterIpAddress;
}
VirtualMachine[] vms = host.getVms();
for (VirtualMachine vm : vms) {
String vmName = vm.getName();
if (!vmName.toLowerCase().contains(vmNamePrefix.toLowerCase())) {
continue;
}
// Assumption here is that VMware Tools are installed
// and IP address is available
GuestInfo guestInfo = vm.getGuest();
if (guestInfo == null) {
s_logger.debug(" Host: " + hostName +
" vm:" + vmName + " GuestInfo - VMware Tools " +
" NOT installed");
continue;
}
GuestNicInfo[] nicInfos = guestInfo.getNet();
if (nicInfos == null) {
s_logger.debug(" Host: " + hostName +
" vm:" + vmName + " GuestNicInfo - VMware Tools " +
" NOT installed");
continue;
}
for (GuestNicInfo nicInfo : nicInfos) {
// Extract the IP address associated with simple port
// group. Assumption here is that Contrail VRouter VM will
// have only one standard port group
String networkName = nicInfo.getNetwork();
if (networkName == null || !networkName.equals(contrailIpFabricPgName)) {
continue;
}
Network network = (Network)
inventoryNavigator.searchManagedEntity("Network",
networkName);
if (network == null) {
s_logger.debug("Host: " +
hostName + " vm: " + vmName + " network: " +
networkName + " NOT found");
continue;
}
NetIpConfigInfo ipConfigInfo = nicInfo.getIpConfig();
if (ipConfigInfo == null) {
continue;
}
NetIpConfigInfoIpAddress[] ipAddrConfigInfos =
ipConfigInfo.getIpAddress();
if (ipAddrConfigInfos == null ||
ipAddrConfigInfos.length == 0) {
continue;
}
for (NetIpConfigInfoIpAddress ipAddrConfigInfo :
ipAddrConfigInfos) {
String ipAddress = ipAddrConfigInfo.getIpAddress();
// Choose IPv4 only
InetAddress ipAddr = InetAddress.getByName(ipAddress);
if (ipAddr instanceof Inet4Address) {
// found vRouter VM ip-fabric address. Store it.
esxiToVRouterIpMap.put(hostName, ipAddress);
return ipAddress;
}
}
}
}
return null;
}
public static String getVirtualMachineIpAddress(VirtualMachine vm,
String dvPgName) throws Exception {
// Assumption here is that VMware Tools are installed
// and IP address is available
GuestInfo guestInfo = vm.getGuest();
String vmName = vm.getName();
if (guestInfo == null) {
s_logger.debug("dvPg: " + dvPgName + " vm:" + vmName
+ " GuestInfo - VMware Tools " + " NOT installed");
return null;
}
GuestNicInfo[] nicInfos = guestInfo.getNet();
if (nicInfos == null) {
s_logger.debug("dvPg: " + dvPgName + " vm:" + vmName
+ " GuestNicInfo - VMware Tools " + " NOT installed");
return null;
}
for (GuestNicInfo nicInfo : nicInfos) {
// Extract the IP address associated with simple port
// group. Assumption here is that Contrail VRouter VM will
// have only one standard port group
String networkName = nicInfo.getNetwork();
if (networkName == null || !networkName.equals(dvPgName)) {
continue;
}
NetIpConfigInfo ipConfigInfo = nicInfo.getIpConfig();
if (ipConfigInfo == null) {
continue;
}
NetIpConfigInfoIpAddress[] ipAddrConfigInfos =
ipConfigInfo.getIpAddress();
if (ipAddrConfigInfos == null ||
ipAddrConfigInfos.length == 0) {
continue;
}
for (NetIpConfigInfoIpAddress ipAddrConfigInfo :
ipAddrConfigInfos) {
String ipAddress = ipAddrConfigInfo.getIpAddress();
// Choose IPv4 only
InetAddress ipAddr = InetAddress.getByName(ipAddress);
if (ipAddr instanceof Inet4Address) {
return ipAddress;
}
}
}
return null;
}
private static boolean doIgnoreVirtualNetwork(DVPortSetting portSetting) {
// Ignore dvPgs that do not have PVLAN/VLAN configured
if (portSetting instanceof VMwareDVSPortSetting) {
VMwareDVSPortSetting vPortSetting =
(VMwareDVSPortSetting) portSetting;
VmwareDistributedVirtualSwitchVlanSpec vlanSpec =
vPortSetting.getVlan();
if (vlanSpec instanceof VmwareDistributedVirtualSwitchPvlanSpec) {
return false;
}
if (vlanSpec instanceof VmwareDistributedVirtualSwitchVlanIdSpec) {
return false;
}
}
return true;
}
public HashMap<String, Short> getVlanInfo(DistributedVirtualPortgroup dvPg,
DVPortSetting portSetting,
VMwareDVSPvlanMapEntry[] pvlanMapArray) {
// Create HashMap which will store private vlan info
HashMap<String, Short> vlan = new HashMap<String, Short>();
if (portSetting instanceof VMwareDVSPortSetting) {
VMwareDVSPortSetting vPortSetting =
(VMwareDVSPortSetting) portSetting;
VmwareDistributedVirtualSwitchVlanSpec vlanSpec =
vPortSetting.getVlan();
if (vlanSpec instanceof VmwareDistributedVirtualSwitchPvlanSpec) {
// Find isolated secondary VLAN Id
VmwareDistributedVirtualSwitchPvlanSpec pvlanSpec =
(VmwareDistributedVirtualSwitchPvlanSpec) vlanSpec;
short isolatedVlanId = (short)pvlanSpec.getPvlanId();
// Find primaryVLAN corresponding to isolated secondary VLAN
short primaryVlanId = 0;
for (short i=0; i < pvlanMapArray.length; i++) {
if ((short)pvlanMapArray[i].getSecondaryVlanId() != isolatedVlanId)
continue;
if (!pvlanMapArray[i].getPvlanType().equals("isolated"))
continue;
s_logger.debug(" VlanType = PrivateVLAN"
+ " PrimaryVLAN = " + pvlanMapArray[i].getPrimaryVlanId()
+ " IsolatedVLAN = " + pvlanMapArray[i].getSecondaryVlanId());
primaryVlanId = (short)pvlanMapArray[i].getPrimaryVlanId();
}
vlan.put("primary-vlan", primaryVlanId);
vlan.put("secondary-vlan", isolatedVlanId);
} else if (vlanSpec instanceof VmwareDistributedVirtualSwitchVlanIdSpec) {
VmwareDistributedVirtualSwitchVlanIdSpec vlanIdSpec =
(VmwareDistributedVirtualSwitchVlanIdSpec) vlanSpec;
short vlanId = (short)vlanIdSpec.getVlanId();
s_logger.debug(" VlanType = VLAN " + " VlanId = " + vlanId);
vlan.put("primary-vlan", vlanId);
vlan.put("secondary-vlan", vlanId);
} else {
s_logger.error("dvPg: " + dvPg.getName() +
" port setting: " + vPortSetting +
": INVALID vlan spec: " + vlanSpec);
return null;
}
}
return vlan;
}
public boolean getExternalIpamInfo(DVPortgroupConfigInfo configInfo, String vnName) throws Exception {
DistributedVirtualSwitchKeyedOpaqueBlob[] opaqueBlobs
= configInfo.getVendorSpecificConfig();
if ((opaqueBlobs == null) || (opaqueBlobs.length == 0)) {
return false;
}
for (DistributedVirtualSwitchKeyedOpaqueBlob opaqueBlob : opaqueBlobs) {
s_logger.debug("pg (" + vnName + ") " + "opaqueBlob: key ("
+ opaqueBlob.getKey() + " ) opaqueData ("
+ opaqueBlob.getOpaqueData() + ")");
if (opaqueBlob.getKey().equals("external_ipam")) {
if (opaqueBlob.getOpaqueData().equals("true")) {
return true;
} else {
return false;
}
}
}
return false;
}
public boolean getExternalIpamInfo(DistributedVirtualSwitchKeyedOpaqueBlob[] opaqueBlobs, String vnName) {
if ((opaqueBlobs == null) || (opaqueBlobs.length == 0)) {
return false;
}
for (DistributedVirtualSwitchKeyedOpaqueBlob opaqueBlob : opaqueBlobs) {
s_logger.debug("pg (" + vnName + ") " + "opaqueBlob: key ("
+ opaqueBlob.getKey() + " ) opaqueData ("
+ opaqueBlob.getOpaqueData() + ")");
if (opaqueBlob.getKey().equals("external_ipam")) {
if (opaqueBlob.getOpaqueData().equals("true")) {
return true;
} else {
return false;
}
}
}
return false;
}
public String getVcenterUrl() {
return vcenterUrl;
}
public Datacenter getVmwareDatacenter(String name)
throws RemoteException {
String description = "<datacenter " + name
+ ", vCenter " + vcenterUrl + ">.";
if (datacenters.containsKey(name)) {
s_logger.info("Found in cache " + description);
return datacenters.get(name);
}
Folder rootFolder = serviceInstance.getRootFolder();
if (rootFolder == null) {
String msg = "Failed to get rootfolder for vCenter " + vcenterUrl;
s_logger.error(msg);
throw new RemoteException(msg);
}
InventoryNavigator inventoryNavigator = new InventoryNavigator(rootFolder);
Datacenter dc = null;
try {
dc = (Datacenter) inventoryNavigator.searchManagedEntity(
"Datacenter", name);
} catch (RemoteException e ) {
String msg = "Failed to retrieve " + description;
s_logger.error(msg);
throw new RemoteException(msg, e);
}
if (dc == null) {
String msg = "Failed to retrieve " + description;
s_logger.error(msg);
throw new RemoteException(msg);
}
datacenters.put(name, dc);
s_logger.info("Found " + description);
return dc;
}
public VmwareDistributedVirtualSwitch getVmwareDvs(String name,
Datacenter dc, String dcName)
throws RemoteException {
String description = "<dvs " + name
+ ", datacenter " + dcName
+ ", vCenter " + vcenterUrl + ">.";
if (dvswitches.containsKey(name)) {
s_logger.info("Found in cache " + description);
return dvswitches.get(name);
}
InventoryNavigator inventoryNavigator = new InventoryNavigator(dc);
VmwareDistributedVirtualSwitch dvs = null;
try {
dvs = (VmwareDistributedVirtualSwitch)inventoryNavigator.searchManagedEntity(
"VmwareDistributedVirtualSwitch", name);
} catch (RemoteException e ) {
String msg = "Failed to retrieve " + description;
s_logger.error(msg);
throw new RemoteException(msg, e);
}
if (dvs == null) {
String msg = "Failed to retrieve " + description;
s_logger.error(msg);
throw new RemoteException(msg);
}
s_logger.info("Found " + description);
dvswitches.put(name, dvs);
return dvs;
}
public Network getVmwareNetwork(String name,
VmwareDistributedVirtualSwitch dvs, String dvsName, String dcName)
throws RemoteException {
String description = "<network " + name
+ ", dvs " + dvsName + ", datacenter " + dcName
+ ", vCenter " + vcenterUrl + ">.";
// funny but search on the dvs does not work, we need to use rootFolder
InventoryNavigator inventoryNavigator = new InventoryNavigator(rootFolder);
Network nw = null;
try {
nw = (Network)inventoryNavigator.searchManagedEntity(
"Network", name);
} catch (RemoteException e ) {
String msg = "Failed to retrieve " + description;
s_logger.error(msg);
throw new RemoteException(msg, e);
}
if (nw == null) {
String msg = "Failed to retrieve " + description;
s_logger.error(msg);
throw new RemoteException(msg);
}
s_logger.info("Found " + description);
return nw;
}
public DistributedVirtualPortgroup getVmwareDpg(String name,
VmwareDistributedVirtualSwitch dvs, String dvsName, String dcName)
throws RemoteException {
String description = "<dpg " + name + ", dvs " + dvsName
+ ", datacenter " + dcName + ", vCenter " + vcenterUrl + ">.";
// funny but search on the dvs does not work, we need to use rootFolder
InventoryNavigator inventoryNavigator = new InventoryNavigator(rootFolder);
DistributedVirtualPortgroup dpg = null;
try {
dpg = (DistributedVirtualPortgroup)inventoryNavigator.searchManagedEntity(
"DistributedVirtualPortgroup", name);
} catch (RemoteException e ) {
String msg = "Failed to retrieve " + description;
s_logger.error(msg);
throw new RemoteException(msg, e);
}
if (dpg == null) {
String msg = "Failed to retrieve " + description;
s_logger.error(msg);
throw new RemoteException(msg);
}
s_logger.info("Found " + description);
return dpg;
}
public HostSystem getVmwareHost(String name,
Datacenter dc, String dcName)
throws RemoteException {
String description = "<host " + name
+ ", datacenter " + dcName + ", vCenter " + vcenterUrl +">.";
// narrow the search to the dc level
InventoryNavigator inventoryNavigator = new InventoryNavigator(dc);
HostSystem host = null;
try {
host = (HostSystem)inventoryNavigator.searchManagedEntity(
"HostSystem", name);
} catch (RemoteException e ) {
String msg = "Failed to retrieve " + description;
s_logger.error(msg);
throw new RemoteException(msg, e);
}
if (host == null) {
String msg = "Failed to retrieve " + description;
s_logger.error(msg);
throw new RemoteException(msg);
}
s_logger.info("Found " + description);
return host;
}
public VirtualMachine getVmwareVirtualMachine(String name,
HostSystem host, String hostName, String dcName)
throws RemoteException {
String description = "<virtual machine " + name + ", host " + hostName
+ ", datacenter " + dcName + ", vCenter " + vcenterUrl +">.";
// narrow the search to the host level
InventoryNavigator inventoryNavigator = new InventoryNavigator(host);
VirtualMachine vm = null;
try {
vm = (VirtualMachine)inventoryNavigator.searchManagedEntity(
"VirtualMachine", name);
} catch (RemoteException e ) {
String msg = "Failed to retrieve " + description;
s_logger.error(msg);
throw new RemoteException(msg, e);
}
if (vm == null) {
String msg = "Failed to retrieve " + description;
s_logger.error(msg);
throw new RemoteException(msg);
}
s_logger.info("Found " + description);
return vm;
}
public SortedMap<String, VirtualNetworkInfo> readVirtualNetworks()
throws Exception {
SortedMap<String, VirtualNetworkInfo> map =
new ConcurrentSkipListMap<String, VirtualNetworkInfo>();
if (contrailDVS == null) {
s_logger.error("dvSwitch: " + contrailDvSwitchName +
" NOT configured");
return map;
}
// Extract distributed virtual port groups
DistributedVirtualPortgroup[] dvPgs = contrailDVS.getPortgroup();
if (dvPgs == null || dvPgs.length == 0) {
s_logger.error("dvSwitch: " + contrailDvSwitchName +
" Distributed portgroups NOT configured");
return map;
}
// Extract IP Pools
IpPool[] ipPools = ipPoolManager.queryIpPools(contrailDC);
if ((mode != Mode.VCENTER_AS_COMPUTE) && (ipPools == null || ipPools.length == 0)) {
s_logger.debug("dvSwitch: " + contrailDvSwitchName +
" Datacenter: " + contrailDC.getName() + " IP Pools NOT " +
"configured");
return map;
}
// Extract private vlan entries for the virtual switch
VMwareDVSConfigInfo dvsConfigInfo = (VMwareDVSConfigInfo) contrailDVS.getConfig();
if (dvsConfigInfo == null) {
s_logger.error("dvSwitch: " + contrailDvSwitchName +
" Datacenter: " + contrailDC.getName() + " ConfigInfo " +
"is empty");
return map;
}
if (!(dvsConfigInfo instanceof VMwareDVSConfigInfo)) {
s_logger.error("dvSwitch: " + contrailDvSwitchName +
" Datacenter: " + contrailDC.getName() + " ConfigInfo " +
"isn't instanceof VMwareDVSConfigInfo");
return map;
}
VMwareDVSPvlanMapEntry[] pvlanMapArray = dvsConfigInfo.getPvlanConfig();
if (pvlanMapArray == null) {
s_logger.error("dvSwitch: " + contrailDvSwitchName +
" Datacenter: " + contrailDC.getName() + " Private VLAN NOT" +
"configured");
return map;
}
Hashtable[] pTables = PropertyCollectorUtil.retrieveProperties(dvPgs,
"DistributedVirtualPortgroup",
new String[] {"name",
"config.key",
"config.defaultPortConfig",
"config.vendorSpecificConfig",
"summary.ipPoolId",
"summary.ipPoolName",
});
for (int i=0; i < dvPgs.length; i++) {
// Extract dvPg configuration info and port setting
DVPortSetting portSetting = (DVPortSetting) pTables[i].get("config.defaultPortConfig");
if (doIgnoreVirtualNetwork(portSetting)) {
continue;
}
VirtualNetworkInfo vnInfo =
new VirtualNetworkInfo(
this, dvPgs[i], pTables[i], contrailDC, contrailDataCenterName,
contrailDVS, contrailDvSwitchName,
ipPools, pvlanMapArray);
VCenterNotify.watchVn(vnInfo);
map.put(vnInfo.getUuid(), vnInfo);
}
return map;
}
private void readVirtualMachines(SortedMap<String, VirtualMachineInfo> map,
ManagedEntity me,
Datacenter dc, String dcName)
throws Exception {
ManagedEntity[] vms = new InventoryNavigator(me).searchManagedEntities("VirtualMachine");
if (vms == null || vms.length == 0) {
s_logger.debug("Datacenter: " + dcName +
" NO virtual machines connected");
return;
}
String vrouterIpAddress = null;
HostSystem host = null;
// If the passed Managed Entity is at the host-level, then pass
// the hostName and Contrail VM IP Address instead of finding
// it every time for every VM which is costly.
if (me instanceof HostSystem) {
host = (HostSystem) me;
String hostName = host.getName();
vrouterIpAddress = getVRouterVMIpFabricAddress(hostName, host, dcName);
}
Hashtable[] pTables = PropertyCollectorUtil.retrieveProperties(vms, "VirtualMachine",
new String[] {"name",
"config.instanceUuid",
"runtime.powerState",
"runtime.host",
"guest.toolsRunningStatus",
"guest.net"
});
for (int i=0; i < vms.length; i++) {
VirtualMachineInfo vmInfo = new VirtualMachineInfo(this,
dc, dcName,
(VirtualMachine)vms[i], pTables[i],
host, vrouterIpAddress);
readVirtualMachineInterfaces(vmInfo);
// Ignore virtual machine?
if (vmInfo.ignore()) {
s_logger.debug(" Ignoring vm: " + vmInfo.getName());
continue;
}
map.put(vmInfo.getUuid(), vmInfo);
}
}
SortedMap<String, VirtualMachineInfo> readVirtualMachines()
throws IOException, Exception {
SortedMap<String, VirtualMachineInfo> map =
new ConcurrentSkipListMap<String, VirtualMachineInfo>();
/* the method below can be called in a loop to read multiple
* datacenters and read VMs per hosts
* for (dc: datacenters)
* for (host: dc)
readVirtualMachines(map, host, dc, dcName);
*/
Folder hostsFolder = contrailDC.getHostFolder();
List<HostSystem> hostsList = new ArrayList<HostSystem>();
for (ManagedEntity e : hostsFolder.getChildEntity()) {
// This is a cluster resource. Delve deeper to
// find more hosts.
if (e instanceof ComputeResource) {
ComputeResource cluster = (ComputeResource) e;
for(HostSystem host : cluster.getHosts()) {
hostsList.add((HostSystem)host);
}
}
if (e instanceof HostSystem) {
hostsList.add((HostSystem)e);
}
}
for (HostSystem host : hostsList) {
readVirtualMachines(map, host, contrailDC, contrailDataCenterName);
}
return map;
}
public void readVirtualMachineInterfaces(VirtualMachineInfo vmInfo)
throws IOException, Exception {
VirtualMachine vm = vmInfo.vm;
Network[] nets = vm.getNetworks();
for (Network net: nets) {
VirtualNetworkInfo vnInfo = null;
switch (mode) {
case VCENTER_ONLY:
String netName = net.getName();
vnInfo = MainDB.getVnByName(netName);
if (vnInfo == null) {
if (mode == Mode.VCENTER_ONLY) {
s_logger.info("Skipping VMI in unmanaged network " + netName);
continue;
}
}
break;
case VCENTER_AS_COMPUTE:
// network is managed by Openstack or other entity
// UUID is used in the name because name is not unique
String uuid = net.getName();
vnInfo = MainDB.getVnById(uuid);
if (vnInfo == null) {
s_logger.info("Skipping VMI in unmanaged network " + uuid);
continue;
}
break;
default:
throw new Exception("Unhandled mode " + mode.name());
}
VirtualMachineInterfaceInfo vmiInfo =
new VirtualMachineInterfaceInfo(vmInfo, vnInfo);
vmiInfo.setMacAddress(getVirtualMachineMacAddress(vm.getConfig(), vnInfo.getDpg()));
if (mode != Mode.VCENTER_AS_COMPUTE && vnInfo.getExternalIpam() ){
if (vmInfo.getToolsRunningStatus().equals(
VirtualMachineToolsRunningStatus.guestToolsRunning.toString())) {
// static IP Address & vmWare tools installed
// see if we can read it from Guest Nic Info
vmiInfo.setIpAddress(getVirtualMachineIpAddress(vm, vnInfo.getName()));
}
VCenterNotify.watchVm(vmiInfo.vmInfo);
}
vmInfo.created(vmiInfo);
}
}
public boolean isAlive() {
try {
Calendar currTime = serviceInstance.currentTime();
if (currTime == null) {
String msg = "Failed aliveness check for vCenter " + vcenterUrl;
s_logger.error(msg);
return false;
}
} catch (Exception e) {
return false;
}
return true;