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 | |
| 34 | /* |
| 35 | * Sends Group Stats Request and collect the group statistics with a time interval. |
| 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 | /** |
| 51 | * Creates a GroupStatsCollector object. |
| 52 | * |
| 53 | * @param sw Open Flow switch |
| 54 | * @param interval time interval for collecting group statistic |
| 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 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 84 | private void sendPortStatistic() { |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 85 | if (sw.getRole() != RoleState.MASTER) { |
| 86 | return; |
| 87 | } |
| 88 | Long statsXid = xidAtomic.getAndIncrement(); |
| 89 | OFPortStatsRequest statsRequest = sw.factory().buildPortStatsRequest() |
| 90 | .setPortNo(OFPort.ANY) |
| 91 | .setXid(statsXid) |
| 92 | .build(); |
| 93 | sw.sendMsg(statsRequest); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Starts the collector. |
| 98 | */ |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 99 | public synchronized void start() { |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 100 | log.info("Starting Port Stats collection thread for {}", sw.getStringId()); |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 101 | stopped = false; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 102 | timeout = timer.newTimeout(this, 1, TimeUnit.SECONDS); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Stops the collector. |
| 107 | */ |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 108 | public synchronized void stop() { |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 109 | log.info("Stopping Port Stats collection thread for {}", sw.getStringId()); |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 110 | stopped = true; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 111 | timeout.cancel(); |
| 112 | } |
| 113 | } |