blob: dd8dce85ef9324d0fcbeaf7375bf091349da6a68 [file] [log] [blame]
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -07001package org.onlab.onos.net.trivial.flow.impl;
2
3import org.onlab.onos.net.DeviceId;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -07004import org.onlab.onos.net.flow.DefaultFlowRule;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -07005import org.onlab.onos.net.flow.FlowRule;
6import org.onlab.onos.net.flow.FlowRuleEvent;
7
8import com.google.common.collect.HashMultimap;
9import com.google.common.collect.ImmutableSet;
10import com.google.common.collect.Multimap;
11
12import static org.onlab.onos.net.flow.FlowRuleEvent.Type.*;
13
14/**
15 * Manages inventory of flow rules using trivial in-memory implementation.
16 */
17public class SimpleFlowRuleStore {
18
19 // store entries as a pile of rules, no info about device tables
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070020 private final Multimap<DeviceId, FlowRule> flowEntries = HashMultimap.create();
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070021
22 /**
23 * Returns the flow entries associated with a device.
24 *
25 * @param deviceId the device ID
26 * @return the flow entries
27 */
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070028 Iterable<FlowRule> getFlowEntries(DeviceId deviceId) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070029 return ImmutableSet.copyOf(flowEntries.get(deviceId));
30 }
31
32 /**
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070033 * Stores a new flow rule, and generates a FlowRule for it.
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070034 *
35 * @param rule the flow rule to add
36 * @return a flow entry
37 */
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070038 FlowRule storeFlowRule(FlowRule rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070039 DeviceId did = rule.deviceId();
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070040 FlowRule entry = new DefaultFlowRule(did,
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070041 rule.selector(), rule.treatment(), rule.priority());
42 flowEntries.put(did, entry);
43 return entry;
44 }
45
46 /**
47 * Stores a new flow rule, or updates an existing entry.
48 *
49 * @param rule the flow rule to add or update
50 * @return flow_added event, or null if just an update
51 */
52 FlowRuleEvent addOrUpdateFlowRule(FlowRule rule) {
53 DeviceId did = rule.deviceId();
54
55 // check if this new rule is an update to an existing entry
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070056 for (FlowRule fe : flowEntries.get(did)) {
57 if (rule.equals(fe)) {
58 // TODO update the stats on this FlowRule?
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070059 return null;
60 }
61 }
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070062 flowEntries.put(did, rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070063 return new FlowRuleEvent(RULE_ADDED, rule);
64 }
65
66 /**
67 *
68 * @param rule the flow rule to remove
69 * @return flow_removed event, or null if nothing removed
70 */
71 FlowRuleEvent removeFlowRule(FlowRule rule) {
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070072
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070073 synchronized (this) {
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070074 if (flowEntries.remove(rule.deviceId(), rule)) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070075 return new FlowRuleEvent(RULE_REMOVED, rule);
76 } else {
77 return null;
78 }
79 }
80 }
81
82}