blob: 88ba50b77e253e3cdfbd9c45395771e6ada16627 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.provider.of.flow.impl;
alshabibeec3a062014-09-17 18:01:26 -070017
Madan Jampani84382b92016-06-22 08:26:49 -070018import static com.google.common.base.Preconditions.checkNotNull;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070019import org.onlab.util.SharedExecutors;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.openflow.controller.OpenFlowSwitch;
21import org.onosproject.openflow.controller.RoleState;
alshabibeec3a062014-09-17 18:01:26 -070022import org.projectfloodlight.openflow.protocol.OFFlowStatsRequest;
23import org.projectfloodlight.openflow.types.OFPort;
24import org.projectfloodlight.openflow.types.TableId;
25import org.slf4j.Logger;
26
Thomas Vachuska75aaa672015-04-29 12:24:43 -070027import java.util.Timer;
28import java.util.TimerTask;
29
30import static org.slf4j.LoggerFactory.getLogger;
31
32/**
33 * Collects flow statistics for the specified switch.
34 */
Thomas Vachuskaa394b952016-06-14 15:02:09 -070035class FlowStatsCollector implements SwitchDataCollector {
alshabibeec3a062014-09-17 18:01:26 -070036
37 private final Logger log = getLogger(getClass());
38
Thomas Vachuska75aaa672015-04-29 12:24:43 -070039 public static final int SECONDS = 1000;
40
alshabibeec3a062014-09-17 18:01:26 -070041 private final OpenFlowSwitch sw;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070042 private Timer timer;
43 private TimerTask task;
alshabibeec3a062014-09-17 18:01:26 -070044
Thomas Vachuska75aaa672015-04-29 12:24:43 -070045 private int pollInterval;
alshabibeec3a062014-09-17 18:01:26 -070046
Thomas Vachuska75aaa672015-04-29 12:24:43 -070047 /**
48 * Creates a new collector for the given switch and poll frequency.
49 *
50 * @param timer timer to use for scheduling
51 * @param sw switch to pull
52 * @param pollInterval poll frequency in seconds
53 */
54 FlowStatsCollector(Timer timer, OpenFlowSwitch sw, int pollInterval) {
55 this.timer = timer;
Madan Jampani84382b92016-06-22 08:26:49 -070056 this.sw = checkNotNull(sw, "Null switch");
Thomas Vachuska75aaa672015-04-29 12:24:43 -070057 this.pollInterval = pollInterval;
alshabibeec3a062014-09-17 18:01:26 -070058 }
59
Thomas Vachuska75aaa672015-04-29 12:24:43 -070060 /**
61 * Adjusts poll frequency.
62 *
63 * @param pollInterval poll frequency in seconds
64 */
65 synchronized void adjustPollInterval(int pollInterval) {
66 this.pollInterval = pollInterval;
67 task.cancel();
68 task = new InternalTimerTask();
69 timer.scheduleAtFixedRate(task, pollInterval * SECONDS, pollInterval * 1000);
70 }
alshabibeec3a062014-09-17 18:01:26 -070071
Thomas Vachuska75aaa672015-04-29 12:24:43 -070072 private class InternalTimerTask extends TimerTask {
73 @Override
74 public void run() {
75 if (sw.getRole() == RoleState.MASTER) {
76 log.trace("Collecting stats for {}", sw.getStringId());
77 OFFlowStatsRequest request = sw.factory().buildFlowStatsRequest()
78 .setMatch(sw.factory().matchWildcardAll())
79 .setTableId(TableId.ALL)
80 .setOutPort(OFPort.NO_MASK)
81 .build();
82 sw.sendMsg(request);
83 }
alshabibeec3a062014-09-17 18:01:26 -070084 }
alshabibeec3a062014-09-17 18:01:26 -070085 }
86
Thomas Vachuska75aaa672015-04-29 12:24:43 -070087 public synchronized void start() {
88 // Initially start polling quickly. Then drop down to configured value
89 log.debug("Starting Stats collection thread for {}", sw.getStringId());
90 task = new InternalTimerTask();
91 SharedExecutors.getTimer().scheduleAtFixedRate(task, 1 * SECONDS,
92 pollInterval * SECONDS);
alshabibeec3a062014-09-17 18:01:26 -070093 }
94
Thomas Vachuska75aaa672015-04-29 12:24:43 -070095 public synchronized void stop() {
96 log.debug("Stopping Stats collection thread for {}", sw.getStringId());
97 task.cancel();
98 task = null;
alshabibeec3a062014-09-17 18:01:26 -070099 }
100
101}