blob: a96cacb953d5738ab7c7d494eb853c2fc27d1012 [file] [log] [blame]
tomea961ff2014-10-01 12:45:15 -07001package org.onlab.onos.store.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_REMOVED;
4import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI605347c2014-10-17 21:05:23 -07005import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -07006import java.util.Collections;
Yuta HIGUCHI605347c2014-10-17 21:05:23 -07007import java.util.HashSet;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -07008import java.util.List;
Yuta HIGUCHI605347c2014-10-17 21:05:23 -07009import java.util.Set;
10import java.util.concurrent.ConcurrentHashMap;
11import java.util.concurrent.ConcurrentMap;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070012import java.util.concurrent.CopyOnWriteArrayList;
alshabiba68eb962014-09-24 20:34:13 -070013
tom85c612b2014-09-19 19:25:47 -070014import org.apache.felix.scr.annotations.Activate;
15import org.apache.felix.scr.annotations.Component;
16import org.apache.felix.scr.annotations.Deactivate;
17import org.apache.felix.scr.annotations.Service;
alshabiba68eb962014-09-24 20:34:13 -070018import org.onlab.onos.ApplicationId;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070019import org.onlab.onos.net.DeviceId;
alshabib1c319ff2014-10-04 20:29:09 -070020import org.onlab.onos.net.flow.DefaultFlowEntry;
21import org.onlab.onos.net.flow.FlowEntry;
22import org.onlab.onos.net.flow.FlowEntry.FlowEntryState;
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070023import org.onlab.onos.net.flow.FlowId;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070024import org.onlab.onos.net.flow.FlowRule;
25import org.onlab.onos.net.flow.FlowRuleEvent;
alshabib219ebaa2014-09-22 15:41:24 -070026import org.onlab.onos.net.flow.FlowRuleEvent.Type;
tombe988312014-09-19 18:38:47 -070027import org.onlab.onos.net.flow.FlowRuleStore;
tomc78acee2014-09-24 15:16:55 -070028import org.onlab.onos.net.flow.FlowRuleStoreDelegate;
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -070029import org.onlab.onos.net.flow.StoredFlowEntry;
tomc78acee2014-09-24 15:16:55 -070030import org.onlab.onos.store.AbstractStore;
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070031import org.onlab.util.NewConcurrentHashMap;
tom85c612b2014-09-19 19:25:47 -070032import org.slf4j.Logger;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070033
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070034import com.google.common.base.Function;
35import com.google.common.collect.FluentIterable;
36
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070037/**
38 * Manages inventory of flow rules using trivial in-memory implementation.
39 */
tom85c612b2014-09-19 19:25:47 -070040@Component(immediate = true)
41@Service
tomc78acee2014-09-24 15:16:55 -070042public class SimpleFlowRuleStore
43 extends AbstractStore<FlowRuleEvent, FlowRuleStoreDelegate>
44 implements FlowRuleStore {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070045
tom85c612b2014-09-19 19:25:47 -070046 private final Logger log = getLogger(getClass());
47
alshabiba68eb962014-09-24 20:34:13 -070048
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070049 // inner Map is Device flow table
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070050 // inner Map value (FlowId synonym list) must be synchronized before modifying
51 private final ConcurrentMap<DeviceId, ConcurrentMap<FlowId, List<StoredFlowEntry>>>
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070052 flowEntries = new ConcurrentHashMap<>();
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070053
tom85c612b2014-09-19 19:25:47 -070054 @Activate
55 public void activate() {
56 log.info("Started");
57 }
58
59 @Deactivate
60 public void deactivate() {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070061 flowEntries.clear();
tom85c612b2014-09-19 19:25:47 -070062 log.info("Stopped");
63 }
64
alshabiba68eb962014-09-24 20:34:13 -070065
tombe988312014-09-19 18:38:47 -070066 @Override
tom9b4030d2014-10-06 10:39:03 -070067 public int getFlowRuleCount() {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070068 int sum = 0;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070069 for (ConcurrentMap<FlowId, List<StoredFlowEntry>> ft : flowEntries.values()) {
70 for (List<StoredFlowEntry> fes : ft.values()) {
71 sum += fes.size();
72 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070073 }
74 return sum;
75 }
76
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070077 private static NewConcurrentHashMap<FlowId, List<StoredFlowEntry>> lazyEmptyFlowTable() {
78 return NewConcurrentHashMap.<FlowId, List<StoredFlowEntry>>ifNeeded();
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070079 }
80
81 /**
82 * Returns the flow table for specified device.
83 *
84 * @param deviceId identifier of the device
85 * @return Map representing Flow Table of given device.
86 */
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070087 private ConcurrentMap<FlowId, List<StoredFlowEntry>> getFlowTable(DeviceId deviceId) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070088 return createIfAbsentUnchecked(flowEntries,
89 deviceId, lazyEmptyFlowTable());
90 }
91
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070092 private List<StoredFlowEntry> getFlowEntries(DeviceId deviceId, FlowId flowId) {
93 final ConcurrentMap<FlowId, List<StoredFlowEntry>> flowTable = getFlowTable(deviceId);
94 List<StoredFlowEntry> r = flowTable.get(flowId);
95 if (r == null) {
96 final List<StoredFlowEntry> concurrentlyAdded;
97 r = new CopyOnWriteArrayList<>();
98 concurrentlyAdded = flowTable.putIfAbsent(flowId, r);
99 if (concurrentlyAdded != null) {
100 return concurrentlyAdded;
101 }
102 }
103 return r;
104 }
105
106 private FlowEntry getFlowEntryInternal(DeviceId deviceId, FlowRule rule) {
107 List<StoredFlowEntry> fes = getFlowEntries(deviceId, rule.id());
108 for (StoredFlowEntry fe : fes) {
109 if (fe.equals(rule)) {
110 return fe;
111 }
112 }
113 return null;
tom9b4030d2014-10-06 10:39:03 -0700114 }
115
116 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700117 public FlowEntry getFlowEntry(FlowRule rule) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700118 return getFlowEntryInternal(rule.deviceId(), rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700119 }
120
121 @Override
122 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700123 // flatten and make iterator unmodifiable
124 return FluentIterable.from(getFlowTable(deviceId).values())
125 .transformAndConcat(
126 new Function<List<StoredFlowEntry>, Iterable<? extends FlowEntry>>() {
127
128 @Override
129 public Iterable<? extends FlowEntry> apply(
130 List<StoredFlowEntry> input) {
131 return Collections.unmodifiableList(input);
132 }
133 });
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700134 }
135
136 @Override
137 public Iterable<FlowRule> getFlowRulesByAppId(ApplicationId appId) {
138
139 Set<FlowRule> rules = new HashSet<>();
140 for (DeviceId did : flowEntries.keySet()) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700141 for (FlowEntry fe : getFlowEntries(did)) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700142 if (fe.appId() == appId.id()) {
143 rules.add(fe);
144 }
alshabiba68eb962014-09-24 20:34:13 -0700145 }
146 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700147 return rules;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700148 }
149
tombe988312014-09-19 18:38:47 -0700150 @Override
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700151 public boolean storeFlowRule(FlowRule rule) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700152 final boolean added = storeFlowRuleInternal(rule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700153 return added;
alshabib219ebaa2014-09-22 15:41:24 -0700154 }
155
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700156 private boolean storeFlowRuleInternal(FlowRule rule) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700157 StoredFlowEntry f = new DefaultFlowEntry(rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700158 final DeviceId did = f.deviceId();
159 final FlowId fid = f.id();
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700160 List<StoredFlowEntry> existing = getFlowEntries(did, fid);
161 synchronized (existing) {
162 for (StoredFlowEntry fe : existing) {
163 if (fe.equals(rule)) {
164 // was already there? ignore
165 return false;
166 }
167 }
168 // new flow rule added
169 existing.add(f);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700170 // TODO: Should we notify only if it's "remote" event?
171 //notifyDelegate(new FlowRuleEvent(Type.RULE_ADD_REQUESTED, rule));
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700172 return true;
alshabiba68eb962014-09-24 20:34:13 -0700173 }
174 }
175
176 @Override
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700177 public boolean deleteFlowRule(FlowRule rule) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700178
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700179 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
180 synchronized (entries) {
181 for (StoredFlowEntry entry : entries) {
182 if (entry.equals(rule)) {
183 synchronized (entry) {
184 entry.setState(FlowEntryState.PENDING_REMOVE);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700185 // TODO: Should we notify only if it's "remote" event?
186 //notifyDelegate(new FlowRuleEvent(Type.RULE_REMOVE_REQUESTED, rule));
187 return true;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700188 }
189 }
190 }
alshabib219ebaa2014-09-22 15:41:24 -0700191 }
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700192 //log.warn("Cannot find rule {}", rule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700193 return false;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700194 }
195
tombe988312014-09-19 18:38:47 -0700196 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700197 public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700198 // check if this new rule is an update to an existing entry
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700199 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
200 synchronized (entries) {
201 for (StoredFlowEntry stored : entries) {
202 if (stored.equals(rule)) {
203 synchronized (stored) {
204 stored.setBytes(rule.bytes());
205 stored.setLife(rule.life());
206 stored.setPackets(rule.packets());
207 if (stored.state() == FlowEntryState.PENDING_ADD) {
208 stored.setState(FlowEntryState.ADDED);
209 // TODO: Do we need to change `rule` state?
210 return new FlowRuleEvent(Type.RULE_ADDED, rule);
211 }
212 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
213 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700214 }
alshabibba5ac482014-10-02 17:15:20 -0700215 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700216 }
alshabib219ebaa2014-09-22 15:41:24 -0700217
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700218 // should not reach here
219 // storeFlowRule was expected to be called
220 log.error("FlowRule was not found in store {} to update", rule);
221
alshabib8ca53902014-10-07 13:11:17 -0700222 //flowEntries.put(did, rule);
alshabibba5ac482014-10-02 17:15:20 -0700223 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700224 }
225
tombe988312014-09-19 18:38:47 -0700226 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700227 public FlowRuleEvent removeFlowRule(FlowEntry rule) {
alshabib1c319ff2014-10-04 20:29:09 -0700228 // This is where one could mark a rule as removed and still keep it in the store.
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700229 final DeviceId did = rule.deviceId();
230
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700231 List<StoredFlowEntry> entries = getFlowEntries(did, rule.id());
232 synchronized (entries) {
233 if (entries.remove(rule)) {
234 return new FlowRuleEvent(RULE_REMOVED, rule);
235 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700236 }
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700237 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700238 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700239}