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