tom | ea961ff | 2014-10-01 12:45:15 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.trivial.impl; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 2 | |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 3 | import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
| 4 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 5 | import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 6 | |
| 7 | import java.util.Arrays; |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 8 | import java.util.Collections; |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 9 | import java.util.HashSet; |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 10 | import java.util.List; |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 11 | import java.util.Set; |
| 12 | import java.util.concurrent.ConcurrentHashMap; |
| 13 | import java.util.concurrent.ConcurrentMap; |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 14 | import java.util.concurrent.CopyOnWriteArrayList; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 15 | import java.util.concurrent.Future; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 16 | |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 17 | import org.apache.felix.scr.annotations.Activate; |
| 18 | import org.apache.felix.scr.annotations.Component; |
| 19 | import org.apache.felix.scr.annotations.Deactivate; |
| 20 | import org.apache.felix.scr.annotations.Service; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 21 | import org.onlab.onos.ApplicationId; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 22 | import org.onlab.onos.net.DeviceId; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 23 | import org.onlab.onos.net.flow.CompletedBatchOperation; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 24 | import org.onlab.onos.net.flow.DefaultFlowEntry; |
| 25 | import org.onlab.onos.net.flow.FlowEntry; |
| 26 | import org.onlab.onos.net.flow.FlowEntry.FlowEntryState; |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.flow.FlowId; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.flow.FlowRule; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 29 | import org.onlab.onos.net.flow.FlowRuleBatchEntry; |
| 30 | import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
| 31 | import org.onlab.onos.net.flow.FlowRuleBatchEvent; |
| 32 | import org.onlab.onos.net.flow.FlowRuleBatchOperation; |
| 33 | import org.onlab.onos.net.flow.FlowRuleBatchRequest; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.flow.FlowRuleEvent; |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 35 | import org.onlab.onos.net.flow.FlowRuleEvent.Type; |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.flow.FlowRuleStore; |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 37 | import org.onlab.onos.net.flow.FlowRuleStoreDelegate; |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 38 | import org.onlab.onos.net.flow.StoredFlowEntry; |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 39 | import org.onlab.onos.store.AbstractStore; |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 40 | import org.onlab.util.NewConcurrentHashMap; |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 41 | import org.slf4j.Logger; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 42 | |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 43 | import com.google.common.base.Function; |
| 44 | import com.google.common.collect.FluentIterable; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 45 | import com.google.common.util.concurrent.Futures; |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 46 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Manages inventory of flow rules using trivial in-memory implementation. |
| 49 | */ |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 50 | @Component(immediate = true) |
| 51 | @Service |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 52 | public class SimpleFlowRuleStore |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 53 | extends AbstractStore<FlowRuleBatchEvent, FlowRuleStoreDelegate> |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 54 | implements FlowRuleStore { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 55 | |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 56 | private final Logger log = getLogger(getClass()); |
| 57 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 58 | |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 59 | // inner Map is Device flow table |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 60 | // inner Map value (FlowId synonym list) must be synchronized before modifying |
| 61 | private final ConcurrentMap<DeviceId, ConcurrentMap<FlowId, List<StoredFlowEntry>>> |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 62 | flowEntries = new ConcurrentHashMap<>(); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 63 | |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 64 | @Activate |
| 65 | public void activate() { |
| 66 | log.info("Started"); |
| 67 | } |
| 68 | |
| 69 | @Deactivate |
| 70 | public void deactivate() { |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 71 | flowEntries.clear(); |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 72 | log.info("Stopped"); |
| 73 | } |
| 74 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 75 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 76 | @Override |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 77 | public int getFlowRuleCount() { |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 78 | int sum = 0; |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 79 | for (ConcurrentMap<FlowId, List<StoredFlowEntry>> ft : flowEntries.values()) { |
| 80 | for (List<StoredFlowEntry> fes : ft.values()) { |
| 81 | sum += fes.size(); |
| 82 | } |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 83 | } |
| 84 | return sum; |
| 85 | } |
| 86 | |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 87 | private static NewConcurrentHashMap<FlowId, List<StoredFlowEntry>> lazyEmptyFlowTable() { |
| 88 | return NewConcurrentHashMap.<FlowId, List<StoredFlowEntry>>ifNeeded(); |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns the flow table for specified device. |
| 93 | * |
| 94 | * @param deviceId identifier of the device |
| 95 | * @return Map representing Flow Table of given device. |
| 96 | */ |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 97 | private ConcurrentMap<FlowId, List<StoredFlowEntry>> getFlowTable(DeviceId deviceId) { |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 98 | return createIfAbsentUnchecked(flowEntries, |
| 99 | deviceId, lazyEmptyFlowTable()); |
| 100 | } |
| 101 | |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 102 | private List<StoredFlowEntry> getFlowEntries(DeviceId deviceId, FlowId flowId) { |
| 103 | final ConcurrentMap<FlowId, List<StoredFlowEntry>> flowTable = getFlowTable(deviceId); |
| 104 | List<StoredFlowEntry> r = flowTable.get(flowId); |
| 105 | if (r == null) { |
| 106 | final List<StoredFlowEntry> concurrentlyAdded; |
| 107 | r = new CopyOnWriteArrayList<>(); |
| 108 | concurrentlyAdded = flowTable.putIfAbsent(flowId, r); |
| 109 | if (concurrentlyAdded != null) { |
| 110 | return concurrentlyAdded; |
| 111 | } |
| 112 | } |
| 113 | return r; |
| 114 | } |
| 115 | |
| 116 | private FlowEntry getFlowEntryInternal(DeviceId deviceId, FlowRule rule) { |
| 117 | List<StoredFlowEntry> fes = getFlowEntries(deviceId, rule.id()); |
| 118 | for (StoredFlowEntry fe : fes) { |
| 119 | if (fe.equals(rule)) { |
| 120 | return fe; |
| 121 | } |
| 122 | } |
| 123 | return null; |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 127 | public FlowEntry getFlowEntry(FlowRule rule) { |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 128 | return getFlowEntryInternal(rule.deviceId(), rule); |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 133 | // flatten and make iterator unmodifiable |
| 134 | return FluentIterable.from(getFlowTable(deviceId).values()) |
| 135 | .transformAndConcat( |
| 136 | new Function<List<StoredFlowEntry>, Iterable<? extends FlowEntry>>() { |
| 137 | |
| 138 | @Override |
| 139 | public Iterable<? extends FlowEntry> apply( |
| 140 | List<StoredFlowEntry> input) { |
| 141 | return Collections.unmodifiableList(input); |
| 142 | } |
| 143 | }); |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public Iterable<FlowRule> getFlowRulesByAppId(ApplicationId appId) { |
| 148 | |
| 149 | Set<FlowRule> rules = new HashSet<>(); |
| 150 | for (DeviceId did : flowEntries.keySet()) { |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 151 | for (FlowEntry fe : getFlowEntries(did)) { |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 152 | if (fe.appId() == appId.id()) { |
| 153 | rules.add(fe); |
| 154 | } |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 157 | return rules; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 158 | } |
| 159 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 160 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 161 | public void storeFlowRule(FlowRule rule) { |
| 162 | storeFlowRuleInternal(rule); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 165 | private void storeFlowRuleInternal(FlowRule rule) { |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 166 | StoredFlowEntry f = new DefaultFlowEntry(rule); |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 167 | final DeviceId did = f.deviceId(); |
| 168 | final FlowId fid = f.id(); |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 169 | List<StoredFlowEntry> existing = getFlowEntries(did, fid); |
| 170 | synchronized (existing) { |
| 171 | for (StoredFlowEntry fe : existing) { |
| 172 | if (fe.equals(rule)) { |
| 173 | // was already there? ignore |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 174 | return; |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | // new flow rule added |
| 178 | existing.add(f); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 179 | notifyDelegate(FlowRuleBatchEvent.create( |
| 180 | new FlowRuleBatchRequest( |
| 181 | Arrays.<FlowEntry>asList(f), |
| 182 | Collections.<FlowEntry>emptyList()))); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
| 186 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 187 | public void deleteFlowRule(FlowRule rule) { |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 188 | |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 189 | List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id()); |
| 190 | synchronized (entries) { |
| 191 | for (StoredFlowEntry entry : entries) { |
| 192 | if (entry.equals(rule)) { |
| 193 | synchronized (entry) { |
| 194 | entry.setState(FlowEntryState.PENDING_REMOVE); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 195 | // TODO: Should we notify only if it's "remote" event? |
| 196 | //notifyDelegate(new FlowRuleEvent(Type.RULE_REMOVE_REQUESTED, rule)); |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 200 | } |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 201 | //log.warn("Cannot find rule {}", rule); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 202 | } |
| 203 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 204 | @Override |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 205 | public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 206 | // check if this new rule is an update to an existing entry |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 207 | List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id()); |
| 208 | synchronized (entries) { |
| 209 | for (StoredFlowEntry stored : entries) { |
| 210 | if (stored.equals(rule)) { |
| 211 | synchronized (stored) { |
| 212 | stored.setBytes(rule.bytes()); |
| 213 | stored.setLife(rule.life()); |
| 214 | stored.setPackets(rule.packets()); |
| 215 | if (stored.state() == FlowEntryState.PENDING_ADD) { |
| 216 | stored.setState(FlowEntryState.ADDED); |
| 217 | // TODO: Do we need to change `rule` state? |
| 218 | return new FlowRuleEvent(Type.RULE_ADDED, rule); |
| 219 | } |
| 220 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
| 221 | } |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 222 | } |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 223 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 224 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 225 | |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 226 | // should not reach here |
| 227 | // storeFlowRule was expected to be called |
| 228 | log.error("FlowRule was not found in store {} to update", rule); |
| 229 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 230 | //flowEntries.put(did, rule); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 231 | return null; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 232 | } |
| 233 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 234 | @Override |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 235 | public FlowRuleEvent removeFlowRule(FlowEntry rule) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 236 | // This is where one could mark a rule as removed and still keep it in the store. |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 237 | final DeviceId did = rule.deviceId(); |
| 238 | |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 239 | List<StoredFlowEntry> entries = getFlowEntries(did, rule.id()); |
| 240 | synchronized (entries) { |
| 241 | if (entries.remove(rule)) { |
| 242 | return new FlowRuleEvent(RULE_REMOVED, rule); |
| 243 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 244 | } |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 245 | return null; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 246 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 247 | |
| 248 | @Override |
| 249 | public Future<CompletedBatchOperation> storeBatch( |
| 250 | FlowRuleBatchOperation batchOperation) { |
| 251 | for (FlowRuleBatchEntry entry : batchOperation.getOperations()) { |
| 252 | if (entry.getOperator().equals(FlowRuleOperation.ADD)) { |
| 253 | storeFlowRule(entry.getTarget()); |
| 254 | } else if (entry.getOperator().equals(FlowRuleOperation.REMOVE)) { |
| 255 | deleteFlowRule(entry.getTarget()); |
| 256 | } else { |
| 257 | throw new UnsupportedOperationException("Unsupported operation type"); |
| 258 | } |
| 259 | } |
| 260 | return Futures.immediateFuture(new CompletedBatchOperation(true, Collections.<FlowEntry>emptySet())); |
| 261 | } |
| 262 | |
| 263 | @Override |
| 264 | public void batchOperationComplete(FlowRuleBatchEvent event) { |
| 265 | notifyDelegate(event); |
| 266 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 267 | } |