blob: 5a5592aefb39627c8a11ca11b2867e9167b2b329 [file] [log] [blame]
alshabib339a3d92014-09-26 17:54:32 -07001package org.onlab.onos.store.flow.impl;
2
3import 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
7import java.util.Collection;
8import java.util.Collections;
9
10import 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;
14import org.onlab.onos.ApplicationId;
15import org.onlab.onos.net.DeviceId;
16import org.onlab.onos.net.flow.DefaultFlowRule;
17import org.onlab.onos.net.flow.FlowRule;
18import org.onlab.onos.net.flow.FlowRule.FlowRuleState;
19import org.onlab.onos.net.flow.FlowRuleEvent;
20import org.onlab.onos.net.flow.FlowRuleEvent.Type;
21import org.onlab.onos.net.flow.FlowRuleStore;
22import org.onlab.onos.net.flow.FlowRuleStoreDelegate;
23import org.onlab.onos.store.AbstractStore;
24import org.slf4j.Logger;
25
26import com.google.common.collect.ArrayListMultimap;
27import com.google.common.collect.ImmutableSet;
28import com.google.common.collect.Multimap;
29
30/**
31 * Manages inventory of flow rules using trivial in-memory implementation.
32 */
33//FIXME: I LIE I AM NOT DISTRIBUTED
34@Component(immediate = true)
35@Service
36public class DistributedFlowRuleStore
37extends AbstractStore<FlowRuleEvent, FlowRuleStoreDelegate>
38implements FlowRuleStore {
39
40 private final Logger log = getLogger(getClass());
41
42 // store entries as a pile of rules, no info about device tables
43 private final Multimap<DeviceId, FlowRule> flowEntries =
44 ArrayListMultimap.<DeviceId, FlowRule>create();
45
46 private final Multimap<ApplicationId, FlowRule> flowEntriesById =
47 ArrayListMultimap.<ApplicationId, FlowRule>create();
48
49 @Activate
50 public void activate() {
51 log.info("Started");
52 }
53
54 @Deactivate
55 public void deactivate() {
56 log.info("Stopped");
57 }
58
59
60 @Override
61 public synchronized FlowRule getFlowRule(FlowRule rule) {
62 for (FlowRule f : flowEntries.get(rule.deviceId())) {
63 if (f.equals(rule)) {
64 return f;
65 }
66 }
67 return null;
68 }
69
70 @Override
71 public synchronized Iterable<FlowRule> getFlowEntries(DeviceId deviceId) {
72 Collection<FlowRule> rules = flowEntries.get(deviceId);
73 if (rules == null) {
74 return Collections.emptyList();
75 }
76 return ImmutableSet.copyOf(rules);
77 }
78
79 @Override
80 public synchronized Iterable<FlowRule> getFlowEntriesByAppId(ApplicationId appId) {
81 Collection<FlowRule> rules = flowEntriesById.get(appId);
82 if (rules == null) {
83 return Collections.emptyList();
84 }
85 return ImmutableSet.copyOf(rules);
86 }
87
88 @Override
89 public synchronized void storeFlowRule(FlowRule rule) {
90 FlowRule f = new DefaultFlowRule(rule, FlowRuleState.PENDING_ADD);
91 DeviceId did = f.deviceId();
92 if (!flowEntries.containsEntry(did, f)) {
93 flowEntries.put(did, f);
94 flowEntriesById.put(rule.appId(), f);
95 }
96 }
97
98 @Override
99 public synchronized void deleteFlowRule(FlowRule rule) {
100 FlowRule f = new DefaultFlowRule(rule, FlowRuleState.PENDING_REMOVE);
101 DeviceId did = f.deviceId();
102
103 /*
104 * find the rule and mark it for deletion.
105 * Ultimately a flow removed will come remove it.
106 */
107
108 if (flowEntries.containsEntry(did, f)) {
109 //synchronized (flowEntries) {
110 flowEntries.remove(did, f);
111 flowEntries.put(did, f);
112 flowEntriesById.remove(rule.appId(), rule);
113 //}
114 }
115 }
116
117 @Override
118 public synchronized FlowRuleEvent addOrUpdateFlowRule(FlowRule rule) {
119 DeviceId did = rule.deviceId();
120
121 // check if this new rule is an update to an existing entry
122 if (flowEntries.containsEntry(did, rule)) {
123 //synchronized (flowEntries) {
124 // Multimaps support duplicates so we have to remove our rule
125 // and replace it with the current version.
126 flowEntries.remove(did, rule);
127 flowEntries.put(did, rule);
128 //}
129 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
130 }
131
132 flowEntries.put(did, rule);
133 return new FlowRuleEvent(RULE_ADDED, rule);
134 }
135
136 @Override
137 public synchronized FlowRuleEvent removeFlowRule(FlowRule rule) {
138 //synchronized (this) {
139 if (flowEntries.remove(rule.deviceId(), rule)) {
140 return new FlowRuleEvent(RULE_REMOVED, rule);
141 } else {
142 return null;
143 }
144 //}
145 }
146
147
148
149
150
151
152
153}