Hello checho,
I'm not certain that the code you posted will actually spawn another thread. I think that what your code is doing is using the Java framework (Runnable) without getting the Runnable to run in a separate thread.
To accomplish what you've described, I'd recommend the following:
1. Create an OSGI service that provides the ability to store and look up the data (similar to NodeService or LinkService).
2. Create a REST API Resource file that uses a reference to your OSGI service to provide the data externally.
3. When your service is initialized in the @Activate method, spawn a thread that will monitor each datapath or each flow.
Typically the number of flows is much higher than the number of datapaths, so you'd want to spawn a thread per datapath. If you spawn too many threads, it may consume too many system resources (memory/CPU) to handle them all.
You'll want to spawn a single thread like this:
StatService statService = ...; // To be created for collecting/retrieving stats
BlockingQueue<Runnable> threadQueue = new LinkedBlockingQueue<>(MAX_QUEUED_THREADS);
ThreadPoolExecutor exec = new ThreadPoolExecutor(MAX_THREADS, MAX_THREADS, 1, TimeUnit.MINUTES, threadQueue, new ThreadPoolExecutor.DiscardPolicy());
exec.setThreadFactory(new NamedThreadFactory("checho"));
for (int i = 0; i < num_threads; i++) {
exec.submit(new StatsCollectorByCookie(statService, dpId));
}
Then each StatsCollectorByCookie could constantly monitor the datapath, then add collected stats to the StatService.
If you want StatsCollectorByCookie() to run periodically and only collect stats once each time it runs, you could use java.util.Timer.scheduleAtFixedRate() and make your StatsCollectorByCookie a TimerTask.
Be sure that your code correctly handles a datapath disconnect/reconnect, in case the switch reboots or has a control-plane disruption.