blob: 4e7181829ad2aaf0f4b45dfa1ffb1765247d1112 [file] [log] [blame]
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.provider.of.flow.impl;
17
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070018import org.onosproject.openflow.controller.OpenFlowSwitch;
19import org.onosproject.openflow.controller.RoleState;
20import org.projectfloodlight.openflow.protocol.OFTableStatsRequest;
21import org.slf4j.Logger;
22
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070023import java.util.TimerTask;
pier809fd4d2020-01-09 13:10:04 +010024import java.util.concurrent.ScheduledExecutorService;
25import java.util.concurrent.ScheduledFuture;
26import java.util.concurrent.TimeUnit;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070027
28import static org.slf4j.LoggerFactory.getLogger;
29
30/**
31 * Collects Table statistics for the specified switch.
32 */
Thomas Vachuskaa394b952016-06-14 15:02:09 -070033class TableStatisticsCollector implements SwitchDataCollector {
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070034
35 private final Logger log = getLogger(getClass());
36
pier809fd4d2020-01-09 13:10:04 +010037 public static final int MS = 1000;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070038
39 private final OpenFlowSwitch sw;
pier809fd4d2020-01-09 13:10:04 +010040 private ScheduledExecutorService executorService;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070041 private TimerTask task;
pier809fd4d2020-01-09 13:10:04 +010042 private ScheduledFuture<?> scheduledTask;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070043
44 private int pollInterval;
45
46 /**
47 * Creates a new table statistics collector for the given switch and poll frequency.
48 *
pier809fd4d2020-01-09 13:10:04 +010049 * @param executorService executor used for scheduling
50 * @param sw switch to pull
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070051 * @param pollInterval poll frequency in seconds
52 */
pier809fd4d2020-01-09 13:10:04 +010053 TableStatisticsCollector(ScheduledExecutorService executorService, OpenFlowSwitch sw, int pollInterval) {
54 this.executorService = executorService;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070055 this.sw = sw;
56 this.pollInterval = pollInterval;
57 }
58
59 /**
60 * Adjusts poll frequency.
61 *
62 * @param pollInterval poll frequency in seconds
63 */
64 synchronized void adjustPollInterval(int pollInterval) {
65 this.pollInterval = pollInterval;
pier809fd4d2020-01-09 13:10:04 +010066 if (task != null) {
67 task.cancel();
68 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070069 task = new InternalTimerTask();
pier809fd4d2020-01-09 13:10:04 +010070 if (scheduledTask != null) {
71 scheduledTask.cancel(false);
72 }
73 scheduledTask = executorService.scheduleAtFixedRate(task, pollInterval * MS,
74 pollInterval * MS, TimeUnit.MILLISECONDS);
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070075 }
76
77 private class InternalTimerTask extends TimerTask {
78 @Override
79 public void run() {
80 if (sw.getRole() == RoleState.MASTER) {
81 log.trace("Collecting stats for {}", sw.getStringId());
82 OFTableStatsRequest request = sw.factory().buildTableStatsRequest()
83 .build();
84 sw.sendMsg(request);
85 }
86 }
87 }
88
89 public synchronized void start() {
90 // Initially start polling quickly. Then drop down to configured value
91 log.debug("Starting Table Stats collection thread for {}", sw.getStringId());
92 task = new InternalTimerTask();
pier809fd4d2020-01-09 13:10:04 +010093 scheduledTask = executorService.scheduleAtFixedRate(task, 1 * MS,
94 pollInterval * MS, TimeUnit.MILLISECONDS);
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070095 }
96
97 public synchronized void stop() {
98 log.debug("Stopping Table Stats collection thread for {}", sw.getStringId());
pier809fd4d2020-01-09 13:10:04 +010099 if (task != null) {
100 task.cancel();
101 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700102 task = null;
pier809fd4d2020-01-09 13:10:04 +0100103 if (scheduledTask != null) {
104 scheduledTask.cancel(false);
105 }
106 scheduledTask = null;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700107 }
108
109}