blob: f3c8f344e0041ffdd8136d9b24ce3f2b848d0cd4 [file] [log] [blame]
alshabib339a3d92014-09-26 17:54:32 -07001package org.onlab.onos.store.flow.impl;
2
alshabib339a3d92014-09-26 17:54:32 -07003import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
4import static org.slf4j.LoggerFactory.getLogger;
5
6import java.util.Collection;
7import java.util.Collections;
8
9import org.apache.felix.scr.annotations.Activate;
10import org.apache.felix.scr.annotations.Component;
11import org.apache.felix.scr.annotations.Deactivate;
12import org.apache.felix.scr.annotations.Service;
13import org.onlab.onos.ApplicationId;
14import org.onlab.onos.net.DeviceId;
alshabib1c319ff2014-10-04 20:29:09 -070015import org.onlab.onos.net.flow.DefaultFlowEntry;
16import org.onlab.onos.net.flow.FlowEntry;
17import org.onlab.onos.net.flow.FlowEntry.FlowEntryState;
alshabib339a3d92014-09-26 17:54:32 -070018import org.onlab.onos.net.flow.FlowRule;
alshabib339a3d92014-09-26 17:54:32 -070019import org.onlab.onos.net.flow.FlowRuleEvent;
20import org.onlab.onos.net.flow.FlowRuleEvent.Type;
21import org.onlab.onos.net.flow.FlowRuleStore;
22import org.onlab.onos.net.flow.FlowRuleStoreDelegate;
23import org.onlab.onos.store.AbstractStore;
24import org.slf4j.Logger;
25
26import com.google.common.collect.ArrayListMultimap;
27import com.google.common.collect.ImmutableSet;
28import com.google.common.collect.Multimap;
29
30/**
31 * Manages inventory of flow rules using trivial in-memory implementation.
32 */
alshabib1c319ff2014-10-04 20:29:09 -070033//FIXME I LIE. I AIN'T DISTRIBUTED
alshabib339a3d92014-09-26 17:54:32 -070034@Component(immediate = true)
35@Service
36public class DistributedFlowRuleStore
alshabib1c319ff2014-10-04 20:29:09 -070037 extends AbstractStore<FlowRuleEvent, FlowRuleStoreDelegate>
38 implements FlowRuleStore {
alshabib339a3d92014-09-26 17:54:32 -070039
40 private final Logger log = getLogger(getClass());
41
42 // store entries as a pile of rules, no info about device tables
alshabib1c319ff2014-10-04 20:29:09 -070043 private final Multimap<DeviceId, FlowEntry> flowEntries =
44 ArrayListMultimap.<DeviceId, FlowEntry>create();
alshabib339a3d92014-09-26 17:54:32 -070045
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
alshabib1c319ff2014-10-04 20:29:09 -070061 public synchronized FlowEntry getFlowEntry(FlowRule rule) {
62 for (FlowEntry f : flowEntries.get(rule.deviceId())) {
alshabib339a3d92014-09-26 17:54:32 -070063 if (f.equals(rule)) {
64 return f;
65 }
66 }
67 return null;
68 }
69
70 @Override
alshabib1c319ff2014-10-04 20:29:09 -070071 public synchronized Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
72 Collection<FlowEntry> rules = flowEntries.get(deviceId);
alshabib339a3d92014-09-26 17:54:32 -070073 if (rules == null) {
74 return Collections.emptyList();
75 }
76 return ImmutableSet.copyOf(rules);
77 }
78
79 @Override
alshabib1c319ff2014-10-04 20:29:09 -070080 public synchronized Iterable<FlowRule> getFlowRulesByAppId(ApplicationId appId) {
alshabib339a3d92014-09-26 17:54:32 -070081 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) {
alshabib1c319ff2014-10-04 20:29:09 -070090 FlowEntry f = new DefaultFlowEntry(rule);
alshabib339a3d92014-09-26 17:54:32 -070091 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) {
alshabib1c319ff2014-10-04 20:29:09 -0700100 FlowEntry entry = getFlowEntry(rule);
101 if (entry == null) {
102 return;
alshabib339a3d92014-09-26 17:54:32 -0700103 }
alshabib1c319ff2014-10-04 20:29:09 -0700104 entry.setState(FlowEntryState.PENDING_REMOVE);
alshabib339a3d92014-09-26 17:54:32 -0700105 }
106
107 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700108 public synchronized FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) {
alshabib339a3d92014-09-26 17:54:32 -0700109 DeviceId did = rule.deviceId();
110
111 // check if this new rule is an update to an existing entry
alshabib1c319ff2014-10-04 20:29:09 -0700112 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 }
alshabib339a3d92014-09-26 17:54:32 -0700121 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
122 }
123
124 flowEntries.put(did, rule);
alshabib1c319ff2014-10-04 20:29:09 -0700125 return null;
alshabib339a3d92014-09-26 17:54:32 -0700126 }
127
128 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700129 public synchronized FlowRuleEvent removeFlowRule(FlowEntry rule) {
130 // This is where one could mark a rule as removed and still keep it in the store.
alshabib339a3d92014-09-26 17:54:32 -0700131 if (flowEntries.remove(rule.deviceId(), rule)) {
132 return new FlowRuleEvent(RULE_REMOVED, rule);
133 } else {
134 return null;
135 }
alshabib339a3d92014-09-26 17:54:32 -0700136 }
alshabib339a3d92014-09-26 17:54:32 -0700137}