Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.trivial.flow.impl; |
| 2 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame^] | 3 | import com.google.common.collect.HashMultimap; |
| 4 | import com.google.common.collect.ImmutableSet; |
| 5 | import com.google.common.collect.Multimap; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 6 | import org.onlab.onos.net.DeviceId; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 7 | import org.onlab.onos.net.flow.DefaultFlowRule; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 8 | import org.onlab.onos.net.flow.FlowRule; |
| 9 | import org.onlab.onos.net.flow.FlowRuleEvent; |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame^] | 10 | import org.onlab.onos.net.flow.FlowRuleStore; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 11 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame^] | 12 | import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_ADDED; |
| 13 | import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 14 | |
| 15 | /** |
| 16 | * Manages inventory of flow rules using trivial in-memory implementation. |
| 17 | */ |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame^] | 18 | public class SimpleFlowRuleStore implements FlowRuleStore { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 19 | |
| 20 | // store entries as a pile of rules, no info about device tables |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 21 | private final Multimap<DeviceId, FlowRule> flowEntries = HashMultimap.create(); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 22 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame^] | 23 | @Override |
| 24 | public Iterable<FlowRule> getFlowEntries(DeviceId deviceId) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 25 | return ImmutableSet.copyOf(flowEntries.get(deviceId)); |
| 26 | } |
| 27 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame^] | 28 | @Override |
| 29 | public FlowRule storeFlowRule(FlowRule rule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 30 | DeviceId did = rule.deviceId(); |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 31 | FlowRule entry = new DefaultFlowRule(did, |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame^] | 32 | rule.selector(), rule.treatment(), rule.priority()); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 33 | flowEntries.put(did, entry); |
| 34 | return entry; |
| 35 | } |
| 36 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame^] | 37 | @Override |
| 38 | public FlowRuleEvent addOrUpdateFlowRule(FlowRule rule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 39 | DeviceId did = rule.deviceId(); |
| 40 | |
| 41 | // check if this new rule is an update to an existing entry |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 42 | for (FlowRule fe : flowEntries.get(did)) { |
| 43 | if (rule.equals(fe)) { |
| 44 | // TODO update the stats on this FlowRule? |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 45 | return null; |
| 46 | } |
| 47 | } |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 48 | flowEntries.put(did, rule); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 49 | return new FlowRuleEvent(RULE_ADDED, rule); |
| 50 | } |
| 51 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame^] | 52 | @Override |
| 53 | public FlowRuleEvent removeFlowRule(FlowRule rule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 54 | synchronized (this) { |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 55 | if (flowEntries.remove(rule.deviceId(), rule)) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 56 | return new FlowRuleEvent(RULE_REMOVED, rule); |
| 57 | } else { |
| 58 | return null; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | } |