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