blob: d41b58bf146e394cc249051740092919696b72f0 [file] [log] [blame]
tom202175a2014-09-19 19:00:11 -07001package org.onlab.onos.net.trivial.impl;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -07002
tombe988312014-09-19 18:38:47 -07003import com.google.common.collect.HashMultimap;
4import com.google.common.collect.ImmutableSet;
5import com.google.common.collect.Multimap;
tom85c612b2014-09-19 19:25:47 -07006import org.apache.felix.scr.annotations.Activate;
7import org.apache.felix.scr.annotations.Component;
8import org.apache.felix.scr.annotations.Deactivate;
9import org.apache.felix.scr.annotations.Service;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070010import org.onlab.onos.net.DeviceId;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070011import org.onlab.onos.net.flow.DefaultFlowRule;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070012import org.onlab.onos.net.flow.FlowRule;
13import org.onlab.onos.net.flow.FlowRuleEvent;
tombe988312014-09-19 18:38:47 -070014import org.onlab.onos.net.flow.FlowRuleStore;
tom85c612b2014-09-19 19:25:47 -070015import org.slf4j.Logger;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070016
tombe988312014-09-19 18:38:47 -070017import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_ADDED;
18import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
tom85c612b2014-09-19 19:25:47 -070019import static org.slf4j.LoggerFactory.getLogger;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070020
21/**
22 * Manages inventory of flow rules using trivial in-memory implementation.
23 */
tom85c612b2014-09-19 19:25:47 -070024@Component(immediate = true)
25@Service
tombe988312014-09-19 18:38:47 -070026public class SimpleFlowRuleStore implements FlowRuleStore {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070027
tom85c612b2014-09-19 19:25:47 -070028 private final Logger log = getLogger(getClass());
29
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070030 // store entries as a pile of rules, no info about device tables
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070031 private final Multimap<DeviceId, FlowRule> flowEntries = HashMultimap.create();
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070032
tom85c612b2014-09-19 19:25:47 -070033 @Activate
34 public void activate() {
35 log.info("Started");
36 }
37
38 @Deactivate
39 public void deactivate() {
40 log.info("Stopped");
41 }
42
tombe988312014-09-19 18:38:47 -070043 @Override
44 public Iterable<FlowRule> getFlowEntries(DeviceId deviceId) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070045 return ImmutableSet.copyOf(flowEntries.get(deviceId));
46 }
47
tombe988312014-09-19 18:38:47 -070048 @Override
49 public FlowRule storeFlowRule(FlowRule rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070050 DeviceId did = rule.deviceId();
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070051 FlowRule entry = new DefaultFlowRule(did,
tombe988312014-09-19 18:38:47 -070052 rule.selector(), rule.treatment(), rule.priority());
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070053 flowEntries.put(did, entry);
54 return entry;
55 }
56
tombe988312014-09-19 18:38:47 -070057 @Override
58 public FlowRuleEvent addOrUpdateFlowRule(FlowRule rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070059 DeviceId did = rule.deviceId();
60
61 // check if this new rule is an update to an existing entry
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070062 for (FlowRule fe : flowEntries.get(did)) {
63 if (rule.equals(fe)) {
64 // TODO update the stats on this FlowRule?
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070065 return null;
66 }
67 }
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070068 flowEntries.put(did, rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070069 return new FlowRuleEvent(RULE_ADDED, rule);
70 }
71
tombe988312014-09-19 18:38:47 -070072 @Override
73 public FlowRuleEvent removeFlowRule(FlowRule rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070074 synchronized (this) {
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070075 if (flowEntries.remove(rule.deviceId(), rule)) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070076 return new FlowRuleEvent(RULE_REMOVED, rule);
77 } else {
78 return null;
79 }
80 }
81 }
82
83}