Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.provider.of.flow.impl; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 17 | |
Madan Jampani | 84382b9 | 2016-06-22 08:26:49 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 19 | import org.onlab.util.SharedExecutors; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 21 | import org.onosproject.openflow.controller.RoleState; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 22 | import org.projectfloodlight.openflow.protocol.OFFlowStatsRequest; |
| 23 | import org.projectfloodlight.openflow.types.OFPort; |
| 24 | import org.projectfloodlight.openflow.types.TableId; |
| 25 | import org.slf4j.Logger; |
| 26 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 27 | import java.util.Timer; |
| 28 | import java.util.TimerTask; |
| 29 | |
| 30 | import static org.slf4j.LoggerFactory.getLogger; |
| 31 | |
| 32 | /** |
| 33 | * Collects flow statistics for the specified switch. |
| 34 | */ |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 35 | class FlowStatsCollector implements SwitchDataCollector { |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 36 | |
| 37 | private final Logger log = getLogger(getClass()); |
| 38 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 39 | public static final int SECONDS = 1000; |
| 40 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 41 | private final OpenFlowSwitch sw; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 42 | private Timer timer; |
| 43 | private TimerTask task; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 44 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 45 | private int pollInterval; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 46 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 47 | /** |
| 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 Jampani | 84382b9 | 2016-06-22 08:26:49 -0700 | [diff] [blame] | 56 | this.sw = checkNotNull(sw, "Null switch"); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 57 | this.pollInterval = pollInterval; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 60 | /** |
| 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 | } |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 71 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 72 | 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 | } |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 84 | } |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 87 | 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); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 95 | public synchronized void stop() { |
| 96 | log.debug("Stopping Stats collection thread for {}", sw.getStringId()); |
| 97 | task.cancel(); |
| 98 | task = null; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | } |