-
Notifications
You must be signed in to change notification settings - Fork 12
/
VirtualMachineInfo.java
545 lines (457 loc) · 17.1 KB
/
VirtualMachineInfo.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
package net.juniper.contrail.vcenter;
import com.vmware.vim25.VirtualMachinePowerState;
import com.vmware.vim25.VirtualMachineRuntimeInfo;
import com.vmware.vim25.VirtualMachineConfigInfo;
import com.vmware.vim25.VirtualMachineToolsRunningStatus;
import com.vmware.vim25.mo.HostSystem;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.concurrent.ConcurrentSkipListMap;
import com.vmware.vim25.Event;
import com.vmware.vim25.GuestNicInfo;
import com.vmware.vim25.GuestInfo;
import com.vmware.vim25.ManagedObjectReference;
public class VirtualMachineInfo extends VCenterObject {
private String uuid; // required attribute, key for this object
private String name;
private String displayName;
private String hostName;
private String vrouterIpAddress;
private VirtualMachinePowerState powerState;
private String toolsRunningStatus = VirtualMachineToolsRunningStatus.guestToolsNotRunning.toString();
private SortedMap<String, VirtualMachineInterfaceInfo> vmiInfoMap;
/* keyed by MAC address, contains only interfaces
* belonging to managed networks
*/
protected static final String contrailVRouterVmNamePrefix = "contrailVM";
// Vmware objects
com.vmware.vim25.mo.VirtualMachine vm;
com.vmware.vim25.mo.HostSystem host;
com.vmware.vim25.mo.VmwareDistributedVirtualSwitch dvs;
String dvsName;
com.vmware.vim25.mo.Datacenter dc;
String dcName;
//API server objects
net.juniper.contrail.api.types.VirtualMachine apiVm;
public VirtualMachineInfo(String uuid) {
if (uuid == null) {
throw new IllegalArgumentException("Cannot init VM with null uuid");
}
this.uuid = uuid;
vmiInfoMap = new ConcurrentSkipListMap<String, VirtualMachineInterfaceInfo>();
}
public VirtualMachineInfo(String uuid, String name, String hostName, String vrouterIpAddress,
VirtualMachinePowerState powerState)
{
this.uuid = uuid;
this.name = name;
this.displayName = name;
this.hostName = hostName;
this.vrouterIpAddress = vrouterIpAddress;
this.powerState = powerState;
vmiInfoMap = new ConcurrentSkipListMap<String, VirtualMachineInterfaceInfo>();
}
public VirtualMachineInfo(VirtualMachineInfo vmInfo)
{
if (vmInfo == null) {
throw new IllegalArgumentException("Cannot init VM from null VM");
}
this.uuid = vmInfo.uuid;
this.name = vmInfo.name;
this.displayName = vmInfo.displayName;
this.hostName = vmInfo.hostName;
this.vrouterIpAddress = vmInfo.vrouterIpAddress;
this.powerState = vmInfo.powerState;
this.vmiInfoMap = vmInfo.vmiInfoMap;
}
public VirtualMachineInfo(Event event, VCenterDB vcenterDB, VncDB vncDB) throws Exception {
if (event.getDatacenter() != null) {
dcName = event.getDatacenter().getName();
dc = vcenterDB.getVmwareDatacenter(dcName);
}
if (event.getDvs() != null) {
dvsName = event.getDvs().getName();
dvs = vcenterDB.getVmwareDvs(dvsName, dc, dcName);
} else {
dvsName = vcenterDB.contrailDvSwitchName;
dvs = vcenterDB.getVmwareDvs(dvsName, dc, dcName);
}
if (event.getHost() != null) {
hostName = event.getHost().getName();
host = vcenterDB.getVmwareHost(hostName, dc, dcName);
if (event.getVm() != null) {
name = event.getVm().getName();
vm = vcenterDB.getVmwareVirtualMachine(name, host, hostName, dcName);
if (vcenterDB.mode == Mode.VCENTER_AS_COMPUTE
&& !name.toLowerCase().contains(
contrailVRouterVmNamePrefix.toLowerCase())) {
displayName = vm.getConfig().getAnnotation();
} else {
displayName = name;
}
}
}
vrouterIpAddress = vcenterDB.getVRouterVMIpFabricAddress(
hostName, host, contrailVRouterVmNamePrefix);
VirtualMachineConfigInfo config = vm.getConfig();
if (config != null) {
uuid = config.getInstanceUuid();
}
VirtualMachineRuntimeInfo vmRuntimeInfo = vm.getRuntime();
if (vmRuntimeInfo != null) {
powerState = vmRuntimeInfo.getPowerState();
}
GuestInfo guest = vm.getGuest();
if (guest != null) {
toolsRunningStatus = guest.getToolsRunningStatus();
}
vmiInfoMap = new ConcurrentSkipListMap<String, VirtualMachineInterfaceInfo>();
vcenterDB.readVirtualMachineInterfaces(this);
if (vcenterDB.mode == Mode.VCENTER_AS_COMPUTE) {
// ipAddress and UUID must be read from Vnc
for (Map.Entry<String, VirtualMachineInterfaceInfo> entry:
vmiInfoMap.entrySet()) {
VirtualMachineInterfaceInfo vmiInfo = entry.getValue();
vncDB.readVirtualMachineInterface(vmiInfo);
}
}
}
public VirtualMachineInfo(net.juniper.contrail.api.types.VirtualMachine vm) {
if (vm == null) {
throw new IllegalArgumentException("Cannot init VM with null API VM");
}
vmiInfoMap = new ConcurrentSkipListMap<String, VirtualMachineInterfaceInfo>();
apiVm = vm;
uuid = vm.getUuid();
name = vm.getName();
displayName = vm.getDisplayName();
}
public VirtualMachineInfo(VCenterDB vcenterDB,
com.vmware.vim25.mo.Datacenter dc, String dcName,
com.vmware.vim25.mo.VirtualMachine vm, @SuppressWarnings("rawtypes") Hashtable pTable,
com.vmware.vim25.mo.HostSystem host,
String vrouterIpAddress)
throws Exception {
if (vcenterDB == null || dc == null || dcName == null
|| vm == null || pTable == null) {
throw new IllegalArgumentException("Cannot init VM");
}
this.dc = dc;
this.dcName = dcName;
this.vm = vm;
// Name
uuid = (String) pTable.get("config.instanceUuid");
name = (String) pTable.get("name");
if (vcenterDB.mode == Mode.VCENTER_AS_COMPUTE
&& !name.toLowerCase().contains(
contrailVRouterVmNamePrefix.toLowerCase())) {
displayName = (String) pTable.get("config.annotation");
} else {
displayName = name;
}
if (host == null) {
ManagedObjectReference hostHmor = (ManagedObjectReference) pTable.get("runtime.host");
host = new HostSystem(vm.getServerConnection(), hostHmor);
}
hostName = host.getName();
powerState = (VirtualMachinePowerState)pTable.get("runtime.powerState");
toolsRunningStatus = (String) pTable.get("guest.toolsRunningStatus");
if (vrouterIpAddress == null) {
vrouterIpAddress = vcenterDB.getVRouterVMIpFabricAddress(
hostName, host, contrailVRouterVmNamePrefix);
}
this.vrouterIpAddress = vrouterIpAddress;
vmiInfoMap = new ConcurrentSkipListMap<String, VirtualMachineInterfaceInfo>();
}
public String getHostName() {
return hostName;
}
public void setHostName(String hostName) {
this.hostName = hostName;
}
public String getVrouterIpAddress() {
return vrouterIpAddress;
}
public void setVrouterIpAddress(String vrouterIpAddress) {
this.vrouterIpAddress = vrouterIpAddress;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public VirtualMachinePowerState getPowerState() {
return powerState;
}
public void setPowerState(VirtualMachinePowerState powerState) {
this.powerState = powerState;
}
public boolean isPowerStateEqual(VirtualMachinePowerState powerState) {
if (this.powerState == powerState)
return true;
else
return false;
}
public boolean isPoweredOnState() {
if (powerState == VirtualMachinePowerState.poweredOn)
return true;
else
return false;
}
public String getToolsRunningStatus() {
return toolsRunningStatus;
}
public void setToolsRunningStatus(String toolsRunningStatus) {
this.toolsRunningStatus = toolsRunningStatus;
}
public SortedMap<String, VirtualMachineInterfaceInfo> getVmiInfo() {
return vmiInfoMap;
}
public void setVmiInfo(SortedMap<String, VirtualMachineInterfaceInfo> vmiInfoMap) {
this.vmiInfoMap = vmiInfoMap;
}
public void updatedGuestNics(GuestNicInfo[] nics, VncDB vncDB)
throws Exception {
if (nics == null) {
return;
}
for (GuestNicInfo nic: nics) {
if (nic == null) {
continue;
}
String mac = nic.getMacAddress();
if (vmiInfoMap.containsKey(mac)) {
VirtualMachineInterfaceInfo oldVmi = vmiInfoMap.get(mac);
oldVmi.updatedGuestNic(nic, vncDB);
}
}
}
public void created(VirtualMachineInterfaceInfo vmiInfo) {
vmiInfoMap.put(vmiInfo.getMacAddress(), vmiInfo);
}
public void updated(VirtualMachineInterfaceInfo vmiInfo) {
if (!vmiInfoMap.containsKey(vmiInfo.getMacAddress())) {
vmiInfoMap.put(vmiInfo.getMacAddress(), vmiInfo);
}
}
public void deleted(VirtualMachineInterfaceInfo vmiInfo) {
if (vmiInfoMap.containsKey(vmiInfo.getMacAddress())) {
vmiInfoMap.remove(vmiInfo.getMacAddress());
}
}
public boolean contains(VirtualMachineInterfaceInfo vmiInfo) {
return vmiInfoMap.containsKey(vmiInfo.getMacAddress());
}
public boolean equals(VirtualMachineInfo vm) {
if (vm == null) {
return false;
}
if ((uuid != null && !uuid.equals(vm.uuid))
|| (uuid == null && vm.uuid != null)) {
return false;
}
if ((name != null && !name.equals(vm.name))
|| (name == null && vm.name != null)) {
return false;
}
if ((displayName != null && !displayName.equals(vm.displayName))
|| (displayName == null && vm.displayName != null)) {
return false;
}
if ((vrouterIpAddress != null && !vrouterIpAddress.equals(vm.vrouterIpAddress))
|| (vrouterIpAddress == null && vm.vrouterIpAddress != null)) {
return false;
}
if ((hostName != null && !hostName.equals(vm.hostName))
|| (hostName == null && vm.hostName != null)) {
return false;
}
if ((powerState != null && !powerState.equals(vm.powerState))
|| (powerState == null && vm.powerState != null)) {
return false;
}
return equalVmi(vm);
}
public boolean equalVmi(VirtualMachineInfo vm) {
if (vm == null) {
return false;
}
if (vmiInfoMap.size() != vm.vmiInfoMap.size()) {
return false;
}
Iterator<Entry<String, VirtualMachineInterfaceInfo>> iter1 =
vmiInfoMap.entrySet().iterator();
Iterator<Entry<String, VirtualMachineInterfaceInfo>> iter2 =
vm.vmiInfoMap.entrySet().iterator();
while (iter1.hasNext() && iter2.hasNext()) {
Entry<String, VirtualMachineInterfaceInfo> entry1 = iter1.next();
Entry<String, VirtualMachineInterfaceInfo> entry2 = iter2.next();
if (!entry1.getKey().equals(entry2.getKey())
|| !entry1.getValue().equals(entry2.getValue())) {
return false;
}
}
return true;
}
public String toString() {
return "VM <" + displayName + ", host " + hostName + ", " + uuid + ">";
}
public StringBuffer toStringBuffer() {
StringBuffer s = new StringBuffer(
"VM <" + displayName + ", host " + hostName + ", " + uuid + ">\n\n");
for (Map.Entry<String, VirtualMachineInterfaceInfo> entry:
vmiInfoMap.entrySet()) {
VirtualMachineInterfaceInfo vmiInfo = entry.getValue();
s.append("\t")
.append(vmiInfo).append("\n");
}
s.append("\n");
return s;
}
boolean ignore() {
// by design we skip unconnected VMs
if (vrouterIpAddress == null || vmiInfoMap.size() == 0) {
return true;
}
return false;
}
@Override
void create(VncDB vncDB) throws Exception {
if (ignore()) {
return;
}
vncDB.createVirtualMachine(this);
for (Map.Entry<String, VirtualMachineInterfaceInfo> entry:
vmiInfoMap.entrySet()) {
VirtualMachineInterfaceInfo vmiInfo = entry.getValue();
vmiInfo.create(vncDB);
}
MainDB.created(this);
}
@Override
void update(
VCenterObject obj,
VncDB vncDB) throws Exception {
VirtualMachineInfo newVmInfo = (VirtualMachineInfo)obj;
if (newVmInfo.hostName != null) {
hostName = newVmInfo.hostName;
}
if (newVmInfo.name != null) {
name = newVmInfo.name;
}
if (newVmInfo.displayName != null) {
displayName = newVmInfo.displayName;
}
if (newVmInfo.powerState != null) {
powerState = newVmInfo.powerState;
}
if (newVmInfo.toolsRunningStatus != null) {
toolsRunningStatus = newVmInfo.toolsRunningStatus;
}
if (newVmInfo.vm != null) {
vm = newVmInfo.vm;
}
if (newVmInfo.host != null) {
host = newVmInfo.host;
}
if (newVmInfo.dvs != null) {
dvs = newVmInfo.dvs;
}
if (newVmInfo.dvsName != null) {
dvsName = newVmInfo.dvsName;
}
if (newVmInfo.dc != null) {
dc = newVmInfo.dc;
}
if (newVmInfo.dcName != null) {
dcName = newVmInfo.dcName;
}
if (newVmInfo.apiVm != null) {
apiVm = newVmInfo.apiVm;
}
if (apiVm != null) {
newVmInfo.apiVm = apiVm;
}
if (newVmInfo.vrouterIpAddress != null
&& newVmInfo.vrouterIpAddress.equals(vrouterIpAddress)) {
for (Map.Entry<String, VirtualMachineInterfaceInfo> entry:
newVmInfo.vmiInfoMap.entrySet()) {
VirtualMachineInterfaceInfo vmiInfo = entry.getValue();
vmiInfo.setVmInfo(this);
}
MainDB.update(vmiInfoMap, newVmInfo.vmiInfoMap);
} else {
// VM has been migrated to new host
// delete ports on the old vrouter and create ports on the new vrouter
for (Map.Entry<String, VirtualMachineInterfaceInfo> entry:
vmiInfoMap.entrySet()) {
VirtualMachineInterfaceInfo vmiInfo = entry.getValue();
vmiInfo.deletePort();
}
vrouterIpAddress = newVmInfo.vrouterIpAddress;
for (Map.Entry<String, VirtualMachineInterfaceInfo> entry:
vmiInfoMap.entrySet()) {
VirtualMachineInterfaceInfo vmiInfo = entry.getValue();
vmiInfo.addPort();
}
}
if (vmiInfoMap.size() == 0) {
delete(vncDB);
}
}
@Override
void sync(
VCenterObject obj,
VncDB vncDB) throws Exception {
VirtualMachineInfo oldVmInfo = (VirtualMachineInfo)obj;
if (apiVm == null && oldVmInfo.apiVm != null) {
apiVm = oldVmInfo.apiVm;
}
if (vrouterIpAddress != null && oldVmInfo.vrouterIpAddress == null) {
oldVmInfo.vrouterIpAddress = vrouterIpAddress;
}
// in what cases do we really update the VM
//vncDB.updateVirtualMachine(this);
MainDB.sync(oldVmInfo.vmiInfoMap, this.vmiInfoMap);
if (vmiInfoMap.size() == 0) {
delete(vncDB);
}
}
@Override
void delete(VncDB vncDB)
throws IOException {
for (Map.Entry<String, VirtualMachineInterfaceInfo> entry:
vmiInfoMap.entrySet()) {
VirtualMachineInterfaceInfo vmiInfo = entry.getValue();
vmiInfo.delete(vncDB);
}
vncDB.deleteVirtualMachine(this);
MainDB.deleted(this);
}
public boolean ignore(String vmName) {
// Ignore contrailVRouterVMs since those should not be reflected in
// Contrail VNC
if (name.toLowerCase().contains(
contrailVRouterVmNamePrefix.toLowerCase())) {
return true;
}
return false;
}
}