blob: 146ccc353fae7101454eb0fb8a0efbdc84b66bcc [file] [log] [blame]
sangho5afd02a2015-02-03 20:07:35 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
sangho5afd02a2015-02-03 20:07:35 -08003 *
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.group.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.OFGroupDescStatsRequest;
26import org.projectfloodlight.openflow.protocol.OFGroupStatsRequest;
27import org.projectfloodlight.openflow.types.OFGroup;
28import org.slf4j.Logger;
29
30import java.util.concurrent.TimeUnit;
sangho5afd02a2015-02-03 20:07:35 -080031
32import static org.slf4j.LoggerFactory.getLogger;
33
34/*
35 * Sends Group Stats Request and collect the group statistics with a time interval.
36 */
37public class GroupStatsCollector implements TimerTask {
38
39 private final HashedWheelTimer timer = Timer.getTimer();
40 private final OpenFlowSwitch sw;
41 private final Logger log = getLogger(getClass());
42 private final int refreshInterval;
43
44 private Timeout timeout;
sangho5afd02a2015-02-03 20:07:35 -080045
46 private boolean stopTimer = false;
47
48 /**
49 * Creates a GroupStatsCollector object.
50 *
51 * @param sw Open Flow switch
52 * @param interval time interval for collecting group statistic
53 */
54 public GroupStatsCollector(OpenFlowSwitch sw, int interval) {
55 this.sw = sw;
56 this.refreshInterval = interval;
57 }
58
59 @Override
60 public void run(Timeout timeout) throws Exception {
61 log.trace("Collecting stats for {}", sw.getStringId());
62
63 sendGroupStatistic();
64
65 if (!this.stopTimer) {
66 log.trace("Scheduling stats collection in {} seconds for {}",
67 this.refreshInterval, this.sw.getStringId());
68 timeout.getTimer().newTimeout(this, refreshInterval,
69 TimeUnit.SECONDS);
70 }
71 }
72
73 private void sendGroupStatistic() {
74 if (log.isTraceEnabled()) {
75 log.trace("sendGroupStatistics {}:{}", sw.getStringId(), sw.getRole());
76 }
77 if (sw.getRole() != RoleState.MASTER) {
78 return;
79 }
Yuta HIGUCHI07752ec2016-06-29 22:49:20 -070080 if (!sw.isConnected()) {
81 return;
82 }
sanghoa09f2742015-02-06 14:49:47 -080083 Long statsXid = OpenFlowGroupProvider.getXidAndAdd(2);
sangho5afd02a2015-02-03 20:07:35 -080084 OFGroupStatsRequest statsRequest = sw.factory().buildGroupStatsRequest()
85 .setGroup(OFGroup.ALL)
86 .setXid(statsXid)
87 .build();
88 sw.sendMsg(statsRequest);
89
90 Long descXid = statsXid + 1;
91 OFGroupDescStatsRequest descStatsRequest =
92 sw.factory().buildGroupDescStatsRequest()
93 .setXid(descXid)
94 .build();
95 sw.sendMsg(descStatsRequest);
96 }
97
98 /**
99 * Starts the collector.
100 */
101 public void start() {
Jonathan Harte106e4b2015-03-09 16:44:56 -0700102 log.info("Starting Group Stats collection thread for {}", sw.getStringId());
sangho5afd02a2015-02-03 20:07:35 -0800103 timeout = timer.newTimeout(this, 1, TimeUnit.SECONDS);
104 }
105
106 /**
107 * Stops the collector.
108 */
109 public void stop() {
110 log.info("Stopping Group Stats collection thread for {}", sw.getStringId());
111 this.stopTimer = true;
112 timeout.cancel();
113 }
114}