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 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import java.util.concurrent.TimeUnit; |
| 21 | |
| 22 | import org.jboss.netty.util.HashedWheelTimer; |
| 23 | import org.jboss.netty.util.Timeout; |
| 24 | import org.jboss.netty.util.TimerTask; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 26 | import org.onosproject.openflow.controller.RoleState; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 27 | import org.onlab.util.Timer; |
| 28 | import org.projectfloodlight.openflow.protocol.OFFlowStatsRequest; |
| 29 | import org.projectfloodlight.openflow.types.OFPort; |
| 30 | import org.projectfloodlight.openflow.types.TableId; |
| 31 | import org.slf4j.Logger; |
| 32 | |
| 33 | public class FlowStatsCollector implements TimerTask { |
| 34 | |
| 35 | private final Logger log = getLogger(getClass()); |
| 36 | |
| 37 | private final HashedWheelTimer timer = Timer.getTimer(); |
| 38 | private final OpenFlowSwitch sw; |
| 39 | private final int refreshInterval; |
| 40 | |
| 41 | private Timeout timeout; |
| 42 | |
| 43 | private boolean stopTimer = false;; |
| 44 | |
| 45 | public FlowStatsCollector(OpenFlowSwitch sw, int refreshInterval) { |
| 46 | this.sw = sw; |
| 47 | this.refreshInterval = refreshInterval; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public void run(Timeout timeout) throws Exception { |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 52 | log.trace("Collecting stats for {}", this.sw.getStringId()); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 53 | |
| 54 | sendFlowStatistics(); |
| 55 | |
| 56 | if (!this.stopTimer) { |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 57 | log.trace("Scheduling stats collection in {} seconds for {}", |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 58 | this.refreshInterval, this.sw.getStringId()); |
| 59 | timeout.getTimer().newTimeout(this, refreshInterval, |
| 60 | TimeUnit.SECONDS); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | } |
| 65 | |
| 66 | private void sendFlowStatistics() { |
Yuta HIGUCHI | 1b6f568 | 2014-10-25 21:33:59 -0700 | [diff] [blame] | 67 | if (log.isTraceEnabled()) { |
| 68 | log.trace("sendFlowStatistics {}:{}", sw.getStringId(), sw.getRole()); |
| 69 | } |
| 70 | if (sw.getRole() != RoleState.MASTER) { |
| 71 | // Switch not master. |
| 72 | return; |
| 73 | } |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 74 | OFFlowStatsRequest request = sw.factory().buildFlowStatsRequest() |
| 75 | .setMatch(sw.factory().matchWildcardAll()) |
| 76 | .setTableId(TableId.ALL) |
| 77 | .setOutPort(OFPort.NO_MASK) |
| 78 | .build(); |
| 79 | |
| 80 | this.sw.sendMsg(request); |
| 81 | |
| 82 | } |
| 83 | |
| 84 | public void start() { |
| 85 | |
| 86 | /* |
| 87 | * Initially start polling quickly. Then drop down to configured value |
| 88 | */ |
| 89 | log.info("Starting Stats collection thread for {}", |
| 90 | this.sw.getStringId()); |
| 91 | timeout = timer.newTimeout(this, 1, TimeUnit.SECONDS); |
| 92 | } |
| 93 | |
| 94 | public void stop() { |
| 95 | log.info("Stopping Stats collection thread for {}", |
| 96 | this.sw.getStringId()); |
| 97 | this.stopTimer = true; |
| 98 | timeout.cancel(); |
| 99 | } |
| 100 | |
| 101 | } |