blob: c4c81afa2319370ee338f2363a505e2b99c3cdc5 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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
Thomas Vachuska75aaa672015-04-29 12:24:43 -070018import org.onlab.util.SharedExecutors;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import org.onosproject.openflow.controller.OpenFlowSwitch;
20import org.onosproject.openflow.controller.RoleState;
alshabibeec3a062014-09-17 18:01:26 -070021import org.projectfloodlight.openflow.protocol.OFFlowStatsRequest;
22import org.projectfloodlight.openflow.types.OFPort;
23import org.projectfloodlight.openflow.types.TableId;
24import org.slf4j.Logger;
25
Thomas Vachuska75aaa672015-04-29 12:24:43 -070026import java.util.Timer;
27import java.util.TimerTask;
28
29import static org.slf4j.LoggerFactory.getLogger;
30
31/**
32 * Collects flow statistics for the specified switch.
33 */
34class FlowStatsCollector {
alshabibeec3a062014-09-17 18:01:26 -070035
36 private final Logger log = getLogger(getClass());
37
Thomas Vachuska75aaa672015-04-29 12:24:43 -070038 public static final int SECONDS = 1000;
39
alshabibeec3a062014-09-17 18:01:26 -070040 private final OpenFlowSwitch sw;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070041 private Timer timer;
42 private TimerTask task;
alshabibeec3a062014-09-17 18:01:26 -070043
Thomas Vachuska75aaa672015-04-29 12:24:43 -070044 private int pollInterval;
alshabibeec3a062014-09-17 18:01:26 -070045
Thomas Vachuska75aaa672015-04-29 12:24:43 -070046 /**
47 * Creates a new collector for the given switch and poll frequency.
48 *
49 * @param timer timer to use for scheduling
50 * @param sw switch to pull
51 * @param pollInterval poll frequency in seconds
52 */
53 FlowStatsCollector(Timer timer, OpenFlowSwitch sw, int pollInterval) {
54 this.timer = timer;
alshabibeec3a062014-09-17 18:01:26 -070055 this.sw = sw;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070056 this.pollInterval = pollInterval;
alshabibeec3a062014-09-17 18:01:26 -070057 }
58
Thomas Vachuska75aaa672015-04-29 12:24:43 -070059 /**
60 * Adjusts poll frequency.
61 *
62 * @param pollInterval poll frequency in seconds
63 */
64 synchronized void adjustPollInterval(int pollInterval) {
65 this.pollInterval = pollInterval;
66 task.cancel();
67 task = new InternalTimerTask();
68 timer.scheduleAtFixedRate(task, pollInterval * SECONDS, pollInterval * 1000);
69 }
alshabibeec3a062014-09-17 18:01:26 -070070
Thomas Vachuska75aaa672015-04-29 12:24:43 -070071 private class InternalTimerTask extends TimerTask {
72 @Override
73 public void run() {
74 if (sw.getRole() == RoleState.MASTER) {
75 log.trace("Collecting stats for {}", sw.getStringId());
76 OFFlowStatsRequest request = sw.factory().buildFlowStatsRequest()
77 .setMatch(sw.factory().matchWildcardAll())
78 .setTableId(TableId.ALL)
79 .setOutPort(OFPort.NO_MASK)
80 .build();
81 sw.sendMsg(request);
82 }
alshabibeec3a062014-09-17 18:01:26 -070083 }
alshabibeec3a062014-09-17 18:01:26 -070084 }
85
Thomas Vachuska75aaa672015-04-29 12:24:43 -070086 public synchronized void start() {
87 // Initially start polling quickly. Then drop down to configured value
88 log.debug("Starting Stats collection thread for {}", sw.getStringId());
89 task = new InternalTimerTask();
90 SharedExecutors.getTimer().scheduleAtFixedRate(task, 1 * SECONDS,
91 pollInterval * SECONDS);
alshabibeec3a062014-09-17 18:01:26 -070092 }
93
Thomas Vachuska75aaa672015-04-29 12:24:43 -070094 public synchronized void stop() {
95 log.debug("Stopping Stats collection thread for {}", sw.getStringId());
96 task.cancel();
97 task = null;
alshabibeec3a062014-09-17 18:01:26 -070098 }
99
100}