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