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