blob: 2f0de264667e5f4fb5df79c219453098ae510909 [file] [log] [blame]
tom202175a2014-09-19 19:00:11 -07001package org.onlab.onos.net.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_ADDED;
4import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
5import static org.slf4j.LoggerFactory.getLogger;
6
tom85c612b2014-09-19 19:25:47 -07007import org.apache.felix.scr.annotations.Activate;
8import org.apache.felix.scr.annotations.Component;
9import org.apache.felix.scr.annotations.Deactivate;
10import org.apache.felix.scr.annotations.Service;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070011import org.onlab.onos.net.DeviceId;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070012import org.onlab.onos.net.flow.FlowRule;
13import org.onlab.onos.net.flow.FlowRuleEvent;
alshabib219ebaa2014-09-22 15:41:24 -070014import org.onlab.onos.net.flow.FlowRuleEvent.Type;
tombe988312014-09-19 18:38:47 -070015import org.onlab.onos.net.flow.FlowRuleStore;
tom85c612b2014-09-19 19:25:47 -070016import org.slf4j.Logger;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070017
alshabib219ebaa2014-09-22 15:41:24 -070018import com.google.common.collect.ArrayListMultimap;
19import com.google.common.collect.ImmutableSet;
20import com.google.common.collect.Multimap;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070021
22/**
23 * Manages inventory of flow rules using trivial in-memory implementation.
24 */
tom85c612b2014-09-19 19:25:47 -070025@Component(immediate = true)
26@Service
tombe988312014-09-19 18:38:47 -070027public class SimpleFlowRuleStore implements FlowRuleStore {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070028
tom85c612b2014-09-19 19:25:47 -070029 private final Logger log = getLogger(getClass());
30
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070031 // store entries as a pile of rules, no info about device tables
alshabib219ebaa2014-09-22 15:41:24 -070032 private final Multimap<DeviceId, FlowRule> flowEntries = ArrayListMultimap.create();
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070033
tom85c612b2014-09-19 19:25:47 -070034 @Activate
35 public void activate() {
36 log.info("Started");
37 }
38
39 @Deactivate
40 public void deactivate() {
41 log.info("Stopped");
42 }
43
tombe988312014-09-19 18:38:47 -070044 @Override
45 public Iterable<FlowRule> getFlowEntries(DeviceId deviceId) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070046 return ImmutableSet.copyOf(flowEntries.get(deviceId));
47 }
48
tombe988312014-09-19 18:38:47 -070049 @Override
alshabib219ebaa2014-09-22 15:41:24 -070050 public void storeFlowRule(FlowRule rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070051 DeviceId did = rule.deviceId();
alshabib219ebaa2014-09-22 15:41:24 -070052 flowEntries.put(did, rule);
53 }
54
55 @Override
56 public void deleteFlowRule(FlowRule rule) {
57 DeviceId did = rule.deviceId();
58
59 /*
60 * find the rule and mark it for deletion.
61 * Ultimately a flow removed will come remove it.
62 */
63 if (flowEntries.containsEntry(did, rule)) {
64 synchronized (flowEntries) {
65
66 flowEntries.remove(did, rule);
67 flowEntries.put(did, rule);
68 }
69 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070070 }
71
tombe988312014-09-19 18:38:47 -070072 @Override
73 public FlowRuleEvent addOrUpdateFlowRule(FlowRule rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070074 DeviceId did = rule.deviceId();
75
76 // check if this new rule is an update to an existing entry
alshabib219ebaa2014-09-22 15:41:24 -070077 if (flowEntries.containsEntry(did, rule)) {
78 synchronized (flowEntries) {
79 // Multimaps support duplicates so we have to remove our rule
80 // and replace it with the current version.
81
82 flowEntries.remove(did, rule);
83 flowEntries.put(did, rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070084 }
alshabib219ebaa2014-09-22 15:41:24 -070085 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070086 }
alshabib219ebaa2014-09-22 15:41:24 -070087
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070088 flowEntries.put(did, rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070089 return new FlowRuleEvent(RULE_ADDED, rule);
90 }
91
tombe988312014-09-19 18:38:47 -070092 @Override
93 public FlowRuleEvent removeFlowRule(FlowRule rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070094 synchronized (this) {
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070095 if (flowEntries.remove(rule.deviceId(), rule)) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070096 return new FlowRuleEvent(RULE_REMOVED, rule);
97 } else {
98 return null;
99 }
100 }
101 }
102
alshabib219ebaa2014-09-22 15:41:24 -0700103
104
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700105}