sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
sangho | 538108b | 2015-04-08 14:29:20 -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 | |
| 17 | package org.onosproject.provider.of.device.impl; |
| 18 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 19 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 20 | import org.onosproject.openflow.controller.RoleState; |
| 21 | import org.projectfloodlight.openflow.protocol.OFPortStatsRequest; |
| 22 | import org.projectfloodlight.openflow.types.OFPort; |
| 23 | import org.slf4j.Logger; |
| 24 | |
sangyun-han | 07a572f | 2016-07-29 21:14:27 +0900 | [diff] [blame] | 25 | import java.util.Timer; |
| 26 | import java.util.TimerTask; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 27 | import java.util.concurrent.atomic.AtomicLong; |
| 28 | |
sangyun-han | 07a572f | 2016-07-29 21:14:27 +0900 | [diff] [blame] | 29 | import static com.google.common.base.Preconditions.checkNotNull; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 30 | import static org.slf4j.LoggerFactory.getLogger; |
| 31 | |
Jian Li | 889cffb | 2016-02-08 10:14:46 -0800 | [diff] [blame] | 32 | /** |
| 33 | * Sends Port Stats Request and collect the port statistics with a time interval. |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 34 | */ |
sangyun-han | 07a572f | 2016-07-29 21:14:27 +0900 | [diff] [blame] | 35 | public class PortStatsCollector { |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 36 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 37 | private final Logger log = getLogger(getClass()); |
sangyun-han | 07a572f | 2016-07-29 21:14:27 +0900 | [diff] [blame] | 38 | |
Ray Milkey | 3717e60 | 2018-02-01 13:49:47 -0800 | [diff] [blame] | 39 | private static final long SECONDS = 1000L; |
sangyun-han | 07a572f | 2016-07-29 21:14:27 +0900 | [diff] [blame] | 40 | |
| 41 | private OpenFlowSwitch sw; |
| 42 | private Timer timer; |
| 43 | private TimerTask task; |
| 44 | |
Dusan Pajin | bab8a5e | 2015-07-24 17:37:19 +0200 | [diff] [blame] | 45 | private int refreshInterval; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 46 | private final AtomicLong xidAtomic = new AtomicLong(1); |
| 47 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 48 | /** |
sangyun-han | 07a572f | 2016-07-29 21:14:27 +0900 | [diff] [blame] | 49 | * Creates a port states collector object. |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 50 | * |
sangyun-han | 07a572f | 2016-07-29 21:14:27 +0900 | [diff] [blame] | 51 | * @param timer timer to use for scheduling |
| 52 | * @param sw switch to pull |
| 53 | * @param interval interval for collecting port statistic |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 54 | */ |
sangyun-han | 07a572f | 2016-07-29 21:14:27 +0900 | [diff] [blame] | 55 | PortStatsCollector(Timer timer, OpenFlowSwitch sw, int interval) { |
| 56 | this.timer = timer; |
| 57 | this.sw = checkNotNull(sw, "Null switch"); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 58 | this.refreshInterval = interval; |
| 59 | } |
| 60 | |
sangyun-han | 07a572f | 2016-07-29 21:14:27 +0900 | [diff] [blame] | 61 | private class InternalTimerTask extends TimerTask { |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 62 | |
sangyun-han | 07a572f | 2016-07-29 21:14:27 +0900 | [diff] [blame] | 63 | @Override |
| 64 | public void run() { |
Palash Kala | f95c38b | 2017-05-15 23:52:48 +0900 | [diff] [blame] | 65 | sendPortStatisticRequest(); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
sangyun-han | 07a572f | 2016-07-29 21:14:27 +0900 | [diff] [blame] | 69 | /** |
| 70 | * Starts the port statistic collector. |
| 71 | */ |
| 72 | public synchronized void start() { |
| 73 | log.info("Starting Port Stats collection thread for {}", sw.getStringId()); |
| 74 | task = new InternalTimerTask(); |
Victor Silva | ff5871b | 2016-10-04 18:08:47 -0300 | [diff] [blame] | 75 | timer.scheduleAtFixedRate(task, 1 * SECONDS, |
| 76 | refreshInterval * SECONDS); |
sangyun-han | 07a572f | 2016-07-29 21:14:27 +0900 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Stops the port statistic collector. |
| 81 | */ |
| 82 | public synchronized void stop() { |
| 83 | log.info("Stopping Port Stats collection thread for {}", sw.getStringId()); |
| 84 | task.cancel(); |
| 85 | task = null; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Adjusts poll interval of the port statistic collector and restart. |
| 90 | * |
| 91 | * @param pollInterval period of collecting port statistic |
| 92 | */ |
| 93 | public synchronized void adjustPollInterval(int pollInterval) { |
Dusan Pajin | bab8a5e | 2015-07-24 17:37:19 +0200 | [diff] [blame] | 94 | this.refreshInterval = pollInterval; |
sangyun-han | 07a572f | 2016-07-29 21:14:27 +0900 | [diff] [blame] | 95 | task.cancel(); |
| 96 | task = new InternalTimerTask(); |
| 97 | timer.scheduleAtFixedRate(task, refreshInterval * SECONDS, |
| 98 | refreshInterval * SECONDS); |
Dusan Pajin | bab8a5e | 2015-07-24 17:37:19 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Jian Li | 889cffb | 2016-02-08 10:14:46 -0800 | [diff] [blame] | 101 | /** |
| 102 | * Sends port statistic request to switch. |
| 103 | */ |
Palash Kala | f95c38b | 2017-05-15 23:52:48 +0900 | [diff] [blame] | 104 | private void sendPortStatisticRequest() { |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 105 | if (sw.getRole() != RoleState.MASTER) { |
| 106 | return; |
| 107 | } |
| 108 | Long statsXid = xidAtomic.getAndIncrement(); |
| 109 | OFPortStatsRequest statsRequest = sw.factory().buildPortStatsRequest() |
| 110 | .setPortNo(OFPort.ANY) |
| 111 | .setXid(statsXid) |
| 112 | .build(); |
| 113 | sw.sendMsg(statsRequest); |
| 114 | } |
sangyun-han | 07a572f | 2016-07-29 21:14:27 +0900 | [diff] [blame] | 115 | } |