sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.provider.of.group.impl; |
| 18 | |
| 19 | import org.jboss.netty.util.HashedWheelTimer; |
| 20 | import org.jboss.netty.util.Timeout; |
| 21 | import org.jboss.netty.util.TimerTask; |
| 22 | import org.onlab.util.Timer; |
| 23 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 24 | import org.onosproject.openflow.controller.RoleState; |
| 25 | import org.projectfloodlight.openflow.protocol.OFGroupDescStatsRequest; |
| 26 | import org.projectfloodlight.openflow.protocol.OFGroupStatsRequest; |
| 27 | import org.projectfloodlight.openflow.types.OFGroup; |
| 28 | import org.slf4j.Logger; |
| 29 | |
| 30 | import java.util.concurrent.TimeUnit; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 31 | |
| 32 | import static org.slf4j.LoggerFactory.getLogger; |
| 33 | |
| 34 | /* |
| 35 | * Sends Group Stats Request and collect the group statistics with a time interval. |
| 36 | */ |
| 37 | public class GroupStatsCollector implements TimerTask { |
| 38 | |
| 39 | private final HashedWheelTimer timer = Timer.getTimer(); |
| 40 | private final OpenFlowSwitch sw; |
| 41 | private final Logger log = getLogger(getClass()); |
| 42 | private final int refreshInterval; |
| 43 | |
| 44 | private Timeout timeout; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 45 | |
| 46 | private boolean stopTimer = false; |
| 47 | |
| 48 | /** |
| 49 | * Creates a GroupStatsCollector object. |
| 50 | * |
| 51 | * @param sw Open Flow switch |
| 52 | * @param interval time interval for collecting group statistic |
| 53 | */ |
| 54 | public GroupStatsCollector(OpenFlowSwitch sw, int interval) { |
| 55 | this.sw = sw; |
| 56 | this.refreshInterval = interval; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public void run(Timeout timeout) throws Exception { |
| 61 | log.trace("Collecting stats for {}", sw.getStringId()); |
| 62 | |
| 63 | sendGroupStatistic(); |
| 64 | |
| 65 | if (!this.stopTimer) { |
| 66 | log.trace("Scheduling stats collection in {} seconds for {}", |
| 67 | this.refreshInterval, this.sw.getStringId()); |
| 68 | timeout.getTimer().newTimeout(this, refreshInterval, |
| 69 | TimeUnit.SECONDS); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | private void sendGroupStatistic() { |
| 74 | if (log.isTraceEnabled()) { |
| 75 | log.trace("sendGroupStatistics {}:{}", sw.getStringId(), sw.getRole()); |
| 76 | } |
| 77 | if (sw.getRole() != RoleState.MASTER) { |
| 78 | return; |
| 79 | } |
Yuta HIGUCHI | 07752ec | 2016-06-29 22:49:20 -0700 | [diff] [blame] | 80 | if (!sw.isConnected()) { |
| 81 | return; |
| 82 | } |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 83 | Long statsXid = OpenFlowGroupProvider.getXidAndAdd(2); |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 84 | OFGroupStatsRequest statsRequest = sw.factory().buildGroupStatsRequest() |
| 85 | .setGroup(OFGroup.ALL) |
| 86 | .setXid(statsXid) |
| 87 | .build(); |
| 88 | sw.sendMsg(statsRequest); |
| 89 | |
| 90 | Long descXid = statsXid + 1; |
| 91 | OFGroupDescStatsRequest descStatsRequest = |
| 92 | sw.factory().buildGroupDescStatsRequest() |
| 93 | .setXid(descXid) |
| 94 | .build(); |
| 95 | sw.sendMsg(descStatsRequest); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Starts the collector. |
| 100 | */ |
| 101 | public void start() { |
Jonathan Hart | e106e4b | 2015-03-09 16:44:56 -0700 | [diff] [blame] | 102 | log.info("Starting Group Stats collection thread for {}", sw.getStringId()); |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 103 | timeout = timer.newTimeout(this, 1, TimeUnit.SECONDS); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Stops the collector. |
| 108 | */ |
| 109 | public void stop() { |
| 110 | log.info("Stopping Group Stats collection thread for {}", sw.getStringId()); |
| 111 | this.stopTimer = true; |
| 112 | timeout.cancel(); |
| 113 | } |
| 114 | } |