/** Called periodically to retrieve all port statistics of all switches */
  @Override
  public synchronized void run() {
    // get a list of all the switches found
    Map<Long, IOFSwitch> switches = floodlightProvider.getSwitches();
    Collection<IOFSwitch> swList = switches.values();
    Iterator<IOFSwitch> it = swList.iterator();
    try {
      fw = new FileWriter("netstatslog.txt", true);
      bw = new BufferedWriter(fw);

      // for each switch, retrieve the statistics
      while (it.hasNext()) {
        IOFSwitch sw = it.next();
        List<OFStatistics> stats = new ArrayList<OFStatistics>();

        // make a statistics request
        OFStatisticsRequest req = new OFStatisticsRequest();
        req.setStatisticType(OFStatisticsType.PORT);
        req.setXid(sw.getNextTransactionId());

        // fill it with a specific request
        OFPortStatisticsRequest specReq = new OFPortStatisticsRequest();
        specReq.setPortNumber(OFPort.OFPP_NONE.getValue());
        stats.add((OFStatistics) specReq);
        req.setStatistics(stats);
        req.setLengthU(req.getLengthU() + specReq.getLength());

        // attempt to retrieve the statistics
        Future<List<OFStatistics>> future = null;
        List<OFStatistics> values = null;

        try {
          future = sw.getStatistics(req);
          values = future.get();
        } catch (Exception e) {
          System.err.println("Exception while retrieving statistics for switch: " + sw + " " + e);
        }

        // process the statistics
        if (!values.isEmpty()) {
          OFPortStatisticsReply reply = (OFPortStatisticsReply) values.get(0);

          // determine the new load
          long newload = 0;
          if (currentLoad.containsKey(sw.getId())) {
            // we've seen the switch before
            newload = reply.getTransmitBytes();
            loadHistory.put(sw.getId(), reply.getTransmitBytes());
          } else {
            // this is the first time we see the switch
            newload = reply.getTransmitBytes();
            loadHistory.put(sw.getId(), newload);
          }
          // System.out.println("Capa: " + sw.getCapabilities() * 1024);
          // System.out.println("Load: " + newload);
          currentLoad.put(sw.getId(), (sw.getCapabilities() * 1024) - newload);
          bw.write("\n id: " + sw.getId() + " ;load: " + ((sw.getCapabilities() * 1024) - newload));

          // System.out.println("Load for switch: " + currentLoad.get(sw.getId()));
        }
      }
      bw.write("\n******************");

      bw.close();
    } catch (Exception e) { // Catch exception if any
      System.err.println("Error: " + e.getMessage());
    }
  }
Example #2
0
 public static void flush_all() {
   Map<IOFSwitch, List<OFMessage>> msg_buffer_map = local_msg_buffer.get();
   for (IOFSwitch sw : msg_buffer_map.keySet()) {
     sw.flush();
   }
 }