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