blob: 9681f3b0a0295b3fd83ddd1a755bbba35002b68c [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;
tomc78acee2014-09-24 15:16:55 -070016import org.onlab.onos.net.flow.FlowRuleStoreDelegate;
17import org.onlab.onos.store.AbstractStore;
tom85c612b2014-09-19 19:25:47 -070018import org.slf4j.Logger;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070019
alshabib219ebaa2014-09-22 15:41:24 -070020import com.google.common.collect.ArrayListMultimap;
21import com.google.common.collect.ImmutableSet;
22import com.google.common.collect.Multimap;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070023
24/**
25 * Manages inventory of flow rules using trivial in-memory implementation.
26 */
tom85c612b2014-09-19 19:25:47 -070027@Component(immediate = true)
28@Service
tomc78acee2014-09-24 15:16:55 -070029public class SimpleFlowRuleStore
30 extends AbstractStore<FlowRuleEvent, FlowRuleStoreDelegate>
31 implements FlowRuleStore {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070032
tom85c612b2014-09-19 19:25:47 -070033 private final Logger log = getLogger(getClass());
34
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070035 // store entries as a pile of rules, no info about device tables
alshabib219ebaa2014-09-22 15:41:24 -070036 private final Multimap<DeviceId, FlowRule> flowEntries = ArrayListMultimap.create();
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070037
tom85c612b2014-09-19 19:25:47 -070038 @Activate
39 public void activate() {
40 log.info("Started");
41 }
42
43 @Deactivate
44 public void deactivate() {
45 log.info("Stopped");
46 }
47
tombe988312014-09-19 18:38:47 -070048 @Override
49 public Iterable<FlowRule> getFlowEntries(DeviceId deviceId) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070050 return ImmutableSet.copyOf(flowEntries.get(deviceId));
51 }
52
tombe988312014-09-19 18:38:47 -070053 @Override
alshabib219ebaa2014-09-22 15:41:24 -070054 public void storeFlowRule(FlowRule rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070055 DeviceId did = rule.deviceId();
alshabib219ebaa2014-09-22 15:41:24 -070056 flowEntries.put(did, rule);
57 }
58
59 @Override
60 public void deleteFlowRule(FlowRule rule) {
61 DeviceId did = rule.deviceId();
62
63 /*
64 * find the rule and mark it for deletion.
65 * Ultimately a flow removed will come remove it.
66 */
alshabibbb8b1282014-09-22 17:00:18 -070067
alshabib219ebaa2014-09-22 15:41:24 -070068 if (flowEntries.containsEntry(did, rule)) {
69 synchronized (flowEntries) {
70
71 flowEntries.remove(did, rule);
72 flowEntries.put(did, rule);
73 }
74 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070075 }
76
tombe988312014-09-19 18:38:47 -070077 @Override
78 public FlowRuleEvent addOrUpdateFlowRule(FlowRule rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070079 DeviceId did = rule.deviceId();
80
81 // check if this new rule is an update to an existing entry
alshabib219ebaa2014-09-22 15:41:24 -070082 if (flowEntries.containsEntry(did, rule)) {
83 synchronized (flowEntries) {
84 // Multimaps support duplicates so we have to remove our rule
85 // and replace it with the current version.
86
87 flowEntries.remove(did, rule);
88 flowEntries.put(did, rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070089 }
alshabib219ebaa2014-09-22 15:41:24 -070090 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070091 }
alshabib219ebaa2014-09-22 15:41:24 -070092
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070093 flowEntries.put(did, rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070094 return new FlowRuleEvent(RULE_ADDED, rule);
95 }
96
tombe988312014-09-19 18:38:47 -070097 @Override
98 public FlowRuleEvent removeFlowRule(FlowRule rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070099 synchronized (this) {
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700100 if (flowEntries.remove(rule.deviceId(), rule)) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700101 return new FlowRuleEvent(RULE_REMOVED, rule);
102 } else {
103 return null;
104 }
105 }
106 }
107
alshabib219ebaa2014-09-22 15:41:24 -0700108
109
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700110}