floodlight多线程

来源:互联网 发布:淘宝开店收费 编辑:程序博客网 时间:2024/06/01 11:45

模块该部分承接上文,添加三个模块,来实现网络带宽使用、丢包率、链路时延等的测量。当然,读者也可以把他们三个木块合并,只是那样的话,每一个service就不单一了。

一、说在前面

一般来讲,我们在Floodlight中添加自定义的模块,最基础的需要实现以下两个步骤:

  1. 自定义一个interface,里面写上自定义的方法,当然,需要extends IFloodlightServcice.
  2. 自定义一个class,实现我们的那个interface,还需要实现IFloodlightModule。如果还需要处理OpenFlow消息还需要实现IOFMessageListener,其他类似;如果需要使用FL提供的线程池,那就需要添加改依赖。
在getModuleDependencies()中添加
  • 1
  • 1

接下来,我主要记录我们是如何采集到链路带宽使用情况,以及需要依赖那些service,另外两个模块与这个答题类似,就只贴上代码了。

二、链路带宽使用情况
定义的interface : IMonitorBandwidthService

public interface IMonitorBandwidthService extends IFloodlightService {    //带宽使用情况    public Map<NodePortTuple,SwitchPortBandwidth> getBandwidthMap();}
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

然后是定义的MonitorBandwidth

package aaa.net.floodlightcontroller.test;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;import java.util.concurrent.ScheduledFuture;import java.util.concurrent.TimeUnit;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import net.floodlightcontroller.core.IFloodlightProviderService;import net.floodlightcontroller.core.internal.IOFSwitchService;import net.floodlightcontroller.core.module.FloodlightModuleContext;import net.floodlightcontroller.core.module.FloodlightModuleException;import net.floodlightcontroller.core.module.IFloodlightModule;import net.floodlightcontroller.core.module.IFloodlightService;import net.floodlightcontroller.statistics.IStatisticsService;import net.floodlightcontroller.statistics.StatisticsCollector;import net.floodlightcontroller.statistics.SwitchPortBandwidth;import net.floodlightcontroller.threadpool.IThreadPoolService;import net.floodlightcontroller.topology.NodePortTuple;/** * 带宽获取模块 * @author xjtu * */public class MonitorBandwidth implements IFloodlightModule,IMonitorBandwidthService{    //日志工具    private static final Logger log = LoggerFactory.getLogger(StatisticsCollector.class);    //Floodlight最核心的service类,其他service类需要该类提供    protected static IFloodlightProviderService floodlightProvider;    //链路数据分析模块,已经由Floodlight实现了,我们只需要调用一下就可以,然后对结果稍做加工,便于我们自己使用    protected static IStatisticsService statisticsService;    //Floodllight实现的线程池,当然我们也可以使用Java自带的,但推荐使用这个    private static IThreadPoolService threadPoolService;    //Future类,不明白的可以百度 Java现成future,其实C++11也有这个玩意了    private static ScheduledFuture<?> portBandwidthCollector;    //交换机相关的service,通过这个服务,我们可以获取所有的交换机,即DataPath    private static IOFSwitchService switchService;    //存放每条俩路的带宽使用情况     private static Map<NodePortTuple,SwitchPortBandwidth> bandwidth;    //搜集数据的周期    private static final int portBandwidthInterval = 4;    //告诉FL,我们添加了一个模块,提供了IMonitorBandwidthService    @Override    public Collection<Class<? extends IFloodlightService>> getModuleServices() {        Collection<Class<? extends IFloodlightService>> l = new ArrayList<Class<? extends IFloodlightService>>();        l.add(IMonitorBandwidthService.class);        return l;    }    //我们前面声明了几个需要使用的service,在这里说明一下实现类    @Override    public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {        Map<Class<? extends IFloodlightService>, IFloodlightService> m = new HashMap<Class<? extends IFloodlightService>, IFloodlightService>();        m.put(IMonitorBandwidthService.class, this);        return m;    }    //告诉FL我们以来那些服务,以便于加载    @Override    public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {        Collection<Class<? extends IFloodlightService>> l = new ArrayList<Class<? extends IFloodlightService>>();        l.add(IFloodlightProviderService.class);        l.add(IStatisticsService.class);        l.add(IOFSwitchService.class);        l.add(IThreadPoolService.class);        return l;    }    //初始化这些service,个人理解这个要早于startUp()方法的执行,验证很简单,在两个方法里打印当前时间就可以。    @Override    public void init(FloodlightModuleContext context) throws FloodlightModuleException {        floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);        statisticsService = context.getServiceImpl(IStatisticsService.class);        switchService = context.getServiceImpl(IOFSwitchService.class);        threadPoolService = context.getServiceImpl(IThreadPoolService.class);    }    @Override    public void startUp(FloodlightModuleContext context) throws FloodlightModuleException {        startCollectBandwidth();    }    //自定义的开始收集数据的方法,使用了线程池,定周期的执行    private synchronized void startCollectBandwidth(){        portBandwidthCollector = threadPoolService.getScheduledExecutor().scheduleAtFixedRate(new GetBandwidthThread(), portBandwidthInterval, portBandwidthInterval, TimeUnit.SECONDS);        log.warn("Statistics collection thread(s) started");    }    //自定义的线程类,在上面的方法中实例化,并被调用    /**     * Single thread for collecting switch statistics and     * containing the reply.     */    private class GetBandwidthThread extends Thread implements Runnable  {        private Map<NodePortTuple,SwitchPortBandwidth> bandwidth;        public Map<NodePortTuple, SwitchPortBandwidth> getBandwidth() {            return bandwidth;        }//      public void setBandwidth(Map<NodePortTuple, SwitchPortBandwidth> bandwidth) {//          this.bandwidth = bandwidth;//      }        @Override        public void run() {            System.out.println("GetBandwidthThread run()....");            bandwidth =getBandwidthMap();             System.out.println("bandwidth.size():"+bandwidth.size());        }    }    /**     * 获取带宽使用情况     * 需要简单的换算         根据 switchPortBand.getBitsPerSecondRx().getValue()/(8*1024) + switchPortBand.getBitsPerSecondTx().getValue()/(8*1024)        计算带宽     */    public Map<NodePortTuple,SwitchPortBandwidth> getBandwidthMap(){        bandwidth = statisticsService.getBandwidthConsumption();//      //      for(NodePortTuple tuple:bandwidth.keySet()){//          System.out.println(tuple.getNodeId().toString()+","+tuple.getPortId().getPortNumber());//          System.out.println();//      }        Iterator<Entry<NodePortTuple,SwitchPortBandwidth>> iter = bandwidth.entrySet().iterator();        while (iter.hasNext()) {            Entry<NodePortTuple,SwitchPortBandwidth> entry = iter.next();            NodePortTuple tuple  = entry.getKey();            SwitchPortBandwidth switchPortBand = entry.getValue();            System.out.print(tuple.getNodeId()+","+tuple.getPortId().getPortNumber()+",");            System.out.println(switchPortBand.getBitsPerSecondRx().getValue()/(8*1024) + switchPortBand.getBitsPerSecondTx().getValue()/(8*1024));        }        return bandwidth;    }}
  • 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
  • 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

三、链路丢包率统计

import net.floodlightcontroller.core.module.IFloodlightService;public interface IMonitorPkLossService extends IFloodlightService {//  public Map<> getPkLoss(Object o1,Object o2);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5
package aaa.net.floodlightcontroller.test;import java.lang.Thread.State;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Set;import java.util.Map.Entry;import java.util.concurrent.ScheduledFuture;import java.util.concurrent.TimeUnit;import org.projectfloodlight.openflow.protocol.OFMessage;import org.projectfloodlight.openflow.protocol.OFPortStatsEntry;import org.projectfloodlight.openflow.protocol.OFPortStatsReply;import org.projectfloodlight.openflow.protocol.OFStatsReply;import org.projectfloodlight.openflow.protocol.OFStatsRequest;import org.projectfloodlight.openflow.protocol.OFStatsType;import org.projectfloodlight.openflow.protocol.OFType;import org.projectfloodlight.openflow.protocol.OFVersion;import org.projectfloodlight.openflow.protocol.match.Match;import org.projectfloodlight.openflow.protocol.ver13.OFMeterSerializerVer13;import org.projectfloodlight.openflow.types.DatapathId;import org.projectfloodlight.openflow.types.OFPort;import org.projectfloodlight.openflow.types.TableId;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.google.common.primitives.UnsignedLong;import com.google.common.util.concurrent.ListenableFuture;import net.floodlightcontroller.core.FloodlightContext;import net.floodlightcontroller.core.IFloodlightProviderService;import net.floodlightcontroller.core.IOFMessageListener;import net.floodlightcontroller.core.IOFSwitch;import net.floodlightcontroller.core.internal.IOFSwitchService;import net.floodlightcontroller.core.module.FloodlightModuleContext;import net.floodlightcontroller.core.module.FloodlightModuleException;import net.floodlightcontroller.core.module.IFloodlightModule;import net.floodlightcontroller.core.module.IFloodlightService;import net.floodlightcontroller.statistics.IStatisticsService;import net.floodlightcontroller.statistics.StatisticsCollector;import net.floodlightcontroller.threadpool.IThreadPoolService;import net.floodlightcontroller.topology.NodePortTuple;/** * 获取丢包率模块 * @author xjtu * */public class MonitorPkLoss implements IMonitorPkLossService,IFloodlightModule,IOFMessageListener {    private static final Logger log = LoggerFactory.getLogger(StatisticsCollector.class);    private static  HashMap<NodePortTuple, Long> DPID_PK_LOSS = new HashMap<NodePortTuple, Long>();    protected static IFloodlightProviderService floodlightProvider;    protected static IStatisticsService statisticsService;    private static IOFSwitchService switchService;    private static IThreadPoolService threadPoolService;    private static ScheduledFuture<?> portStatsCollector;    private static int portStatsInterval = 4;    @Override    public Collection<Class<? extends IFloodlightService>> getModuleServices() {        Collection<Class<? extends IFloodlightService>> l = new ArrayList<Class<? extends IFloodlightService>>();        l.add(IMonitorPkLossService.class);        return l;    }    @Override    public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {        Map<Class<? extends IFloodlightService>, IFloodlightService> m = new HashMap<Class<? extends IFloodlightService>, IFloodlightService>();        m.put(IMonitorPkLossService.class, this);        return m;    }    @Override    public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {        Collection<Class<? extends IFloodlightService>> l = new ArrayList<Class<? extends IFloodlightService>>();        l.add(IFloodlightProviderService.class);        l.add(IStatisticsService.class);        l.add(IOFSwitchService.class);        l.add(IThreadPoolService.class);        return l;    }    @Override    public void init(FloodlightModuleContext context) throws FloodlightModuleException {        floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);        statisticsService = context.getServiceImpl(IStatisticsService.class);        switchService = context.getServiceImpl(IOFSwitchService.class);        threadPoolService = context.getServiceImpl(IThreadPoolService.class);    }    @Override    public void startUp(FloodlightModuleContext context) throws FloodlightModuleException {         floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);         startStatisticsCollection();    }    @Override    public String getName() {        return "monitorpkloss";    }    @Override    public boolean isCallbackOrderingPrereq(OFType type, String name) {        return false;    }    @Override    public boolean isCallbackOrderingPostreq(OFType type, String name) {        return false;    }    @Override    public Command receive(IOFSwitch sw, OFMessage msg,FloodlightContext cntx) {//      Ethernet eth = IFloodlightProviderService.bcStore.get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD);//      Long sourceMACHash = eth.getSourceMACAddress().getLong();//      OFFactory factory = sw.getOFFactory();        /**         * 获取丢包率         *///      if(msg.getType() == OFType.STATS_REPLY){//          OFStatsReply reply = (OFStatsReply) msg;//          OFPortStatsReply psr = (OFPortStatsReply) reply;//          OFPortStatsEntry pse = (OFPortStatsEntry) psr;//          System.out.println("rx bytes:"+pse.getRxBytes().getValue());//          System.out.println("rx_dropped bytes:"+pse.getRxDropped().getValue());//          System.out.println("tx bytes:"+pse.getTxBytes().getValue());//          System.out.println("tx_dropped bytes:"+pse.getTxDropped().getValue());//      }        return Command.CONTINUE;    }    /**     * Start all stats threads.     */    private synchronized void startStatisticsCollection() {        portStatsCollector = threadPoolService.getScheduledExecutor().scheduleAtFixedRate(new PortStatsCollector(), portStatsInterval, portStatsInterval, TimeUnit.SECONDS);        log.warn("Statistics collection thread(s) started");    }    /**     * Single thread for collecting switch statistics and     * containing the reply.     */    private class GetStatisticsThread extends Thread {        private List<OFStatsReply> statsReply;        private DatapathId switchId;        private OFStatsType statType;        public GetStatisticsThread(DatapathId switchId, OFStatsType statType) {            this.switchId = switchId;            this.statType = statType;            this.statsReply = null;        }        public List<OFStatsReply> getStatisticsReply() {            return statsReply;        }        public DatapathId getSwitchId() {            return switchId;        }        @Override        public void run() {//          System.out.println("run............");            statsReply = getSwitchStatistics(switchId, statType);        }    }    /**     * Get statistics from a switch.     * @param switchId     * @param statsType     * @return     */    @SuppressWarnings("unchecked")    protected List<OFStatsReply> getSwitchStatistics(DatapathId switchId, OFStatsType statsType) {        IOFSwitch sw = switchService.getSwitch(switchId);//      System.out.println("getSwitchStatistics............");        ListenableFuture<?> future;        List<OFStatsReply> values = null;        Match match;        if (sw != null) {            OFStatsRequest<?> req = null;            switch (statsType) {            case FLOW:                match = sw.getOFFactory().buildMatch().build();                req = sw.getOFFactory().buildFlowStatsRequest()                        .setMatch(match)                        .setOutPort(OFPort.ANY)                        .setTableId(TableId.ALL)                        .build();                break;            case AGGREGATE:                match = sw.getOFFactory().buildMatch().build();                req = sw.getOFFactory().buildAggregateStatsRequest()                        .setMatch(match)                        .setOutPort(OFPort.ANY)                        .setTableId(TableId.ALL)                        .build();                break;            case PORT:                req = sw.getOFFactory().buildPortStatsRequest()                .setPortNo(OFPort.ANY)                .build();                break;            case QUEUE:                req = sw.getOFFactory().buildQueueStatsRequest()                .setPortNo(OFPort.ANY)                .setQueueId(UnsignedLong.MAX_VALUE.longValue())                .build();                break;            case DESC:                req = sw.getOFFactory().buildDescStatsRequest()                .build();                break;            case GROUP:                if (sw.getOFFactory().getVersion().compareTo(OFVersion.OF_10) > 0) {                    req = sw.getOFFactory().buildGroupStatsRequest()                                            .build();                }                break;            case METER:                if (sw.getOFFactory().getVersion().compareTo(OFVersion.OF_13) >= 0) {                    req = sw.getOFFactory().buildMeterStatsRequest()                            .setMeterId(OFMeterSerializerVer13.ALL_VAL)                            .build();                }                break;            case GROUP_DESC:                            if (sw.getOFFactory().getVersion().compareTo(OFVersion.OF_10) > 0) {                    req = sw.getOFFactory().buildGroupDescStatsRequest()                                        .build();                }                break;            case GROUP_FEATURES:                if (sw.getOFFactory().getVersion().compareTo(OFVersion.OF_10) > 0) {                    req = sw.getOFFactory().buildGroupFeaturesStatsRequest()                            .build();                }                break;            case METER_CONFIG:                if (sw.getOFFactory().getVersion().compareTo(OFVersion.OF_13) >= 0) {                    req = sw.getOFFactory().buildMeterConfigStatsRequest()                            .build();                }                break;            case METER_FEATURES:                if (sw.getOFFactory().getVersion().compareTo(OFVersion.OF_13) >= 0) {                    req = sw.getOFFactory().buildMeterFeaturesStatsRequest()                            .build();                }                break;            case TABLE:                if (sw.getOFFactory().getVersion().compareTo(OFVersion.OF_10) > 0) {                    req = sw.getOFFactory().buildTableStatsRequest()                            .build();                }                break;            case TABLE_FEATURES:                    if (sw.getOFFactory().getVersion().compareTo(OFVersion.OF_10) > 0) {                    req = sw.getOFFactory().buildTableFeaturesStatsRequest()                            .build();                       }                break;            case PORT_DESC:                if (sw.getOFFactory().getVersion().compareTo(OFVersion.OF_13) >= 0) {                    req = sw.getOFFactory().buildPortDescStatsRequest()                            .build();                }                break;            case EXPERIMENTER:                  default:                log.error("Stats Request Type {} not implemented yet", statsType.name());                break;            }            try {                if (req != null) {                    future = sw.writeStatsRequest(req);                     values = (List<OFStatsReply>) future.get(portStatsInterval / 2, TimeUnit.SECONDS);                }            } catch (Exception e) {                log.error("Failure retrieving statistics from switch {}. {}", sw, e);            }        }        return values;    }    /**     * Retrieve the statistics from all switches in parallel.     * @param dpids     * @param statsType     * @return     */    private Map<DatapathId, List<OFStatsReply>> getSwitchStatistics(Set<DatapathId> dpids, OFStatsType statsType) {        HashMap<DatapathId, List<OFStatsReply>> model = new HashMap<DatapathId, List<OFStatsReply>>();        List<GetStatisticsThread> activeThreads = new ArrayList<GetStatisticsThread>(dpids.size());        List<GetStatisticsThread> pendingRemovalThreads = new ArrayList<GetStatisticsThread>();        GetStatisticsThread t;        for (DatapathId d : dpids) {            t = new GetStatisticsThread(d, statsType);            activeThreads.add(t);            t.start();        }        /* Join all the threads after the timeout. Set a hard timeout         * of 12 seconds for the threads to finish. If the thread has not         * finished the switch has not replied yet and therefore we won't         * add the switch's stats to the reply.         */        for (int iSleepCycles = 0; iSleepCycles < portStatsInterval; iSleepCycles++) {            for (GetStatisticsThread curThread : activeThreads) {                if (curThread.getState() == State.TERMINATED) {                    model.put(curThread.getSwitchId(), curThread.getStatisticsReply());                    pendingRemovalThreads.add(curThread);                }            }            /* remove the threads that have completed the queries to the switches */            for (GetStatisticsThread curThread : pendingRemovalThreads) {                activeThreads.remove(curThread);            }            /* clear the list so we don't try to double remove them */            pendingRemovalThreads.clear();            /* if we are done finish early */            if (activeThreads.isEmpty()) {                break;            }            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                log.error("Interrupted while waiting for statistics", e);            }        }        return model;    }    /**     * Run periodically to collect all port statistics. This only collects     * bandwidth stats right now, but it could be expanded to record other     * information as well. The difference between the most recent and the     * current RX/TX bytes is used to determine the "elapsed" bytes. A      * timestamp is saved each time stats results are saved to compute the     * bits per second over the elapsed time. There isn't a better way to     * compute the precise bandwidth unless the switch were to include a     * timestamp in the stats reply message, which would be nice but isn't     * likely to happen. It would be even better if the switch recorded      * bandwidth and reported bandwidth directly.     *      * Stats are not reported unless at least two iterations have occurred     * for a single switch's reply. This must happen to compare the byte      * counts and to get an elapsed time.     *     */    private class PortStatsCollector implements Runnable {        @Override        public void run() {//          System.out.println("Runnable run()....");            Map<DatapathId, List<OFStatsReply>> replies = getSwitchStatistics(switchService.getAllSwitchDpids(), OFStatsType.PORT);//          System.out.println("replies.size():"+replies.size());            for (Entry<DatapathId, List<OFStatsReply>> e : replies.entrySet()) {                for (OFStatsReply r : e.getValue()) {                    OFPortStatsReply psr = (OFPortStatsReply) r;                    for (OFPortStatsEntry pse : psr.getEntries()) {//                      System.out.println("dpid:"+e.getKey().toString());//                      System.out.println("for (OFPortStatsEntry pse : psr.getEntries())");                        long pk_loss = 0;                        if(e.getKey().toString().equals("") || e.getKey() == null){//                          System.out.println("e.getKey() is null....");                        }//                      System.out.println("--------------------------------------------");                        NodePortTuple npt = new NodePortTuple(e.getKey(), pse.getPortNo());//                      System.out.println("--------------------------------------------");//                      System.out.println(pse.getRxDropped().getValue() + pse.getTxDropped().getValue());//                      System.out.println(pse.getRxBytes().getValue() + pse.getTxBytes().getValue());                        if((pse.getRxBytes().getValue() + pse.getTxBytes().getValue()) != 0l){                            pk_loss = (pse.getRxDropped().getValue() + pse.getTxDropped().getValue())/(pse.getRxBytes().getValue() + pse.getTxBytes().getValue()) ;                        }else{                            pk_loss = 0;                        }//                      System.out.println("PK_LOSS:"+pk_loss+",dpid:"+e.getKey().toString());                        DPID_PK_LOSS.put(npt, pk_loss);                    }                }            }        }    }}
  • 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
  • 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

四、链路时延

public interface IMonitorDelayService extends IFloodlightService{    /**     * 获取链路之间的时间延迟     * @return Map<MyEntry<NodePortTuple,NodePortTuple>,Integer> 链路:时延     *///  public Map<MyEntry<NodePortTuple,NodePortTuple>,Integer> getLinkDelay(); }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
package aaa.net.floodlightcontroller.test;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;import org.projectfloodlight.openflow.protocol.OFMessage;import org.projectfloodlight.openflow.protocol.OFPortDesc;import org.projectfloodlight.openflow.protocol.OFType;import org.projectfloodlight.openflow.types.DatapathId;import net.floodlightcontroller.core.FloodlightContext;import net.floodlightcontroller.core.IFloodlightProviderService;import net.floodlightcontroller.core.IOFMessageListener;import net.floodlightcontroller.core.IOFSwitch;import net.floodlightcontroller.core.IOFSwitchListener;import net.floodlightcontroller.core.PortChangeType;import net.floodlightcontroller.core.internal.IOFSwitchService;import net.floodlightcontroller.core.module.FloodlightModuleContext;import net.floodlightcontroller.core.module.FloodlightModuleException;import net.floodlightcontroller.core.module.IFloodlightModule;import net.floodlightcontroller.core.module.IFloodlightService;import net.floodlightcontroller.linkdiscovery.ILinkDiscoveryService;import net.floodlightcontroller.linkdiscovery.internal.LinkInfo;import net.floodlightcontroller.routing.Link;import net.floodlightcontroller.threadpool.IThreadPoolService;import net.floodlightcontroller.topology.NodePortTuple;public class MonitorDelay implements IFloodlightModule, IOFMessageListener, IMonitorDelayService,IOFSwitchListener {    private IThreadPoolService threadPoolServcie;    private IOFSwitchService switchService;    private ILinkDiscoveryService linkDiscoveryService;    private IFloodlightProviderService floodlightProviderService;    @Override    public String getName() {        return "LinkDelay";    }    @Override    public boolean isCallbackOrderingPrereq(OFType type, String name) {        return false;    }    @Override    public boolean isCallbackOrderingPostreq(OFType type, String name) {        return false;    }    @Override    public Command receive(IOFSwitch sw, OFMessage msg,            FloodlightContext cntx) {        return null;    }    @Override    public Collection<Class<? extends IFloodlightService>> getModuleServices() {        Collection<Class<? extends IFloodlightService>> l = new ArrayList<Class<? extends IFloodlightService>>();        l.add(IMonitorDelayService.class);        return l;    }    @Override    public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {        Map<Class<? extends IFloodlightService>, IFloodlightService> l = new HashMap<>();        l.put(IMonitorDelayService.class, this);        return l;    }    @Override    public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {        Collection<Class<? extends IFloodlightService>> l = new ArrayList<Class<? extends IFloodlightService>>();        l.add(IFloodlightProviderService.class);        l.add(IThreadPoolService.class);        l.add(IOFSwitchService.class);        l.add(ILinkDiscoveryService.class);        return l;    }    @Override    public void init(FloodlightModuleContext context) throws FloodlightModuleException {        floodlightProviderService = context.getServiceImpl(IFloodlightProviderService.class);        switchService = context.getServiceImpl(IOFSwitchService.class);        linkDiscoveryService = context.getServiceImpl(ILinkDiscoveryService.class);        threadPoolServcie = context.getServiceImpl(IThreadPoolService.class);    }    @Override    public void startUp(FloodlightModuleContext context) throws FloodlightModuleException {    }    @Override    public void switchAdded(DatapathId switchId) {    }    @Override    public void switchRemoved(DatapathId switchId) {    }    @Override    public void switchActivated(DatapathId switchId) {    }    @Override    public void switchPortChanged(DatapathId switchId, OFPortDesc port, PortChangeType type) {    }    @Override    public void switchChanged(DatapathId switchId) {    }    public void testFunc(){        Map<Link,LinkInfo> linkInfo = linkDiscoveryService.getLinks();        Iterator<Entry<Link, LinkInfo>> iter = linkInfo.entrySet().iterator();        while(iter.hasNext()){            Entry<Link, LinkInfo> node = iter.next();            System.out.println("源交换机:"+node.getKey().getSrc().toString()+",源端口:"+node.getKey().getSrcPort());            System.out.println("目的交换机:"+node.getKey().getDst().toString()+",目的端口:"+node.getKey().getDstPort());            System.out.println("链路时延:"+node.getKey().getLatency());            System.out.println("当前时延:"+node.getValue().getCurrentLatency());        }    }}
0 0
原创粉丝点击