alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | |
| 17 | package org.onosproject.provider.of.meter.impl; |
| 18 | |
| 19 | import org.jboss.netty.util.HashedWheelTimer; |
| 20 | import org.jboss.netty.util.Timeout; |
| 21 | import org.jboss.netty.util.TimerTask; |
| 22 | import org.onlab.util.Timer; |
| 23 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 24 | import org.onosproject.openflow.controller.RoleState; |
| 25 | import org.projectfloodlight.openflow.protocol.OFMeterStatsRequest; |
| 26 | import org.slf4j.Logger; |
| 27 | |
| 28 | import java.util.concurrent.TimeUnit; |
| 29 | |
| 30 | import static org.slf4j.LoggerFactory.getLogger; |
| 31 | |
| 32 | /* |
| 33 | * Sends Meter Stats Request and collect the Meter statistics with a time interval. |
| 34 | */ |
| 35 | public class MeterStatsCollector implements TimerTask { |
| 36 | |
| 37 | private final HashedWheelTimer timer = Timer.getTimer(); |
| 38 | private final OpenFlowSwitch sw; |
| 39 | private final Logger log = getLogger(getClass()); |
| 40 | private final int refreshInterval; |
| 41 | |
| 42 | private Timeout timeout; |
| 43 | |
| 44 | private boolean stopTimer = false; |
| 45 | |
| 46 | /** |
| 47 | * Creates a GroupStatsCollector object. |
| 48 | * |
| 49 | * @param sw Open Flow switch |
| 50 | * @param interval time interval for collecting group statistic |
| 51 | */ |
| 52 | public MeterStatsCollector(OpenFlowSwitch sw, int interval) { |
| 53 | this.sw = sw; |
| 54 | this.refreshInterval = interval; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public void run(Timeout timeout) throws Exception { |
Madan Jampani | 84382b9 | 2016-06-22 08:26:49 -0700 | [diff] [blame] | 59 | if (!sw.isConnected()) { |
| 60 | log.debug("Switch {} disconnected. Aborting meter stats collection", sw.getStringId()); |
| 61 | return; |
| 62 | } |
| 63 | |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 64 | log.trace("Collecting stats for {}", sw.getStringId()); |
| 65 | |
| 66 | sendMeterStatistic(); |
| 67 | |
| 68 | if (!this.stopTimer) { |
| 69 | log.trace("Scheduling stats collection in {} seconds for {}", |
| 70 | this.refreshInterval, this.sw.getStringId()); |
| 71 | timeout.getTimer().newTimeout(this, refreshInterval, |
| 72 | TimeUnit.SECONDS); |
| 73 | } |
| 74 | } |
| 75 | |
Jordi Ortiz | 9fe79a2 | 2017-04-25 12:57:11 +0200 | [diff] [blame^] | 76 | public void sendMeterStatistic() { |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 77 | if (log.isTraceEnabled()) { |
| 78 | log.trace("sendMeterStatistics {}:{}", sw.getStringId(), sw.getRole()); |
| 79 | } |
| 80 | if (sw.getRole() != RoleState.MASTER) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | OFMeterStatsRequest.Builder builder = |
| 85 | sw.factory().buildMeterStatsRequest(); |
| 86 | builder.setXid(0).setMeterId(0xFFFFFFFF); |
| 87 | |
| 88 | sw.sendMsg(builder.build()); |
| 89 | |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Starts the collector. |
| 94 | */ |
| 95 | public void start() { |
| 96 | log.info("Starting Meter Stats collection thread for {}", sw.getStringId()); |
| 97 | timeout = timer.newTimeout(this, 1, TimeUnit.SECONDS); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Stops the collector. |
| 102 | */ |
| 103 | public void stop() { |
| 104 | log.info("Stopping Meter Stats collection thread for {}", sw.getStringId()); |
| 105 | this.stopTimer = true; |
| 106 | timeout.cancel(); |
| 107 | } |
| 108 | } |