Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.provider.of.flow.impl; |
| 17 | |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 18 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 19 | import org.onosproject.openflow.controller.RoleState; |
| 20 | import org.projectfloodlight.openflow.protocol.OFTableStatsRequest; |
| 21 | import org.slf4j.Logger; |
| 22 | |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 23 | import java.util.TimerTask; |
pier | 809fd4d | 2020-01-09 13:10:04 +0100 | [diff] [blame^] | 24 | import java.util.concurrent.ScheduledExecutorService; |
| 25 | import java.util.concurrent.ScheduledFuture; |
| 26 | import java.util.concurrent.TimeUnit; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 27 | |
| 28 | import static org.slf4j.LoggerFactory.getLogger; |
| 29 | |
| 30 | /** |
| 31 | * Collects Table statistics for the specified switch. |
| 32 | */ |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 33 | class TableStatisticsCollector implements SwitchDataCollector { |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 34 | |
| 35 | private final Logger log = getLogger(getClass()); |
| 36 | |
pier | 809fd4d | 2020-01-09 13:10:04 +0100 | [diff] [blame^] | 37 | public static final int MS = 1000; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 38 | |
| 39 | private final OpenFlowSwitch sw; |
pier | 809fd4d | 2020-01-09 13:10:04 +0100 | [diff] [blame^] | 40 | private ScheduledExecutorService executorService; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 41 | private TimerTask task; |
pier | 809fd4d | 2020-01-09 13:10:04 +0100 | [diff] [blame^] | 42 | private ScheduledFuture<?> scheduledTask; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 43 | |
| 44 | private int pollInterval; |
| 45 | |
| 46 | /** |
| 47 | * Creates a new table statistics collector for the given switch and poll frequency. |
| 48 | * |
pier | 809fd4d | 2020-01-09 13:10:04 +0100 | [diff] [blame^] | 49 | * @param executorService executor used for scheduling |
| 50 | * @param sw switch to pull |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 51 | * @param pollInterval poll frequency in seconds |
| 52 | */ |
pier | 809fd4d | 2020-01-09 13:10:04 +0100 | [diff] [blame^] | 53 | TableStatisticsCollector(ScheduledExecutorService executorService, OpenFlowSwitch sw, int pollInterval) { |
| 54 | this.executorService = executorService; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 55 | 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; |
pier | 809fd4d | 2020-01-09 13:10:04 +0100 | [diff] [blame^] | 66 | if (task != null) { |
| 67 | task.cancel(); |
| 68 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 69 | task = new InternalTimerTask(); |
pier | 809fd4d | 2020-01-09 13:10:04 +0100 | [diff] [blame^] | 70 | if (scheduledTask != null) { |
| 71 | scheduledTask.cancel(false); |
| 72 | } |
| 73 | scheduledTask = executorService.scheduleAtFixedRate(task, pollInterval * MS, |
| 74 | pollInterval * MS, TimeUnit.MILLISECONDS); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 75 | } |
| 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(); |
pier | 809fd4d | 2020-01-09 13:10:04 +0100 | [diff] [blame^] | 93 | scheduledTask = executorService.scheduleAtFixedRate(task, 1 * MS, |
| 94 | pollInterval * MS, TimeUnit.MILLISECONDS); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | public synchronized void stop() { |
| 98 | log.debug("Stopping Table Stats collection thread for {}", sw.getStringId()); |
pier | 809fd4d | 2020-01-09 13:10:04 +0100 | [diff] [blame^] | 99 | if (task != null) { |
| 100 | task.cancel(); |
| 101 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 102 | task = null; |
pier | 809fd4d | 2020-01-09 13:10:04 +0100 | [diff] [blame^] | 103 | if (scheduledTask != null) { |
| 104 | scheduledTask.cancel(false); |
| 105 | } |
| 106 | scheduledTask = null; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | } |