blob: 02d9d9bafec435bfcda25023b893b000a17044d5 [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
sangyun-han07a572f2016-07-29 21:14:27 +090019import org.onlab.util.SharedExecutors;
sangho538108b2015-04-08 14:29:20 -070020import org.onosproject.openflow.controller.OpenFlowSwitch;
21import org.onosproject.openflow.controller.RoleState;
22import org.projectfloodlight.openflow.protocol.OFPortStatsRequest;
23import org.projectfloodlight.openflow.types.OFPort;
24import org.slf4j.Logger;
25
sangyun-han07a572f2016-07-29 21:14:27 +090026import java.util.Timer;
27import java.util.TimerTask;
sangho538108b2015-04-08 14:29:20 -070028import java.util.concurrent.atomic.AtomicLong;
29
sangyun-han07a572f2016-07-29 21:14:27 +090030import static com.google.common.base.Preconditions.checkNotNull;
sangho538108b2015-04-08 14:29:20 -070031import static org.slf4j.LoggerFactory.getLogger;
32
Jian Li889cffb2016-02-08 10:14:46 -080033/**
34 * Sends Port Stats Request and collect the port statistics with a time interval.
sangho538108b2015-04-08 14:29:20 -070035 */
sangyun-han07a572f2016-07-29 21:14:27 +090036public class PortStatsCollector {
sangho538108b2015-04-08 14:29:20 -070037
sangho538108b2015-04-08 14:29:20 -070038 private final Logger log = getLogger(getClass());
sangyun-han07a572f2016-07-29 21:14:27 +090039
40 private static final int SECONDS = 1000;
41
42 private OpenFlowSwitch sw;
43 private Timer timer;
44 private TimerTask task;
45
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020046 private int refreshInterval;
sangho538108b2015-04-08 14:29:20 -070047 private final AtomicLong xidAtomic = new AtomicLong(1);
48
sangho538108b2015-04-08 14:29:20 -070049 /**
sangyun-han07a572f2016-07-29 21:14:27 +090050 * Creates a port states collector object.
sangho538108b2015-04-08 14:29:20 -070051 *
sangyun-han07a572f2016-07-29 21:14:27 +090052 * @param timer timer to use for scheduling
53 * @param sw switch to pull
54 * @param interval interval for collecting port statistic
sangho538108b2015-04-08 14:29:20 -070055 */
sangyun-han07a572f2016-07-29 21:14:27 +090056 PortStatsCollector(Timer timer, OpenFlowSwitch sw, int interval) {
57 this.timer = timer;
58 this.sw = checkNotNull(sw, "Null switch");
sangho538108b2015-04-08 14:29:20 -070059 this.refreshInterval = interval;
60 }
61
sangyun-han07a572f2016-07-29 21:14:27 +090062 private class InternalTimerTask extends TimerTask {
sangho538108b2015-04-08 14:29:20 -070063
sangyun-han07a572f2016-07-29 21:14:27 +090064 @Override
65 public void run() {
66 sendPortStatistic();
sangho538108b2015-04-08 14:29:20 -070067 }
68 }
69
sangyun-han07a572f2016-07-29 21:14:27 +090070 /**
71 * Starts the port statistic collector.
72 */
73 public synchronized void start() {
74 log.info("Starting Port Stats collection thread for {}", sw.getStringId());
75 task = new InternalTimerTask();
76 SharedExecutors.getTimer().scheduleAtFixedRate(task, 1 * SECONDS,
77 refreshInterval * SECONDS);
78 }
79
80 /**
81 * Stops the port statistic collector.
82 */
83 public synchronized void stop() {
84 log.info("Stopping Port Stats collection thread for {}", sw.getStringId());
85 task.cancel();
86 task = null;
87 }
88
89 /**
90 * Adjusts poll interval of the port statistic collector and restart.
91 *
92 * @param pollInterval period of collecting port statistic
93 */
94 public synchronized void adjustPollInterval(int pollInterval) {
Dusan Pajinbab8a5e2015-07-24 17:37:19 +020095 this.refreshInterval = pollInterval;
sangyun-han07a572f2016-07-29 21:14:27 +090096 task.cancel();
97 task = new InternalTimerTask();
98 timer.scheduleAtFixedRate(task, refreshInterval * SECONDS,
99 refreshInterval * SECONDS);
Dusan Pajinbab8a5e2015-07-24 17:37:19 +0200100 }
101
Jian Li889cffb2016-02-08 10:14:46 -0800102 /**
103 * Sends port statistic request to switch.
104 */
sangho538108b2015-04-08 14:29:20 -0700105 private void sendPortStatistic() {
sangho538108b2015-04-08 14:29:20 -0700106 if (sw.getRole() != RoleState.MASTER) {
107 return;
108 }
109 Long statsXid = xidAtomic.getAndIncrement();
110 OFPortStatsRequest statsRequest = sw.factory().buildPortStatsRequest()
111 .setPortNo(OFPort.ANY)
112 .setXid(statsXid)
113 .build();
114 sw.sendMsg(statsRequest);
115 }
sangyun-han07a572f2016-07-29 21:14:27 +0900116}