blob: 8ea00e72f1ace698c3d025fa606ea70f8e03e601 [file] [log] [blame]
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -07001package org.onlab.onos.net.trivial.flow.impl;
2
tombe988312014-09-19 18:38:47 -07003import com.google.common.collect.HashMultimap;
4import com.google.common.collect.ImmutableSet;
5import com.google.common.collect.Multimap;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -07006import org.onlab.onos.net.DeviceId;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -07007import org.onlab.onos.net.flow.DefaultFlowRule;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -07008import org.onlab.onos.net.flow.FlowRule;
9import org.onlab.onos.net.flow.FlowRuleEvent;
tombe988312014-09-19 18:38:47 -070010import org.onlab.onos.net.flow.FlowRuleStore;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070011
tombe988312014-09-19 18:38:47 -070012import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_ADDED;
13import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070014
15/**
16 * Manages inventory of flow rules using trivial in-memory implementation.
17 */
tombe988312014-09-19 18:38:47 -070018public class SimpleFlowRuleStore implements FlowRuleStore {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070019
20 // store entries as a pile of rules, no info about device tables
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070021 private final Multimap<DeviceId, FlowRule> flowEntries = HashMultimap.create();
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070022
tombe988312014-09-19 18:38:47 -070023 @Override
24 public Iterable<FlowRule> getFlowEntries(DeviceId deviceId) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070025 return ImmutableSet.copyOf(flowEntries.get(deviceId));
26 }
27
tombe988312014-09-19 18:38:47 -070028 @Override
29 public FlowRule storeFlowRule(FlowRule rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070030 DeviceId did = rule.deviceId();
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070031 FlowRule entry = new DefaultFlowRule(did,
tombe988312014-09-19 18:38:47 -070032 rule.selector(), rule.treatment(), rule.priority());
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070033 flowEntries.put(did, entry);
34 return entry;
35 }
36
tombe988312014-09-19 18:38:47 -070037 @Override
38 public FlowRuleEvent addOrUpdateFlowRule(FlowRule rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070039 DeviceId did = rule.deviceId();
40
41 // check if this new rule is an update to an existing entry
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070042 for (FlowRule fe : flowEntries.get(did)) {
43 if (rule.equals(fe)) {
44 // TODO update the stats on this FlowRule?
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070045 return null;
46 }
47 }
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070048 flowEntries.put(did, rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070049 return new FlowRuleEvent(RULE_ADDED, rule);
50 }
51
tombe988312014-09-19 18:38:47 -070052 @Override
53 public FlowRuleEvent removeFlowRule(FlowRule rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070054 synchronized (this) {
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070055 if (flowEntries.remove(rule.deviceId(), rule)) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070056 return new FlowRuleEvent(RULE_REMOVED, rule);
57 } else {
58 return null;
59 }
60 }
61 }
62
63}