blob: 7627f5c6cabcd83813016cab059f95d450658628 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
alshabibeec3a062014-09-17 18:01:26 -070016package org.onlab.onos.provider.of.flow.impl;
17
18import static org.slf4j.LoggerFactory.getLogger;
19
20import java.util.concurrent.TimeUnit;
21
22import org.jboss.netty.util.HashedWheelTimer;
23import org.jboss.netty.util.Timeout;
24import org.jboss.netty.util.TimerTask;
25import org.onlab.onos.openflow.controller.OpenFlowSwitch;
Yuta HIGUCHI1b6f5682014-10-25 21:33:59 -070026import org.onlab.onos.openflow.controller.RoleState;
alshabibeec3a062014-09-17 18:01:26 -070027import org.onlab.util.Timer;
28import org.projectfloodlight.openflow.protocol.OFFlowStatsRequest;
29import org.projectfloodlight.openflow.types.OFPort;
30import org.projectfloodlight.openflow.types.TableId;
31import org.slf4j.Logger;
32
33public class FlowStatsCollector implements TimerTask {
34
35 private final Logger log = getLogger(getClass());
36
37 private final HashedWheelTimer timer = Timer.getTimer();
38 private final OpenFlowSwitch sw;
39 private final int refreshInterval;
40
41 private Timeout timeout;
42
43 private boolean stopTimer = false;;
44
45 public FlowStatsCollector(OpenFlowSwitch sw, int refreshInterval) {
46 this.sw = sw;
47 this.refreshInterval = refreshInterval;
48 }
49
50 @Override
51 public void run(Timeout timeout) throws Exception {
Yuta HIGUCHIfaf9e1c2014-11-20 00:31:29 -080052 log.trace("Collecting stats for {}", this.sw.getStringId());
alshabibeec3a062014-09-17 18:01:26 -070053
54 sendFlowStatistics();
55
56 if (!this.stopTimer) {
Yuta HIGUCHIfaf9e1c2014-11-20 00:31:29 -080057 log.trace("Scheduling stats collection in {} seconds for {}",
alshabibeec3a062014-09-17 18:01:26 -070058 this.refreshInterval, this.sw.getStringId());
59 timeout.getTimer().newTimeout(this, refreshInterval,
60 TimeUnit.SECONDS);
61 }
62
63
64 }
65
66 private void sendFlowStatistics() {
Yuta HIGUCHI1b6f5682014-10-25 21:33:59 -070067 if (log.isTraceEnabled()) {
68 log.trace("sendFlowStatistics {}:{}", sw.getStringId(), sw.getRole());
69 }
70 if (sw.getRole() != RoleState.MASTER) {
71 // Switch not master.
72 return;
73 }
alshabibeec3a062014-09-17 18:01:26 -070074 OFFlowStatsRequest request = sw.factory().buildFlowStatsRequest()
75 .setMatch(sw.factory().matchWildcardAll())
76 .setTableId(TableId.ALL)
77 .setOutPort(OFPort.NO_MASK)
78 .build();
79
80 this.sw.sendMsg(request);
81
82 }
83
84 public void start() {
85
86 /*
87 * Initially start polling quickly. Then drop down to configured value
88 */
89 log.info("Starting Stats collection thread for {}",
90 this.sw.getStringId());
91 timeout = timer.newTimeout(this, 1, TimeUnit.SECONDS);
92 }
93
94 public void stop() {
95 log.info("Stopping Stats collection thread for {}",
96 this.sw.getStringId());
97 this.stopTimer = true;
98 timeout.cancel();
99 }
100
101}