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