blob: 3b667a89e6a4e6208800a86eba22098e5714eed2 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Patryk Konopka7e40c012017-06-06 13:38:06 +020025import com.google.common.collect.Streams;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080026import com.google.common.util.concurrent.SettableFuture;
sangyun-hanc1806862016-04-05 11:42:03 +090027import org.onlab.util.Tools;
Daniele Moro43ac2892021-07-15 17:02:59 +020028import org.onosproject.core.ApplicationId;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.DeviceId;
30import org.onosproject.net.flow.CompletedBatchOperation;
31import org.onosproject.net.flow.DefaultFlowEntry;
32import org.onosproject.net.flow.FlowEntry;
33import org.onosproject.net.flow.FlowEntry.FlowEntryState;
34import org.onosproject.net.flow.FlowId;
35import org.onosproject.net.flow.FlowRule;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.flow.FlowRuleEvent;
37import org.onosproject.net.flow.FlowRuleEvent.Type;
38import org.onosproject.net.flow.FlowRuleStore;
39import org.onosproject.net.flow.FlowRuleStoreDelegate;
40import org.onosproject.net.flow.StoredFlowEntry;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070041import org.onosproject.net.flow.TableStatisticsEntry;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070042import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry;
43import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry.FlowRuleOperation;
44import org.onosproject.net.flow.oldbatch.FlowRuleBatchEvent;
45import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation;
46import org.onosproject.net.flow.oldbatch.FlowRuleBatchRequest;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import org.onosproject.store.AbstractStore;
sangyun-hanc1806862016-04-05 11:42:03 +090048import org.osgi.service.component.ComponentContext;
tom85c612b2014-09-19 19:25:47 -070049import org.slf4j.Logger;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070050
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080051import java.util.ArrayList;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070052import java.util.Collections;
sangyun-hanc1806862016-04-05 11:42:03 +090053import java.util.Dictionary;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070054import java.util.List;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070055import java.util.concurrent.ConcurrentHashMap;
56import java.util.concurrent.ConcurrentMap;
57import java.util.concurrent.CopyOnWriteArrayList;
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -080058import java.util.concurrent.ExecutionException;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080059import java.util.concurrent.TimeUnit;
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -080060import java.util.concurrent.TimeoutException;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080061import java.util.concurrent.atomic.AtomicInteger;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070062
Brian O'Connorabafb502014-12-02 22:26:20 -080063import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070064import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070065
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070066/**
67 * Manages inventory of flow rules using trivial in-memory implementation.
68 */
tomc78acee2014-09-24 15:16:55 -070069public class SimpleFlowRuleStore
Madan Jampani117aaae2014-10-23 10:04:05 -070070 extends AbstractStore<FlowRuleBatchEvent, FlowRuleStoreDelegate>
tomc78acee2014-09-24 15:16:55 -070071 implements FlowRuleStore {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070072
tom85c612b2014-09-19 19:25:47 -070073 private final Logger log = getLogger(getClass());
74
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070075 // inner Map is Device flow table
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070076 // inner Map value (FlowId synonym list) must be synchronized before modifying
77 private final ConcurrentMap<DeviceId, ConcurrentMap<FlowId, List<StoredFlowEntry>>>
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070078 flowEntries = new ConcurrentHashMap<>();
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070079
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070080 private final ConcurrentMap<DeviceId, List<TableStatisticsEntry>>
81 deviceTableStats = new ConcurrentHashMap<>();
82
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080083 private final AtomicInteger localBatchIdGen = new AtomicInteger();
84
sangyun-hanc1806862016-04-05 11:42:03 +090085 private static final int DEFAULT_PENDING_FUTURE_TIMEOUT_MINUTES = 5;
sangyun-hanc1806862016-04-05 11:42:03 +090086 private int pendingFutureTimeoutMinutes = DEFAULT_PENDING_FUTURE_TIMEOUT_MINUTES;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080087
88 private Cache<Integer, SettableFuture<CompletedBatchOperation>> pendingFutures =
89 CacheBuilder.newBuilder()
90 .expireAfterWrite(pendingFutureTimeoutMinutes, TimeUnit.MINUTES)
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -080091 .removalListener(new TimeoutFuture())
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080092 .build();
93
tom85c612b2014-09-19 19:25:47 -070094 public void activate() {
95 log.info("Started");
96 }
97
tom85c612b2014-09-19 19:25:47 -070098 public void deactivate() {
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070099 deviceTableStats.clear();
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700100 flowEntries.clear();
tom85c612b2014-09-19 19:25:47 -0700101 log.info("Stopped");
102 }
103
sangyun-hanc1806862016-04-05 11:42:03 +0900104 public void modified(ComponentContext context) {
sangyun-hanc1806862016-04-05 11:42:03 +0900105 readComponentConfiguration(context);
106
107 // Reset Cache and copy all.
108 Cache<Integer, SettableFuture<CompletedBatchOperation>> prevFutures = pendingFutures;
109 pendingFutures = CacheBuilder.newBuilder()
110 .expireAfterWrite(pendingFutureTimeoutMinutes, TimeUnit.MINUTES)
111 .removalListener(new TimeoutFuture())
112 .build();
113
114 pendingFutures.putAll(prevFutures.asMap());
115 }
116
alshabiba68eb962014-09-24 20:34:13 -0700117
tombe988312014-09-19 18:38:47 -0700118 @Override
tom9b4030d2014-10-06 10:39:03 -0700119 public int getFlowRuleCount() {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700120 int sum = 0;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700121 for (ConcurrentMap<FlowId, List<StoredFlowEntry>> ft : flowEntries.values()) {
122 for (List<StoredFlowEntry> fes : ft.values()) {
123 sum += fes.size();
124 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700125 }
126 return sum;
127 }
128
sangyun-hanc1806862016-04-05 11:42:03 +0900129 /**
130 * Extracts properties from the component configuration context.
131 *
132 * @param context the component context
133 */
134 private void readComponentConfiguration(ComponentContext context) {
135 Dictionary<?, ?> properties = context.getProperties();
136
137 Integer newPendingFutureTimeoutMinutes =
138 Tools.getIntegerProperty(properties, "pendingFutureTimeoutMinutes");
139 if (newPendingFutureTimeoutMinutes == null) {
140 pendingFutureTimeoutMinutes = DEFAULT_PENDING_FUTURE_TIMEOUT_MINUTES;
141 log.info("Pending future timeout is not configured, " +
142 "using current value of {}", pendingFutureTimeoutMinutes);
143 } else {
144 pendingFutureTimeoutMinutes = newPendingFutureTimeoutMinutes;
145 log.info("Configured. Pending future timeout is configured to {}",
146 pendingFutureTimeoutMinutes);
147 }
148 }
149
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700150 /**
151 * Returns the flow table for specified device.
152 *
153 * @param deviceId identifier of the device
154 * @return Map representing Flow Table of given device.
155 */
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700156 private ConcurrentMap<FlowId, List<StoredFlowEntry>> getFlowTable(DeviceId deviceId) {
Yuta HIGUCHIc2e68152016-08-16 13:47:36 -0700157 return flowEntries.computeIfAbsent(deviceId, k -> new ConcurrentHashMap<>());
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700158 }
159
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700160 private List<StoredFlowEntry> getFlowEntries(DeviceId deviceId, FlowId flowId) {
161 final ConcurrentMap<FlowId, List<StoredFlowEntry>> flowTable = getFlowTable(deviceId);
162 List<StoredFlowEntry> r = flowTable.get(flowId);
163 if (r == null) {
164 final List<StoredFlowEntry> concurrentlyAdded;
165 r = new CopyOnWriteArrayList<>();
166 concurrentlyAdded = flowTable.putIfAbsent(flowId, r);
167 if (concurrentlyAdded != null) {
168 return concurrentlyAdded;
169 }
170 }
171 return r;
172 }
173
174 private FlowEntry getFlowEntryInternal(DeviceId deviceId, FlowRule rule) {
175 List<StoredFlowEntry> fes = getFlowEntries(deviceId, rule.id());
176 for (StoredFlowEntry fe : fes) {
177 if (fe.equals(rule)) {
178 return fe;
179 }
180 }
181 return null;
tom9b4030d2014-10-06 10:39:03 -0700182 }
183
184 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700185 public FlowEntry getFlowEntry(FlowRule rule) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700186 return getFlowEntryInternal(rule.deviceId(), rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700187 }
188
189 @Override
190 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700191 // flatten and make iterator unmodifiable
192 return FluentIterable.from(getFlowTable(deviceId).values())
Sho SHIMIZU74626412015-09-11 11:46:27 -0700193 .transformAndConcat(Collections::unmodifiableList);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700194 }
195
196 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700197 public void storeFlowRule(FlowRule rule) {
198 storeFlowRuleInternal(rule);
alshabib219ebaa2014-09-22 15:41:24 -0700199 }
200
Madan Jampani117aaae2014-10-23 10:04:05 -0700201 private void storeFlowRuleInternal(FlowRule rule) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700202 StoredFlowEntry f = new DefaultFlowEntry(rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700203 final DeviceId did = f.deviceId();
204 final FlowId fid = f.id();
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700205 List<StoredFlowEntry> existing = getFlowEntries(did, fid);
206 synchronized (existing) {
207 for (StoredFlowEntry fe : existing) {
208 if (fe.equals(rule)) {
209 // was already there? ignore
Madan Jampani117aaae2014-10-23 10:04:05 -0700210 return;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700211 }
212 }
213 // new flow rule added
214 existing.add(f);
alshabiba68eb962014-09-24 20:34:13 -0700215 }
216 }
217
218 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700219 public void deleteFlowRule(FlowRule rule) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700220
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700221 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
alshabib2374fc92014-10-22 11:03:23 -0700222
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700223 synchronized (entries) {
224 for (StoredFlowEntry entry : entries) {
225 if (entry.equals(rule)) {
226 synchronized (entry) {
227 entry.setState(FlowEntryState.PENDING_REMOVE);
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700228 }
229 }
230 }
alshabib219ebaa2014-09-22 15:41:24 -0700231 }
Madan Jampani31961c12014-10-23 12:06:58 -0700232
233
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700234 //log.warn("Cannot find rule {}", rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700235 }
236
tombe988312014-09-19 18:38:47 -0700237 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700238 public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700239 // check if this new rule is an update to an existing entry
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700240 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
241 synchronized (entries) {
242 for (StoredFlowEntry stored : entries) {
243 if (stored.equals(rule)) {
244 synchronized (stored) {
Brian O'Connora3e5cd52015-12-05 15:59:19 -0800245 //FIXME modification of "stored" flow entry outside of flow table
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700246 stored.setBytes(rule.bytes());
247 stored.setLife(rule.life());
248 stored.setPackets(rule.packets());
249 if (stored.state() == FlowEntryState.PENDING_ADD) {
250 stored.setState(FlowEntryState.ADDED);
251 // TODO: Do we need to change `rule` state?
252 return new FlowRuleEvent(Type.RULE_ADDED, rule);
253 }
254 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
255 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700256 }
alshabibba5ac482014-10-02 17:15:20 -0700257 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700258 }
alshabib219ebaa2014-09-22 15:41:24 -0700259
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700260 // should not reach here
261 // storeFlowRule was expected to be called
262 log.error("FlowRule was not found in store {} to update", rule);
263
alshabib8ca53902014-10-07 13:11:17 -0700264 //flowEntries.put(did, rule);
alshabibba5ac482014-10-02 17:15:20 -0700265 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700266 }
267
tombe988312014-09-19 18:38:47 -0700268 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700269 public FlowRuleEvent removeFlowRule(FlowEntry rule) {
alshabib1c319ff2014-10-04 20:29:09 -0700270 // This is where one could mark a rule as removed and still keep it in the store.
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700271 final DeviceId did = rule.deviceId();
272
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700273 List<StoredFlowEntry> entries = getFlowEntries(did, rule.id());
274 synchronized (entries) {
275 if (entries.remove(rule)) {
276 return new FlowRuleEvent(RULE_REMOVED, rule);
277 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700278 }
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700279 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700280 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700281
282 @Override
Charles Chan93fa7272016-01-26 22:27:02 -0800283 public FlowRuleEvent pendingFlowRule(FlowEntry rule) {
284 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
285 synchronized (entries) {
286 for (StoredFlowEntry entry : entries) {
287 if (entry.equals(rule) &&
288 entry.state() != FlowEntryState.PENDING_ADD) {
289 synchronized (entry) {
290 entry.setState(FlowEntryState.PENDING_ADD);
291 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
292 }
293 }
294 }
295 }
296 return null;
297 }
298
Yuta HIGUCHIc2e68152016-08-16 13:47:36 -0700299 @Override
Charles Chan0c7c43b2016-01-14 17:39:20 -0800300 public void purgeFlowRule(DeviceId deviceId) {
301 flowEntries.remove(deviceId);
302 }
303
Charles Chan93fa7272016-01-26 22:27:02 -0800304 @Override
Daniele Moro43ac2892021-07-15 17:02:59 +0200305 public boolean purgeFlowRules(DeviceId deviceId, ApplicationId appId) {
306 flowEntries.get(deviceId).values()
307 .removeIf(storedFlowEntries -> {
308 storedFlowEntries.removeIf(storedFlowEntry -> storedFlowEntry.appId() == appId.id());
309 return storedFlowEntries.isEmpty();
310 });
311 return true;
312 }
313
314 @Override
Victor Silva139bca42016-08-18 11:46:35 -0300315 public void purgeFlowRules() {
316 flowEntries.clear();
317 }
318
319 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800320 public void storeBatch(
321 FlowRuleBatchOperation operation) {
Brian O'Connor427a1762014-11-19 18:40:32 -0800322 List<FlowRuleBatchEntry> toAdd = new ArrayList<>();
323 List<FlowRuleBatchEntry> toRemove = new ArrayList<>();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800324
325 for (FlowRuleBatchEntry entry : operation.getOperations()) {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800326 final FlowRule flowRule = entry.target();
327 if (entry.operator().equals(FlowRuleOperation.ADD)) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800328 if (!getFlowEntries(flowRule.deviceId(), flowRule.id()).contains(flowRule)) {
329 storeFlowRule(flowRule);
Brian O'Connor427a1762014-11-19 18:40:32 -0800330 toAdd.add(entry);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800331 }
Sho SHIMIZUaba9d002015-01-29 14:51:04 -0800332 } else if (entry.operator().equals(FlowRuleOperation.REMOVE)) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800333 if (getFlowEntries(flowRule.deviceId(), flowRule.id()).contains(flowRule)) {
334 deleteFlowRule(flowRule);
Brian O'Connor427a1762014-11-19 18:40:32 -0800335 toRemove.add(entry);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800336 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700337 } else {
338 throw new UnsupportedOperationException("Unsupported operation type");
339 }
340 }
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800341
342 if (toAdd.isEmpty() && toRemove.isEmpty()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800343 notifyDelegate(FlowRuleBatchEvent.completed(
344 new FlowRuleBatchRequest(operation.id(), Collections.emptySet()),
345 new CompletedBatchOperation(true, Collections.emptySet(),
346 operation.deviceId())));
347 return;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800348 }
349
350 SettableFuture<CompletedBatchOperation> r = SettableFuture.create();
351 final int batchId = localBatchIdGen.incrementAndGet();
352
353 pendingFutures.put(batchId, r);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800354
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800355 toAdd.addAll(toRemove);
356 notifyDelegate(FlowRuleBatchEvent.requested(
357 new FlowRuleBatchRequest(batchId, Sets.newHashSet(toAdd)), operation.deviceId()));
358
Madan Jampani117aaae2014-10-23 10:04:05 -0700359 }
360
361 @Override
362 public void batchOperationComplete(FlowRuleBatchEvent event) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800363 final Long batchId = event.subject().batchId();
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800364 SettableFuture<CompletedBatchOperation> future
365 = pendingFutures.getIfPresent(batchId);
366 if (future != null) {
367 future.set(event.result());
368 pendingFutures.invalidate(batchId);
369 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700370 notifyDelegate(event);
371 }
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -0800372
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700373 @Override
374 public FlowRuleEvent updateTableStatistics(DeviceId deviceId,
375 List<TableStatisticsEntry> tableStats) {
376 deviceTableStats.put(deviceId, tableStats);
377 return null;
378 }
379
380 @Override
381 public Iterable<TableStatisticsEntry> getTableStatistics(DeviceId deviceId) {
382 List<TableStatisticsEntry> tableStats = deviceTableStats.get(deviceId);
383 if (tableStats == null) {
384 return Collections.emptyList();
385 }
386 return ImmutableList.copyOf(tableStats);
387 }
sangyun-hanc1806862016-04-05 11:42:03 +0900388
Patryk Konopka7e40c012017-06-06 13:38:06 +0200389 @Override
390 public long getActiveFlowRuleCount(DeviceId deviceId) {
391 return Streams.stream(getTableStatistics(deviceId))
392 .mapToLong(TableStatisticsEntry::activeFlowEntries)
393 .sum();
394 }
395
sangyun-hanc1806862016-04-05 11:42:03 +0900396 private static final class TimeoutFuture
397 implements RemovalListener<Integer, SettableFuture<CompletedBatchOperation>> {
398 @Override
399 public void onRemoval(RemovalNotification<Integer, SettableFuture<CompletedBatchOperation>> notification) {
400 // wrapping in ExecutionException to support Future.get
401 if (notification.wasEvicted()) {
402 notification.getValue()
403 .setException(new ExecutionException("Timed out",
404 new TimeoutException()));
405 }
406 }
407 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700408}