alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.flow.impl; |
| 2 | |
| 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 | |
| 7 | import java.util.Collection; |
| 8 | import java.util.Collections; |
| 9 | |
| 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; |
| 14 | import org.onlab.onos.ApplicationId; |
| 15 | import org.onlab.onos.net.DeviceId; |
| 16 | import org.onlab.onos.net.flow.DefaultFlowRule; |
| 17 | import org.onlab.onos.net.flow.FlowRule; |
| 18 | import org.onlab.onos.net.flow.FlowRule.FlowRuleState; |
| 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 | */ |
| 33 | //FIXME: I LIE I AM NOT DISTRIBUTED |
| 34 | @Component(immediate = true) |
| 35 | @Service |
| 36 | public class DistributedFlowRuleStore |
| 37 | extends AbstractStore<FlowRuleEvent, FlowRuleStoreDelegate> |
| 38 | implements FlowRuleStore { |
| 39 | |
| 40 | private final Logger log = getLogger(getClass()); |
| 41 | |
| 42 | // store entries as a pile of rules, no info about device tables |
| 43 | private final Multimap<DeviceId, FlowRule> flowEntries = |
| 44 | ArrayListMultimap.<DeviceId, FlowRule>create(); |
| 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 |
| 61 | public synchronized FlowRule getFlowRule(FlowRule rule) { |
| 62 | for (FlowRule f : flowEntries.get(rule.deviceId())) { |
| 63 | if (f.equals(rule)) { |
| 64 | return f; |
| 65 | } |
| 66 | } |
| 67 | return null; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public synchronized Iterable<FlowRule> getFlowEntries(DeviceId deviceId) { |
| 72 | Collection<FlowRule> rules = flowEntries.get(deviceId); |
| 73 | if (rules == null) { |
| 74 | return Collections.emptyList(); |
| 75 | } |
| 76 | return ImmutableSet.copyOf(rules); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public synchronized Iterable<FlowRule> getFlowEntriesByAppId(ApplicationId appId) { |
| 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) { |
| 90 | FlowRule f = new DefaultFlowRule(rule, FlowRuleState.PENDING_ADD); |
| 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) { |
| 100 | FlowRule f = new DefaultFlowRule(rule, FlowRuleState.PENDING_REMOVE); |
| 101 | DeviceId did = f.deviceId(); |
| 102 | |
| 103 | /* |
| 104 | * find the rule and mark it for deletion. |
| 105 | * Ultimately a flow removed will come remove it. |
| 106 | */ |
| 107 | |
| 108 | if (flowEntries.containsEntry(did, f)) { |
| 109 | //synchronized (flowEntries) { |
| 110 | flowEntries.remove(did, f); |
| 111 | flowEntries.put(did, f); |
| 112 | flowEntriesById.remove(rule.appId(), rule); |
| 113 | //} |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public synchronized FlowRuleEvent addOrUpdateFlowRule(FlowRule rule) { |
| 119 | DeviceId did = rule.deviceId(); |
| 120 | |
| 121 | // check if this new rule is an update to an existing entry |
| 122 | if (flowEntries.containsEntry(did, rule)) { |
| 123 | //synchronized (flowEntries) { |
| 124 | // Multimaps support duplicates so we have to remove our rule |
| 125 | // and replace it with the current version. |
| 126 | flowEntries.remove(did, rule); |
| 127 | flowEntries.put(did, rule); |
| 128 | //} |
| 129 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
| 130 | } |
| 131 | |
| 132 | flowEntries.put(did, rule); |
| 133 | return new FlowRuleEvent(RULE_ADDED, rule); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public synchronized FlowRuleEvent removeFlowRule(FlowRule rule) { |
| 138 | //synchronized (this) { |
| 139 | if (flowEntries.remove(rule.deviceId(), rule)) { |
| 140 | return new FlowRuleEvent(RULE_REMOVED, rule); |
| 141 | } else { |
| 142 | return null; |
| 143 | } |
| 144 | //} |
| 145 | } |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | } |