blob: 4a4934f3e38ba92c947d17447e5c5fefaa7d388f [file] [log] [blame]
sangho5afd02a2015-02-03 20:07:35 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Laszlo Pappcd91bfd2018-01-29 14:30:34 +000022import org.projectfloodlight.openflow.protocol.OFCapabilities;
sangho5afd02a2015-02-03 20:07:35 -080023import org.projectfloodlight.openflow.protocol.OFGroupDescStatsRequest;
24import org.projectfloodlight.openflow.protocol.OFGroupStatsRequest;
25import org.projectfloodlight.openflow.types.OFGroup;
26import org.slf4j.Logger;
27
Yuta HIGUCHI19afc032017-05-20 23:44:17 -070028import io.netty.util.Timeout;
29import io.netty.util.TimerTask;
30
sangho5afd02a2015-02-03 20:07:35 -080031import java.util.concurrent.TimeUnit;
sangho5afd02a2015-02-03 20:07:35 -080032
33import static org.slf4j.LoggerFactory.getLogger;
34
35/*
36 * Sends Group Stats Request and collect the group statistics with a time interval.
37 */
38public class GroupStatsCollector implements TimerTask {
39
sangho5afd02a2015-02-03 20:07:35 -080040 private final OpenFlowSwitch sw;
41 private final Logger log = getLogger(getClass());
dvaddireb57fdb02017-06-20 00:14:08 +053042 private int refreshInterval;
sangho5afd02a2015-02-03 20:07:35 -080043
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
Laszlo Pappcd91bfd2018-01-29 14:30:34 +000063 sendGroupStatisticRequests();
sangho5afd02a2015-02-03 20:07:35 -080064
65 if (!this.stopTimer) {
66 log.trace("Scheduling stats collection in {} seconds for {}",
67 this.refreshInterval, this.sw.getStringId());
Yuta HIGUCHI19afc032017-05-20 23:44:17 -070068 timeout.timer().newTimeout(this, refreshInterval,
sangho5afd02a2015-02-03 20:07:35 -080069 TimeUnit.SECONDS);
70 }
71 }
72
Laszlo Pappcd91bfd2018-01-29 14:30:34 +000073 private void sendGroupDescStatisticRequest(long xid) {
74 OFGroupDescStatsRequest descStatsRequest =
75 sw.factory().buildGroupDescStatsRequest()
76 .setXid(xid)
77 .build();
78 sw.sendMsg(descStatsRequest);
79 }
80
81 private void sendGroupStatisticRequest(long xid) {
82 OFGroupStatsRequest statsRequest = sw.factory().buildGroupStatsRequest()
83 .setGroup(OFGroup.ALL)
84 .setXid(xid)
85 .build();
86 sw.sendMsg(statsRequest);
87 }
88
89 private void sendGroupStatisticRequests() {
sangho5afd02a2015-02-03 20:07:35 -080090 if (log.isTraceEnabled()) {
91 log.trace("sendGroupStatistics {}:{}", sw.getStringId(), sw.getRole());
92 }
93 if (sw.getRole() != RoleState.MASTER) {
94 return;
95 }
Yuta HIGUCHI07752ec2016-06-29 22:49:20 -070096 if (!sw.isConnected()) {
97 return;
98 }
sangho5afd02a2015-02-03 20:07:35 -080099
Laszlo Pappcd91bfd2018-01-29 14:30:34 +0000100 if (sw.features().getCapabilities().contains(OFCapabilities.GROUP_STATS)) {
101 long xid = OpenFlowGroupProvider.getXidAndAdd(2);
Thomas Vachuskafca3c6a2018-04-02 16:21:59 -0700102 sendGroupStatisticRequest(xid);
103 sendGroupDescStatisticRequest(xid + 1);
Laszlo Pappcd91bfd2018-01-29 14:30:34 +0000104 } else {
105 long xid = OpenFlowGroupProvider.getXidAndAdd(1);
106 sendGroupDescStatisticRequest(xid);
107 }
sangho5afd02a2015-02-03 20:07:35 -0800108 }
109
dvaddireb57fdb02017-06-20 00:14:08 +0530110 public void adjustRate(int pollInterval) {
111 this.refreshInterval = pollInterval;
112 }
113
sangho5afd02a2015-02-03 20:07:35 -0800114 /**
115 * Starts the collector.
116 */
117 public void start() {
Jonathan Harte106e4b2015-03-09 16:44:56 -0700118 log.info("Starting Group Stats collection thread for {}", sw.getStringId());
Yuta HIGUCHI19afc032017-05-20 23:44:17 -0700119 timeout = Timer.newTimeout(this, 1, TimeUnit.SECONDS);
sangho5afd02a2015-02-03 20:07:35 -0800120 }
121
122 /**
123 * Stops the collector.
124 */
125 public void stop() {
126 log.info("Stopping Group Stats collection thread for {}", sw.getStringId());
127 this.stopTimer = true;
128 timeout.cancel();
129 }
130}