blob: 816ea63e2dd64bcf2fc65fa84742c82d58dc3676 [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
alshabiba68eb962014-09-24 20:34:13 -07007import java.util.Collection;
8import java.util.Collections;
9
tom85c612b2014-09-19 19:25:47 -070010import org.apache.felix.scr.annotations.Activate;
11import org.apache.felix.scr.annotations.Component;
12import org.apache.felix.scr.annotations.Deactivate;
13import org.apache.felix.scr.annotations.Service;
alshabiba68eb962014-09-24 20:34:13 -070014import org.onlab.onos.ApplicationId;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070015import org.onlab.onos.net.DeviceId;
alshabiba68eb962014-09-24 20:34:13 -070016import org.onlab.onos.net.flow.DefaultFlowRule;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070017import org.onlab.onos.net.flow.FlowRule;
alshabiba68eb962014-09-24 20:34:13 -070018import org.onlab.onos.net.flow.FlowRule.FlowRuleState;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070019import org.onlab.onos.net.flow.FlowRuleEvent;
alshabib219ebaa2014-09-22 15:41:24 -070020import org.onlab.onos.net.flow.FlowRuleEvent.Type;
tombe988312014-09-19 18:38:47 -070021import org.onlab.onos.net.flow.FlowRuleStore;
tom85c612b2014-09-19 19:25:47 -070022import org.slf4j.Logger;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070023
alshabib219ebaa2014-09-22 15:41:24 -070024import com.google.common.collect.ArrayListMultimap;
25import com.google.common.collect.ImmutableSet;
26import com.google.common.collect.Multimap;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070027
28/**
29 * Manages inventory of flow rules using trivial in-memory implementation.
30 */
tom85c612b2014-09-19 19:25:47 -070031@Component(immediate = true)
32@Service
tombe988312014-09-19 18:38:47 -070033public class SimpleFlowRuleStore implements FlowRuleStore {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070034
tom85c612b2014-09-19 19:25:47 -070035 private final Logger log = getLogger(getClass());
36
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070037 // store entries as a pile of rules, no info about device tables
alshabiba68eb962014-09-24 20:34:13 -070038 private final Multimap<DeviceId, FlowRule> flowEntries =
39 ArrayListMultimap.<DeviceId, FlowRule>create();
40
41 private final Multimap<ApplicationId, FlowRule> flowEntriesById =
42 ArrayListMultimap.<ApplicationId, FlowRule>create();
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070043
tom85c612b2014-09-19 19:25:47 -070044 @Activate
45 public void activate() {
46 log.info("Started");
47 }
48
49 @Deactivate
50 public void deactivate() {
51 log.info("Stopped");
52 }
53
alshabiba68eb962014-09-24 20:34:13 -070054
tombe988312014-09-19 18:38:47 -070055 @Override
alshabiba68eb962014-09-24 20:34:13 -070056 public synchronized FlowRule getFlowRule(FlowRule rule) {
57 for (FlowRule f : flowEntries.get(rule.deviceId())) {
58 if (f.equals(rule)) {
59 return f;
60 }
61 }
62 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070063 }
64
tombe988312014-09-19 18:38:47 -070065 @Override
alshabiba68eb962014-09-24 20:34:13 -070066 public synchronized Iterable<FlowRule> getFlowEntries(DeviceId deviceId) {
67 Collection<FlowRule> rules = flowEntries.get(deviceId);
68 if (rules == null) {
69 return Collections.emptyList();
70 }
71 return ImmutableSet.copyOf(rules);
alshabib219ebaa2014-09-22 15:41:24 -070072 }
73
74 @Override
alshabiba68eb962014-09-24 20:34:13 -070075 public synchronized Iterable<FlowRule> getFlowEntriesByAppId(ApplicationId appId) {
76 Collection<FlowRule> rules = flowEntriesById.get(appId);
77 if (rules == null) {
78 return Collections.emptyList();
79 }
80 return ImmutableSet.copyOf(rules);
81 }
82
83 @Override
84 public synchronized void storeFlowRule(FlowRule rule) {
85 FlowRule f = new DefaultFlowRule(rule, FlowRuleState.PENDING_ADD);
86 DeviceId did = f.deviceId();
87 if (!flowEntries.containsEntry(did, f)) {
88 flowEntries.put(did, f);
89 flowEntriesById.put(rule.appId(), f);
90 }
91 }
92
93 @Override
94 public synchronized void deleteFlowRule(FlowRule rule) {
95 FlowRule f = new DefaultFlowRule(rule, FlowRuleState.PENDING_REMOVE);
96 DeviceId did = f.deviceId();
alshabib219ebaa2014-09-22 15:41:24 -070097
98 /*
99 * find the rule and mark it for deletion.
100 * Ultimately a flow removed will come remove it.
101 */
alshabibbb8b1282014-09-22 17:00:18 -0700102
alshabiba68eb962014-09-24 20:34:13 -0700103 if (flowEntries.containsEntry(did, f)) {
104 //synchronized (flowEntries) {
105 flowEntries.remove(did, f);
106 flowEntries.put(did, f);
107 flowEntriesById.remove(rule.appId(), rule);
108 //}
alshabib219ebaa2014-09-22 15:41:24 -0700109 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700110 }
111
tombe988312014-09-19 18:38:47 -0700112 @Override
alshabiba68eb962014-09-24 20:34:13 -0700113 public synchronized FlowRuleEvent addOrUpdateFlowRule(FlowRule rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700114 DeviceId did = rule.deviceId();
115
116 // check if this new rule is an update to an existing entry
alshabib219ebaa2014-09-22 15:41:24 -0700117 if (flowEntries.containsEntry(did, rule)) {
alshabiba68eb962014-09-24 20:34:13 -0700118 //synchronized (flowEntries) {
119 // Multimaps support duplicates so we have to remove our rule
120 // and replace it with the current version.
121 flowEntries.remove(did, rule);
122 flowEntries.put(did, rule);
123 //}
alshabib219ebaa2014-09-22 15:41:24 -0700124 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700125 }
alshabib219ebaa2014-09-22 15:41:24 -0700126
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700127 flowEntries.put(did, rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700128 return new FlowRuleEvent(RULE_ADDED, rule);
129 }
130
tombe988312014-09-19 18:38:47 -0700131 @Override
alshabiba68eb962014-09-24 20:34:13 -0700132 public synchronized FlowRuleEvent removeFlowRule(FlowRule rule) {
133 //synchronized (this) {
134 if (flowEntries.remove(rule.deviceId(), rule)) {
135 return new FlowRuleEvent(RULE_REMOVED, rule);
136 } else {
137 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700138 }
alshabiba68eb962014-09-24 20:34:13 -0700139 //}
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700140 }
141
alshabib219ebaa2014-09-22 15:41:24 -0700142
143
alshabiba68eb962014-09-24 20:34:13 -0700144
145
146
147
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700148}