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 | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 15 | import org.onlab.onos.net.flow.DefaultFlowEntry; |
| 16 | import org.onlab.onos.net.flow.FlowEntry; |
| 17 | import org.onlab.onos.net.flow.FlowEntry.FlowEntryState; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 18 | import org.onlab.onos.net.flow.FlowRule; |
| 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 | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 42 | private final Multimap<DeviceId, FlowEntry> flowEntries = |
| 43 | ArrayListMultimap.<DeviceId, FlowEntry>create(); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 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 |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 60 | public int getFlowRuleCount() { |
| 61 | return flowEntries.size(); |
| 62 | } |
| 63 | |
| 64 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 65 | public synchronized FlowEntry getFlowEntry(FlowRule rule) { |
| 66 | for (FlowEntry f : flowEntries.get(rule.deviceId())) { |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 67 | if (f.equals(rule)) { |
| 68 | return f; |
| 69 | } |
| 70 | } |
| 71 | return null; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 72 | } |
| 73 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 74 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 75 | public synchronized Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
| 76 | Collection<FlowEntry> rules = flowEntries.get(deviceId); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 77 | if (rules == null) { |
| 78 | return Collections.emptyList(); |
| 79 | } |
| 80 | return ImmutableSet.copyOf(rules); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 84 | public synchronized Iterable<FlowRule> getFlowRulesByAppId(ApplicationId appId) { |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 85 | Collection<FlowRule> rules = flowEntriesById.get(appId); |
| 86 | if (rules == null) { |
| 87 | return Collections.emptyList(); |
| 88 | } |
| 89 | return ImmutableSet.copyOf(rules); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public synchronized void storeFlowRule(FlowRule rule) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 94 | FlowEntry f = new DefaultFlowEntry(rule); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 95 | DeviceId did = f.deviceId(); |
| 96 | if (!flowEntries.containsEntry(did, f)) { |
| 97 | flowEntries.put(did, f); |
| 98 | flowEntriesById.put(rule.appId(), f); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public synchronized void deleteFlowRule(FlowRule rule) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 104 | FlowEntry entry = getFlowEntry(rule); |
| 105 | if (entry == null) { |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame^] | 106 | //log.warn("Cannot find rule {}", rule); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 107 | return; |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 108 | } |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 109 | entry.setState(FlowEntryState.PENDING_REMOVE); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 110 | } |
| 111 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 112 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 113 | public synchronized FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 114 | DeviceId did = rule.deviceId(); |
| 115 | |
| 116 | // check if this new rule is an update to an existing entry |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 117 | FlowEntry stored = getFlowEntry(rule); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 118 | if (stored != null) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 119 | stored.setBytes(rule.bytes()); |
| 120 | stored.setLife(rule.life()); |
| 121 | stored.setPackets(rule.packets()); |
| 122 | if (stored.state() == FlowEntryState.PENDING_ADD) { |
| 123 | stored.setState(FlowEntryState.ADDED); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 124 | return new FlowRuleEvent(Type.RULE_ADDED, rule); |
| 125 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 126 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 127 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 128 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame^] | 129 | //flowEntries.put(did, rule); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 130 | return null; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 131 | } |
| 132 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 133 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 134 | public synchronized FlowRuleEvent removeFlowRule(FlowEntry rule) { |
| 135 | // This is where one could mark a rule as removed and still keep it in the store. |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 136 | if (flowEntries.remove(rule.deviceId(), rule)) { |
| 137 | return new FlowRuleEvent(RULE_REMOVED, rule); |
| 138 | } else { |
| 139 | return null; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 142 | } |