ONOS源码笔记--机制

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
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值