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 |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame^] | 61 | public int getFlowRuleCount() { |
| 62 | return flowEntries.size(); |
| 63 | } |
| 64 | |
| 65 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 66 | public synchronized FlowEntry getFlowEntry(FlowRule rule) { |
| 67 | for (FlowEntry f : flowEntries.get(rule.deviceId())) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 68 | if (f.equals(rule)) { |
| 69 | return f; |
| 70 | } |
| 71 | } |
| 72 | return null; |
| 73 | } |
| 74 | |
| 75 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 76 | public synchronized Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
| 77 | Collection<FlowEntry> rules = flowEntries.get(deviceId); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 78 | if (rules == null) { |
| 79 | return Collections.emptyList(); |
| 80 | } |
| 81 | return ImmutableSet.copyOf(rules); |
| 82 | } |
| 83 | |
| 84 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 85 | public synchronized Iterable<FlowRule> getFlowRulesByAppId(ApplicationId appId) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 86 | Collection<FlowRule> rules = flowEntriesById.get(appId); |
| 87 | if (rules == null) { |
| 88 | return Collections.emptyList(); |
| 89 | } |
| 90 | return ImmutableSet.copyOf(rules); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public synchronized void storeFlowRule(FlowRule rule) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 95 | FlowEntry f = new DefaultFlowEntry(rule); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 96 | DeviceId did = f.deviceId(); |
| 97 | if (!flowEntries.containsEntry(did, f)) { |
| 98 | flowEntries.put(did, f); |
| 99 | flowEntriesById.put(rule.appId(), f); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public synchronized void deleteFlowRule(FlowRule rule) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 105 | FlowEntry entry = getFlowEntry(rule); |
| 106 | if (entry == null) { |
| 107 | return; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 108 | } |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 109 | entry.setState(FlowEntryState.PENDING_REMOVE); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 113 | public synchronized FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -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); |
| 118 | if (stored != null) { |
| 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); |
| 124 | return new FlowRuleEvent(Type.RULE_ADDED, rule); |
| 125 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 126 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
| 127 | } |
| 128 | |
| 129 | flowEntries.put(did, rule); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 130 | return null; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 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 | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 136 | if (flowEntries.remove(rule.deviceId(), rule)) { |
| 137 | return new FlowRuleEvent(RULE_REMOVED, rule); |
| 138 | } else { |
| 139 | return null; |
| 140 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 141 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 142 | } |