sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
| 19 | import org.jboss.netty.util.HashedWheelTimer; |
| 20 | import org.jboss.netty.util.Timeout; |
| 21 | import org.jboss.netty.util.TimerTask; |
| 22 | import org.onlab.util.Timer; |
| 23 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 24 | import org.onosproject.openflow.controller.RoleState; |
| 25 | import org.projectfloodlight.openflow.protocol.OFPortStatsRequest; |
| 26 | import org.projectfloodlight.openflow.types.OFPort; |
| 27 | import org.slf4j.Logger; |
| 28 | |
| 29 | import java.util.concurrent.TimeUnit; |
| 30 | import java.util.concurrent.atomic.AtomicLong; |
| 31 | |
| 32 | import static org.slf4j.LoggerFactory.getLogger; |
| 33 | |
Jian Li | 889cffb | 2016-02-08 10:14:46 -0800 | [diff] [blame] | 34 | /** |
| 35 | * Sends Port Stats Request and collect the port statistics with a time interval. |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 36 | */ |
| 37 | public class PortStatsCollector implements TimerTask { |
| 38 | |
| 39 | // TODO: Refactoring is required using ScheduledExecutorService |
| 40 | |
| 41 | private final HashedWheelTimer timer = Timer.getTimer(); |
| 42 | private final OpenFlowSwitch sw; |
| 43 | private final Logger log = getLogger(getClass()); |
Dusan Pajin | bab8a5e | 2015-07-24 17:37:19 +0200 | [diff] [blame] | 44 | private int refreshInterval; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 45 | private final AtomicLong xidAtomic = new AtomicLong(1); |
| 46 | |
| 47 | private Timeout timeout; |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 48 | private volatile boolean stopped; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
Jian Li | 889cffb | 2016-02-08 10:14:46 -0800 | [diff] [blame] | 51 | * Creates a PortStatsCollector object. |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 52 | * |
| 53 | * @param sw Open Flow switch |
Jian Li | 889cffb | 2016-02-08 10:14:46 -0800 | [diff] [blame] | 54 | * @param interval time interval for collecting port statistic |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 55 | */ |
| 56 | public PortStatsCollector(OpenFlowSwitch sw, int interval) { |
| 57 | this.sw = sw; |
| 58 | this.refreshInterval = interval; |
| 59 | } |
| 60 | |
| 61 | @Override |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 62 | public void run(Timeout to) throws Exception { |
| 63 | if (stopped || timeout.isCancelled()) { |
| 64 | return; |
| 65 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 66 | log.trace("Collecting stats for {}", sw.getStringId()); |
| 67 | |
| 68 | sendPortStatistic(); |
| 69 | |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 70 | if (!stopped && !timeout.isCancelled()) { |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 71 | log.trace("Scheduling stats collection in {} seconds for {}", |
| 72 | this.refreshInterval, this.sw.getStringId()); |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 73 | timeout.getTimer().newTimeout(this, refreshInterval, TimeUnit.SECONDS); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Dusan Pajin | bab8a5e | 2015-07-24 17:37:19 +0200 | [diff] [blame] | 77 | synchronized void adjustPollInterval(int pollInterval) { |
| 78 | this.refreshInterval = pollInterval; |
| 79 | // task.cancel(); |
| 80 | // task = new InternalTimerTask(); |
| 81 | // timer.scheduleAtFixedRate(task, pollInterval * SECONDS, pollInterval * 1000); |
| 82 | } |
| 83 | |
Jian Li | 889cffb | 2016-02-08 10:14:46 -0800 | [diff] [blame] | 84 | /** |
| 85 | * Sends port statistic request to switch. |
| 86 | */ |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 87 | private void sendPortStatistic() { |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 88 | if (sw.getRole() != RoleState.MASTER) { |
| 89 | return; |
| 90 | } |
| 91 | Long statsXid = xidAtomic.getAndIncrement(); |
| 92 | OFPortStatsRequest statsRequest = sw.factory().buildPortStatsRequest() |
| 93 | .setPortNo(OFPort.ANY) |
| 94 | .setXid(statsXid) |
| 95 | .build(); |
| 96 | sw.sendMsg(statsRequest); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Starts the collector. |
| 101 | */ |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 102 | public synchronized void start() { |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 103 | log.info("Starting Port Stats collection thread for {}", sw.getStringId()); |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 104 | stopped = false; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 105 | timeout = timer.newTimeout(this, 1, TimeUnit.SECONDS); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Stops the collector. |
| 110 | */ |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 111 | public synchronized void stop() { |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 112 | log.info("Stopping Port Stats collection thread for {}", sw.getStringId()); |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 113 | stopped = true; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 114 | timeout.cancel(); |
| 115 | } |
| 116 | } |