Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame^] | 1 | /* |
| 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 | */ |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 19 | package org.onlab.onos.provider.of.flow.impl; |
| 20 | |
| 21 | import static org.slf4j.LoggerFactory.getLogger; |
| 22 | |
| 23 | import java.util.concurrent.TimeUnit; |
| 24 | |
| 25 | import org.jboss.netty.util.HashedWheelTimer; |
| 26 | import org.jboss.netty.util.Timeout; |
| 27 | import org.jboss.netty.util.TimerTask; |
| 28 | import org.onlab.onos.openflow.controller.OpenFlowSwitch; |
Yuta HIGUCHI | 1b6f568 | 2014-10-25 21:33:59 -0700 | [diff] [blame] | 29 | import org.onlab.onos.openflow.controller.RoleState; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 30 | import org.onlab.util.Timer; |
| 31 | import org.projectfloodlight.openflow.protocol.OFFlowStatsRequest; |
| 32 | import org.projectfloodlight.openflow.types.OFPort; |
| 33 | import org.projectfloodlight.openflow.types.TableId; |
| 34 | import org.slf4j.Logger; |
| 35 | |
| 36 | public 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 HIGUCHI | 1b6f568 | 2014-10-25 21:33:59 -0700 | [diff] [blame] | 70 | 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 | } |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 77 | 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 | } |