blob: 5fa92f3047ccbbcce5e30b89a95660cd33fbcf57 [file] [log] [blame]
tomea961ff2014-10-01 12:45:15 -07001package org.onlab.onos.store.trivial.impl;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -07002
alshabib219ebaa2014-09-22 15:41:24 -07003import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
4import static org.slf4j.LoggerFactory.getLogger;
5
alshabiba68eb962014-09-24 20:34:13 -07006import java.util.Collection;
7import java.util.Collections;
8
tom85c612b2014-09-19 19:25:47 -07009import 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;
alshabiba68eb962014-09-24 20:34:13 -070013import org.onlab.onos.ApplicationId;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070014import 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;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070018import org.onlab.onos.net.flow.FlowRule;
19import org.onlab.onos.net.flow.FlowRuleEvent;
alshabib219ebaa2014-09-22 15:41:24 -070020import org.onlab.onos.net.flow.FlowRuleEvent.Type;
tombe988312014-09-19 18:38:47 -070021import org.onlab.onos.net.flow.FlowRuleStore;
tomc78acee2014-09-24 15:16:55 -070022import org.onlab.onos.net.flow.FlowRuleStoreDelegate;
23import org.onlab.onos.store.AbstractStore;
tom85c612b2014-09-19 19:25:47 -070024import org.slf4j.Logger;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070025
alshabib219ebaa2014-09-22 15:41:24 -070026import com.google.common.collect.ArrayListMultimap;
27import com.google.common.collect.ImmutableSet;
28import com.google.common.collect.Multimap;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070029
30/**
31 * Manages inventory of flow rules using trivial in-memory implementation.
32 */
tom85c612b2014-09-19 19:25:47 -070033@Component(immediate = true)
34@Service
tomc78acee2014-09-24 15:16:55 -070035public class SimpleFlowRuleStore
36 extends AbstractStore<FlowRuleEvent, FlowRuleStoreDelegate>
37 implements FlowRuleStore {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070038
tom85c612b2014-09-19 19:25:47 -070039 private final Logger log = getLogger(getClass());
40
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070041 // store entries as a pile of rules, no info about device tables
alshabib1c319ff2014-10-04 20:29:09 -070042 private final Multimap<DeviceId, FlowEntry> flowEntries =
43 ArrayListMultimap.<DeviceId, FlowEntry>create();
alshabiba68eb962014-09-24 20:34:13 -070044
45 private final Multimap<ApplicationId, FlowRule> flowEntriesById =
46 ArrayListMultimap.<ApplicationId, FlowRule>create();
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070047
tom85c612b2014-09-19 19:25:47 -070048 @Activate
49 public void activate() {
50 log.info("Started");
51 }
52
53 @Deactivate
54 public void deactivate() {
55 log.info("Stopped");
56 }
57
alshabiba68eb962014-09-24 20:34:13 -070058
tombe988312014-09-19 18:38:47 -070059 @Override
alshabib1c319ff2014-10-04 20:29:09 -070060 public synchronized FlowEntry getFlowEntry(FlowRule rule) {
61 for (FlowEntry f : flowEntries.get(rule.deviceId())) {
alshabiba68eb962014-09-24 20:34:13 -070062 if (f.equals(rule)) {
63 return f;
64 }
65 }
66 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070067 }
68
tombe988312014-09-19 18:38:47 -070069 @Override
alshabib1c319ff2014-10-04 20:29:09 -070070 public synchronized Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
71 Collection<FlowEntry> rules = flowEntries.get(deviceId);
alshabiba68eb962014-09-24 20:34:13 -070072 if (rules == null) {
73 return Collections.emptyList();
74 }
75 return ImmutableSet.copyOf(rules);
alshabib219ebaa2014-09-22 15:41:24 -070076 }
77
78 @Override
alshabib1c319ff2014-10-04 20:29:09 -070079 public synchronized Iterable<FlowRule> getFlowRulesByAppId(ApplicationId appId) {
alshabiba68eb962014-09-24 20:34:13 -070080 Collection<FlowRule> rules = flowEntriesById.get(appId);
81 if (rules == null) {
82 return Collections.emptyList();
83 }
84 return ImmutableSet.copyOf(rules);
85 }
86
87 @Override
88 public synchronized void storeFlowRule(FlowRule rule) {
alshabib1c319ff2014-10-04 20:29:09 -070089 FlowEntry f = new DefaultFlowEntry(rule);
alshabiba68eb962014-09-24 20:34:13 -070090 DeviceId did = f.deviceId();
91 if (!flowEntries.containsEntry(did, f)) {
92 flowEntries.put(did, f);
93 flowEntriesById.put(rule.appId(), f);
94 }
95 }
96
97 @Override
98 public synchronized void deleteFlowRule(FlowRule rule) {
alshabib1c319ff2014-10-04 20:29:09 -070099 FlowEntry entry = getFlowEntry(rule);
100 if (entry == null) {
101 return;
alshabib219ebaa2014-09-22 15:41:24 -0700102 }
alshabib1c319ff2014-10-04 20:29:09 -0700103 entry.setState(FlowEntryState.PENDING_REMOVE);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700104 }
105
tombe988312014-09-19 18:38:47 -0700106 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700107 public synchronized FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700108 DeviceId did = rule.deviceId();
109
110 // check if this new rule is an update to an existing entry
alshabib1c319ff2014-10-04 20:29:09 -0700111 FlowEntry stored = getFlowEntry(rule);
alshabibba5ac482014-10-02 17:15:20 -0700112 if (stored != null) {
alshabib1c319ff2014-10-04 20:29:09 -0700113 stored.setBytes(rule.bytes());
114 stored.setLife(rule.life());
115 stored.setPackets(rule.packets());
116 if (stored.state() == FlowEntryState.PENDING_ADD) {
117 stored.setState(FlowEntryState.ADDED);
alshabibba5ac482014-10-02 17:15:20 -0700118 return new FlowRuleEvent(Type.RULE_ADDED, rule);
119 }
alshabib219ebaa2014-09-22 15:41:24 -0700120 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700121 }
alshabib219ebaa2014-09-22 15:41:24 -0700122
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700123 flowEntries.put(did, rule);
alshabibba5ac482014-10-02 17:15:20 -0700124 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700125 }
126
tombe988312014-09-19 18:38:47 -0700127 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700128 public synchronized FlowRuleEvent removeFlowRule(FlowEntry rule) {
129 // This is where one could mark a rule as removed and still keep it in the store.
alshabiba68eb962014-09-24 20:34:13 -0700130 if (flowEntries.remove(rule.deviceId(), rule)) {
131 return new FlowRuleEvent(RULE_REMOVED, rule);
132 } else {
133 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700134 }
135 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700136}