From 29c271d5197c40a81d77623bba5b909fa95b0c67 Mon Sep 17 00:00:00 2001 From: Ryan Izard <ryan.izard@bigswitch.com> Date: Sat, 18 Jun 2016 17:45:22 -0400 Subject: [PATCH] Fix bug in statistics module REST API to correctly retrieve requested statistics for given DPID and port. --- .../statistics/web/BandwidthResource.java | 102 +++++++++--------- 1 file changed, 53 insertions(+), 49 deletions(-) diff --git a/src/main/java/net/floodlightcontroller/statistics/web/BandwidthResource.java b/src/main/java/net/floodlightcontroller/statistics/web/BandwidthResource.java index a09e5f9e0..55b2e619a 100644 --- a/src/main/java/net/floodlightcontroller/statistics/web/BandwidthResource.java +++ b/src/main/java/net/floodlightcontroller/statistics/web/BandwidthResource.java @@ -17,59 +17,63 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class BandwidthResource extends ServerResource { - private static final Logger log = LoggerFactory.getLogger(BandwidthResource.class); + private static final Logger log = LoggerFactory.getLogger(BandwidthResource.class); - @Get("json") - public Object retrieve() { - IStatisticsService statisticsService = (IStatisticsService) getContext().getAttributes().get(IStatisticsService.class.getCanonicalName()); - IOFSwitchService switchService = (IOFSwitchService) getContext().getAttributes().get(IOFSwitchService.class.getCanonicalName()); + @Get("json") + public Object retrieve() { + IStatisticsService statisticsService = (IStatisticsService) getContext().getAttributes().get(IStatisticsService.class.getCanonicalName()); + IOFSwitchService switchService = (IOFSwitchService) getContext().getAttributes().get(IOFSwitchService.class.getCanonicalName()); - String d = (String) getRequestAttributes().get(SwitchStatisticsWebRoutable.DPID_STR); - String p = (String) getRequestAttributes().get(SwitchStatisticsWebRoutable.PORT_STR); + String d = (String) getRequestAttributes().get(SwitchStatisticsWebRoutable.DPID_STR); + String p = (String) getRequestAttributes().get(SwitchStatisticsWebRoutable.PORT_STR); - DatapathId dpid = DatapathId.NONE; + DatapathId dpid = DatapathId.NONE; - if (!d.trim().equalsIgnoreCase("all")) { - try { - dpid = DatapathId.of(d); - } catch (Exception e) { - log.error("Could not parse DPID {}", d); - return Collections.singletonMap("ERROR", "Could not parse DPID" + d); - } - } /* else assume it's all */ + if (!d.trim().equalsIgnoreCase("all")) { + try { + dpid = DatapathId.of(d); + } catch (Exception e) { + log.error("Could not parse DPID {}", d); + return Collections.singletonMap("ERROR", "Could not parse DPID " + d); + } + } /* else assume it's all */ - OFPort port = OFPort.ALL; - if (!p.trim().equalsIgnoreCase("all")) { - try { - port = OFPort.of(Integer.parseInt(p)); - } catch (Exception e) { - log.error("Could not parse port {}", p); - return Collections.singletonMap("ERROR", "Could not parse port" + p); - } - } + OFPort port = OFPort.ALL; + if (!p.trim().equalsIgnoreCase("all")) { + try { + port = OFPort.of(Integer.parseInt(p)); + } catch (Exception e) { + log.error("Could not parse port {}", p); + return Collections.singletonMap("ERROR", "Could not parse port " + p); + } + } - Set<SwitchPortBandwidth> spbs; - if (dpid.equals(DatapathId.NONE)) { /* do all DPIDs */ - if (port.equals(OFPort.ALL)) { /* do all ports */ - spbs = new HashSet<SwitchPortBandwidth>(statisticsService.getBandwidthConsumption().values()); - } else { - spbs = new HashSet<SwitchPortBandwidth>(); - for (DatapathId id : switchService.getAllSwitchDpids()) { - SwitchPortBandwidth spb = statisticsService.getBandwidthConsumption(id, port); - if (spb != null) { - spbs.add(spb); - } - } - } - } else { - spbs = new HashSet<SwitchPortBandwidth>(); - for (OFPortDesc pd : switchService.getSwitch(dpid).getPorts()) { - SwitchPortBandwidth spb = statisticsService.getBandwidthConsumption(dpid, pd.getPortNo()); - if (spb != null) { - spbs.add(spb); - } - } - } - return spbs; - } + Set<SwitchPortBandwidth> spbs; + if (dpid.equals(DatapathId.NONE)) { /* do all DPIDs */ + if (port.equals(OFPort.ALL)) { /* do all ports --> do all DPIDs; do all ports */ + spbs = new HashSet<SwitchPortBandwidth>(statisticsService.getBandwidthConsumption().values()); + } else { + spbs = new HashSet<SwitchPortBandwidth>(); + for (DatapathId id : switchService.getAllSwitchDpids()) { /* do all DPIDs; do specific port */ + SwitchPortBandwidth spb = statisticsService.getBandwidthConsumption(id, port); + if (spb != null) { + spbs.add(spb); + } + } + } + } else { /* do specific DPID */ + if (!port.equals(OFPort.ALL)) { /* do specific port --> do specific DPID; do specific port */ + spbs = new HashSet<SwitchPortBandwidth>(Collections.singleton(statisticsService.getBandwidthConsumption(dpid, port))); + } else { + spbs = new HashSet<SwitchPortBandwidth>(); + for (OFPortDesc pd : switchService.getSwitch(dpid).getPorts()) { /* do specific DPID; do all ports */ + SwitchPortBandwidth spb = statisticsService.getBandwidthConsumption(dpid, pd.getPortNo()); + if (spb != null) { + spbs.add(spb); + } + } + } + } + return spbs; + } } \ No newline at end of file -- GitLab