blob: d49e00b287d6fdfe19409de05e489c7b2358809f [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
tom9b4030d2014-10-06 10:39:03 -070061 public int getFlowRuleCount() {
62 return flowEntries.size();
63 }
64
65 @Override
alshabib1c319ff2014-10-04 20:29:09 -070066 public synchronized FlowEntry getFlowEntry(FlowRule rule) {
67 for (FlowEntry f : flowEntries.get(rule.deviceId())) {
alshabib339a3d92014-09-26 17:54:32 -070068 if (f.equals(rule)) {
69 return f;
70 }
71 }
72 return null;
73 }
74
75 @Override
alshabib1c319ff2014-10-04 20:29:09 -070076 public synchronized Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
77 Collection<FlowEntry> rules = flowEntries.get(deviceId);
alshabib339a3d92014-09-26 17:54:32 -070078 if (rules == null) {
79 return Collections.emptyList();
80 }
81 return ImmutableSet.copyOf(rules);
82 }
83
84 @Override
alshabib1c319ff2014-10-04 20:29:09 -070085 public synchronized Iterable<FlowRule> getFlowRulesByAppId(ApplicationId appId) {
alshabib339a3d92014-09-26 17:54:32 -070086 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) {
alshabib1c319ff2014-10-04 20:29:09 -070095 FlowEntry f = new DefaultFlowEntry(rule);
alshabib339a3d92014-09-26 17:54:32 -070096 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) {
alshabib1c319ff2014-10-04 20:29:09 -0700105 FlowEntry entry = getFlowEntry(rule);
106 if (entry == null) {
107 return;
alshabib339a3d92014-09-26 17:54:32 -0700108 }
alshabib1c319ff2014-10-04 20:29:09 -0700109 entry.setState(FlowEntryState.PENDING_REMOVE);
alshabib339a3d92014-09-26 17:54:32 -0700110 }
111
112 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700113 public synchronized FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) {
alshabib339a3d92014-09-26 17:54:32 -0700114 DeviceId did = rule.deviceId();
115
116 // check if this new rule is an update to an existing entry
alshabib1c319ff2014-10-04 20:29:09 -0700117 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 }
alshabib339a3d92014-09-26 17:54:32 -0700126 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
127 }
128
129 flowEntries.put(did, rule);
alshabib1c319ff2014-10-04 20:29:09 -0700130 return null;
alshabib339a3d92014-09-26 17:54:32 -0700131 }
132
133 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700134 public synchronized FlowRuleEvent removeFlowRule(FlowEntry rule) {
135 // This is where one could mark a rule as removed and still keep it in the store.
alshabib339a3d92014-09-26 17:54:32 -0700136 if (flowEntries.remove(rule.deviceId(), rule)) {
137 return new FlowRuleEvent(RULE_REMOVED, rule);
138 } else {
139 return null;
140 }
alshabib339a3d92014-09-26 17:54:32 -0700141 }
alshabib339a3d92014-09-26 17:54:32 -0700142}