Thomas Vachuska | 7c27ad7 | 2014-11-14 16:20:10 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 7c27ad7 | 2014-11-14 16:20:10 -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 | */ |
Thomas Vachuska | c97aa61 | 2015-06-23 16:00:18 -0700 | [diff] [blame] | 16 | package org.onosproject.store.trivial; |
Thomas Vachuska | 7c27ad7 | 2014-11-14 16:20:10 -0800 | [diff] [blame] | 17 | |
| 18 | import com.google.common.collect.Sets; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 19 | import org.onosproject.net.ConnectPoint; |
| 20 | import org.onosproject.net.PortNumber; |
| 21 | import org.onosproject.net.flow.FlowEntry; |
| 22 | import org.onosproject.net.flow.FlowRule; |
| 23 | import org.onosproject.net.flow.instructions.Instruction; |
| 24 | import org.onosproject.net.flow.instructions.Instructions; |
| 25 | import org.onosproject.net.statistic.StatisticStore; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 26 | import org.osgi.service.component.annotations.Activate; |
| 27 | import org.osgi.service.component.annotations.Component; |
| 28 | import org.osgi.service.component.annotations.Deactivate; |
Thomas Vachuska | 7c27ad7 | 2014-11-14 16:20:10 -0800 | [diff] [blame] | 29 | import org.slf4j.Logger; |
| 30 | |
alshabib | 9c57bdd | 2014-11-28 19:14:06 -0500 | [diff] [blame] | 31 | import java.util.Collections; |
Thomas Vachuska | 7c27ad7 | 2014-11-14 16:20:10 -0800 | [diff] [blame] | 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 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 45 | @Component(immediate = true, service = StatisticStore.class) |
Thomas Vachuska | 7c27ad7 | 2014-11-14 16:20:10 -0800 | [diff] [blame] | 46 | public class SimpleStatisticStore implements StatisticStore { |
| 47 | |
| 48 | private final Logger log = getLogger(getClass()); |
| 49 | |
| 50 | private Map<ConnectPoint, InternalStatisticRepresentation> |
| 51 | representations = new ConcurrentHashMap<>(); |
| 52 | |
| 53 | private Map<ConnectPoint, Set<FlowEntry>> previous = new ConcurrentHashMap<>(); |
| 54 | private Map<ConnectPoint, Set<FlowEntry>> current = new ConcurrentHashMap<>(); |
| 55 | |
| 56 | @Activate |
| 57 | public void activate() { |
| 58 | log.info("Started"); |
| 59 | } |
| 60 | |
| 61 | @Deactivate |
| 62 | public void deactivate() { |
| 63 | log.info("Stopped"); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public void prepareForStatistics(FlowRule rule) { |
| 68 | ConnectPoint cp = buildConnectPoint(rule); |
| 69 | if (cp == null) { |
| 70 | return; |
| 71 | } |
| 72 | InternalStatisticRepresentation rep; |
| 73 | synchronized (representations) { |
| 74 | rep = getOrCreateRepresentation(cp); |
| 75 | } |
| 76 | rep.prepare(); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public synchronized void removeFromStatistics(FlowRule rule) { |
| 81 | ConnectPoint cp = buildConnectPoint(rule); |
| 82 | if (cp == null) { |
| 83 | return; |
| 84 | } |
| 85 | InternalStatisticRepresentation rep = representations.get(cp); |
alshabib | 9c57bdd | 2014-11-28 19:14:06 -0500 | [diff] [blame] | 86 | if (rep != null && rep.remove(rule)) { |
| 87 | updatePublishedStats(cp, Collections.emptySet()); |
Thomas Vachuska | 7c27ad7 | 2014-11-14 16:20:10 -0800 | [diff] [blame] | 88 | } |
| 89 | Set<FlowEntry> values = current.get(cp); |
| 90 | if (values != null) { |
| 91 | values.remove(rule); |
| 92 | } |
| 93 | values = previous.get(cp); |
| 94 | if (values != null) { |
| 95 | values.remove(rule); |
| 96 | } |
| 97 | |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void addOrUpdateStatistic(FlowEntry rule) { |
| 102 | ConnectPoint cp = buildConnectPoint(rule); |
| 103 | if (cp == null) { |
| 104 | return; |
| 105 | } |
| 106 | InternalStatisticRepresentation rep = representations.get(cp); |
| 107 | if (rep != null && rep.submit(rule)) { |
| 108 | updatePublishedStats(cp, rep.get()); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | private synchronized void updatePublishedStats(ConnectPoint cp, |
| 113 | Set<FlowEntry> flowEntries) { |
| 114 | Set<FlowEntry> curr = current.get(cp); |
| 115 | if (curr == null) { |
| 116 | curr = new HashSet<>(); |
| 117 | } |
| 118 | previous.put(cp, curr); |
| 119 | current.put(cp, flowEntries); |
| 120 | |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public Set<FlowEntry> getCurrentStatistic(ConnectPoint connectPoint) { |
| 125 | return getCurrentStatisticInternal(connectPoint); |
| 126 | } |
| 127 | |
| 128 | private synchronized Set<FlowEntry> getCurrentStatisticInternal(ConnectPoint connectPoint) { |
| 129 | return current.get(connectPoint); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public Set<FlowEntry> getPreviousStatistic(ConnectPoint connectPoint) { |
| 134 | return getPreviousStatisticInternal(connectPoint); |
| 135 | } |
| 136 | |
| 137 | private synchronized Set<FlowEntry> getPreviousStatisticInternal(ConnectPoint connectPoint) { |
| 138 | return previous.get(connectPoint); |
| 139 | } |
| 140 | |
| 141 | private InternalStatisticRepresentation getOrCreateRepresentation(ConnectPoint cp) { |
| 142 | |
| 143 | if (representations.containsKey(cp)) { |
| 144 | return representations.get(cp); |
| 145 | } else { |
| 146 | InternalStatisticRepresentation rep = new InternalStatisticRepresentation(); |
| 147 | representations.put(cp, rep); |
| 148 | return rep; |
| 149 | } |
| 150 | |
| 151 | } |
| 152 | |
| 153 | private ConnectPoint buildConnectPoint(FlowRule rule) { |
| 154 | PortNumber port = getOutput(rule); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 155 | |
Thomas Vachuska | 7c27ad7 | 2014-11-14 16:20:10 -0800 | [diff] [blame] | 156 | if (port == null) { |
Thomas Vachuska | 7c27ad7 | 2014-11-14 16:20:10 -0800 | [diff] [blame] | 157 | return null; |
| 158 | } |
| 159 | ConnectPoint cp = new ConnectPoint(rule.deviceId(), port); |
| 160 | return cp; |
| 161 | } |
| 162 | |
| 163 | private PortNumber getOutput(FlowRule rule) { |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 164 | for (Instruction i : rule.treatment().immediate()) { |
Thomas Vachuska | 7c27ad7 | 2014-11-14 16:20:10 -0800 | [diff] [blame] | 165 | if (i.type() == Instruction.Type.OUTPUT) { |
| 166 | Instructions.OutputInstruction out = (Instructions.OutputInstruction) i; |
| 167 | return out.port(); |
| 168 | } |
Thomas Vachuska | 7c27ad7 | 2014-11-14 16:20:10 -0800 | [diff] [blame] | 169 | } |
| 170 | return null; |
| 171 | } |
| 172 | |
| 173 | private class InternalStatisticRepresentation { |
| 174 | |
| 175 | private final AtomicInteger counter = new AtomicInteger(0); |
| 176 | private final Set<FlowEntry> rules = new HashSet<>(); |
| 177 | |
| 178 | public void prepare() { |
| 179 | counter.incrementAndGet(); |
| 180 | } |
| 181 | |
alshabib | 9c57bdd | 2014-11-28 19:14:06 -0500 | [diff] [blame] | 182 | public synchronized boolean remove(FlowRule rule) { |
Thomas Vachuska | 7c27ad7 | 2014-11-14 16:20:10 -0800 | [diff] [blame] | 183 | rules.remove(rule); |
alshabib | 9c57bdd | 2014-11-28 19:14:06 -0500 | [diff] [blame] | 184 | return counter.decrementAndGet() == 0; |
Thomas Vachuska | 7c27ad7 | 2014-11-14 16:20:10 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | public synchronized boolean submit(FlowEntry rule) { |
| 188 | if (rules.contains(rule)) { |
| 189 | rules.remove(rule); |
| 190 | } |
| 191 | rules.add(rule); |
| 192 | if (counter.get() == 0) { |
| 193 | return true; |
| 194 | } else { |
| 195 | return counter.decrementAndGet() == 0; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | public synchronized Set<FlowEntry> get() { |
| 200 | counter.set(rules.size()); |
| 201 | return Sets.newHashSet(rules); |
| 202 | } |
| 203 | |
| 204 | } |
| 205 | |
| 206 | } |