/
VCenterNotify.java
729 lines (656 loc) · 28.3 KB
/
VCenterNotify.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
/**
* Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
*/
package net.juniper.contrail.vcenter;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import com.vmware.vim25.ArrayOfEvent;
import com.vmware.vim25.ArrayOfGuestNicInfo;
import com.vmware.vim25.Event;
import com.vmware.vim25.EventFilterSpec;
import com.vmware.vim25.EventFilterSpecByEntity;
import com.vmware.vim25.EventFilterSpecRecursionOption;
import com.vmware.vim25.ObjectSpec;
import com.vmware.vim25.ObjectUpdate;
import com.vmware.vim25.PropertyChange;
import com.vmware.vim25.PropertyChangeOp;
import com.vmware.vim25.PropertyFilterSpec;
import com.vmware.vim25.PropertyFilterUpdate;
import com.vmware.vim25.PropertySpec;
import com.vmware.vim25.RequestCanceled;
import com.vmware.vim25.SelectionSpec;
import com.vmware.vim25.UpdateSet;
import com.vmware.vim25.VirtualMachineToolsRunningStatus;
import com.vmware.vim25.WaitOptions;
import com.vmware.vim25.mo.EventHistoryCollector;
import com.vmware.vim25.mo.EventManager;
import com.vmware.vim25.mo.ManagedObject;
import com.vmware.vim25.mo.PropertyCollector;
import com.vmware.vim25.mo.PropertyFilter;
import net.juniper.contrail.watchdog.TaskWatchDog;
import com.vmware.vim25.EnteredMaintenanceModeEvent;
import com.vmware.vim25.ExitMaintenanceModeEvent;
import com.vmware.vim25.GuestNicInfo;
import com.vmware.vim25.HostConnectedEvent;
import com.vmware.vim25.HostConnectionLostEvent;
import com.vmware.vim25.ManagedObjectReference;
import com.vmware.vim25.RuntimeFault;
import com.vmware.vim25.mo.VmwareDistributedVirtualSwitch;
import com.vmware.vim25.mo.HostSystem;
import com.google.common.base.Throwables;
import com.vmware.vim25.InvalidState;
import org.apache.log4j.Logger;
/**
* @author Sachchidanand Vaidya
*
*/
public class VCenterNotify implements Runnable
{
private static final Logger s_logger =
Logger.getLogger(VCenterNotify.class);
static volatile VCenterDB vcenterDB = null;
static volatile VncDB vncDB;
private VCenterEventHandler eventHandler;
private static boolean vCenterConnected = false;
private final static String[] guestProps = { "guest.toolsRunningStatus", "guest.net" };
private final static String[] ipPoolProps = { "summary.ipPoolId" };
private static Map<String, VirtualMachineInfo> watchedVMs
= new HashMap<String, VirtualMachineInfo>();
private static Map<String, VirtualNetworkInfo> watchedVNs
= new HashMap<String, VirtualNetworkInfo>();
private static Map<ManagedObject, PropertyFilter> watchedFilters
= new HashMap<ManagedObject, PropertyFilter>();
private static Boolean shouldRun;
private static Thread watchUpdates = null;
private static final int VCENTER_READ_TIMEOUT = 30000; //30 sec
private static final int VCENTER_CONNECT_TIMEOUT = 30000; //30 sec
private static final int VCENTER_WAIT_FOR_UPDATES_READ_TIMEOUT = 180000; // 3 minutes
private static final int VCENTER_WAIT_FOR_UPDATES_SERVER_TIMEOUT = 120; // 2 minutes
private static final int VCENTER_TIMEOUT_DELTA = 10000; // 10 seconds
public VCenterNotify(
String vcenterUrl, String vcenterUsername,
String vcenterPassword, String dcName,
String dvsName, String ipFabricPgName,
String _apiServerAddress, int _apiServerPort,
String _username, String _password,
String _tenant,
String _authtype, String _authurl, Mode mode)
{
vcenterDB = new VCenterDB(vcenterUrl, vcenterUsername, vcenterPassword,
dcName, dvsName, ipFabricPgName, mode);
switch (mode) {
case VCENTER_ONLY:
vncDB = new VncDB(_apiServerAddress, _apiServerPort, mode);
break;
case VCENTER_AS_COMPUTE:
vncDB = new VncDB(_apiServerAddress, _apiServerPort, _username, _password,
_tenant,
_authtype, _authurl, mode);
break;
default:
vncDB = new VncDB(_apiServerAddress, _apiServerPort, mode);
}
eventHandler = new VCenterEventHandler(vcenterDB, vncDB);
}
public static VCenterDB getVcenterDB() {
return vcenterDB;
}
public static boolean getVCenterConnected() {
return vCenterConnected;
}
private void cleanupEventFilters() {
for (Map.Entry<ManagedObject, PropertyFilter> entry: watchedFilters.entrySet())
try
{
PropertyFilter pf = entry.getValue();
pf.destroyPropertyFilter();
} catch (RemoteException e)
{
// it is ok, we can receive exception if vcenter was restarted
}
watchedFilters.clear();
watchedVMs.clear();
watchedVNs.clear();
}
public static void watchVm(VirtualMachineInfo vmInfo) {
if (vmInfo.vm == null) {
return;
}
if (VCenterMonitor.mode == Mode.VCENTER_AS_COMPUTE
|| watchedVMs.containsKey(vmInfo.vm.getMOR().getVal())) {
return;
}
watchedVMs.put(vmInfo.vm.getMOR().getVal(), vmInfo);
watchManagedObject(vmInfo.vm, guestProps);
}
public static void unwatchVm(VirtualMachineInfo vmInfo) {
if (vmInfo.vm == null) {
return;
}
if (VCenterMonitor.mode == Mode.VCENTER_AS_COMPUTE
|| !watchedVMs.containsKey(vmInfo.vm.getMOR().getVal())) {
return;
}
watchedVMs.remove(vmInfo.vm.getMOR().getVal());
unwatchManagedObject(vmInfo.vm);
}
public static void watchVn(VirtualNetworkInfo vnInfo) {
if (vnInfo.dpg == null) {
return;
}
if (VCenterMonitor.mode == Mode.VCENTER_AS_COMPUTE
|| watchedVNs.containsKey(vnInfo.dpg.getMOR().getVal())) {
return;
}
watchedVNs.put(vnInfo.dpg.getMOR().getVal(), vnInfo);
watchManagedObject(vnInfo.dpg, ipPoolProps);
}
public static void unwatchVn(VirtualNetworkInfo vnInfo) {
if (vnInfo.dpg == null) {
return;
}
if (VCenterMonitor.mode == Mode.VCENTER_AS_COMPUTE
|| !watchedVNs.containsKey(vnInfo.dpg.getMOR().getVal())) {
return;
}
watchedVNs.remove(vnInfo.dpg.getMOR().getVal());
unwatchManagedObject(vnInfo.dpg);
}
private static void watchManagedObject(ManagedObject mos, String[] propNames)
{
PropertyFilterSpec pfs = new PropertyFilterSpec();
ObjectSpec[] oss = new ObjectSpec[1];
oss[0] = new ObjectSpec();
oss[0].setObj(mos.getMOR());
pfs.setObjectSet(oss);
PropertySpec ps = new PropertySpec();
ps.setType(mos.getMOR().getType());
ps.setPathSet(propNames);
pfs.setPropSet(new PropertySpec[] { ps });
try
{
PropertyCollector propColl = vcenterDB.getServiceInstance().getPropertyCollector();
PropertyFilter pf = propColl.createFilter(pfs, true); //report only nesting properties, not enclosing ones.
watchedFilters.put(mos, pf);
} catch(RemoteException re)
{
throw new RuntimeException(re);
}
}
private static void unwatchManagedObject(ManagedObject mos)
{
if (watchedFilters.containsKey(mos)) {
try
{
PropertyFilter pf = watchedFilters.remove(mos);
pf.destroyPropertyFilter();
} catch (RemoteException e)
{
s_logger.error("Cannot unwatchManagedObject " + mos + " due to exception " + e);
s_logger.error(Throwables.getStackTraceAsString(e));
}
}
}
private EventHistoryCollector createEventHistoryCollector(ManagedObject mo,
String[] events) throws InvalidState, RuntimeFault, RemoteException
{
// Create an Entity Event Filter Spec to
// specify the MoRef of the VM to be get events filtered for
EventFilterSpecByEntity entitySpec = new EventFilterSpecByEntity();
entitySpec.setEntity(mo.getMOR());
entitySpec.setRecursion(EventFilterSpecRecursionOption.children);
// set the entity spec in the EventFilter
EventFilterSpec eventFilter = new EventFilterSpec();
eventFilter.setEntity(entitySpec);
// we are only interested in getting events for the VM.
// Add as many events you want to track relating to vm.
// Refer to API Data Object vmEvent and see the extends class list for
// elaborate list of vmEvents
eventFilter.setType(events);
// create the EventHistoryCollector to monitor events for a VM
// and get the ManagedObjectReference of the EventHistoryCollector
// returned
EventManager eventManager = vcenterDB.getServiceInstance().getEventManager();
if (eventManager != null) {
return eventManager.createCollectorForEvents(eventFilter);
}
return null;
}
private PropertyFilterSpec createEventFilterSpec(EventHistoryCollector collector)
throws Exception
{
// Set up a PropertySpec to use the latestPage attribute
// of the EventHistoryCollector
PropertySpec propSpec = new PropertySpec();
propSpec.setAll(new Boolean(false));
propSpec.setPathSet(new String[] { "latestPage" });
propSpec.setType(collector.getMOR().getType());
// PropertySpecs are wrapped in a PropertySpec array
PropertySpec[] propSpecAry = new PropertySpec[] { propSpec };
// Set up an ObjectSpec with the above PropertySpec for the
// EventHistoryCollector we just created
// as the Root or Starting Object to get Attributes for.
ObjectSpec objSpec = new ObjectSpec();
objSpec.setObj(collector.getMOR());
objSpec.setSkip(new Boolean(false));
// Get Event objects in "latestPage" from "EventHistoryCollector"
// and no "traversal" further, so, no SelectionSpec is specified
objSpec.setSelectSet(new SelectionSpec[] {});
// ObjectSpecs are wrapped in an ObjectSpec array
ObjectSpec[] objSpecAry = new ObjectSpec[] { objSpec };
PropertyFilterSpec spec = new PropertyFilterSpec();
spec.setPropSet(propSpecAry);
spec.setObjectSet(objSpecAry);
return spec;
}
private void handleUpdate(UpdateSet update) throws Exception
{
ObjectUpdate[] vmUpdates;
PropertyFilterUpdate[] pfus = update.getFilterSet();
if (pfus == null) {
return;
}
for (int pfui = 0; pfui < pfus.length; pfui++)
{
vmUpdates = pfus[pfui].getObjectSet();
if (vmUpdates == null) {
continue;
}
for (ObjectUpdate vmi : vmUpdates)
{
handleChanges(vmi);
}
}
}
void handleChanges(ObjectUpdate oUpdate) throws Exception
{
PropertyChange[] changes = oUpdate.getChangeSet();
if (changes == null) {
return;
}
String toolsRunningStatus = null;
GuestNicInfo[] nics = null;
for (int pci = 0; pci < changes.length; ++pci)
{
if (changes[pci] == null) {
continue;
}
Object value = changes[pci].getVal();
String propName = changes[pci].getName();
PropertyChangeOp op = changes[pci].getOp();
if (op!= PropertyChangeOp.remove) {
if (propName.equals("summary.ipPoolId")) {
s_logger.info("Received summary.ipPoolId property change");
Integer newPoolId = (Integer)value;
ManagedObjectReference mor = oUpdate.getObj();
if (watchedVNs.containsKey(mor.getVal())) {
VirtualNetworkInfo vnInfo = watchedVNs.get(mor.getVal());
Integer oldPoolId = vnInfo.getIpPoolId();
if ((oldPoolId == null && newPoolId == null)
|| (oldPoolId != null && newPoolId != null
&& oldPoolId.equals(newPoolId))) {
s_logger.info("Done processing property update, nothing changed");
continue;
}
VirtualNetworkInfo newVnInfo = new VirtualNetworkInfo(vnInfo);
newVnInfo.setIpPoolId(newPoolId, vcenterDB);
if ((newVnInfo.getIpPoolId() != null) &&
!newVnInfo.getIpPoolId().equals(oldPoolId)) {
s_logger.info("IP Pool ID for " + newVnInfo + " set to "
+ newVnInfo.getIpPoolId());
vnInfo.update(newVnInfo, vncDB);
}
}
s_logger.info("Done processing property update");
} else if (propName.equals("guest.toolsRunningStatus")) {
s_logger.info("Received guest.toolsRunningStatus property change");
toolsRunningStatus = (String)value;
} else if (value instanceof ArrayOfEvent) {
s_logger.info("Received ArrayOfEvent");
ArrayOfEvent aoe = (ArrayOfEvent) value;
Event[] evts = aoe.getEvent();
if (evts == null) {
continue;
}
for (int evtID = 0; evtID < evts.length; ++evtID)
{
Event anEvent = evts[evtID];
if (anEvent == null) {
continue;
}
printEvent(anEvent);
}
s_logger.info("Done processing array of events");
} else if ((value instanceof EnteredMaintenanceModeEvent) || (value instanceof HostConnectionLostEvent)) {
Event anEvent = (Event) value;
printEvent(anEvent);
String vRouterIpAddress = vcenterDB.esxiToVRouterIpMap.get(anEvent.getHost().getName());
if (vRouterIpAddress != null) {
VCenterDB.vRouterActiveMap.put(vRouterIpAddress, false);
s_logger.info("\nEntering maintenance mode. Marking the host " + vRouterIpAddress +" inactive");
} else {
s_logger.info("\nNot managing the host " + vRouterIpAddress +" inactive");
}
s_logger.info("Done processing event " + anEvent.getFullFormattedMessage());
} else if ((value instanceof ExitMaintenanceModeEvent) || (value instanceof HostConnectedEvent)) {
Event anEvent = (Event) value;
printEvent(anEvent);
String vRouterIpAddress = vcenterDB.esxiToVRouterIpMap.get(anEvent.getHost().getName());
if (vRouterIpAddress != null) {
VCenterDB.vRouterActiveMap.put(vRouterIpAddress, true);
s_logger.info("\nExit maintenance mode. Marking the host " + vRouterIpAddress +" active");
} else {
s_logger.info("\nNot managing the host " + vRouterIpAddress +" inactive");
}
s_logger.info("Done processing event " + anEvent.getFullFormattedMessage());
} else if (value instanceof ArrayOfGuestNicInfo) {
s_logger.info("Received event update array of GuestNics");
ArrayOfGuestNicInfo aog = (ArrayOfGuestNicInfo) value;
nics = aog.getGuestNicInfo();
} else if (value instanceof Event) {
Event anEvent = (Event)value;
printEvent(anEvent);
eventHandler.handle(anEvent);
} else {
if (value != null) {
s_logger.info("\n Received unhandled property");
} else {
s_logger.info("\n Received unhandled null value");
}
}
} else if (op == PropertyChangeOp.remove) {
}
}
if (toolsRunningStatus != null || nics != null) {
ManagedObjectReference mor = oUpdate.getObj();
if (watchedVMs.containsKey(mor.getVal())) {
VirtualMachineInfo vmInfo = watchedVMs.get(mor.getVal());
if (toolsRunningStatus != null) {
vmInfo.setToolsRunningStatus(toolsRunningStatus);
}
if (vmInfo.getToolsRunningStatus().equals(
VirtualMachineToolsRunningStatus.guestToolsRunning.toString())) {
vmInfo.updatedGuestNics(nics, vncDB);
}
}
s_logger.info("Done processing property update");
}
}
public void start() {
try
{
watchUpdates = new Thread(this);
shouldRun = true;
watchUpdates.start();
} catch (Exception e)
{
s_logger.error("Caught Exception : " + " Name : "
+ e.getClass().getName() + " Message : " + e.getMessage()
+ " Trace : ");
s_logger.error(Throwables.getStackTraceAsString(e));
}
}
@SuppressWarnings("deprecation")
public void terminate() throws Exception {
shouldRun = false;
PropertyCollector propColl = vcenterDB.getServiceInstance().getPropertyCollector();
propColl.cancelWaitForUpdates();
cleanupEventFilters();
vcenterDB.getServiceInstance().getServerConnection().logout();
watchUpdates.stop();
}
private boolean connect2vnc() {
TaskWatchDog.startMonitoring(this, "Init Vnc",
300000, TimeUnit.MILLISECONDS);
try {
s_logger.info("Connecting to the API server ...");
while (vncDB.Initialize() != true) {
s_logger.info("Waiting for API server ...");
Thread.sleep(5000);
}
s_logger.info("Connected to the API server ...");
} catch (Exception e) {
s_logger.error("Error while initializing connection with the API server: " + e);
s_logger.error(Throwables.getStackTraceAsString(e));
TaskWatchDog.stopMonitoring(this);
return false;
}
TaskWatchDog.stopMonitoring(this);
return true;
}
private void connect2vcenter() {
TaskWatchDog.startMonitoring(this, "Init VCenter",
300000, TimeUnit.MILLISECONDS);
try {
if (vcenterDB.connect(VCENTER_CONNECT_TIMEOUT) == true) {
createEventFilters();
vCenterConnected = true;
}
} catch (Exception e) {
s_logger.error("Error while initializing VCenter connection: ");
s_logger.error(Throwables.getStackTraceAsString(e));
}
TaskWatchDog.stopMonitoring(this);
}
@Override
public void run()
{
try
{
if (connect2vnc() == false) {
return;
}
boolean syncNeeded = true;
for (String version = "" ; shouldRun; )
{
//check if you are the master from time to time
//sometimes things don't go as planned
if (VCenterMonitor.isZookeeperLeader() == false) {
s_logger.warn("Lost zookeeper leadership. Restarting myself\n");
System.exit(0);
}
if (vCenterConnected == false) {
connect2vcenter();
version = "";
syncNeeded = true;
}
while (vncDB.isVncApiServerAlive() == false) {
s_logger.info("Waiting for API server... ");
Thread.sleep(5000);
}
// Perform sync between VNC and VCenter DBs.
if (syncNeeded) {
TaskWatchDog.startMonitoring(this, "Sync",
300000, TimeUnit.MILLISECONDS);
// When sync is run, it also does
// addPort to vrouter agent for existing VMIs.
try {
s_logger.info("+++++++++++++ Starting sync +++++++++++++++++++++");
vcenterDB.setReadTimeout(VCENTER_READ_TIMEOUT);
MainDB.sync(vcenterDB, vncDB, VCenterMonitor.mode);
syncNeeded = false;
} catch (Exception e) {
vCenterConnected = false;
s_logger.error("Error in sync: " + e);
s_logger.error(Throwables.getStackTraceAsString(e));
TaskWatchDog.stopMonitoring(this);
continue;
}
TaskWatchDog.stopMonitoring(this);
s_logger.info("+++++++++++++ Sync complete +++++++++++++++++++++");
}
s_logger.info("+++++++++++++ Waiting for events +++++++++++++++++++++");
try
{
WaitOptions wOpt = new WaitOptions();
wOpt.setMaxWaitSeconds(VCENTER_WAIT_FOR_UPDATES_SERVER_TIMEOUT);
for ( ; ; ) {
vcenterDB.setReadTimeout(VCENTER_WAIT_FOR_UPDATES_READ_TIMEOUT);
PropertyCollector propColl = vcenterDB.getServiceInstance().getPropertyCollector();
TaskWatchDog.startMonitoring(this, "WaitForUpdatesEx",
VCENTER_WAIT_FOR_UPDATES_READ_TIMEOUT + VCENTER_TIMEOUT_DELTA,
TimeUnit.MILLISECONDS);
UpdateSet update = propColl.waitForUpdatesEx(version, wOpt);
TaskWatchDog.stopMonitoring(this);
if (update != null && update.getFilterSet() != null)
{
version = update.getVersion();
this.handleUpdate(update);
} else
{
vcenterDB.setReadTimeout(VCENTER_READ_TIMEOUT);
TaskWatchDog.startMonitoring(this, "AlivenessCheck",
VCENTER_READ_TIMEOUT + VCENTER_TIMEOUT_DELTA,
TimeUnit.MILLISECONDS);
if (vcenterDB.isAlive() == false) {
s_logger.error("Vcenter connection lost, reconnect and resync needed");
vCenterConnected = false;
TaskWatchDog.stopMonitoring(this);
break;
}
TaskWatchDog.stopMonitoring(this);
}
}
} catch (RemoteException e) {
vCenterConnected = false;
s_logger.info("Vcenter disconnected, reconnect and resync needed: " + e);
s_logger.info(Throwables.getStackTraceAsString(e));
} catch (Exception e) {
vCenterConnected = false;
s_logger.error("Error in event handling, reconnect and resync needed");
s_logger.error(Throwables.getStackTraceAsString(e));
}
}
} catch (Exception e)
{
if (e instanceof RequestCanceled)
{
System.out.println("OK");
} else
{
s_logger.error("Caught Exception : " + " Name : "
+ e.getClass().getName() + " Message : "
+ e.getMessage() + " Trace : ");
s_logger.error(Throwables.getStackTraceAsString(e));
}
}
}
public static void printEvent(Event event) {
s_logger.info("\n----------" + "\n Event ID: "
+ event.getKey() + "\n Event: "
+ event.getClass().getName()
+ ", happened on: "
+ event.getCreatedTime().getTime()
+ "\n FullFormattedMessage: "
+ event.getFullFormattedMessage()
+ "\n----------\n");
}
public static VncDB getVncDB() {
return vncDB;
}
private void createEventFilters() throws RemoteException {
cleanupEventFilters();
createDvsEventFilter(vcenterDB.getContrailDvs());
for (String hostName : vcenterDB.esxiToVRouterIpMap.keySet()) {
createHostEventFilter(hostName);
}
}
private void createDvsEventFilter(VmwareDistributedVirtualSwitch dvs)
throws RemoteException {
String[] dvsEventNames = {
// DV Port group events
// DV port create
"DVPortgroupCreatedEvent",
// DV port modify
"DVPortgroupReconfiguredEvent",
"DVPortgroupRenamedEvent",
// DV port delete
"DVPortgroupDestroyedEvent"
};
watchManagedObjectEvents(dvs, dvsEventNames);
}
private void createHostEventFilter(String hostName) throws RemoteException {
HostSystem host = vcenterDB.getVmwareHost(hostName,
vcenterDB.contrailDC, vcenterDB.contrailDataCenterName);
if (host == null) {
s_logger.error("Cannot register for events for host " + hostName + ", not found.");
return;
}
s_logger.info("Register for events on host " + hostName);
String[] hostEventNames = {
// Host events
"HostConnectionLostEvent",
"HostConnectedEvent",
"EnteredMaintenanceModeEvent",
"ExitMaintenanceModeEvent",
// VM events
// VM create events
"VmCreatedEvent",
"VmClonedEvent",
"VmDeployedEvent",
// VM modify events
"VmPoweredOnEvent",
"DrsVmPoweredOnEvent",
"VmPoweredOffEvent",
"VmRenamedEvent",
"VmMacChangedEvent",
"VmMacAssignedEvent",
"VmReconfiguredEvent",
// VM Migration events
"DrsVmMigratedEvent",
"VmMigratedEvent",
// VM delete events
"VmRemovedEvent"
};
watchManagedObjectEvents(host, hostEventNames);
}
private void watchManagedObjectEvents(ManagedObject mos, String[] events)
{
if (mos == null || events == null) {
s_logger.error("Null arguments in watchManagedObjectEvents");
return;
}
try
{
EventHistoryCollector collector =
createEventHistoryCollector(mos, events);
PropertyFilterSpec eventFilterSpec = createEventFilterSpec(collector);
PropertyCollector propColl = vcenterDB.getServiceInstance().getPropertyCollector();
PropertyFilter propFilter = propColl.createFilter(eventFilterSpec, true);
//report only nesting properties, not enclosing ones.
if (propFilter != null) {
watchedFilters.put(mos, propFilter);
} else {
s_logger.error("Cannot create event filter for managed object ");
}
} catch(Exception e)
{
s_logger.error("Cannot watchManagedObjectEvents for " + mos + ", exception " + e);
s_logger.error(Throwables.getStackTraceAsString(e));
throw new RuntimeException(e);
}
}
@SuppressWarnings("unused")
private void unwatchManagedObjectEvents(ManagedObject mos)
{
if (mos == null) {
s_logger.error("Null arguments in unwatchManagedObjectEvents");
return;
}
if (watchedFilters.containsKey(mos)) {
try
{
PropertyFilter pf = watchedFilters.remove(mos);
if (pf != null) {
pf.destroyPropertyFilter();
}
} catch (RemoteException e)
{
s_logger.error("Cannot unwatchManagedObjectEvents for " + mos + ", exception " + e);
s_logger.error(Throwables.getStackTraceAsString(e));
}
}
}
}