blob: 90c700e7898fcabe85cf7f61c832b6b7a86d5e15 [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;
31import java.util.concurrent.atomic.AtomicLong;
32
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
40 private final HashedWheelTimer timer = Timer.getTimer();
41 private final OpenFlowSwitch sw;
42 private final Logger log = getLogger(getClass());
43 private final int refreshInterval;
44
45 private Timeout timeout;
46 private final AtomicLong xidCounter = new AtomicLong(1);
47
48 private boolean stopTimer = false;
49
50 /**
51 * Creates a GroupStatsCollector object.
52 *
53 * @param sw Open Flow switch
54 * @param interval time interval for collecting group statistic
55 */
56 public GroupStatsCollector(OpenFlowSwitch sw, int interval) {
57 this.sw = sw;
58 this.refreshInterval = interval;
59 }
60
61 @Override
62 public void run(Timeout timeout) throws Exception {
63 log.trace("Collecting stats for {}", sw.getStringId());
64
65 sendGroupStatistic();
66
67 if (!this.stopTimer) {
68 log.trace("Scheduling stats collection in {} seconds for {}",
69 this.refreshInterval, this.sw.getStringId());
70 timeout.getTimer().newTimeout(this, refreshInterval,
71 TimeUnit.SECONDS);
72 }
73 }
74
75 private void sendGroupStatistic() {
76 if (log.isTraceEnabled()) {
77 log.trace("sendGroupStatistics {}:{}", sw.getStringId(), sw.getRole());
78 }
79 if (sw.getRole() != RoleState.MASTER) {
80 return;
81 }
82 Long statsXid = xidCounter.getAndAdd(2);
83 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() {
101 log.info("Staring Group Stats collection thread for {}", sw.getStringId());
102 timeout = timer.newTimeout(this, 1, TimeUnit.SECONDS);
103 }
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}