blob: 38af8db044ae4749555cd11d80d71d33a89ce481 [file] [log] [blame]
alshabibc7911792015-07-30 17:55:30 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
alshabibc7911792015-07-30 17:55:30 -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 */
16
17package org.onosproject.provider.of.meter.impl;
18
19import org.jboss.netty.util.HashedWheelTimer;
20import org.jboss.netty.util.Timeout;
21import org.jboss.netty.util.TimerTask;
22import org.onlab.util.Timer;
23import org.onosproject.openflow.controller.OpenFlowSwitch;
24import org.onosproject.openflow.controller.RoleState;
25import org.projectfloodlight.openflow.protocol.OFMeterStatsRequest;
26import org.slf4j.Logger;
27
28import java.util.concurrent.TimeUnit;
29
30import static org.slf4j.LoggerFactory.getLogger;
31
32/*
33 * Sends Meter Stats Request and collect the Meter statistics with a time interval.
34 */
35public 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 Jampani84382b92016-06-22 08:26:49 -070059 if (!sw.isConnected()) {
60 log.debug("Switch {} disconnected. Aborting meter stats collection", sw.getStringId());
61 return;
62 }
63
alshabibc7911792015-07-30 17:55:30 -070064 log.trace("Collecting stats for {}", sw.getStringId());
65
Palash Kalaf95c38b2017-05-15 23:52:48 +090066 sendMeterStatisticRequest();
alshabibc7911792015-07-30 17:55:30 -070067
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
Palash Kalaf95c38b2017-05-15 23:52:48 +090076 public void sendMeterStatisticRequest() {
alshabibc7911792015-07-30 17:55:30 -070077 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}