blob: cb6f262346cf0a970b1a94c45e44d1388bf71ff8 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
alshabibeec3a062014-09-17 18:01:26 -070019package org.onlab.onos.provider.of.flow.impl;
20
21import static org.slf4j.LoggerFactory.getLogger;
22
23import java.util.concurrent.TimeUnit;
24
25import org.jboss.netty.util.HashedWheelTimer;
26import org.jboss.netty.util.Timeout;
27import org.jboss.netty.util.TimerTask;
28import org.onlab.onos.openflow.controller.OpenFlowSwitch;
Yuta HIGUCHI1b6f5682014-10-25 21:33:59 -070029import org.onlab.onos.openflow.controller.RoleState;
alshabibeec3a062014-09-17 18:01:26 -070030import org.onlab.util.Timer;
31import org.projectfloodlight.openflow.protocol.OFFlowStatsRequest;
32import org.projectfloodlight.openflow.types.OFPort;
33import org.projectfloodlight.openflow.types.TableId;
34import org.slf4j.Logger;
35
36public class FlowStatsCollector implements TimerTask {
37
38 private final Logger log = getLogger(getClass());
39
40 private final HashedWheelTimer timer = Timer.getTimer();
41 private final OpenFlowSwitch sw;
42 private final int refreshInterval;
43
44 private Timeout timeout;
45
46 private boolean stopTimer = false;;
47
48 public FlowStatsCollector(OpenFlowSwitch sw, int refreshInterval) {
49 this.sw = sw;
50 this.refreshInterval = refreshInterval;
51 }
52
53 @Override
54 public void run(Timeout timeout) throws Exception {
55 log.debug("Collecting stats for {}", this.sw.getStringId());
56
57 sendFlowStatistics();
58
59 if (!this.stopTimer) {
60 log.debug("Scheduling stats collection in {} seconds for {}",
61 this.refreshInterval, this.sw.getStringId());
62 timeout.getTimer().newTimeout(this, refreshInterval,
63 TimeUnit.SECONDS);
64 }
65
66
67 }
68
69 private void sendFlowStatistics() {
Yuta HIGUCHI1b6f5682014-10-25 21:33:59 -070070 if (log.isTraceEnabled()) {
71 log.trace("sendFlowStatistics {}:{}", sw.getStringId(), sw.getRole());
72 }
73 if (sw.getRole() != RoleState.MASTER) {
74 // Switch not master.
75 return;
76 }
alshabibeec3a062014-09-17 18:01:26 -070077 OFFlowStatsRequest request = sw.factory().buildFlowStatsRequest()
78 .setMatch(sw.factory().matchWildcardAll())
79 .setTableId(TableId.ALL)
80 .setOutPort(OFPort.NO_MASK)
81 .build();
82
83 this.sw.sendMsg(request);
84
85 }
86
87 public void start() {
88
89 /*
90 * Initially start polling quickly. Then drop down to configured value
91 */
92 log.info("Starting Stats collection thread for {}",
93 this.sw.getStringId());
94 timeout = timer.newTimeout(this, 1, TimeUnit.SECONDS);
95 }
96
97 public void stop() {
98 log.info("Stopping Stats collection thread for {}",
99 this.sw.getStringId());
100 this.stopTimer = true;
101 timeout.cancel();
102 }
103
104}