blob: 18b939ff556a967d4fccf01e511dfc0159aad4c8 [file] [log] [blame]
sangho538108b2015-04-08 14:29:20 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
sangho538108b2015-04-08 14:29:20 -07003 *
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
sangho538108b2015-04-08 14:29:20 -070019import org.onosproject.openflow.controller.OpenFlowSwitch;
20import org.onosproject.openflow.controller.RoleState;
21import org.projectfloodlight.openflow.protocol.OFPortStatsRequest;
22import org.projectfloodlight.openflow.types.OFPort;
23import org.slf4j.Logger;
24
sangyun-han07a572f2016-07-29 21:14:27 +090025import java.util.Timer;
26import java.util.TimerTask;
sangho538108b2015-04-08 14:29:20 -070027import java.util.concurrent.atomic.AtomicLong;
28
sangyun-han07a572f2016-07-29 21:14:27 +090029import static com.google.common.base.Preconditions.checkNotNull;
sangho538108b2015-04-08 14:29:20 -070030import static org.slf4j.LoggerFactory.getLogger;
31
Jian Li889cffb2016-02-08 10:14:46 -080032/**
33 * Sends Port Stats Request and collect the port statistics with a time interval.
sangho538108b2015-04-08 14:29:20 -070034 */
sangyun-han07a572f2016-07-29 21:14:27 +090035public class PortStatsCollector {
sangho538108b2015-04-08 14:29:20 -070036
sangho538108b2015-04-08 14:29:20 -070037 private final Logger log = getLogger(getClass());
sangyun-han07a572f2016-07-29 21:14:27 +090038
39 private static final int SECONDS = 1000;
40
41 private OpenFlowSwitch sw;
42 private Timer timer;
43 private TimerTask task;
44
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020045 private int refreshInterval;
sangho538108b2015-04-08 14:29:20 -070046 private final AtomicLong xidAtomic = new AtomicLong(1);
47
sangho538108b2015-04-08 14:29:20 -070048 /**
sangyun-han07a572f2016-07-29 21:14:27 +090049 * Creates a port states collector object.
sangho538108b2015-04-08 14:29:20 -070050 *
sangyun-han07a572f2016-07-29 21:14:27 +090051 * @param timer timer to use for scheduling
52 * @param sw switch to pull
53 * @param interval interval for collecting port statistic
sangho538108b2015-04-08 14:29:20 -070054 */
sangyun-han07a572f2016-07-29 21:14:27 +090055 PortStatsCollector(Timer timer, OpenFlowSwitch sw, int interval) {
56 this.timer = timer;
57 this.sw = checkNotNull(sw, "Null switch");
sangho538108b2015-04-08 14:29:20 -070058 this.refreshInterval = interval;
59 }
60
sangyun-han07a572f2016-07-29 21:14:27 +090061 private class InternalTimerTask extends TimerTask {
sangho538108b2015-04-08 14:29:20 -070062
sangyun-han07a572f2016-07-29 21:14:27 +090063 @Override
64 public void run() {
Palash Kalaf95c38b2017-05-15 23:52:48 +090065 sendPortStatisticRequest();
sangho538108b2015-04-08 14:29:20 -070066 }
67 }
68
sangyun-han07a572f2016-07-29 21:14:27 +090069 /**
70 * Starts the port statistic collector.
71 */
72 public synchronized void start() {
73 log.info("Starting Port Stats collection thread for {}", sw.getStringId());
74 task = new InternalTimerTask();
Victor Silvaff5871b2016-10-04 18:08:47 -030075 timer.scheduleAtFixedRate(task, 1 * SECONDS,
76 refreshInterval * SECONDS);
sangyun-han07a572f2016-07-29 21:14:27 +090077 }
78
79 /**
80 * Stops the port statistic collector.
81 */
82 public synchronized void stop() {
83 log.info("Stopping Port Stats collection thread for {}", sw.getStringId());
84 task.cancel();
85 task = null;
86 }
87
88 /**
89 * Adjusts poll interval of the port statistic collector and restart.
90 *
91 * @param pollInterval period of collecting port statistic
92 */
93 public synchronized void adjustPollInterval(int pollInterval) {
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020094 this.refreshInterval = pollInterval;
sangyun-han07a572f2016-07-29 21:14:27 +090095 task.cancel();
96 task = new InternalTimerTask();
97 timer.scheduleAtFixedRate(task, refreshInterval * SECONDS,
98 refreshInterval * SECONDS);
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020099 }
100
Jian Li889cffb2016-02-08 10:14:46 -0800101 /**
102 * Sends port statistic request to switch.
103 */
Palash Kalaf95c38b2017-05-15 23:52:48 +0900104 private void sendPortStatisticRequest() {
sangho538108b2015-04-08 14:29:20 -0700105 if (sw.getRole() != RoleState.MASTER) {
106 return;
107 }
108 Long statsXid = xidAtomic.getAndIncrement();
109 OFPortStatsRequest statsRequest = sw.factory().buildPortStatsRequest()
110 .setPortNo(OFPort.ANY)
111 .setXid(statsXid)
112 .build();
113 sw.sendMsg(statsRequest);
114 }
sangyun-han07a572f2016-07-29 21:14:27 +0900115}