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