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