blob: 9816426b9c9b67430dbb3e76e05dbc22f1489e7b [file] [log] [blame]
sangho5afd02a2015-02-03 20:07:35 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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 }
sanghoa09f2742015-02-06 14:49:47 -080080 Long statsXid = OpenFlowGroupProvider.getXidAndAdd(2);
sangho5afd02a2015-02-03 20:07:35 -080081 OFGroupStatsRequest statsRequest = sw.factory().buildGroupStatsRequest()
82 .setGroup(OFGroup.ALL)
83 .setXid(statsXid)
84 .build();
85 sw.sendMsg(statsRequest);
86
87 Long descXid = statsXid + 1;
88 OFGroupDescStatsRequest descStatsRequest =
89 sw.factory().buildGroupDescStatsRequest()
90 .setXid(descXid)
91 .build();
92 sw.sendMsg(descStatsRequest);
93 }
94
95 /**
96 * Starts the collector.
97 */
98 public void start() {
Jonathan Harte106e4b2015-03-09 16:44:56 -070099 log.info("Starting Group Stats collection thread for {}", sw.getStringId());
sangho5afd02a2015-02-03 20:07:35 -0800100 timeout = timer.newTimeout(this, 1, TimeUnit.SECONDS);
101 }
102
103 /**
104 * Stops the collector.
105 */
106 public void stop() {
107 log.info("Stopping Group Stats collection thread for {}", sw.getStringId());
108 this.stopTimer = true;
109 timeout.cancel();
110 }
111}