blob: 7366806514aa85072e625469b4ddb423a6846df5 [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
sangho5afd02a2015-02-03 20:07:35 -080019import org.onlab.util.Timer;
20import org.onosproject.openflow.controller.OpenFlowSwitch;
21import org.onosproject.openflow.controller.RoleState;
22import org.projectfloodlight.openflow.protocol.OFGroupDescStatsRequest;
23import org.projectfloodlight.openflow.protocol.OFGroupStatsRequest;
24import org.projectfloodlight.openflow.types.OFGroup;
25import org.slf4j.Logger;
26
Yuta HIGUCHI19afc032017-05-20 23:44:17 -070027import io.netty.util.Timeout;
28import io.netty.util.TimerTask;
29
sangho5afd02a2015-02-03 20:07:35 -080030import 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
sangho5afd02a2015-02-03 20:07:35 -080039 private final OpenFlowSwitch sw;
40 private final Logger log = getLogger(getClass());
41 private final int refreshInterval;
42
43 private Timeout timeout;
sangho5afd02a2015-02-03 20:07:35 -080044
45 private boolean stopTimer = false;
46
47 /**
48 * Creates a GroupStatsCollector object.
49 *
50 * @param sw Open Flow switch
51 * @param interval time interval for collecting group statistic
52 */
53 public GroupStatsCollector(OpenFlowSwitch sw, int interval) {
54 this.sw = sw;
55 this.refreshInterval = interval;
56 }
57
58 @Override
59 public void run(Timeout timeout) throws Exception {
60 log.trace("Collecting stats for {}", sw.getStringId());
61
Palash Kalaf95c38b2017-05-15 23:52:48 +090062 sendGroupStatisticRequest();
sangho5afd02a2015-02-03 20:07:35 -080063
64 if (!this.stopTimer) {
65 log.trace("Scheduling stats collection in {} seconds for {}",
66 this.refreshInterval, this.sw.getStringId());
Yuta HIGUCHI19afc032017-05-20 23:44:17 -070067 timeout.timer().newTimeout(this, refreshInterval,
sangho5afd02a2015-02-03 20:07:35 -080068 TimeUnit.SECONDS);
69 }
70 }
71
Palash Kalaf95c38b2017-05-15 23:52:48 +090072 private void sendGroupStatisticRequest() {
sangho5afd02a2015-02-03 20:07:35 -080073 if (log.isTraceEnabled()) {
74 log.trace("sendGroupStatistics {}:{}", sw.getStringId(), sw.getRole());
75 }
76 if (sw.getRole() != RoleState.MASTER) {
77 return;
78 }
Yuta HIGUCHI07752ec2016-06-29 22:49:20 -070079 if (!sw.isConnected()) {
80 return;
81 }
sanghoa09f2742015-02-06 14:49:47 -080082 Long statsXid = OpenFlowGroupProvider.getXidAndAdd(2);
sangho5afd02a2015-02-03 20:07:35 -080083 OFGroupStatsRequest statsRequest = sw.factory().buildGroupStatsRequest()
84 .setGroup(OFGroup.ALL)
85 .setXid(statsXid)
86 .build();
87 sw.sendMsg(statsRequest);
88
89 Long descXid = statsXid + 1;
90 OFGroupDescStatsRequest descStatsRequest =
91 sw.factory().buildGroupDescStatsRequest()
92 .setXid(descXid)
93 .build();
94 sw.sendMsg(descStatsRequest);
95 }
96
97 /**
98 * Starts the collector.
99 */
100 public void start() {
Jonathan Harte106e4b2015-03-09 16:44:56 -0700101 log.info("Starting Group Stats collection thread for {}", sw.getStringId());
Yuta HIGUCHI19afc032017-05-20 23:44:17 -0700102 timeout = Timer.newTimeout(this, 1, TimeUnit.SECONDS);
sangho5afd02a2015-02-03 20:07:35 -0800103 }
104
105 /**
106 * Stops the collector.
107 */
108 public void stop() {
109 log.info("Stopping Group Stats collection thread for {}", sw.getStringId());
110 this.stopTimer = true;
111 timeout.cancel();
112 }
113}