alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.statistic.impl; |
| 2 | |
| 3 | import org.apache.felix.scr.annotations.Activate; |
| 4 | import org.apache.felix.scr.annotations.Component; |
| 5 | import org.apache.felix.scr.annotations.Deactivate; |
| 6 | import org.apache.felix.scr.annotations.Reference; |
| 7 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 8 | import org.apache.felix.scr.annotations.Service; |
| 9 | import org.onlab.onos.net.ConnectPoint; |
| 10 | import org.onlab.onos.net.Link; |
| 11 | import org.onlab.onos.net.Path; |
alshabib | 2374fc9 | 2014-10-22 11:03:23 -0700 | [diff] [blame] | 12 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 13 | import org.onlab.onos.net.flow.FlowEntry; |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 14 | import org.onlab.onos.net.flow.FlowRule; |
| 15 | import org.onlab.onos.net.flow.FlowRuleEvent; |
| 16 | import org.onlab.onos.net.flow.FlowRuleListener; |
| 17 | import org.onlab.onos.net.flow.FlowRuleService; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 18 | import org.onlab.onos.net.statistic.DefaultLoad; |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 19 | import org.onlab.onos.net.statistic.Load; |
| 20 | import org.onlab.onos.net.statistic.StatisticService; |
| 21 | import org.onlab.onos.net.statistic.StatisticStore; |
| 22 | import org.slf4j.Logger; |
| 23 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 24 | import java.util.Set; |
| 25 | |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 26 | import static org.slf4j.LoggerFactory.getLogger; |
| 27 | |
| 28 | /** |
| 29 | * Provides an implementation of the Statistic Service. |
| 30 | */ |
| 31 | @Component(immediate = true) |
| 32 | @Service |
| 33 | public class StatisticManager implements StatisticService { |
| 34 | |
| 35 | private final Logger log = getLogger(getClass()); |
| 36 | |
| 37 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 38 | protected FlowRuleService flowRuleService; |
| 39 | |
| 40 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 41 | protected StatisticStore statisticStore; |
| 42 | |
alshabib | 2374fc9 | 2014-10-22 11:03:23 -0700 | [diff] [blame] | 43 | |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 44 | private final InternalFlowRuleListener listener = new InternalFlowRuleListener(); |
| 45 | |
| 46 | @Activate |
| 47 | public void activate() { |
| 48 | flowRuleService.addListener(listener); |
| 49 | log.info("Started"); |
alshabib | 2374fc9 | 2014-10-22 11:03:23 -0700 | [diff] [blame] | 50 | |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | @Deactivate |
| 54 | public void deactivate() { |
| 55 | flowRuleService.removeListener(listener); |
| 56 | log.info("Stopped"); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public Load load(Link link) { |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 61 | return load(link.src()); |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public Load load(ConnectPoint connectPoint) { |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 66 | return loadInternal(connectPoint); |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public Link max(Path path) { |
| 71 | return null; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public Link min(Path path) { |
| 76 | return null; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public FlowRule highestHitter(ConnectPoint connectPoint) { |
| 81 | return null; |
| 82 | } |
| 83 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 84 | private Load loadInternal(ConnectPoint connectPoint) { |
| 85 | Set<FlowEntry> current; |
| 86 | Set<FlowEntry> previous; |
| 87 | synchronized (statisticStore) { |
| 88 | current = statisticStore.getCurrentStatistic(connectPoint); |
| 89 | previous = statisticStore.getPreviousStatistic(connectPoint); |
| 90 | } |
| 91 | if (current == null || previous == null) { |
| 92 | return new DefaultLoad(); |
| 93 | } |
| 94 | long currentAggregate = aggregate(current); |
| 95 | long previousAggregate = aggregate(previous); |
| 96 | |
| 97 | return new DefaultLoad(currentAggregate, previousAggregate); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Aggregates a set of values. |
| 102 | * @param values the values to aggregate |
| 103 | * @return a long value |
| 104 | */ |
| 105 | private long aggregate(Set<FlowEntry> values) { |
| 106 | long sum = 0; |
| 107 | for (FlowEntry f : values) { |
| 108 | sum += f.bytes(); |
| 109 | } |
| 110 | return sum; |
| 111 | } |
| 112 | |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 113 | /** |
| 114 | * Internal flow rule event listener. |
| 115 | */ |
| 116 | private class InternalFlowRuleListener implements FlowRuleListener { |
| 117 | |
| 118 | @Override |
| 119 | public void event(FlowRuleEvent event) { |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 120 | FlowRule rule = event.subject(); |
| 121 | switch (event.type()) { |
| 122 | case RULE_ADDED: |
| 123 | case RULE_UPDATED: |
| 124 | if (rule instanceof FlowEntry) { |
| 125 | statisticStore.addOrUpdateStatistic((FlowEntry) rule); |
| 126 | } else { |
| 127 | log.warn("IT AIN'T A FLOWENTRY"); |
| 128 | } |
| 129 | break; |
| 130 | case RULE_ADD_REQUESTED: |
| 131 | log.info("Preparing for stats"); |
| 132 | statisticStore.prepareForStatistics(rule); |
| 133 | break; |
| 134 | case RULE_REMOVE_REQUESTED: |
| 135 | log.info("Removing stats"); |
| 136 | statisticStore.removeFromStatistics(rule); |
| 137 | break; |
| 138 | case RULE_REMOVED: |
| 139 | break; |
| 140 | default: |
| 141 | log.warn("Unknown flow rule event {}", event); |
| 142 | } |
alshabib | a43aa25 | 2014-10-21 21:36:41 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | |
| 147 | } |