blob: bed32a2dfed512a51cbeaea6c00ff27a411739e6 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Thomas Vachuskac97aa612015-06-23 16:00:18 -070016package org.onosproject.store.trivial;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070017
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080018import com.google.common.cache.Cache;
19import com.google.common.cache.CacheBuilder;
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -080020import com.google.common.cache.RemovalListener;
21import com.google.common.cache.RemovalNotification;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070022import com.google.common.collect.FluentIterable;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070023import com.google.common.collect.ImmutableList;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080024import com.google.common.collect.Sets;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080025import com.google.common.util.concurrent.SettableFuture;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070026
tom85c612b2014-09-19 19:25:47 -070027import org.apache.felix.scr.annotations.Activate;
28import org.apache.felix.scr.annotations.Component;
29import org.apache.felix.scr.annotations.Deactivate;
30import org.apache.felix.scr.annotations.Service;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080031import org.onlab.util.NewConcurrentHashMap;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.DeviceId;
33import org.onosproject.net.flow.CompletedBatchOperation;
34import org.onosproject.net.flow.DefaultFlowEntry;
35import org.onosproject.net.flow.FlowEntry;
36import org.onosproject.net.flow.FlowEntry.FlowEntryState;
37import org.onosproject.net.flow.FlowId;
38import org.onosproject.net.flow.FlowRule;
39import org.onosproject.net.flow.FlowRuleBatchEntry;
40import org.onosproject.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
41import org.onosproject.net.flow.FlowRuleBatchEvent;
42import org.onosproject.net.flow.FlowRuleBatchOperation;
43import org.onosproject.net.flow.FlowRuleBatchRequest;
44import org.onosproject.net.flow.FlowRuleEvent;
45import org.onosproject.net.flow.FlowRuleEvent.Type;
46import org.onosproject.net.flow.FlowRuleStore;
47import org.onosproject.net.flow.FlowRuleStoreDelegate;
48import org.onosproject.net.flow.StoredFlowEntry;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070049import org.onosproject.net.flow.TableStatisticsEntry;
Brian O'Connorabafb502014-12-02 22:26:20 -080050import org.onosproject.store.AbstractStore;
tom85c612b2014-09-19 19:25:47 -070051import org.slf4j.Logger;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070052
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080053import java.util.ArrayList;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070054import java.util.Collections;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070055import java.util.List;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070056import java.util.concurrent.ConcurrentHashMap;
57import java.util.concurrent.ConcurrentMap;
58import java.util.concurrent.CopyOnWriteArrayList;
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -080059import java.util.concurrent.ExecutionException;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080060import java.util.concurrent.TimeUnit;
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -080061import java.util.concurrent.TimeoutException;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080062import java.util.concurrent.atomic.AtomicInteger;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070063
64import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked;
Brian O'Connorabafb502014-12-02 22:26:20 -080065import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070066import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070067
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070068/**
69 * Manages inventory of flow rules using trivial in-memory implementation.
70 */
tom85c612b2014-09-19 19:25:47 -070071@Component(immediate = true)
72@Service
tomc78acee2014-09-24 15:16:55 -070073public class SimpleFlowRuleStore
Madan Jampani117aaae2014-10-23 10:04:05 -070074 extends AbstractStore<FlowRuleBatchEvent, FlowRuleStoreDelegate>
tomc78acee2014-09-24 15:16:55 -070075 implements FlowRuleStore {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070076
tom85c612b2014-09-19 19:25:47 -070077 private final Logger log = getLogger(getClass());
78
alshabiba68eb962014-09-24 20:34:13 -070079
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070080 // inner Map is Device flow table
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070081 // inner Map value (FlowId synonym list) must be synchronized before modifying
82 private final ConcurrentMap<DeviceId, ConcurrentMap<FlowId, List<StoredFlowEntry>>>
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070083 flowEntries = new ConcurrentHashMap<>();
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070084
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070085 private final ConcurrentMap<DeviceId, List<TableStatisticsEntry>>
86 deviceTableStats = new ConcurrentHashMap<>();
87
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080088 private final AtomicInteger localBatchIdGen = new AtomicInteger();
89
90 // TODO: make this configurable
91 private int pendingFutureTimeoutMinutes = 5;
92
93 private Cache<Integer, SettableFuture<CompletedBatchOperation>> pendingFutures =
94 CacheBuilder.newBuilder()
95 .expireAfterWrite(pendingFutureTimeoutMinutes, TimeUnit.MINUTES)
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -080096 .removalListener(new TimeoutFuture())
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080097 .build();
98
tom85c612b2014-09-19 19:25:47 -070099 @Activate
100 public void activate() {
101 log.info("Started");
102 }
103
104 @Deactivate
105 public void deactivate() {
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700106 deviceTableStats.clear();
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700107 flowEntries.clear();
tom85c612b2014-09-19 19:25:47 -0700108 log.info("Stopped");
109 }
110
alshabiba68eb962014-09-24 20:34:13 -0700111
tombe988312014-09-19 18:38:47 -0700112 @Override
tom9b4030d2014-10-06 10:39:03 -0700113 public int getFlowRuleCount() {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700114 int sum = 0;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700115 for (ConcurrentMap<FlowId, List<StoredFlowEntry>> ft : flowEntries.values()) {
116 for (List<StoredFlowEntry> fes : ft.values()) {
117 sum += fes.size();
118 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700119 }
120 return sum;
121 }
122
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700123 private static NewConcurrentHashMap<FlowId, List<StoredFlowEntry>> lazyEmptyFlowTable() {
124 return NewConcurrentHashMap.<FlowId, List<StoredFlowEntry>>ifNeeded();
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700125 }
126
127 /**
128 * Returns the flow table for specified device.
129 *
130 * @param deviceId identifier of the device
131 * @return Map representing Flow Table of given device.
132 */
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700133 private ConcurrentMap<FlowId, List<StoredFlowEntry>> getFlowTable(DeviceId deviceId) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700134 return createIfAbsentUnchecked(flowEntries,
135 deviceId, lazyEmptyFlowTable());
136 }
137
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700138 private List<StoredFlowEntry> getFlowEntries(DeviceId deviceId, FlowId flowId) {
139 final ConcurrentMap<FlowId, List<StoredFlowEntry>> flowTable = getFlowTable(deviceId);
140 List<StoredFlowEntry> r = flowTable.get(flowId);
141 if (r == null) {
142 final List<StoredFlowEntry> concurrentlyAdded;
143 r = new CopyOnWriteArrayList<>();
144 concurrentlyAdded = flowTable.putIfAbsent(flowId, r);
145 if (concurrentlyAdded != null) {
146 return concurrentlyAdded;
147 }
148 }
149 return r;
150 }
151
152 private FlowEntry getFlowEntryInternal(DeviceId deviceId, FlowRule rule) {
153 List<StoredFlowEntry> fes = getFlowEntries(deviceId, rule.id());
154 for (StoredFlowEntry fe : fes) {
155 if (fe.equals(rule)) {
156 return fe;
157 }
158 }
159 return null;
tom9b4030d2014-10-06 10:39:03 -0700160 }
161
162 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700163 public FlowEntry getFlowEntry(FlowRule rule) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700164 return getFlowEntryInternal(rule.deviceId(), rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700165 }
166
167 @Override
168 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700169 // flatten and make iterator unmodifiable
170 return FluentIterable.from(getFlowTable(deviceId).values())
Sho SHIMIZU74626412015-09-11 11:46:27 -0700171 .transformAndConcat(Collections::unmodifiableList);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700172 }
173
174 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700175 public void storeFlowRule(FlowRule rule) {
176 storeFlowRuleInternal(rule);
alshabib219ebaa2014-09-22 15:41:24 -0700177 }
178
Madan Jampani117aaae2014-10-23 10:04:05 -0700179 private void storeFlowRuleInternal(FlowRule rule) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700180 StoredFlowEntry f = new DefaultFlowEntry(rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700181 final DeviceId did = f.deviceId();
182 final FlowId fid = f.id();
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700183 List<StoredFlowEntry> existing = getFlowEntries(did, fid);
184 synchronized (existing) {
185 for (StoredFlowEntry fe : existing) {
186 if (fe.equals(rule)) {
187 // was already there? ignore
Madan Jampani117aaae2014-10-23 10:04:05 -0700188 return;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700189 }
190 }
191 // new flow rule added
192 existing.add(f);
alshabiba68eb962014-09-24 20:34:13 -0700193 }
194 }
195
196 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700197 public void deleteFlowRule(FlowRule rule) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700198
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700199 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
alshabib2374fc92014-10-22 11:03:23 -0700200
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700201 synchronized (entries) {
202 for (StoredFlowEntry entry : entries) {
203 if (entry.equals(rule)) {
204 synchronized (entry) {
205 entry.setState(FlowEntryState.PENDING_REMOVE);
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700206 }
207 }
208 }
alshabib219ebaa2014-09-22 15:41:24 -0700209 }
Madan Jampani31961c12014-10-23 12:06:58 -0700210
211
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700212 //log.warn("Cannot find rule {}", rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700213 }
214
tombe988312014-09-19 18:38:47 -0700215 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700216 public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700217 // check if this new rule is an update to an existing entry
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700218 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
219 synchronized (entries) {
220 for (StoredFlowEntry stored : entries) {
221 if (stored.equals(rule)) {
222 synchronized (stored) {
223 stored.setBytes(rule.bytes());
224 stored.setLife(rule.life());
225 stored.setPackets(rule.packets());
226 if (stored.state() == FlowEntryState.PENDING_ADD) {
227 stored.setState(FlowEntryState.ADDED);
228 // TODO: Do we need to change `rule` state?
229 return new FlowRuleEvent(Type.RULE_ADDED, rule);
230 }
231 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
232 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700233 }
alshabibba5ac482014-10-02 17:15:20 -0700234 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700235 }
alshabib219ebaa2014-09-22 15:41:24 -0700236
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700237 // should not reach here
238 // storeFlowRule was expected to be called
239 log.error("FlowRule was not found in store {} to update", rule);
240
alshabib8ca53902014-10-07 13:11:17 -0700241 //flowEntries.put(did, rule);
alshabibba5ac482014-10-02 17:15:20 -0700242 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700243 }
244
tombe988312014-09-19 18:38:47 -0700245 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700246 public FlowRuleEvent removeFlowRule(FlowEntry rule) {
alshabib1c319ff2014-10-04 20:29:09 -0700247 // This is where one could mark a rule as removed and still keep it in the store.
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700248 final DeviceId did = rule.deviceId();
249
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700250 List<StoredFlowEntry> entries = getFlowEntries(did, rule.id());
251 synchronized (entries) {
252 if (entries.remove(rule)) {
253 return new FlowRuleEvent(RULE_REMOVED, rule);
254 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700255 }
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700256 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700257 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700258
259 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800260 public void storeBatch(
261 FlowRuleBatchOperation operation) {
Brian O'Connor427a1762014-11-19 18:40:32 -0800262 List<FlowRuleBatchEntry> toAdd = new ArrayList<>();
263 List<FlowRuleBatchEntry> toRemove = new ArrayList<>();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800264
265 for (FlowRuleBatchEntry entry : operation.getOperations()) {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800266 final FlowRule flowRule = entry.target();
267 if (entry.operator().equals(FlowRuleOperation.ADD)) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800268 if (!getFlowEntries(flowRule.deviceId(), flowRule.id()).contains(flowRule)) {
269 storeFlowRule(flowRule);
Brian O'Connor427a1762014-11-19 18:40:32 -0800270 toAdd.add(entry);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800271 }
Sho SHIMIZUaba9d002015-01-29 14:51:04 -0800272 } else if (entry.operator().equals(FlowRuleOperation.REMOVE)) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800273 if (getFlowEntries(flowRule.deviceId(), flowRule.id()).contains(flowRule)) {
274 deleteFlowRule(flowRule);
Brian O'Connor427a1762014-11-19 18:40:32 -0800275 toRemove.add(entry);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800276 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700277 } else {
278 throw new UnsupportedOperationException("Unsupported operation type");
279 }
280 }
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800281
282 if (toAdd.isEmpty() && toRemove.isEmpty()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800283 notifyDelegate(FlowRuleBatchEvent.completed(
284 new FlowRuleBatchRequest(operation.id(), Collections.emptySet()),
285 new CompletedBatchOperation(true, Collections.emptySet(),
286 operation.deviceId())));
287 return;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800288 }
289
290 SettableFuture<CompletedBatchOperation> r = SettableFuture.create();
291 final int batchId = localBatchIdGen.incrementAndGet();
292
293 pendingFutures.put(batchId, r);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800294
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800295 toAdd.addAll(toRemove);
296 notifyDelegate(FlowRuleBatchEvent.requested(
297 new FlowRuleBatchRequest(batchId, Sets.newHashSet(toAdd)), operation.deviceId()));
298
Madan Jampani117aaae2014-10-23 10:04:05 -0700299 }
300
301 @Override
302 public void batchOperationComplete(FlowRuleBatchEvent event) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800303 final Long batchId = event.subject().batchId();
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800304 SettableFuture<CompletedBatchOperation> future
305 = pendingFutures.getIfPresent(batchId);
306 if (future != null) {
307 future.set(event.result());
308 pendingFutures.invalidate(batchId);
309 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700310 notifyDelegate(event);
311 }
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -0800312
313 private static final class TimeoutFuture
314 implements RemovalListener<Integer, SettableFuture<CompletedBatchOperation>> {
315 @Override
316 public void onRemoval(RemovalNotification<Integer, SettableFuture<CompletedBatchOperation>> notification) {
317 // wrapping in ExecutionException to support Future.get
Yuta HIGUCHIc611d922015-02-10 16:07:38 -0800318 if (notification.wasEvicted()) {
319 notification.getValue()
320 .setException(new ExecutionException("Timed out",
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -0800321 new TimeoutException()));
Yuta HIGUCHIc611d922015-02-10 16:07:38 -0800322 }
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -0800323 }
324 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700325
326 @Override
327 public FlowRuleEvent updateTableStatistics(DeviceId deviceId,
328 List<TableStatisticsEntry> tableStats) {
329 deviceTableStats.put(deviceId, tableStats);
330 return null;
331 }
332
333 @Override
334 public Iterable<TableStatisticsEntry> getTableStatistics(DeviceId deviceId) {
335 List<TableStatisticsEntry> tableStats = deviceTableStats.get(deviceId);
336 if (tableStats == null) {
337 return Collections.emptyList();
338 }
339 return ImmutableList.copyOf(tableStats);
340 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700341}