blob: 81a9aaad0ffe4dc59079e6bcd59782fe960950fb [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
Jian Li889cffb2016-02-08 10:14:46 -080034/**
35 * Sends Port Stats Request and collect the port statistics with a time interval.
sangho538108b2015-04-08 14:29:20 -070036 */
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());
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020044 private int refreshInterval;
sangho538108b2015-04-08 14:29:20 -070045 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 /**
Jian Li889cffb2016-02-08 10:14:46 -080051 * Creates a PortStatsCollector object.
sangho538108b2015-04-08 14:29:20 -070052 *
53 * @param sw Open Flow switch
Jian Li889cffb2016-02-08 10:14:46 -080054 * @param interval time interval for collecting port statistic
sangho538108b2015-04-08 14:29:20 -070055 */
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
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020077 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 Li889cffb2016-02-08 10:14:46 -080084 /**
85 * Sends port statistic request to switch.
86 */
sangho538108b2015-04-08 14:29:20 -070087 private void sendPortStatistic() {
sangho538108b2015-04-08 14:29:20 -070088 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 Vachuskafc52fec2015-05-18 19:13:56 -0700102 public synchronized void start() {
sangho538108b2015-04-08 14:29:20 -0700103 log.info("Starting Port Stats collection thread for {}", sw.getStringId());
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700104 stopped = false;
sangho538108b2015-04-08 14:29:20 -0700105 timeout = timer.newTimeout(this, 1, TimeUnit.SECONDS);
106 }
107
108 /**
109 * Stops the collector.
110 */
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700111 public synchronized void stop() {
sangho538108b2015-04-08 14:29:20 -0700112 log.info("Stopping Port Stats collection thread for {}", sw.getStringId());
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700113 stopped = true;
sangho538108b2015-04-08 14:29:20 -0700114 timeout.cancel();
115 }
116}