Skip to content
Snippets Groups Projects
Commit 8dc1e59a authored by Ryan Izard's avatar Ryan Izard
Browse files

Added enable/disable REST API to statistics collection module

parent e810cb71
No related branches found
No related tags found
No related merge requests found
......@@ -13,4 +13,6 @@ public interface IStatisticsService extends IFloodlightService {
public SwitchPortBandwidth getBandwidthConsumption(DatapathId dpid, OFPort p);
public Map<NodePortTuple, SwitchPortBandwidth> getBandwidthConsumption();
public void collectStatistics(boolean collect);
}
......@@ -9,6 +9,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import org.projectfloodlight.openflow.protocol.OFPortStatsEntry;
......@@ -47,10 +48,16 @@ public class StatisticsCollector implements IFloodlightModule, IStatisticsServic
private static IThreadPoolService threadPoolService;
private static IRestApiService restApiService;
private static boolean isEnabled = false;
private static int portStatsInterval = 10;
private static ScheduledFuture<?> portStatsCollector;
private static final long BITS_PER_BYTE = 8;
private static final long MILLIS_PER_SEC = 1000;
private static final String INTERVAL_PORT_STATS_STR = "collectionIntervalPortStatsSeconds";
private static final String ENABLED_STR = "enable";
private static final HashMap<NodePortTuple, SwitchPortBandwidth> portStats = new HashMap<NodePortTuple, SwitchPortBandwidth>();
private static final HashMap<NodePortTuple, SwitchPortBandwidth> tentativePortStats = new HashMap<NodePortTuple, SwitchPortBandwidth>();
......@@ -168,14 +175,33 @@ public class StatisticsCollector implements IFloodlightModule, IStatisticsServic
threadPoolService = context.getServiceImpl(IThreadPoolService.class);
restApiService = context.getServiceImpl(IRestApiService.class);
//TODO Map<String, String> config = context.getConfigParams(this);
Map<String, String> config = context.getConfigParams(this);
if (config.containsKey(ENABLED_STR)) {
try {
isEnabled = Boolean.parseBoolean(config.get(ENABLED_STR).trim());
} catch (Exception e) {
log.error("Could not parse '{}'. Using default of {}", ENABLED_STR, isEnabled);
}
}
log.info("Statistics collection {}", isEnabled ? "enabled" : "disabled");
if (config.containsKey(INTERVAL_PORT_STATS_STR)) {
try {
portStatsInterval = Integer.parseInt(config.get(INTERVAL_PORT_STATS_STR).trim());
} catch (Exception e) {
log.error("Could not parse '{}'. Using default of {}", INTERVAL_PORT_STATS_STR, portStatsInterval);
}
}
log.info("Port statistics collection interval set to {}s", portStatsInterval);
}
@Override
public void startUp(FloodlightModuleContext context)
throws FloodlightModuleException {
threadPoolService.getScheduledExecutor().scheduleAtFixedRate(new PortStatsCollector(), portStatsInterval, portStatsInterval, TimeUnit.SECONDS);
restApiService.addRestletRoutable(new SwitchStatisticsWebRoutable());
if (isEnabled) {
startStatisticsCollection();
}
}
@Override
......@@ -188,6 +214,32 @@ public class StatisticsCollector implements IFloodlightModule, IStatisticsServic
public Map<NodePortTuple, SwitchPortBandwidth> getBandwidthConsumption() {
return Collections.unmodifiableMap(portStats);
}
@Override
public synchronized void collectStatistics(boolean collect) {
if (collect && !isEnabled) {
startStatisticsCollection();
isEnabled = true;
} else if (!collect && isEnabled) {
stopStatisticsCollection();
isEnabled = false;
}
/* otherwise, state is not changing; no-op */
}
private void startStatisticsCollection() {
portStatsCollector = threadPoolService.getScheduledExecutor().scheduleAtFixedRate(new PortStatsCollector(), portStatsInterval, portStatsInterval, TimeUnit.SECONDS);
tentativePortStats.clear(); /* must clear out, otherwise might have huge BW result if present and wait a long time before re-enabling stats */
log.warn("Statistics collection thread(s) started");
}
private void stopStatisticsCollection() {
if (!portStatsCollector.cancel(false)) {
log.error("Could not cancel port stats thread");
} else {
log.warn("Statistics collection thread(s) stopped");
}
}
private Map<DatapathId, List<OFStatsReply>> getSwitchStatistics(Set<DatapathId> dpids, OFStatsType statsType) {
HashMap<DatapathId, List<OFStatsReply>> model = new HashMap<DatapathId, List<OFStatsReply>>();
......
package net.floodlightcontroller.statistics.web;
import java.util.Collections;
import net.floodlightcontroller.statistics.IStatisticsService;
import org.restlet.resource.Post;
import org.restlet.resource.Put;
import org.restlet.resource.ServerResource;
public class ConfigResource extends ServerResource {
@Post
@Put
public Object config() {
IStatisticsService statisticsService = (IStatisticsService) getContext().getAttributes().get(IStatisticsService.class.getCanonicalName());
if (getReference().getPath().contains(SwitchStatisticsWebRoutable.ENABLE_STR)) {
statisticsService.collectStatistics(true);
return Collections.singletonMap("statistics-collection", "enabled");
}
if (getReference().getPath().contains(SwitchStatisticsWebRoutable.DISABLE_STR)) {
statisticsService.collectStatistics(false);
return Collections.singletonMap("statistics-collection", "disabled");
}
return Collections.singletonMap("ERROR", "Unimplemented configuration option");
}
}
\ No newline at end of file
......@@ -9,11 +9,15 @@ import org.restlet.routing.Router;
public class SwitchStatisticsWebRoutable implements RestletRoutable {
protected static final String DPID_STR = "dpid";
protected static final String PORT_STR = "port";
protected static final String ENABLE_STR = "enable";
protected static final String DISABLE_STR = "disable";
@Override
public Router getRestlet(Context context) {
Router router = new Router(context);
router.attach("/bandwidth/{" + DPID_STR + "}/{" + PORT_STR + "}/json", BandwidthResource.class);
router.attach("/config/enable/json", ConfigResource.class);
router.attach("/config/disable/json", ConfigResource.class);
return router;
}
......
......@@ -42,3 +42,5 @@ net.floodlightcontroller.restserver.RestApiServer.useHttps=NO
net.floodlightcontroller.restserver.RestApiServer.useHttp=YES
net.floodlightcontroller.restserver.RestApiServer.httpsPort=8081
net.floodlightcontroller.restserver.RestApiServer.httpPort=8080
net.floodlightcontroller.statistics.StatisticsCollector.enable=TRUE
net.floodlightcontroller.statistics.StatisticsCollector.collectionIntervalPortStatsSeconds=10
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment