blob: c872a82b348a332cf48878ec7b9d8890fe408592 [file] [log] [blame]
sangho538108b2015-04-08 14:29:20 -07001/*
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
17package org.onosproject.provider.of.device.impl;
18
19import org.jboss.netty.util.HashedWheelTimer;
20import org.jboss.netty.util.Timeout;
21import org.jboss.netty.util.TimerTask;
22import org.onlab.util.Timer;
23import org.onosproject.openflow.controller.OpenFlowSwitch;
24import org.onosproject.openflow.controller.RoleState;
25import org.projectfloodlight.openflow.protocol.OFPortStatsRequest;
26import org.projectfloodlight.openflow.types.OFPort;
27import org.slf4j.Logger;
28
29import java.util.concurrent.TimeUnit;
30import java.util.concurrent.atomic.AtomicLong;
31
32import static org.slf4j.LoggerFactory.getLogger;
33
34/*
35 * Sends Group Stats Request and collect the group statistics with a time interval.
36 */
37public 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());
44 private final int refreshInterval;
45 private final AtomicLong xidAtomic = new AtomicLong(1);
46
47 private Timeout timeout;
Thomas Vachuskafc52fec2015-05-18 19:13:56 -070048 private volatile boolean stopped;
sangho538108b2015-04-08 14:29:20 -070049
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 Vachuskafc52fec2015-05-18 19:13:56 -070062 public void run(Timeout to) throws Exception {
63 if (stopped || timeout.isCancelled()) {
64 return;
65 }
sangho538108b2015-04-08 14:29:20 -070066 log.trace("Collecting stats for {}", sw.getStringId());
67
68 sendPortStatistic();
69
Thomas Vachuskafc52fec2015-05-18 19:13:56 -070070 if (!stopped && !timeout.isCancelled()) {
sangho538108b2015-04-08 14:29:20 -070071 log.trace("Scheduling stats collection in {} seconds for {}",
72 this.refreshInterval, this.sw.getStringId());
Thomas Vachuskafc52fec2015-05-18 19:13:56 -070073 timeout.getTimer().newTimeout(this, refreshInterval, TimeUnit.SECONDS);
sangho538108b2015-04-08 14:29:20 -070074 }
75 }
76
77 private void sendPortStatistic() {
sangho538108b2015-04-08 14:29:20 -070078 if (sw.getRole() != RoleState.MASTER) {
79 return;
80 }
81 Long statsXid = xidAtomic.getAndIncrement();
82 OFPortStatsRequest statsRequest = sw.factory().buildPortStatsRequest()
83 .setPortNo(OFPort.ANY)
84 .setXid(statsXid)
85 .build();
86 sw.sendMsg(statsRequest);
87 }
88
89 /**
90 * Starts the collector.
91 */
Thomas Vachuskafc52fec2015-05-18 19:13:56 -070092 public synchronized void start() {
sangho538108b2015-04-08 14:29:20 -070093 log.info("Starting Port Stats collection thread for {}", sw.getStringId());
Thomas Vachuskafc52fec2015-05-18 19:13:56 -070094 stopped = false;
sangho538108b2015-04-08 14:29:20 -070095 timeout = timer.newTimeout(this, 1, TimeUnit.SECONDS);
96 }
97
98 /**
99 * Stops the collector.
100 */
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700101 public synchronized void stop() {
sangho538108b2015-04-08 14:29:20 -0700102 log.info("Stopping Port Stats collection thread for {}", sw.getStringId());
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700103 stopped = true;
sangho538108b2015-04-08 14:29:20 -0700104 timeout.cancel();
105 }
106}