blob: 4b1ce6f213d43c1ebfb6349e0d066d6133a87707 [file] [log] [blame]
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -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 */
16package org.onosproject.provider.of.flow.impl;
17
18import org.onlab.util.SharedExecutors;
19import org.onosproject.openflow.controller.OpenFlowSwitch;
20import org.onosproject.openflow.controller.RoleState;
21import org.projectfloodlight.openflow.protocol.OFTableStatsRequest;
22import org.slf4j.Logger;
23
24import java.util.Timer;
25import java.util.TimerTask;
26
27import static org.slf4j.LoggerFactory.getLogger;
28
29/**
30 * Collects Table statistics for the specified switch.
31 */
32class TableStatisticsCollector {
33
34 private final Logger log = getLogger(getClass());
35
36 public static final int SECONDS = 1000;
37
38 private final OpenFlowSwitch sw;
39 private Timer timer;
40 private TimerTask task;
41
42 private int pollInterval;
43
44 /**
45 * Creates a new table statistics collector for the given switch and poll frequency.
46 *
47 * @param timer timer to use for scheduling
48 * @param sw switch to pull
49 * @param pollInterval poll frequency in seconds
50 */
51 TableStatisticsCollector(Timer timer, OpenFlowSwitch sw, int pollInterval) {
52 this.timer = timer;
53 this.sw = sw;
54 this.pollInterval = pollInterval;
55 }
56
57 /**
58 * Adjusts poll frequency.
59 *
60 * @param pollInterval poll frequency in seconds
61 */
62 synchronized void adjustPollInterval(int pollInterval) {
63 this.pollInterval = pollInterval;
64 task.cancel();
65 task = new InternalTimerTask();
66 timer.scheduleAtFixedRate(task, pollInterval * SECONDS, pollInterval * 1000);
67 }
68
69 private class InternalTimerTask extends TimerTask {
70 @Override
71 public void run() {
72 if (sw.getRole() == RoleState.MASTER) {
73 log.trace("Collecting stats for {}", sw.getStringId());
74 OFTableStatsRequest request = sw.factory().buildTableStatsRequest()
75 .build();
76 sw.sendMsg(request);
77 }
78 }
79 }
80
81 public synchronized void start() {
82 // Initially start polling quickly. Then drop down to configured value
83 log.debug("Starting Table Stats collection thread for {}", sw.getStringId());
84 task = new InternalTimerTask();
85 SharedExecutors.getTimer().scheduleAtFixedRate(task, 1 * SECONDS,
86 pollInterval * SECONDS);
87 }
88
89 public synchronized void stop() {
90 log.debug("Stopping Table Stats collection thread for {}", sw.getStringId());
91 task.cancel();
92 task = null;
93 }
94
95}