SDN Controller Programming Guide

181
}
}
Posting Alerts
In order to illustrate how alerts may be posted using the AlertService published by the controller,
SwitchManager of the sample application will post an alert if a device is updated with an
unreachable status. See Alert Logging on page 19 to get more information.
At this point SwitchManager already depends on the AlertService, so it is ready to use such
service. The following listing illustrates an extract of a modified SwitchManager that posts an alert
when a device is unreachable.
SwitchManager.java Posting Alerts:
package com.hp.hm.impl;
import com.hp.sdn.adm.alert.AlertService;
import com.hp.sdn.adm.alert.AlertTopic;
...
public class SwitchManager implements SwitchService {
...
private AlertService alertService;
private AlertTopic alertTopic;
...
public void setAlertService(AlertService alertService) {
this.alertService = alertService;
alertTopic = alertService.registerTopic("of_controller_hm",
"health-monitor", "Alerts from the health monitor application");
}
...
@Override
public void update(Switch device) {
if (device == null) {
throw new NullPointerException("device cannot be null");
}
if (device.getId() == null) {
throw new IllegalArgumentException(
"a device must be added before updating it");
}
devices.put(device.getId(), device);
if (alertService != null) {
if (device.getActiveState() == ActiveState.OFF) {
String source = "OpenFlow Switch: " +
device.getId().getValue();
String data = "Active State: " + device.getActiveState();