blob: 36d794868424682ede9eebde42cdc25760591395 [file] [log] [blame]
sangho538108b2015-04-08 14:29:20 -07001/*
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.device.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.OFPortStatsRequest;
26import org.projectfloodlight.openflow.types.OFPort;
27import org.slf4j.Logger;
28
29import java.util.concurrent.TimeUnit;
30import java.util.concurrent.atomic.AtomicLong;
31
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 PortStatsCollector implements TimerTask {
38
39 // TODO: Refactoring is required using ScheduledExecutorService
40
41 private final HashedWheelTimer timer = Timer.getTimer();
42 private final OpenFlowSwitch sw;
43 private final Logger log = getLogger(getClass());
44 private final int refreshInterval;
45 private final AtomicLong xidAtomic = new AtomicLong(1);
46
47 private Timeout timeout;
48
49 private boolean stopTimer = false;
50
51 /**
52 * Creates a GroupStatsCollector object.
53 *
54 * @param sw Open Flow switch
55 * @param interval time interval for collecting group statistic
56 */
57 public PortStatsCollector(OpenFlowSwitch sw, int interval) {
58 this.sw = sw;
59 this.refreshInterval = interval;
60 }
61
62 @Override
63 public void run(Timeout timeout) throws Exception {
64 log.trace("Collecting stats for {}", sw.getStringId());
65
66 sendPortStatistic();
67
68 if (!this.stopTimer) {
69 log.trace("Scheduling stats collection in {} seconds for {}",
70 this.refreshInterval, this.sw.getStringId());
71 timeout.getTimer().newTimeout(this, refreshInterval,
72 TimeUnit.SECONDS);
73 }
74 }
75
76 private void sendPortStatistic() {
77 if (log.isTraceEnabled()) {
78 log.trace("sendGroupStatistics {}:{}", sw.getStringId(), sw.getRole());
79 }
80 if (sw.getRole() != RoleState.MASTER) {
81 return;
82 }
83 Long statsXid = xidAtomic.getAndIncrement();
84 OFPortStatsRequest statsRequest = sw.factory().buildPortStatsRequest()
85 .setPortNo(OFPort.ANY)
86 .setXid(statsXid)
87 .build();
88 sw.sendMsg(statsRequest);
89 }
90
91 /**
92 * Starts the collector.
93 */
94 public void start() {
95 log.info("Starting Port Stats collection thread for {}", sw.getStringId());
96 timeout = timer.newTimeout(this, 1, TimeUnit.SECONDS);
97 }
98
99 /**
100 * Stops the collector.
101 */
102 public void stop() {
103 log.info("Stopping Port Stats collection thread for {}", sw.getStringId());
104 this.stopTimer = true;
105 timeout.cancel();
106 }
107}