Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -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.net.statistic.impl; |
| 18 | |
| 19 | import com.google.common.base.MoreObjects; |
| 20 | import com.google.common.base.Predicate; |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.apache.felix.scr.annotations.Service; |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 28 | import org.onosproject.utils.Comparators; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 29 | import org.onosproject.net.ConnectPoint; |
| 30 | import org.onosproject.net.Device; |
| 31 | import org.onosproject.net.Port; |
| 32 | import org.onosproject.net.PortNumber; |
| 33 | import org.onosproject.net.device.DeviceService; |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 34 | import org.onosproject.net.flow.DefaultFlowEntry; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 35 | import org.onosproject.net.flow.DefaultTypedFlowEntry; |
| 36 | import org.onosproject.net.flow.FlowEntry; |
| 37 | import org.onosproject.net.flow.FlowRule; |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 38 | import org.onosproject.net.flow.StoredFlowEntry; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 39 | import org.onosproject.net.flow.TypedStoredFlowEntry; |
| 40 | import org.onosproject.net.flow.instructions.Instruction; |
| 41 | import org.onosproject.net.statistic.DefaultLoad; |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 42 | import org.onosproject.net.statistic.FlowEntryWithLoad; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 43 | import org.onosproject.net.statistic.FlowStatisticService; |
| 44 | import org.onosproject.net.statistic.Load; |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 45 | import org.onosproject.net.statistic.PollInterval; |
| 46 | import org.onosproject.net.statistic.StatisticStore; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 47 | import org.onosproject.net.statistic.SummaryFlowEntryWithLoad; |
| 48 | import org.onosproject.net.statistic.TypedFlowEntryWithLoad; |
| 49 | |
| 50 | import org.slf4j.Logger; |
| 51 | |
| 52 | import java.util.ArrayList; |
| 53 | import java.util.HashMap; |
| 54 | import java.util.List; |
| 55 | import java.util.Map; |
| 56 | import java.util.Objects; |
| 57 | import java.util.Set; |
| 58 | import java.util.TreeMap; |
| 59 | import java.util.stream.Collectors; |
| 60 | |
| 61 | import static com.google.common.base.Preconditions.checkNotNull; |
| 62 | import static org.onosproject.security.AppGuard.checkPermission; |
| 63 | import static org.slf4j.LoggerFactory.getLogger; |
| 64 | import static org.onosproject.security.AppPermission.Type.*; |
| 65 | |
| 66 | /** |
| 67 | * Provides an implementation of the Flow Statistic Service. |
| 68 | */ |
Sho SHIMIZU | 5c396e3 | 2016-08-12 15:19:12 -0700 | [diff] [blame] | 69 | @Component(immediate = true) |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 70 | @Service |
| 71 | public class FlowStatisticManager implements FlowStatisticService { |
| 72 | private final Logger log = getLogger(getClass()); |
| 73 | |
| 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 75 | protected StatisticStore statisticStore; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 76 | |
| 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 78 | protected DeviceService deviceService; |
| 79 | |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 80 | @Activate |
| 81 | public void activate() { |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 82 | log.info("Started"); |
| 83 | } |
| 84 | |
| 85 | @Deactivate |
| 86 | public void deactivate() { |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 87 | log.info("Stopped"); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public Map<ConnectPoint, SummaryFlowEntryWithLoad> loadSummary(Device device) { |
| 92 | checkPermission(STATISTIC_READ); |
| 93 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 94 | Map<ConnectPoint, SummaryFlowEntryWithLoad> summaryLoad = |
| 95 | new TreeMap<>(Comparators.CONNECT_POINT_COMPARATOR); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 96 | |
| 97 | if (device == null) { |
| 98 | return summaryLoad; |
| 99 | } |
| 100 | |
| 101 | List<Port> ports = new ArrayList<>(deviceService.getPorts(device.id())); |
| 102 | |
| 103 | for (Port port : ports) { |
| 104 | ConnectPoint cp = new ConnectPoint(device.id(), port.number()); |
| 105 | SummaryFlowEntryWithLoad sfe = loadSummaryPortInternal(cp); |
| 106 | summaryLoad.put(cp, sfe); |
| 107 | } |
| 108 | |
| 109 | return summaryLoad; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public SummaryFlowEntryWithLoad loadSummary(Device device, PortNumber pNumber) { |
| 114 | checkPermission(STATISTIC_READ); |
| 115 | |
| 116 | ConnectPoint cp = new ConnectPoint(device.id(), pNumber); |
| 117 | return loadSummaryPortInternal(cp); |
| 118 | } |
| 119 | |
| 120 | @Override |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 121 | public Map<ConnectPoint, List<FlowEntryWithLoad>> loadAllByType(Device device, |
| 122 | FlowEntry.FlowLiveType liveType, |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 123 | Instruction.Type instType) { |
| 124 | checkPermission(STATISTIC_READ); |
| 125 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 126 | Map<ConnectPoint, List<FlowEntryWithLoad>> allLoad = |
| 127 | new TreeMap<>(Comparators.CONNECT_POINT_COMPARATOR); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 128 | |
| 129 | if (device == null) { |
| 130 | return allLoad; |
| 131 | } |
| 132 | |
| 133 | List<Port> ports = new ArrayList<>(deviceService.getPorts(device.id())); |
| 134 | |
| 135 | for (Port port : ports) { |
| 136 | ConnectPoint cp = new ConnectPoint(device.id(), port.number()); |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 137 | List<FlowEntryWithLoad> fel = loadAllPortInternal(cp, liveType, instType); |
| 138 | allLoad.put(cp, fel); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | return allLoad; |
| 142 | } |
| 143 | |
| 144 | @Override |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 145 | public List<FlowEntryWithLoad> loadAllByType(Device device, PortNumber pNumber, |
| 146 | FlowEntry.FlowLiveType liveType, |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 147 | Instruction.Type instType) { |
| 148 | checkPermission(STATISTIC_READ); |
| 149 | |
| 150 | ConnectPoint cp = new ConnectPoint(device.id(), pNumber); |
| 151 | return loadAllPortInternal(cp, liveType, instType); |
| 152 | } |
| 153 | |
| 154 | @Override |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 155 | public Map<ConnectPoint, List<FlowEntryWithLoad>> loadTopnByType(Device device, |
| 156 | FlowEntry.FlowLiveType liveType, |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 157 | Instruction.Type instType, |
| 158 | int topn) { |
| 159 | checkPermission(STATISTIC_READ); |
| 160 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 161 | Map<ConnectPoint, List<FlowEntryWithLoad>> allLoad = |
| 162 | new TreeMap<>(Comparators.CONNECT_POINT_COMPARATOR); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 163 | |
| 164 | if (device == null) { |
| 165 | return allLoad; |
| 166 | } |
| 167 | |
| 168 | List<Port> ports = new ArrayList<>(deviceService.getPorts(device.id())); |
| 169 | |
| 170 | for (Port port : ports) { |
| 171 | ConnectPoint cp = new ConnectPoint(device.id(), port.number()); |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 172 | List<FlowEntryWithLoad> fel = loadTopnPortInternal(cp, liveType, instType, topn); |
| 173 | allLoad.put(cp, fel); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | return allLoad; |
| 177 | } |
| 178 | |
| 179 | @Override |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 180 | public List<FlowEntryWithLoad> loadTopnByType(Device device, PortNumber pNumber, |
| 181 | FlowEntry.FlowLiveType liveType, |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 182 | Instruction.Type instType, |
| 183 | int topn) { |
| 184 | checkPermission(STATISTIC_READ); |
| 185 | |
| 186 | ConnectPoint cp = new ConnectPoint(device.id(), pNumber); |
| 187 | return loadTopnPortInternal(cp, liveType, instType, topn); |
| 188 | } |
| 189 | |
| 190 | private SummaryFlowEntryWithLoad loadSummaryPortInternal(ConnectPoint cp) { |
| 191 | checkPermission(STATISTIC_READ); |
| 192 | |
| 193 | Set<FlowEntry> currentStats; |
| 194 | Set<FlowEntry> previousStats; |
| 195 | |
| 196 | TypedStatistics typedStatistics; |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 197 | synchronized (statisticStore) { |
| 198 | currentStats = statisticStore.getCurrentStatistic(cp); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 199 | if (currentStats == null) { |
| 200 | return new SummaryFlowEntryWithLoad(cp, new DefaultLoad()); |
| 201 | } |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 202 | previousStats = statisticStore.getPreviousStatistic(cp); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 203 | if (previousStats == null) { |
| 204 | return new SummaryFlowEntryWithLoad(cp, new DefaultLoad()); |
| 205 | } |
| 206 | // copy to local flow entry |
| 207 | typedStatistics = new TypedStatistics(currentStats, previousStats); |
| 208 | |
| 209 | // Check for validity of this stats data |
| 210 | checkLoadValidity(currentStats, previousStats); |
| 211 | } |
| 212 | |
| 213 | // current and previous set is not empty! |
| 214 | Set<FlowEntry> currentSet = typedStatistics.current(); |
| 215 | Set<FlowEntry> previousSet = typedStatistics.previous(); |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 216 | PollInterval pollIntervalInstance = PollInterval.getInstance(); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 217 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 218 | // We assume that default pollInterval is flowPollFrequency in case adaptiveFlowSampling is true or false |
| 219 | Load totalLoad = new DefaultLoad(aggregateBytesSet(currentSet), aggregateBytesSet(previousSet), |
| 220 | pollIntervalInstance.getPollInterval()); |
| 221 | |
| 222 | Map<FlowRule, FlowEntry> currentMap; |
| 223 | Map<FlowRule, FlowEntry> previousMap; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 224 | |
| 225 | currentMap = typedStatistics.currentImmediate(); |
| 226 | previousMap = typedStatistics.previousImmediate(); |
| 227 | Load immediateLoad = new DefaultLoad(aggregateBytesMap(currentMap), aggregateBytesMap(previousMap), |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 228 | pollIntervalInstance.getPollInterval()); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 229 | |
| 230 | currentMap = typedStatistics.currentShort(); |
| 231 | previousMap = typedStatistics.previousShort(); |
| 232 | Load shortLoad = new DefaultLoad(aggregateBytesMap(currentMap), aggregateBytesMap(previousMap), |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 233 | pollIntervalInstance.getPollInterval()); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 234 | |
| 235 | currentMap = typedStatistics.currentMid(); |
| 236 | previousMap = typedStatistics.previousMid(); |
| 237 | Load midLoad = new DefaultLoad(aggregateBytesMap(currentMap), aggregateBytesMap(previousMap), |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 238 | pollIntervalInstance.getMidPollInterval()); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 239 | |
| 240 | currentMap = typedStatistics.currentLong(); |
| 241 | previousMap = typedStatistics.previousLong(); |
| 242 | Load longLoad = new DefaultLoad(aggregateBytesMap(currentMap), aggregateBytesMap(previousMap), |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 243 | pollIntervalInstance.getLongPollInterval()); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 244 | |
| 245 | currentMap = typedStatistics.currentUnknown(); |
| 246 | previousMap = typedStatistics.previousUnknown(); |
| 247 | Load unknownLoad = new DefaultLoad(aggregateBytesMap(currentMap), aggregateBytesMap(previousMap), |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 248 | pollIntervalInstance.getPollInterval()); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 249 | |
| 250 | return new SummaryFlowEntryWithLoad(cp, totalLoad, immediateLoad, shortLoad, midLoad, longLoad, unknownLoad); |
| 251 | } |
| 252 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 253 | private List<FlowEntryWithLoad> loadAllPortInternal(ConnectPoint cp, |
| 254 | FlowEntry.FlowLiveType liveType, |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 255 | Instruction.Type instType) { |
| 256 | checkPermission(STATISTIC_READ); |
| 257 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 258 | List<FlowEntryWithLoad> retFel = new ArrayList<>(); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 259 | |
| 260 | Set<FlowEntry> currentStats; |
| 261 | Set<FlowEntry> previousStats; |
| 262 | |
| 263 | TypedStatistics typedStatistics; |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 264 | synchronized (statisticStore) { |
| 265 | currentStats = statisticStore.getCurrentStatistic(cp); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 266 | if (currentStats == null) { |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 267 | return retFel; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 268 | } |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 269 | previousStats = statisticStore.getPreviousStatistic(cp); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 270 | if (previousStats == null) { |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 271 | return retFel; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 272 | } |
| 273 | // copy to local flow entry set |
| 274 | typedStatistics = new TypedStatistics(currentStats, previousStats); |
| 275 | |
| 276 | // Check for validity of this stats data |
| 277 | checkLoadValidity(currentStats, previousStats); |
| 278 | } |
| 279 | |
| 280 | // current and previous set is not empty! |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 281 | boolean isAllInstType = (instType == null ? true : false); // null is all inst type |
| 282 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 283 | Map<FlowRule, FlowEntry> currentMap; |
| 284 | Map<FlowRule, FlowEntry> previousMap; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 285 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 286 | if (isAllInstType) { |
| 287 | currentMap = typedStatistics.currentAll(); |
| 288 | previousMap = typedStatistics.previousAll(); |
| 289 | } else { |
| 290 | switch (liveType) { |
| 291 | case IMMEDIATE: |
| 292 | currentMap = typedStatistics.currentImmediate(); |
| 293 | previousMap = typedStatistics.previousImmediate(); |
| 294 | break; |
| 295 | case SHORT: |
| 296 | currentMap = typedStatistics.currentShort(); |
| 297 | previousMap = typedStatistics.previousShort(); |
| 298 | break; |
| 299 | case MID: |
| 300 | currentMap = typedStatistics.currentMid(); |
| 301 | previousMap = typedStatistics.previousMid(); |
| 302 | break; |
| 303 | case LONG: |
| 304 | currentMap = typedStatistics.currentLong(); |
| 305 | previousMap = typedStatistics.previousLong(); |
| 306 | break; |
| 307 | case UNKNOWN: |
| 308 | currentMap = typedStatistics.currentUnknown(); |
| 309 | previousMap = typedStatistics.previousUnknown(); |
| 310 | break; |
| 311 | default: |
| 312 | currentMap = new HashMap<>(); |
| 313 | previousMap = new HashMap<>(); |
| 314 | break; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 318 | return typedFlowEntryLoadByInstInternal(cp, currentMap, previousMap, isAllInstType, instType); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 321 | private List<FlowEntryWithLoad> typedFlowEntryLoadByInstInternal(ConnectPoint cp, |
| 322 | Map<FlowRule, FlowEntry> currentMap, |
| 323 | Map<FlowRule, FlowEntry> previousMap, |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 324 | boolean isAllInstType, |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 325 | Instruction.Type instType) { |
| 326 | List<FlowEntryWithLoad> fel = new ArrayList<>(); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 327 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 328 | currentMap.values().forEach(fe -> { |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 329 | if (isAllInstType || |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 330 | fe.treatment().allInstructions().stream(). |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 331 | filter(i -> i.type() == instType). |
| 332 | findAny().isPresent()) { |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 333 | long currentBytes = fe.bytes(); |
| 334 | long previousBytes = previousMap.getOrDefault(fe, new DefaultFlowEntry(fe)).bytes(); |
| 335 | long liveTypePollInterval = getLiveTypePollInterval(fe.liveType()); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 336 | Load fLoad = new DefaultLoad(currentBytes, previousBytes, liveTypePollInterval); |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 337 | fel.add(new FlowEntryWithLoad(cp, fe, fLoad)); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 338 | } |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 339 | }); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 340 | |
| 341 | return fel; |
| 342 | } |
| 343 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 344 | private List<FlowEntryWithLoad> loadTopnPortInternal(ConnectPoint cp, |
| 345 | FlowEntry.FlowLiveType liveType, |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 346 | Instruction.Type instType, |
| 347 | int topn) { |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 348 | List<FlowEntryWithLoad> fel = loadAllPortInternal(cp, liveType, instType); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 349 | |
| 350 | // Sort with descending order of load |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 351 | List<FlowEntryWithLoad> retFel = |
| 352 | fel.stream().sorted(Comparators.FLOWENTRY_WITHLOAD_COMPARATOR). |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 353 | limit(topn).collect(Collectors.toList()); |
| 354 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 355 | return retFel; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | private long aggregateBytesSet(Set<FlowEntry> setFE) { |
| 359 | return setFE.stream().mapToLong(FlowEntry::bytes).sum(); |
| 360 | } |
| 361 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 362 | private long aggregateBytesMap(Map<FlowRule, FlowEntry> mapFE) { |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 363 | return mapFE.values().stream().mapToLong(FlowEntry::bytes).sum(); |
| 364 | } |
| 365 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 366 | private long getLiveTypePollInterval(FlowEntry.FlowLiveType liveType) { |
| 367 | // returns the flow live type poll interval value |
| 368 | PollInterval pollIntervalInstance = PollInterval.getInstance(); |
| 369 | |
| 370 | switch (liveType) { |
| 371 | case LONG: |
| 372 | return pollIntervalInstance.getLongPollInterval(); |
| 373 | case MID: |
| 374 | return pollIntervalInstance.getMidPollInterval(); |
| 375 | case SHORT: |
| 376 | case IMMEDIATE: |
| 377 | default: // UNKNOWN |
| 378 | return pollIntervalInstance.getPollInterval(); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | // |
| 383 | // Deprecated interfaces... |
| 384 | // |
| 385 | @Override |
| 386 | public Map<ConnectPoint, List<TypedFlowEntryWithLoad>> loadAllByType(Device device, |
| 387 | TypedStoredFlowEntry.FlowLiveType liveType, |
| 388 | Instruction.Type instType) { |
| 389 | FlowEntry.FlowLiveType type = toFlowEntryLiveType(liveType); |
| 390 | |
| 391 | Map<ConnectPoint, List<FlowEntryWithLoad>> loadMap = loadAllByType(device, type, instType); |
| 392 | |
| 393 | return toFlowEntryWithLoadMap(loadMap); |
| 394 | } |
| 395 | |
| 396 | @Override |
| 397 | public List<TypedFlowEntryWithLoad> loadAllByType(Device device, PortNumber pNumber, |
| 398 | TypedStoredFlowEntry.FlowLiveType liveType, |
| 399 | Instruction.Type instType) { |
| 400 | FlowEntry.FlowLiveType type = toFlowEntryLiveType(liveType); |
| 401 | |
| 402 | List<FlowEntryWithLoad> loadList = loadAllByType(device, pNumber, type, instType); |
| 403 | |
| 404 | return toFlowEntryWithLoad(loadList); |
| 405 | } |
| 406 | |
| 407 | @Override |
| 408 | public Map<ConnectPoint, List<TypedFlowEntryWithLoad>> loadTopnByType(Device device, |
| 409 | TypedStoredFlowEntry.FlowLiveType liveType, |
| 410 | Instruction.Type instType, |
| 411 | int topn) { |
| 412 | FlowEntry.FlowLiveType type = toFlowEntryLiveType(liveType); |
| 413 | |
| 414 | Map<ConnectPoint, List<FlowEntryWithLoad>> loadMap = loadTopnByType(device, type, instType, topn); |
| 415 | |
| 416 | return toFlowEntryWithLoadMap(loadMap); |
| 417 | } |
| 418 | |
| 419 | @Override |
| 420 | public List<TypedFlowEntryWithLoad> loadTopnByType(Device device, PortNumber pNumber, |
| 421 | TypedStoredFlowEntry.FlowLiveType liveType, |
| 422 | Instruction.Type instType, |
| 423 | int topn) { |
| 424 | FlowEntry.FlowLiveType type = toFlowEntryLiveType(liveType); |
| 425 | |
| 426 | List<FlowEntryWithLoad> loadList = loadTopnByType(device, pNumber, type, instType, topn); |
| 427 | |
| 428 | return toFlowEntryWithLoad(loadList); |
| 429 | } |
| 430 | |
| 431 | private FlowEntry.FlowLiveType toFlowEntryLiveType(TypedStoredFlowEntry.FlowLiveType liveType) { |
| 432 | if (liveType == null) { |
| 433 | return null; |
| 434 | } |
| 435 | |
| 436 | // convert TypedStoredFlowEntry flow live type to FlowEntry one |
| 437 | switch (liveType) { |
| 438 | case IMMEDIATE_FLOW: |
| 439 | return FlowEntry.FlowLiveType.IMMEDIATE; |
| 440 | case SHORT_FLOW: |
| 441 | return FlowEntry.FlowLiveType.SHORT; |
| 442 | case MID_FLOW: |
| 443 | return FlowEntry.FlowLiveType.MID; |
| 444 | case LONG_FLOW: |
| 445 | return FlowEntry.FlowLiveType.LONG; |
| 446 | default: |
| 447 | return FlowEntry.FlowLiveType.UNKNOWN; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | private TypedStoredFlowEntry.FlowLiveType toTypedStoredFlowEntryLiveType(FlowEntry.FlowLiveType liveType) { |
| 452 | if (liveType == null) { |
| 453 | return null; |
| 454 | } |
| 455 | |
| 456 | // convert TypedStoredFlowEntry flow live type to FlowEntry one |
| 457 | switch (liveType) { |
| 458 | case IMMEDIATE: |
| 459 | return TypedStoredFlowEntry.FlowLiveType.IMMEDIATE_FLOW; |
| 460 | case SHORT: |
| 461 | return TypedStoredFlowEntry.FlowLiveType.SHORT_FLOW; |
| 462 | case MID: |
| 463 | return TypedStoredFlowEntry.FlowLiveType.MID_FLOW; |
| 464 | case LONG: |
| 465 | return TypedStoredFlowEntry.FlowLiveType.LONG_FLOW; |
| 466 | default: |
| 467 | return TypedStoredFlowEntry.FlowLiveType.UNKNOWN_FLOW; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | private Map<ConnectPoint, List<TypedFlowEntryWithLoad>> toFlowEntryWithLoadMap( |
| 472 | Map<ConnectPoint, List<FlowEntryWithLoad>> loadMap) { |
| 473 | // convert FlowEntryWithLoad list to TypedFlowEntryWithLoad list |
| 474 | Map<ConnectPoint, List<TypedFlowEntryWithLoad>> allLoad = |
| 475 | new TreeMap<>(Comparators.CONNECT_POINT_COMPARATOR); |
| 476 | |
| 477 | loadMap.forEach((k, v) -> { |
| 478 | List<TypedFlowEntryWithLoad> tfelList = |
| 479 | toFlowEntryWithLoad(v); |
| 480 | allLoad.put(k, tfelList); |
| 481 | }); |
| 482 | |
| 483 | return allLoad; |
| 484 | } |
| 485 | |
| 486 | private List<TypedFlowEntryWithLoad> toFlowEntryWithLoad(List<FlowEntryWithLoad> loadList) { |
| 487 | // convert FlowEntryWithLoad list to TypedFlowEntryWithLoad list |
| 488 | List<TypedFlowEntryWithLoad> tfelList = new ArrayList<>(); |
| 489 | loadList.forEach(fel -> { |
| 490 | StoredFlowEntry sfe = fel.storedFlowEntry(); |
| 491 | TypedStoredFlowEntry.FlowLiveType liveType = toTypedStoredFlowEntryLiveType(sfe.liveType()); |
| 492 | TypedStoredFlowEntry tfe = new DefaultTypedFlowEntry(sfe, liveType); |
| 493 | TypedFlowEntryWithLoad tfel = new TypedFlowEntryWithLoad(fel.connectPoint(), tfe, fel.load()); |
| 494 | tfelList.add(tfel); |
| 495 | }); |
| 496 | |
| 497 | return tfelList; |
| 498 | } |
| 499 | |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 500 | /** |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 501 | * Internal data class holding two set of flow entries included flow liveType. |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 502 | */ |
| 503 | private static class TypedStatistics { |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 504 | private final ImmutableSet<FlowEntry> current; |
| 505 | private final ImmutableSet<FlowEntry> previous; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 506 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 507 | private final Map<FlowRule, FlowEntry> currentAll = new HashMap<>(); |
| 508 | private final Map<FlowRule, FlowEntry> previousAll = new HashMap<>(); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 509 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 510 | private final Map<FlowRule, FlowEntry> currentImmediate = new HashMap<>(); |
| 511 | private final Map<FlowRule, FlowEntry> previousImmediate = new HashMap<>(); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 512 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 513 | private final Map<FlowRule, FlowEntry> currentShort = new HashMap<>(); |
| 514 | private final Map<FlowRule, FlowEntry> previousShort = new HashMap<>(); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 515 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 516 | private final Map<FlowRule, FlowEntry> currentMid = new HashMap<>(); |
| 517 | private final Map<FlowRule, FlowEntry> previousMid = new HashMap<>(); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 518 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 519 | private final Map<FlowRule, FlowEntry> currentLong = new HashMap<>(); |
| 520 | private final Map<FlowRule, FlowEntry> previousLong = new HashMap<>(); |
| 521 | |
| 522 | private final Map<FlowRule, FlowEntry> currentUnknown = new HashMap<>(); |
| 523 | private final Map<FlowRule, FlowEntry> previousUnknown = new HashMap<>(); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 524 | |
| 525 | public TypedStatistics(Set<FlowEntry> current, Set<FlowEntry> previous) { |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 526 | this.current = ImmutableSet.copyOf(checkNotNull(current)); |
| 527 | this.previous = ImmutableSet.copyOf(checkNotNull(previous)); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 528 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 529 | current.forEach(fe -> { |
| 530 | switch (fe.liveType()) { |
| 531 | case IMMEDIATE: |
| 532 | currentImmediate.put(fe, fe); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 533 | break; |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 534 | case SHORT: |
| 535 | currentShort.put(fe, fe); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 536 | break; |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 537 | case MID: |
| 538 | currentMid.put(fe, fe); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 539 | break; |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 540 | case LONG: |
| 541 | currentLong.put(fe, fe); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 542 | break; |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 543 | default: // unknown |
| 544 | currentUnknown.put(fe, fe); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 545 | break; |
| 546 | } |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 547 | currentAll.put(fe, fe); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 548 | }); |
| 549 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 550 | previous.forEach(fe -> { |
| 551 | switch (fe.liveType()) { |
| 552 | case IMMEDIATE: |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 553 | if (currentImmediate.containsKey(fe)) { |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 554 | previousImmediate.put(fe, fe); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 555 | } else if (currentShort.containsKey(fe)) { |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 556 | previousShort.put(fe, fe); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 557 | } else if (currentMid.containsKey(fe)) { |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 558 | previousMid.put(fe, fe); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 559 | } else if (currentLong.containsKey(fe)) { |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 560 | previousLong.put(fe, fe); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 561 | } else { |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 562 | previousUnknown.put(fe, fe); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 563 | } |
| 564 | break; |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 565 | case SHORT: |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 566 | if (currentShort.containsKey(fe)) { |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 567 | previousShort.put(fe, fe); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 568 | } else if (currentMid.containsKey(fe)) { |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 569 | previousMid.put(fe, fe); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 570 | } else if (currentLong.containsKey(fe)) { |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 571 | previousLong.put(fe, fe); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 572 | } else { |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 573 | previousUnknown.put(fe, fe); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 574 | } |
| 575 | break; |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 576 | case MID: |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 577 | if (currentMid.containsKey(fe)) { |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 578 | previousMid.put(fe, fe); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 579 | } else if (currentLong.containsKey(fe)) { |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 580 | previousLong.put(fe, fe); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 581 | } else { |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 582 | previousUnknown.put(fe, fe); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 583 | } |
| 584 | break; |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 585 | case LONG: |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 586 | if (currentLong.containsKey(fe)) { |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 587 | previousLong.put(fe, fe); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 588 | } else { |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 589 | previousUnknown.put(fe, fe); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 590 | } |
| 591 | break; |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 592 | default: // unknown |
| 593 | previousUnknown.put(fe, fe); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 594 | break; |
| 595 | } |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 596 | previousAll.put(fe, fe); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 597 | }); |
| 598 | } |
| 599 | |
| 600 | /** |
| 601 | * Returns flow entries as the current value. |
| 602 | * |
| 603 | * @return flow entries as the current value |
| 604 | */ |
| 605 | public ImmutableSet<FlowEntry> current() { |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 606 | return current; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | /** |
| 610 | * Returns flow entries as the previous value. |
| 611 | * |
| 612 | * @return flow entries as the previous value |
| 613 | */ |
| 614 | public ImmutableSet<FlowEntry> previous() { |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 615 | return previous; |
| 616 | } |
| 617 | |
| 618 | public Map<FlowRule, FlowEntry> currentAll() { |
| 619 | return currentAll; |
| 620 | } |
| 621 | |
| 622 | public Map<FlowRule, FlowEntry> previousAll() { |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 623 | return previousAll; |
| 624 | } |
| 625 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 626 | public Map<FlowRule, FlowEntry> currentImmediate() { |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 627 | return currentImmediate; |
| 628 | } |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 629 | public Map<FlowRule, FlowEntry> previousImmediate() { |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 630 | return previousImmediate; |
| 631 | } |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 632 | public Map<FlowRule, FlowEntry> currentShort() { |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 633 | return currentShort; |
| 634 | } |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 635 | public Map<FlowRule, FlowEntry> previousShort() { |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 636 | return previousShort; |
| 637 | } |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 638 | public Map<FlowRule, FlowEntry> currentMid() { |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 639 | return currentMid; |
| 640 | } |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 641 | public Map<FlowRule, FlowEntry> previousMid() { |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 642 | return previousMid; |
| 643 | } |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 644 | public Map<FlowRule, FlowEntry> currentLong() { |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 645 | return currentLong; |
| 646 | } |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 647 | public Map<FlowRule, FlowEntry> previousLong() { |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 648 | return previousLong; |
| 649 | } |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 650 | public Map<FlowRule, FlowEntry> currentUnknown() { |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 651 | return currentUnknown; |
| 652 | } |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 653 | public Map<FlowRule, FlowEntry> previousUnknown() { |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 654 | return previousUnknown; |
| 655 | } |
| 656 | |
| 657 | /** |
| 658 | * Validates values are not empty. |
| 659 | * |
| 660 | * @return false if either of the sets is empty. Otherwise, true. |
| 661 | */ |
| 662 | public boolean isValid() { |
| 663 | return !(currentAll.isEmpty() || previousAll.isEmpty()); |
| 664 | } |
| 665 | |
| 666 | @Override |
| 667 | public int hashCode() { |
| 668 | return Objects.hash(currentAll, previousAll); |
| 669 | } |
| 670 | |
| 671 | @Override |
| 672 | public boolean equals(Object obj) { |
| 673 | if (this == obj) { |
| 674 | return true; |
| 675 | } |
| 676 | if (!(obj instanceof TypedStatistics)) { |
| 677 | return false; |
| 678 | } |
| 679 | final TypedStatistics other = (TypedStatistics) obj; |
| 680 | return Objects.equals(this.currentAll, other.currentAll) && |
| 681 | Objects.equals(this.previousAll, other.previousAll); |
| 682 | } |
| 683 | |
| 684 | @Override |
| 685 | public String toString() { |
| 686 | return MoreObjects.toStringHelper(this) |
| 687 | .add("current", currentAll) |
| 688 | .add("previous", previousAll) |
| 689 | .toString(); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | private void checkLoadValidity(Set<FlowEntry> current, Set<FlowEntry> previous) { |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 694 | current.forEach(c -> { |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 695 | FlowEntry f = previous.stream().filter(p -> c.equals(p)). |
| 696 | findAny().orElse(null); |
| 697 | if (f != null && c.bytes() < f.bytes()) { |
| 698 | log.debug("FlowStatisticManager:checkLoadValidity():" + |
| 699 | "Error: " + c + " :Previous bytes=" + f.bytes() + |
| 700 | " is larger than current bytes=" + c.bytes() + " !!!"); |
| 701 | } |
| 702 | }); |
| 703 | |
| 704 | } |
| 705 | |
| 706 | /** |
| 707 | * Creates a predicate that checks the instruction type of a flow entry is the same as |
| 708 | * the specified instruction type. |
| 709 | * |
| 710 | * @param instType instruction type to be checked |
| 711 | * @return predicate |
| 712 | */ |
| 713 | private static Predicate<FlowEntry> hasInstructionType(Instruction.Type instType) { |
| 714 | return new Predicate<FlowEntry>() { |
| 715 | @Override |
| 716 | public boolean apply(FlowEntry flowEntry) { |
| 717 | List<Instruction> allInstructions = flowEntry.treatment().allInstructions(); |
| 718 | |
| 719 | return allInstructions.stream().filter(i -> i.type() == instType).findAny().isPresent(); |
| 720 | } |
| 721 | }; |
| 722 | } |
| 723 | |
| 724 | /** |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 725 | * Creates a predicate that checks the flow type of a flow entry is the same as |
| 726 | * the specified live type. |
| 727 | * |
| 728 | * @param liveType flow live type to be checked |
| 729 | * @return predicate |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 730 | */ |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 731 | private static Predicate<FlowEntry> hasLiveType(FlowEntry.FlowLiveType liveType) { |
| 732 | return flowEntry -> flowEntry.liveType() == liveType; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 733 | } |
| 734 | } |