blob: f839396e2bbc9cf3432646398776cf06597c608e [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;
4import org.onlab.onos.net.flow.DefaultFlowEntry;
5import org.onlab.onos.net.flow.FlowEntry;
6import org.onlab.onos.net.flow.FlowRule;
7import org.onlab.onos.net.flow.FlowRuleEvent;
8
9import com.google.common.collect.HashMultimap;
10import com.google.common.collect.ImmutableSet;
11import com.google.common.collect.Multimap;
12
13import static org.onlab.onos.net.flow.FlowRuleEvent.Type.*;
14
15/**
16 * Manages inventory of flow rules using trivial in-memory implementation.
17 */
18public class SimpleFlowRuleStore {
19
20 // store entries as a pile of rules, no info about device tables
21 private final Multimap<DeviceId, FlowEntry> flowEntries = HashMultimap.create();
22
23 /**
24 * Returns the flow entries associated with a device.
25 *
26 * @param deviceId the device ID
27 * @return the flow entries
28 */
29 Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
30 return ImmutableSet.copyOf(flowEntries.get(deviceId));
31 }
32
33 /**
34 * Stores a new flow rule, and generates a FlowEntry for it.
35 *
36 * @param rule the flow rule to add
37 * @return a flow entry
38 */
39 FlowEntry storeFlowRule(FlowRule rule) {
40 DeviceId did = rule.deviceId();
41 FlowEntry entry = new DefaultFlowEntry(did,
42 rule.selector(), rule.treatment(), rule.priority());
43 flowEntries.put(did, entry);
44 return entry;
45 }
46
47 /**
48 * Stores a new flow rule, or updates an existing entry.
49 *
50 * @param rule the flow rule to add or update
51 * @return flow_added event, or null if just an update
52 */
53 FlowRuleEvent addOrUpdateFlowRule(FlowRule rule) {
54 DeviceId did = rule.deviceId();
55
56 // check if this new rule is an update to an existing entry
57 for (FlowEntry fe : flowEntries.get(did)) {
58 if (rule.equals(fe)) {
59 // TODO update the stats on this flowEntry?
60 return null;
61 }
62 }
63
64 FlowEntry newfe = new DefaultFlowEntry(did,
65 rule.selector(), rule.treatment(), rule.priority());
66 flowEntries.put(did, newfe);
67 return new FlowRuleEvent(RULE_ADDED, rule);
68 }
69
70 /**
71 *
72 * @param rule the flow rule to remove
73 * @return flow_removed event, or null if nothing removed
74 */
75 FlowRuleEvent removeFlowRule(FlowRule rule) {
76 synchronized (this) {
77 if (flowEntries.remove(rule.deviceId(), rule)) {
78 return new FlowRuleEvent(RULE_REMOVED, rule);
79 } else {
80 return null;
81 }
82 }
83 }
84
85}