tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.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_ADDED; |
| 4 | import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
| 5 | import static org.slf4j.LoggerFactory.getLogger; |
| 6 | |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 7 | import org.apache.felix.scr.annotations.Activate; |
| 8 | import org.apache.felix.scr.annotations.Component; |
| 9 | import org.apache.felix.scr.annotations.Deactivate; |
| 10 | import org.apache.felix.scr.annotations.Service; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 11 | import org.onlab.onos.net.DeviceId; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 12 | import org.onlab.onos.net.flow.FlowRule; |
| 13 | import org.onlab.onos.net.flow.FlowRuleEvent; |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 14 | import org.onlab.onos.net.flow.FlowRuleEvent.Type; |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 15 | import org.onlab.onos.net.flow.FlowRuleStore; |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame^] | 16 | import org.onlab.onos.net.flow.FlowRuleStoreDelegate; |
| 17 | import org.onlab.onos.store.AbstractStore; |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 18 | import org.slf4j.Logger; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 19 | |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ArrayListMultimap; |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import com.google.common.collect.Multimap; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Manages inventory of flow rules using trivial in-memory implementation. |
| 26 | */ |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 27 | @Component(immediate = true) |
| 28 | @Service |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame^] | 29 | public class SimpleFlowRuleStore |
| 30 | extends AbstractStore<FlowRuleEvent, FlowRuleStoreDelegate> |
| 31 | implements FlowRuleStore { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 32 | |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 33 | private final Logger log = getLogger(getClass()); |
| 34 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 35 | // store entries as a pile of rules, no info about device tables |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 36 | private final Multimap<DeviceId, FlowRule> flowEntries = ArrayListMultimap.create(); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 37 | |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 38 | @Activate |
| 39 | public void activate() { |
| 40 | log.info("Started"); |
| 41 | } |
| 42 | |
| 43 | @Deactivate |
| 44 | public void deactivate() { |
| 45 | log.info("Stopped"); |
| 46 | } |
| 47 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 48 | @Override |
| 49 | public Iterable<FlowRule> getFlowEntries(DeviceId deviceId) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 50 | return ImmutableSet.copyOf(flowEntries.get(deviceId)); |
| 51 | } |
| 52 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 53 | @Override |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 54 | public void storeFlowRule(FlowRule rule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 55 | DeviceId did = rule.deviceId(); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 56 | flowEntries.put(did, rule); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public void deleteFlowRule(FlowRule rule) { |
| 61 | DeviceId did = rule.deviceId(); |
| 62 | |
| 63 | /* |
| 64 | * find the rule and mark it for deletion. |
| 65 | * Ultimately a flow removed will come remove it. |
| 66 | */ |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 67 | |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 68 | if (flowEntries.containsEntry(did, rule)) { |
| 69 | synchronized (flowEntries) { |
| 70 | |
| 71 | flowEntries.remove(did, rule); |
| 72 | flowEntries.put(did, rule); |
| 73 | } |
| 74 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 75 | } |
| 76 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 77 | @Override |
| 78 | public FlowRuleEvent addOrUpdateFlowRule(FlowRule rule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 79 | DeviceId did = rule.deviceId(); |
| 80 | |
| 81 | // check if this new rule is an update to an existing entry |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 82 | if (flowEntries.containsEntry(did, rule)) { |
| 83 | synchronized (flowEntries) { |
| 84 | // Multimaps support duplicates so we have to remove our rule |
| 85 | // and replace it with the current version. |
| 86 | |
| 87 | flowEntries.remove(did, rule); |
| 88 | flowEntries.put(did, rule); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 89 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 90 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 91 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 92 | |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 93 | flowEntries.put(did, rule); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 94 | return new FlowRuleEvent(RULE_ADDED, rule); |
| 95 | } |
| 96 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 97 | @Override |
| 98 | public FlowRuleEvent removeFlowRule(FlowRule rule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 99 | synchronized (this) { |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 100 | if (flowEntries.remove(rule.deviceId(), rule)) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 101 | return new FlowRuleEvent(RULE_REMOVED, rule); |
| 102 | } else { |
| 103 | return null; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 108 | |
| 109 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 110 | } |