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 | |
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; |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame^] | 6 | import org.apache.felix.scr.annotations.Activate; |
| 7 | import org.apache.felix.scr.annotations.Component; |
| 8 | import org.apache.felix.scr.annotations.Deactivate; |
| 9 | import org.apache.felix.scr.annotations.Service; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 10 | import org.onlab.onos.net.DeviceId; |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 11 | import org.onlab.onos.net.flow.DefaultFlowRule; |
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; |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 14 | import org.onlab.onos.net.flow.FlowRuleStore; |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame^] | 15 | import org.slf4j.Logger; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 16 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 17 | import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_ADDED; |
| 18 | import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame^] | 19 | import static org.slf4j.LoggerFactory.getLogger; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * Manages inventory of flow rules using trivial in-memory implementation. |
| 23 | */ |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame^] | 24 | @Component(immediate = true) |
| 25 | @Service |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 26 | public class SimpleFlowRuleStore implements FlowRuleStore { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 27 | |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame^] | 28 | private final Logger log = getLogger(getClass()); |
| 29 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 30 | // store entries as a pile of rules, no info about device tables |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 31 | private final Multimap<DeviceId, FlowRule> flowEntries = HashMultimap.create(); |
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 | @Activate |
| 34 | public void activate() { |
| 35 | log.info("Started"); |
| 36 | } |
| 37 | |
| 38 | @Deactivate |
| 39 | public void deactivate() { |
| 40 | log.info("Stopped"); |
| 41 | } |
| 42 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 43 | @Override |
| 44 | public Iterable<FlowRule> getFlowEntries(DeviceId deviceId) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 45 | return ImmutableSet.copyOf(flowEntries.get(deviceId)); |
| 46 | } |
| 47 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 48 | @Override |
| 49 | public FlowRule storeFlowRule(FlowRule rule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 50 | DeviceId did = rule.deviceId(); |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 51 | FlowRule entry = new DefaultFlowRule(did, |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 52 | rule.selector(), rule.treatment(), rule.priority()); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 53 | flowEntries.put(did, entry); |
| 54 | return entry; |
| 55 | } |
| 56 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 57 | @Override |
| 58 | public FlowRuleEvent addOrUpdateFlowRule(FlowRule rule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 59 | DeviceId did = rule.deviceId(); |
| 60 | |
| 61 | // check if this new rule is an update to an existing entry |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 62 | for (FlowRule fe : flowEntries.get(did)) { |
| 63 | if (rule.equals(fe)) { |
| 64 | // TODO update the stats on this FlowRule? |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 65 | return null; |
| 66 | } |
| 67 | } |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 68 | flowEntries.put(did, rule); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 69 | return new FlowRuleEvent(RULE_ADDED, rule); |
| 70 | } |
| 71 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 72 | @Override |
| 73 | public FlowRuleEvent removeFlowRule(FlowRule rule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 74 | synchronized (this) { |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 75 | if (flowEntries.remove(rule.deviceId(), rule)) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 76 | return new FlowRuleEvent(RULE_REMOVED, rule); |
| 77 | } else { |
| 78 | return null; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | } |