Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.statistic.impl; |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 17 | |
Sho SHIMIZU | e3cc0b9 | 2014-11-20 16:18:29 -0800 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.base.Predicate; |
| 20 | import com.google.common.collect.FluentIterable; |
| 21 | import com.google.common.collect.ImmutableSet; |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 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; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.GroupId; |
| 30 | import org.onosproject.net.ConnectPoint; |
| 31 | import org.onosproject.net.Link; |
| 32 | import org.onosproject.net.Path; |
alshabib | 2374fc9 | 2014-10-22 11:03:23 -0700 | [diff] [blame] | 33 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.flow.FlowEntry; |
| 35 | import org.onosproject.net.flow.FlowRule; |
| 36 | import org.onosproject.net.flow.FlowRuleEvent; |
| 37 | import org.onosproject.net.flow.FlowRuleListener; |
| 38 | import org.onosproject.net.flow.FlowRuleService; |
| 39 | import org.onosproject.net.statistic.DefaultLoad; |
| 40 | import org.onosproject.net.statistic.Load; |
| 41 | import org.onosproject.net.statistic.StatisticService; |
| 42 | import org.onosproject.net.statistic.StatisticStore; |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 43 | import org.slf4j.Logger; |
Sho SHIMIZU | e3cc0b9 | 2014-11-20 16:18:29 -0800 | [diff] [blame] | 44 | |
| 45 | import java.util.Collections; |
| 46 | import java.util.Objects; |
| 47 | import java.util.Optional; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 48 | import java.util.Set; |
| 49 | |
Sho SHIMIZU | e3cc0b9 | 2014-11-20 16:18:29 -0800 | [diff] [blame] | 50 | import static com.google.common.base.Preconditions.checkNotNull; |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 51 | import static org.slf4j.LoggerFactory.getLogger; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 52 | import static org.onosproject.security.AppGuard.checkPermission; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 53 | import static org.onosproject.security.AppPermission.Type.*; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 54 | |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Provides an implementation of the Statistic Service. |
| 58 | */ |
| 59 | @Component(immediate = true) |
| 60 | @Service |
| 61 | public class StatisticManager implements StatisticService { |
| 62 | |
| 63 | private final Logger log = getLogger(getClass()); |
| 64 | |
| 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 66 | protected FlowRuleService flowRuleService; |
| 67 | |
| 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 69 | protected StatisticStore statisticStore; |
| 70 | |
alshabib | 2374fc9 | 2014-10-22 11:03:23 -0700 | [diff] [blame] | 71 | |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 72 | private final InternalFlowRuleListener listener = new InternalFlowRuleListener(); |
| 73 | |
| 74 | @Activate |
| 75 | public void activate() { |
| 76 | flowRuleService.addListener(listener); |
| 77 | log.info("Started"); |
alshabib | 2374fc9 | 2014-10-22 11:03:23 -0700 | [diff] [blame] | 78 | |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Deactivate |
| 82 | public void deactivate() { |
| 83 | flowRuleService.removeListener(listener); |
| 84 | log.info("Stopped"); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public Load load(Link link) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 89 | checkPermission(STATISTIC_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 90 | |
| 91 | return load(link.src()); |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Override |
Sho SHIMIZU | e3cc0b9 | 2014-11-20 16:18:29 -0800 | [diff] [blame] | 95 | public Load load(Link link, ApplicationId appId, Optional<GroupId> groupId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 96 | checkPermission(STATISTIC_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 97 | |
Sho SHIMIZU | e3cc0b9 | 2014-11-20 16:18:29 -0800 | [diff] [blame] | 98 | Statistics stats = getStatistics(link.src()); |
| 99 | if (!stats.isValid()) { |
| 100 | return new DefaultLoad(); |
| 101 | } |
| 102 | |
| 103 | ImmutableSet<FlowEntry> current = FluentIterable.from(stats.current()) |
| 104 | .filter(hasApplicationId(appId)) |
| 105 | .filter(hasGroupId(groupId)) |
| 106 | .toSet(); |
| 107 | ImmutableSet<FlowEntry> previous = FluentIterable.from(stats.previous()) |
| 108 | .filter(hasApplicationId(appId)) |
| 109 | .filter(hasGroupId(groupId)) |
| 110 | .toSet(); |
| 111 | |
| 112 | return new DefaultLoad(aggregate(current), aggregate(previous)); |
| 113 | } |
| 114 | |
| 115 | @Override |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 116 | public Load load(ConnectPoint connectPoint) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 117 | checkPermission(STATISTIC_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 118 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 119 | return loadInternal(connectPoint); |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public Link max(Path path) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 124 | checkPermission(STATISTIC_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 125 | |
alshabib | 46122d8 | 2014-10-23 09:05:31 -0700 | [diff] [blame] | 126 | if (path.links().isEmpty()) { |
| 127 | return null; |
| 128 | } |
| 129 | Load maxLoad = new DefaultLoad(); |
| 130 | Link maxLink = null; |
| 131 | for (Link link : path.links()) { |
| 132 | Load load = loadInternal(link.src()); |
| 133 | if (load.rate() > maxLoad.rate()) { |
| 134 | maxLoad = load; |
| 135 | maxLink = link; |
| 136 | } |
| 137 | } |
| 138 | return maxLink; |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public Link min(Path path) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 143 | checkPermission(STATISTIC_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 144 | |
alshabib | 46122d8 | 2014-10-23 09:05:31 -0700 | [diff] [blame] | 145 | if (path.links().isEmpty()) { |
| 146 | return null; |
| 147 | } |
| 148 | Load minLoad = new DefaultLoad(); |
| 149 | Link minLink = null; |
| 150 | for (Link link : path.links()) { |
| 151 | Load load = loadInternal(link.src()); |
| 152 | if (load.rate() < minLoad.rate()) { |
| 153 | minLoad = load; |
| 154 | minLink = link; |
| 155 | } |
| 156 | } |
| 157 | return minLink; |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public FlowRule highestHitter(ConnectPoint connectPoint) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 162 | checkPermission(STATISTIC_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 163 | |
alshabib | 46122d8 | 2014-10-23 09:05:31 -0700 | [diff] [blame] | 164 | Set<FlowEntry> hitters = statisticStore.getCurrentStatistic(connectPoint); |
| 165 | if (hitters.isEmpty()) { |
| 166 | return null; |
| 167 | } |
| 168 | |
| 169 | FlowEntry max = hitters.iterator().next(); |
| 170 | for (FlowEntry entry : hitters) { |
| 171 | if (entry.bytes() > max.bytes()) { |
| 172 | max = entry; |
| 173 | } |
| 174 | } |
| 175 | return max; |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 176 | } |
| 177 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 178 | private Load loadInternal(ConnectPoint connectPoint) { |
Sho SHIMIZU | e3cc0b9 | 2014-11-20 16:18:29 -0800 | [diff] [blame] | 179 | Statistics stats = getStatistics(connectPoint); |
| 180 | if (!stats.isValid()) { |
| 181 | return new DefaultLoad(); |
| 182 | } |
| 183 | |
| 184 | return new DefaultLoad(aggregate(stats.current), aggregate(stats.previous)); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Returns statistics of the specified port. |
| 189 | * |
| 190 | * @param connectPoint port to query |
| 191 | * @return statistics |
| 192 | */ |
| 193 | private Statistics getStatistics(ConnectPoint connectPoint) { |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 194 | Set<FlowEntry> current; |
| 195 | Set<FlowEntry> previous; |
| 196 | synchronized (statisticStore) { |
Sho SHIMIZU | e3cc0b9 | 2014-11-20 16:18:29 -0800 | [diff] [blame] | 197 | current = getCurrentStatistic(connectPoint); |
| 198 | previous = getPreviousStatistic(connectPoint); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 199 | } |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 200 | |
Sho SHIMIZU | e3cc0b9 | 2014-11-20 16:18:29 -0800 | [diff] [blame] | 201 | return new Statistics(current, previous); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /** |
Sho SHIMIZU | e3cc0b9 | 2014-11-20 16:18:29 -0800 | [diff] [blame] | 205 | * Returns the current statistic of the specified port. |
| 206 | |
| 207 | * @param connectPoint port to query |
| 208 | * @return set of flow entries |
| 209 | */ |
| 210 | private Set<FlowEntry> getCurrentStatistic(ConnectPoint connectPoint) { |
| 211 | Set<FlowEntry> stats = statisticStore.getCurrentStatistic(connectPoint); |
| 212 | if (stats == null) { |
| 213 | return Collections.emptySet(); |
| 214 | } else { |
| 215 | return stats; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Returns the previous statistic of the specified port. |
| 221 | * |
| 222 | * @param connectPoint port to query |
| 223 | * @return set of flow entries |
| 224 | */ |
| 225 | private Set<FlowEntry> getPreviousStatistic(ConnectPoint connectPoint) { |
alshabib | 507ac37 | 2014-11-28 18:12:07 -0500 | [diff] [blame] | 226 | Set<FlowEntry> stats = statisticStore.getPreviousStatistic(connectPoint); |
Sho SHIMIZU | e3cc0b9 | 2014-11-20 16:18:29 -0800 | [diff] [blame] | 227 | if (stats == null) { |
| 228 | return Collections.emptySet(); |
| 229 | } else { |
| 230 | return stats; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | // TODO: make aggregation function generic by passing a function |
| 235 | // (applying Java 8 Stream API?) |
| 236 | /** |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 237 | * Aggregates a set of values. |
| 238 | * @param values the values to aggregate |
| 239 | * @return a long value |
| 240 | */ |
| 241 | private long aggregate(Set<FlowEntry> values) { |
| 242 | long sum = 0; |
| 243 | for (FlowEntry f : values) { |
| 244 | sum += f.bytes(); |
| 245 | } |
| 246 | return sum; |
| 247 | } |
| 248 | |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 249 | /** |
| 250 | * Internal flow rule event listener. |
| 251 | */ |
| 252 | private class InternalFlowRuleListener implements FlowRuleListener { |
| 253 | |
| 254 | @Override |
| 255 | public void event(FlowRuleEvent event) { |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 256 | FlowRule rule = event.subject(); |
| 257 | switch (event.type()) { |
| 258 | case RULE_ADDED: |
| 259 | case RULE_UPDATED: |
| 260 | if (rule instanceof FlowEntry) { |
| 261 | statisticStore.addOrUpdateStatistic((FlowEntry) rule); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 262 | } |
| 263 | break; |
| 264 | case RULE_ADD_REQUESTED: |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 265 | statisticStore.prepareForStatistics(rule); |
| 266 | break; |
| 267 | case RULE_REMOVE_REQUESTED: |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 268 | statisticStore.removeFromStatistics(rule); |
| 269 | break; |
| 270 | case RULE_REMOVED: |
| 271 | break; |
| 272 | default: |
| 273 | log.warn("Unknown flow rule event {}", event); |
| 274 | } |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
Sho SHIMIZU | e3cc0b9 | 2014-11-20 16:18:29 -0800 | [diff] [blame] | 278 | /** |
| 279 | * Internal data class holding two set of flow entries. |
| 280 | */ |
| 281 | private static class Statistics { |
| 282 | private final ImmutableSet<FlowEntry> current; |
| 283 | private final ImmutableSet<FlowEntry> previous; |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 284 | |
Sho SHIMIZU | e3cc0b9 | 2014-11-20 16:18:29 -0800 | [diff] [blame] | 285 | public Statistics(Set<FlowEntry> current, Set<FlowEntry> previous) { |
| 286 | this.current = ImmutableSet.copyOf(checkNotNull(current)); |
| 287 | this.previous = ImmutableSet.copyOf(checkNotNull(previous)); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Returns flow entries as the current value. |
| 292 | * |
| 293 | * @return flow entries as the current value |
| 294 | */ |
| 295 | public ImmutableSet<FlowEntry> current() { |
| 296 | return current; |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Returns flow entries as the previous value. |
| 301 | * |
| 302 | * @return flow entries as the previous value |
| 303 | */ |
| 304 | public ImmutableSet<FlowEntry> previous() { |
| 305 | return previous; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Validates values are not empty. |
| 310 | * |
| 311 | * @return false if either of the sets is empty. Otherwise, true. |
| 312 | */ |
| 313 | public boolean isValid() { |
| 314 | return !(current.isEmpty() || previous.isEmpty()); |
| 315 | } |
| 316 | |
| 317 | @Override |
| 318 | public int hashCode() { |
| 319 | return Objects.hash(current, previous); |
| 320 | } |
| 321 | |
| 322 | @Override |
| 323 | public boolean equals(Object obj) { |
| 324 | if (this == obj) { |
| 325 | return true; |
| 326 | } |
| 327 | if (!(obj instanceof Statistics)) { |
| 328 | return false; |
| 329 | } |
| 330 | final Statistics other = (Statistics) obj; |
| 331 | return Objects.equals(this.current, other.current) && Objects.equals(this.previous, other.previous); |
| 332 | } |
| 333 | |
| 334 | @Override |
| 335 | public String toString() { |
| 336 | return MoreObjects.toStringHelper(this) |
| 337 | .add("current", current) |
| 338 | .add("previous", previous) |
| 339 | .toString(); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Creates a predicate that checks the application ID of a flow entry is the same as |
| 345 | * the specified application ID. |
| 346 | * |
| 347 | * @param appId application ID to be checked |
| 348 | * @return predicate |
| 349 | */ |
| 350 | private static Predicate<FlowEntry> hasApplicationId(ApplicationId appId) { |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 351 | return flowEntry -> flowEntry.appId() == appId.id(); |
Sho SHIMIZU | e3cc0b9 | 2014-11-20 16:18:29 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Create a predicate that checks the group ID of a flow entry is the same as |
| 356 | * the specified group ID. |
| 357 | * |
| 358 | * @param groupId group ID to be checked |
| 359 | * @return predicate |
| 360 | */ |
| 361 | private static Predicate<FlowEntry> hasGroupId(Optional<GroupId> groupId) { |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 362 | return flowEntry -> { |
| 363 | if (!groupId.isPresent()) { |
| 364 | return false; |
Sho SHIMIZU | e3cc0b9 | 2014-11-20 16:18:29 -0800 | [diff] [blame] | 365 | } |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 366 | // FIXME: The left hand type and right hand type don't match |
| 367 | // FlowEntry.groupId() still returns a short value, not int. |
| 368 | return flowEntry.groupId().equals(groupId.get()); |
Sho SHIMIZU | e3cc0b9 | 2014-11-20 16:18:29 -0800 | [diff] [blame] | 369 | }; |
| 370 | } |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 371 | } |