blob: a11847a57f60d11bb59cc82c8e0a307145733340 [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 {
59 log.trace("Collecting stats for {}", sw.getStringId());
60
61 sendMeterStatistic();
62
63 if (!this.stopTimer) {
64 log.trace("Scheduling stats collection in {} seconds for {}",
65 this.refreshInterval, this.sw.getStringId());
66 timeout.getTimer().newTimeout(this, refreshInterval,
67 TimeUnit.SECONDS);
68 }
69 }
70
71 private void sendMeterStatistic() {
72 if (log.isTraceEnabled()) {
73 log.trace("sendMeterStatistics {}:{}", sw.getStringId(), sw.getRole());
74 }
75 if (sw.getRole() != RoleState.MASTER) {
76 return;
77 }
78
79 OFMeterStatsRequest.Builder builder =
80 sw.factory().buildMeterStatsRequest();
81 builder.setXid(0).setMeterId(0xFFFFFFFF);
82
83 sw.sendMsg(builder.build());
84
85 }
86
87 /**
88 * Starts the collector.
89 */
90 public void start() {
91 log.info("Starting Meter Stats collection thread for {}", sw.getStringId());
92 timeout = timer.newTimeout(this, 1, TimeUnit.SECONDS);
93 }
94
95 /**
96 * Stops the collector.
97 */
98 public void stop() {
99 log.info("Stopping Meter Stats collection thread for {}", sw.getStringId());
100 this.stopTimer = true;
101 timeout.cancel();
102 }
103}