alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.flow.impl; |
| 2 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 3 | import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
| 4 | import static org.slf4j.LoggerFactory.getLogger; |
| 5 | |
| 6 | import java.util.Collection; |
| 7 | import java.util.Collections; |
| 8 | |
| 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; |
| 13 | import org.onlab.onos.ApplicationId; |
| 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; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 18 | import org.onlab.onos.net.flow.FlowRule; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 19 | import org.onlab.onos.net.flow.FlowRuleEvent; |
| 20 | import org.onlab.onos.net.flow.FlowRuleEvent.Type; |
| 21 | import org.onlab.onos.net.flow.FlowRuleStore; |
| 22 | import org.onlab.onos.net.flow.FlowRuleStoreDelegate; |
| 23 | import org.onlab.onos.store.AbstractStore; |
| 24 | import org.slf4j.Logger; |
| 25 | |
| 26 | import com.google.common.collect.ArrayListMultimap; |
| 27 | import com.google.common.collect.ImmutableSet; |
| 28 | import com.google.common.collect.Multimap; |
| 29 | |
| 30 | /** |
| 31 | * Manages inventory of flow rules using trivial in-memory implementation. |
| 32 | */ |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 33 | //FIXME I LIE. I AIN'T DISTRIBUTED |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 34 | @Component(immediate = true) |
| 35 | @Service |
| 36 | public class DistributedFlowRuleStore |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 37 | extends AbstractStore<FlowRuleEvent, FlowRuleStoreDelegate> |
| 38 | implements FlowRuleStore { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 39 | |
| 40 | private final Logger log = getLogger(getClass()); |
| 41 | |
| 42 | // store entries as a pile of rules, no info about device tables |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 43 | private final Multimap<DeviceId, FlowEntry> flowEntries = |
| 44 | ArrayListMultimap.<DeviceId, FlowEntry>create(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 45 | |
| 46 | private final Multimap<ApplicationId, FlowRule> flowEntriesById = |
| 47 | ArrayListMultimap.<ApplicationId, FlowRule>create(); |
| 48 | |
| 49 | @Activate |
| 50 | public void activate() { |
| 51 | log.info("Started"); |
| 52 | } |
| 53 | |
| 54 | @Deactivate |
| 55 | public void deactivate() { |
| 56 | log.info("Stopped"); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 61 | public synchronized FlowEntry getFlowEntry(FlowRule rule) { |
| 62 | for (FlowEntry f : flowEntries.get(rule.deviceId())) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 63 | if (f.equals(rule)) { |
| 64 | return f; |
| 65 | } |
| 66 | } |
| 67 | return null; |
| 68 | } |
| 69 | |
| 70 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 71 | public synchronized Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
| 72 | Collection<FlowEntry> rules = flowEntries.get(deviceId); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 73 | if (rules == null) { |
| 74 | return Collections.emptyList(); |
| 75 | } |
| 76 | return ImmutableSet.copyOf(rules); |
| 77 | } |
| 78 | |
| 79 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 80 | public synchronized Iterable<FlowRule> getFlowRulesByAppId(ApplicationId appId) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 81 | Collection<FlowRule> rules = flowEntriesById.get(appId); |
| 82 | if (rules == null) { |
| 83 | return Collections.emptyList(); |
| 84 | } |
| 85 | return ImmutableSet.copyOf(rules); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public synchronized void storeFlowRule(FlowRule rule) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 90 | FlowEntry f = new DefaultFlowEntry(rule); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 91 | DeviceId did = f.deviceId(); |
| 92 | if (!flowEntries.containsEntry(did, f)) { |
| 93 | flowEntries.put(did, f); |
| 94 | flowEntriesById.put(rule.appId(), f); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public synchronized void deleteFlowRule(FlowRule rule) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 100 | FlowEntry entry = getFlowEntry(rule); |
| 101 | if (entry == null) { |
| 102 | return; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 103 | } |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 104 | entry.setState(FlowEntryState.PENDING_REMOVE); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 108 | public synchronized FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 109 | DeviceId did = rule.deviceId(); |
| 110 | |
| 111 | // check if this new rule is an update to an existing entry |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 112 | FlowEntry stored = getFlowEntry(rule); |
| 113 | if (stored != null) { |
| 114 | stored.setBytes(rule.bytes()); |
| 115 | stored.setLife(rule.life()); |
| 116 | stored.setPackets(rule.packets()); |
| 117 | if (stored.state() == FlowEntryState.PENDING_ADD) { |
| 118 | stored.setState(FlowEntryState.ADDED); |
| 119 | return new FlowRuleEvent(Type.RULE_ADDED, rule); |
| 120 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 121 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
| 122 | } |
| 123 | |
| 124 | flowEntries.put(did, rule); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 125 | return null; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 129 | public synchronized FlowRuleEvent removeFlowRule(FlowEntry rule) { |
| 130 | // This is where one could mark a rule as removed and still keep it in the store. |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 131 | if (flowEntries.remove(rule.deviceId(), rule)) { |
| 132 | return new FlowRuleEvent(RULE_REMOVED, rule); |
| 133 | } else { |
| 134 | return null; |
| 135 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 136 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 137 | } |