blob: 833d6a64e13c16dbc152104ca7437cadb4d01453 [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
tom9b4030d2014-10-06 10:39:03 -070060 public int getFlowRuleCount() {
61 return flowEntries.size();
62 }
63
64 @Override
alshabib1c319ff2014-10-04 20:29:09 -070065 public synchronized FlowEntry getFlowEntry(FlowRule rule) {
66 for (FlowEntry f : flowEntries.get(rule.deviceId())) {
alshabiba68eb962014-09-24 20:34:13 -070067 if (f.equals(rule)) {
68 return f;
69 }
70 }
71 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070072 }
73
tombe988312014-09-19 18:38:47 -070074 @Override
alshabib1c319ff2014-10-04 20:29:09 -070075 public synchronized Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
76 Collection<FlowEntry> rules = flowEntries.get(deviceId);
alshabiba68eb962014-09-24 20:34:13 -070077 if (rules == null) {
78 return Collections.emptyList();
79 }
80 return ImmutableSet.copyOf(rules);
alshabib219ebaa2014-09-22 15:41:24 -070081 }
82
83 @Override
alshabib1c319ff2014-10-04 20:29:09 -070084 public synchronized Iterable<FlowRule> getFlowRulesByAppId(ApplicationId appId) {
alshabiba68eb962014-09-24 20:34:13 -070085 Collection<FlowRule> rules = flowEntriesById.get(appId);
86 if (rules == null) {
87 return Collections.emptyList();
88 }
89 return ImmutableSet.copyOf(rules);
90 }
91
92 @Override
93 public synchronized void storeFlowRule(FlowRule rule) {
alshabib1c319ff2014-10-04 20:29:09 -070094 FlowEntry f = new DefaultFlowEntry(rule);
alshabiba68eb962014-09-24 20:34:13 -070095 DeviceId did = f.deviceId();
96 if (!flowEntries.containsEntry(did, f)) {
97 flowEntries.put(did, f);
98 flowEntriesById.put(rule.appId(), f);
99 }
100 }
101
102 @Override
103 public synchronized void deleteFlowRule(FlowRule rule) {
alshabib1c319ff2014-10-04 20:29:09 -0700104 FlowEntry entry = getFlowEntry(rule);
105 if (entry == null) {
106 return;
alshabib219ebaa2014-09-22 15:41:24 -0700107 }
alshabib1c319ff2014-10-04 20:29:09 -0700108 entry.setState(FlowEntryState.PENDING_REMOVE);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700109 }
110
tombe988312014-09-19 18:38:47 -0700111 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700112 public synchronized FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700113 DeviceId did = rule.deviceId();
114
115 // check if this new rule is an update to an existing entry
alshabib1c319ff2014-10-04 20:29:09 -0700116 FlowEntry stored = getFlowEntry(rule);
alshabibba5ac482014-10-02 17:15:20 -0700117 if (stored != null) {
alshabib1c319ff2014-10-04 20:29:09 -0700118 stored.setBytes(rule.bytes());
119 stored.setLife(rule.life());
120 stored.setPackets(rule.packets());
121 if (stored.state() == FlowEntryState.PENDING_ADD) {
122 stored.setState(FlowEntryState.ADDED);
alshabibba5ac482014-10-02 17:15:20 -0700123 return new FlowRuleEvent(Type.RULE_ADDED, rule);
124 }
alshabib219ebaa2014-09-22 15:41:24 -0700125 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700126 }
alshabib219ebaa2014-09-22 15:41:24 -0700127
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700128 flowEntries.put(did, rule);
alshabibba5ac482014-10-02 17:15:20 -0700129 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700130 }
131
tombe988312014-09-19 18:38:47 -0700132 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700133 public synchronized FlowRuleEvent removeFlowRule(FlowEntry rule) {
134 // This is where one could mark a rule as removed and still keep it in the store.
alshabiba68eb962014-09-24 20:34:13 -0700135 if (flowEntries.remove(rule.deviceId(), rule)) {
136 return new FlowRuleEvent(RULE_REMOVED, rule);
137 } else {
138 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700139 }
140 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700141}