Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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 | */ |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.statistic.impl; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.apache.felix.scr.annotations.Service; |
| 24 | import org.onlab.onos.net.ConnectPoint; |
| 25 | import org.onlab.onos.net.Link; |
| 26 | import org.onlab.onos.net.Path; |
alshabib | 2374fc9 | 2014-10-22 11:03:23 -0700 | [diff] [blame] | 27 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.flow.FlowEntry; |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.flow.FlowRule; |
| 30 | import org.onlab.onos.net.flow.FlowRuleEvent; |
| 31 | import org.onlab.onos.net.flow.FlowRuleListener; |
| 32 | import org.onlab.onos.net.flow.FlowRuleService; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 33 | import org.onlab.onos.net.statistic.DefaultLoad; |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.statistic.Load; |
| 35 | import org.onlab.onos.net.statistic.StatisticService; |
| 36 | import org.onlab.onos.net.statistic.StatisticStore; |
| 37 | import org.slf4j.Logger; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 38 | import java.util.Set; |
| 39 | |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 40 | import static org.slf4j.LoggerFactory.getLogger; |
| 41 | |
| 42 | /** |
| 43 | * Provides an implementation of the Statistic Service. |
| 44 | */ |
| 45 | @Component(immediate = true) |
| 46 | @Service |
| 47 | public class StatisticManager implements StatisticService { |
| 48 | |
| 49 | private final Logger log = getLogger(getClass()); |
| 50 | |
| 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 52 | protected FlowRuleService flowRuleService; |
| 53 | |
| 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 55 | protected StatisticStore statisticStore; |
| 56 | |
alshabib | 2374fc9 | 2014-10-22 11:03:23 -0700 | [diff] [blame] | 57 | |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 58 | private final InternalFlowRuleListener listener = new InternalFlowRuleListener(); |
| 59 | |
| 60 | @Activate |
| 61 | public void activate() { |
| 62 | flowRuleService.addListener(listener); |
| 63 | log.info("Started"); |
alshabib | 2374fc9 | 2014-10-22 11:03:23 -0700 | [diff] [blame] | 64 | |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Deactivate |
| 68 | public void deactivate() { |
| 69 | flowRuleService.removeListener(listener); |
| 70 | log.info("Stopped"); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public Load load(Link link) { |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 75 | return load(link.src()); |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public Load load(ConnectPoint connectPoint) { |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 80 | return loadInternal(connectPoint); |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public Link max(Path path) { |
alshabib | 46122d8 | 2014-10-23 09:05:31 -0700 | [diff] [blame] | 85 | if (path.links().isEmpty()) { |
| 86 | return null; |
| 87 | } |
| 88 | Load maxLoad = new DefaultLoad(); |
| 89 | Link maxLink = null; |
| 90 | for (Link link : path.links()) { |
| 91 | Load load = loadInternal(link.src()); |
| 92 | if (load.rate() > maxLoad.rate()) { |
| 93 | maxLoad = load; |
| 94 | maxLink = link; |
| 95 | } |
| 96 | } |
| 97 | return maxLink; |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public Link min(Path path) { |
alshabib | 46122d8 | 2014-10-23 09:05:31 -0700 | [diff] [blame] | 102 | if (path.links().isEmpty()) { |
| 103 | return null; |
| 104 | } |
| 105 | Load minLoad = new DefaultLoad(); |
| 106 | Link minLink = null; |
| 107 | for (Link link : path.links()) { |
| 108 | Load load = loadInternal(link.src()); |
| 109 | if (load.rate() < minLoad.rate()) { |
| 110 | minLoad = load; |
| 111 | minLink = link; |
| 112 | } |
| 113 | } |
| 114 | return minLink; |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public FlowRule highestHitter(ConnectPoint connectPoint) { |
alshabib | 46122d8 | 2014-10-23 09:05:31 -0700 | [diff] [blame] | 119 | Set<FlowEntry> hitters = statisticStore.getCurrentStatistic(connectPoint); |
| 120 | if (hitters.isEmpty()) { |
| 121 | return null; |
| 122 | } |
| 123 | |
| 124 | FlowEntry max = hitters.iterator().next(); |
| 125 | for (FlowEntry entry : hitters) { |
| 126 | if (entry.bytes() > max.bytes()) { |
| 127 | max = entry; |
| 128 | } |
| 129 | } |
| 130 | return max; |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 131 | } |
| 132 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 133 | private Load loadInternal(ConnectPoint connectPoint) { |
| 134 | Set<FlowEntry> current; |
| 135 | Set<FlowEntry> previous; |
| 136 | synchronized (statisticStore) { |
| 137 | current = statisticStore.getCurrentStatistic(connectPoint); |
| 138 | previous = statisticStore.getPreviousStatistic(connectPoint); |
| 139 | } |
| 140 | if (current == null || previous == null) { |
| 141 | return new DefaultLoad(); |
| 142 | } |
| 143 | long currentAggregate = aggregate(current); |
| 144 | long previousAggregate = aggregate(previous); |
| 145 | |
| 146 | return new DefaultLoad(currentAggregate, previousAggregate); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Aggregates a set of values. |
| 151 | * @param values the values to aggregate |
| 152 | * @return a long value |
| 153 | */ |
| 154 | private long aggregate(Set<FlowEntry> values) { |
| 155 | long sum = 0; |
| 156 | for (FlowEntry f : values) { |
| 157 | sum += f.bytes(); |
| 158 | } |
| 159 | return sum; |
| 160 | } |
| 161 | |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 162 | /** |
| 163 | * Internal flow rule event listener. |
| 164 | */ |
| 165 | private class InternalFlowRuleListener implements FlowRuleListener { |
| 166 | |
| 167 | @Override |
| 168 | public void event(FlowRuleEvent event) { |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 169 | FlowRule rule = event.subject(); |
| 170 | switch (event.type()) { |
| 171 | case RULE_ADDED: |
| 172 | case RULE_UPDATED: |
| 173 | if (rule instanceof FlowEntry) { |
| 174 | statisticStore.addOrUpdateStatistic((FlowEntry) rule); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 175 | } |
| 176 | break; |
| 177 | case RULE_ADD_REQUESTED: |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 178 | statisticStore.prepareForStatistics(rule); |
| 179 | break; |
| 180 | case RULE_REMOVE_REQUESTED: |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 181 | statisticStore.removeFromStatistics(rule); |
| 182 | break; |
| 183 | case RULE_REMOVED: |
| 184 | break; |
| 185 | default: |
| 186 | log.warn("Unknown flow rule event {}", event); |
| 187 | } |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | |
| 192 | } |