app注册
private ApplicationId appId;
appId = coreService .registerApplication("org.onosproject.fwd");
//注册应用,一般在activate函数中完成
拓扑监听器
//自定义拓扑监听器,覆盖event函数,具体功能可自己实现,这里是避免路由黑洞
private class InternalTopologyListener implements TopologyListener {
@Override
public void event(TopologyEvent event) {
List< Event> reasons = event.reasons();
if (reasons != null) {
reasons.forEach(re -> {
if (re instanceof LinkEvent) {
LinkEvent le = (LinkEvent) re;
if (le .type() == LinkEvent.Type.LINK_REMOVED) {
fixBlackhole( le.subject().src());
}
}
});
}
}
}
private final TopologyListener topologyListener = new InternalTopologyListener();
//定义一个拓扑监听器
topologyService.addListener( topologyListener);
//添加监听器,般在activate函数中完成
设备监听器
/**
* Listener to Device Event and OVSDB connection.
* 这段代码来自github,供参考。网址:https://github.com/MaoJianwei/ONOS_OVS_Manager_Bootcamp2016
*/
private class InnerDeviceListener implements DeviceListener {
@Override
public void event(DeviceEvent event) {
if (!event.type().equals(DeviceEvent.Type.DEVICE_ADDED)) {
return;
}
Device device = event.subject();
if (device.type() == Device.Type.CONTROLLER) {
dealController(device.id());
} else {
dealSwitch(device.id());
}
}
/**
* Catch the new OVSDB connection.
* @param deviceId : The OVSDB connection ID.
*/
private void dealController(DeviceId deviceId) {
controllerId = deviceId;
log.info( "controllerId is ready !!!" );
}
/**
* Separate two type of switches.
* @param deviceId : The DeviceId of target device.
*/
private void dealSwitch(DeviceId deviceId) {
String datapathId = deviceId.toString().split( ":")[1];
if (Integer.valueOf(datapathId) > CORE_DEVICEID_CARDINALITY) {
dealCoreSwitch(deviceId);
} else {
dealAccessSwitch(deviceId);
}
}
/**
* Add specific ForwardingObject to CORE switch.
* @param deviceId : The DeviceId of target device.
*/
private void dealCoreSwitch(DeviceId deviceId) {
TrafficSelector.Builder trafficSelectorBuilder0 = DefaultTrafficSelector.builder();
trafficSelectorBuilder0.matchEthType(EthType.EtherType.IPV4.ethType().toShort())
.matchIPDst(IpPrefix.valueOf( "192.168.1.1/28"))
.matchIPSrc(IpPrefix.valueOf( "1.2.3.4/32"));
TrafficTreatment.Builder trafficTreat