Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 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 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 18 | import org.onlab.util.SharedExecutors; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 19 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 20 | import org.onosproject.openflow.controller.RoleState; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 21 | import org.projectfloodlight.openflow.protocol.OFFlowStatsRequest; |
| 22 | import org.projectfloodlight.openflow.types.OFPort; |
| 23 | import org.projectfloodlight.openflow.types.TableId; |
| 24 | import org.slf4j.Logger; |
| 25 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 26 | import java.util.Timer; |
| 27 | import java.util.TimerTask; |
| 28 | |
| 29 | import static org.slf4j.LoggerFactory.getLogger; |
| 30 | |
| 31 | /** |
| 32 | * Collects flow statistics for the specified switch. |
| 33 | */ |
| 34 | class FlowStatsCollector { |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 35 | |
| 36 | private final Logger log = getLogger(getClass()); |
| 37 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 38 | public static final int SECONDS = 1000; |
| 39 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 40 | private final OpenFlowSwitch sw; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 41 | private Timer timer; |
| 42 | private TimerTask task; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 43 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 44 | private int pollInterval; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 45 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 46 | /** |
| 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; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 55 | this.sw = sw; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 56 | this.pollInterval = pollInterval; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 59 | /** |
| 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 | } |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 70 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 71 | 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 | } |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 83 | } |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 86 | 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); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 94 | public synchronized void stop() { |
| 95 | log.debug("Stopping Stats collection thread for {}", sw.getStringId()); |
| 96 | task.cancel(); |
| 97 | task = null; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | } |