Thomas Vachuska | 7c27ad7 | 2014-11-14 16:20:10 -0800 | [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 | */ |
| 16 | package org.onlab.onos.store.trivial.impl; |
| 17 | |
| 18 | import com.google.common.collect.Sets; |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Service; |
| 23 | import org.onlab.onos.net.ConnectPoint; |
| 24 | import org.onlab.onos.net.PortNumber; |
| 25 | import org.onlab.onos.net.flow.FlowEntry; |
| 26 | import org.onlab.onos.net.flow.FlowRule; |
| 27 | import org.onlab.onos.net.flow.instructions.Instruction; |
| 28 | import org.onlab.onos.net.flow.instructions.Instructions; |
| 29 | import org.onlab.onos.net.statistic.StatisticStore; |
| 30 | import org.slf4j.Logger; |
| 31 | |
| 32 | import java.util.HashSet; |
| 33 | import java.util.Map; |
| 34 | import java.util.Set; |
| 35 | import java.util.concurrent.ConcurrentHashMap; |
| 36 | import java.util.concurrent.atomic.AtomicInteger; |
| 37 | |
| 38 | import static org.slf4j.LoggerFactory.getLogger; |
| 39 | |
| 40 | |
| 41 | /** |
| 42 | * Maintains statistics using RPC calls to collect stats from remote instances |
| 43 | * on demand. |
| 44 | */ |
| 45 | @Component(immediate = true) |
| 46 | @Service |
| 47 | public class SimpleStatisticStore implements StatisticStore { |
| 48 | |
| 49 | private final Logger log = getLogger(getClass()); |
| 50 | |
| 51 | private Map<ConnectPoint, InternalStatisticRepresentation> |
| 52 | representations = new ConcurrentHashMap<>(); |
| 53 | |
| 54 | private Map<ConnectPoint, Set<FlowEntry>> previous = new ConcurrentHashMap<>(); |
| 55 | private Map<ConnectPoint, Set<FlowEntry>> current = new ConcurrentHashMap<>(); |
| 56 | |
| 57 | @Activate |
| 58 | public void activate() { |
| 59 | log.info("Started"); |
| 60 | } |
| 61 | |
| 62 | @Deactivate |
| 63 | public void deactivate() { |
| 64 | log.info("Stopped"); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void prepareForStatistics(FlowRule rule) { |
| 69 | ConnectPoint cp = buildConnectPoint(rule); |
| 70 | if (cp == null) { |
| 71 | return; |
| 72 | } |
| 73 | InternalStatisticRepresentation rep; |
| 74 | synchronized (representations) { |
| 75 | rep = getOrCreateRepresentation(cp); |
| 76 | } |
| 77 | rep.prepare(); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public synchronized void removeFromStatistics(FlowRule rule) { |
| 82 | ConnectPoint cp = buildConnectPoint(rule); |
| 83 | if (cp == null) { |
| 84 | return; |
| 85 | } |
| 86 | InternalStatisticRepresentation rep = representations.get(cp); |
| 87 | if (rep != null) { |
| 88 | rep.remove(rule); |
| 89 | } |
| 90 | Set<FlowEntry> values = current.get(cp); |
| 91 | if (values != null) { |
| 92 | values.remove(rule); |
| 93 | } |
| 94 | values = previous.get(cp); |
| 95 | if (values != null) { |
| 96 | values.remove(rule); |
| 97 | } |
| 98 | |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void addOrUpdateStatistic(FlowEntry rule) { |
| 103 | ConnectPoint cp = buildConnectPoint(rule); |
| 104 | if (cp == null) { |
| 105 | return; |
| 106 | } |
| 107 | InternalStatisticRepresentation rep = representations.get(cp); |
| 108 | if (rep != null && rep.submit(rule)) { |
| 109 | updatePublishedStats(cp, rep.get()); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | private synchronized void updatePublishedStats(ConnectPoint cp, |
| 114 | Set<FlowEntry> flowEntries) { |
| 115 | Set<FlowEntry> curr = current.get(cp); |
| 116 | if (curr == null) { |
| 117 | curr = new HashSet<>(); |
| 118 | } |
| 119 | previous.put(cp, curr); |
| 120 | current.put(cp, flowEntries); |
| 121 | |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public Set<FlowEntry> getCurrentStatistic(ConnectPoint connectPoint) { |
| 126 | return getCurrentStatisticInternal(connectPoint); |
| 127 | } |
| 128 | |
| 129 | private synchronized Set<FlowEntry> getCurrentStatisticInternal(ConnectPoint connectPoint) { |
| 130 | return current.get(connectPoint); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public Set<FlowEntry> getPreviousStatistic(ConnectPoint connectPoint) { |
| 135 | return getPreviousStatisticInternal(connectPoint); |
| 136 | } |
| 137 | |
| 138 | private synchronized Set<FlowEntry> getPreviousStatisticInternal(ConnectPoint connectPoint) { |
| 139 | return previous.get(connectPoint); |
| 140 | } |
| 141 | |
| 142 | private InternalStatisticRepresentation getOrCreateRepresentation(ConnectPoint cp) { |
| 143 | |
| 144 | if (representations.containsKey(cp)) { |
| 145 | return representations.get(cp); |
| 146 | } else { |
| 147 | InternalStatisticRepresentation rep = new InternalStatisticRepresentation(); |
| 148 | representations.put(cp, rep); |
| 149 | return rep; |
| 150 | } |
| 151 | |
| 152 | } |
| 153 | |
| 154 | private ConnectPoint buildConnectPoint(FlowRule rule) { |
| 155 | PortNumber port = getOutput(rule); |
| 156 | if (port == null) { |
Brian O'Connor | faaedf4 | 2014-11-17 14:48:48 -0800 | [diff] [blame] | 157 | log.debug("Rule {} has no output.", rule); |
Thomas Vachuska | 7c27ad7 | 2014-11-14 16:20:10 -0800 | [diff] [blame] | 158 | return null; |
| 159 | } |
| 160 | ConnectPoint cp = new ConnectPoint(rule.deviceId(), port); |
| 161 | return cp; |
| 162 | } |
| 163 | |
| 164 | private PortNumber getOutput(FlowRule rule) { |
| 165 | for (Instruction i : rule.treatment().instructions()) { |
| 166 | if (i.type() == Instruction.Type.OUTPUT) { |
| 167 | Instructions.OutputInstruction out = (Instructions.OutputInstruction) i; |
| 168 | return out.port(); |
| 169 | } |
| 170 | if (i.type() == Instruction.Type.DROP) { |
| 171 | return PortNumber.P0; |
| 172 | } |
| 173 | } |
| 174 | return null; |
| 175 | } |
| 176 | |
| 177 | private class InternalStatisticRepresentation { |
| 178 | |
| 179 | private final AtomicInteger counter = new AtomicInteger(0); |
| 180 | private final Set<FlowEntry> rules = new HashSet<>(); |
| 181 | |
| 182 | public void prepare() { |
| 183 | counter.incrementAndGet(); |
| 184 | } |
| 185 | |
| 186 | public synchronized void remove(FlowRule rule) { |
| 187 | rules.remove(rule); |
| 188 | counter.decrementAndGet(); |
| 189 | } |
| 190 | |
| 191 | public synchronized boolean submit(FlowEntry rule) { |
| 192 | if (rules.contains(rule)) { |
| 193 | rules.remove(rule); |
| 194 | } |
| 195 | rules.add(rule); |
| 196 | if (counter.get() == 0) { |
| 197 | return true; |
| 198 | } else { |
| 199 | return counter.decrementAndGet() == 0; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | public synchronized Set<FlowEntry> get() { |
| 204 | counter.set(rules.size()); |
| 205 | return Sets.newHashSet(rules); |
| 206 | } |
| 207 | |
| 208 | } |
| 209 | |
| 210 | } |