blob: b1eaae61f05cb2f57a73836656ee597f60bb8939 [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;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.DeviceId;
29import org.onosproject.net.flow.CompletedBatchOperation;
30import org.onosproject.net.flow.DefaultFlowEntry;
31import org.onosproject.net.flow.FlowEntry;
32import org.onosproject.net.flow.FlowEntry.FlowEntryState;
33import org.onosproject.net.flow.FlowId;
34import org.onosproject.net.flow.FlowRule;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.flow.FlowRuleEvent;
36import org.onosproject.net.flow.FlowRuleEvent.Type;
37import org.onosproject.net.flow.FlowRuleStore;
38import org.onosproject.net.flow.FlowRuleStoreDelegate;
39import org.onosproject.net.flow.StoredFlowEntry;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070040import org.onosproject.net.flow.TableStatisticsEntry;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070041import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry;
42import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry.FlowRuleOperation;
43import org.onosproject.net.flow.oldbatch.FlowRuleBatchEvent;
44import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation;
45import org.onosproject.net.flow.oldbatch.FlowRuleBatchRequest;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import org.onosproject.store.AbstractStore;
sangyun-hanc1806862016-04-05 11:42:03 +090047import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070048import org.osgi.service.component.annotations.Activate;
49import org.osgi.service.component.annotations.Component;
50import org.osgi.service.component.annotations.Deactivate;
51import org.osgi.service.component.annotations.Modified;
tom85c612b2014-09-19 19:25:47 -070052import org.slf4j.Logger;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070053
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080054import java.util.ArrayList;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070055import java.util.Collections;
sangyun-hanc1806862016-04-05 11:42:03 +090056import java.util.Dictionary;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070057import java.util.List;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070058import java.util.concurrent.ConcurrentHashMap;
59import java.util.concurrent.ConcurrentMap;
60import java.util.concurrent.CopyOnWriteArrayList;
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -080061import java.util.concurrent.ExecutionException;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080062import java.util.concurrent.TimeUnit;
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -080063import java.util.concurrent.TimeoutException;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080064import java.util.concurrent.atomic.AtomicInteger;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070065
Brian O'Connorabafb502014-12-02 22:26:20 -080066import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070067import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070068
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070069/**
70 * Manages inventory of flow rules using trivial in-memory implementation.
71 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070072@Component(immediate = true, service = FlowRuleStore.class)
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
sangyun-hanc1806862016-04-05 11:42:03 +090090 private static final int DEFAULT_PENDING_FUTURE_TIMEOUT_MINUTES = 5;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070091 //@Property(name = "pendingFutureTimeoutMinutes", intValue = DEFAULT_PENDING_FUTURE_TIMEOUT_MINUTES,
92 // label = "Expiration time after an entry is created that it should be automatically removed")
sangyun-hanc1806862016-04-05 11:42:03 +090093 private int pendingFutureTimeoutMinutes = DEFAULT_PENDING_FUTURE_TIMEOUT_MINUTES;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080094
95 private Cache<Integer, SettableFuture<CompletedBatchOperation>> pendingFutures =
96 CacheBuilder.newBuilder()
97 .expireAfterWrite(pendingFutureTimeoutMinutes, TimeUnit.MINUTES)
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -080098 .removalListener(new TimeoutFuture())
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080099 .build();
100
tom85c612b2014-09-19 19:25:47 -0700101 @Activate
102 public void activate() {
103 log.info("Started");
104 }
105
106 @Deactivate
107 public void deactivate() {
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700108 deviceTableStats.clear();
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700109 flowEntries.clear();
tom85c612b2014-09-19 19:25:47 -0700110 log.info("Stopped");
111 }
112
sangyun-hanc1806862016-04-05 11:42:03 +0900113 @Modified
114 public void modified(ComponentContext context) {
115
116 readComponentConfiguration(context);
117
118 // Reset Cache and copy all.
119 Cache<Integer, SettableFuture<CompletedBatchOperation>> prevFutures = pendingFutures;
120 pendingFutures = CacheBuilder.newBuilder()
121 .expireAfterWrite(pendingFutureTimeoutMinutes, TimeUnit.MINUTES)
122 .removalListener(new TimeoutFuture())
123 .build();
124
125 pendingFutures.putAll(prevFutures.asMap());
126 }
127
alshabiba68eb962014-09-24 20:34:13 -0700128
tombe988312014-09-19 18:38:47 -0700129 @Override
tom9b4030d2014-10-06 10:39:03 -0700130 public int getFlowRuleCount() {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700131 int sum = 0;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700132 for (ConcurrentMap<FlowId, List<StoredFlowEntry>> ft : flowEntries.values()) {
133 for (List<StoredFlowEntry> fes : ft.values()) {
134 sum += fes.size();
135 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700136 }
137 return sum;
138 }
139
sangyun-hanc1806862016-04-05 11:42:03 +0900140 /**
141 * Extracts properties from the component configuration context.
142 *
143 * @param context the component context
144 */
145 private void readComponentConfiguration(ComponentContext context) {
146 Dictionary<?, ?> properties = context.getProperties();
147
148 Integer newPendingFutureTimeoutMinutes =
149 Tools.getIntegerProperty(properties, "pendingFutureTimeoutMinutes");
150 if (newPendingFutureTimeoutMinutes == null) {
151 pendingFutureTimeoutMinutes = DEFAULT_PENDING_FUTURE_TIMEOUT_MINUTES;
152 log.info("Pending future timeout is not configured, " +
153 "using current value of {}", pendingFutureTimeoutMinutes);
154 } else {
155 pendingFutureTimeoutMinutes = newPendingFutureTimeoutMinutes;
156 log.info("Configured. Pending future timeout is configured to {}",
157 pendingFutureTimeoutMinutes);
158 }
159 }
160
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700161 /**
162 * Returns the flow table for specified device.
163 *
164 * @param deviceId identifier of the device
165 * @return Map representing Flow Table of given device.
166 */
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700167 private ConcurrentMap<FlowId, List<StoredFlowEntry>> getFlowTable(DeviceId deviceId) {
Yuta HIGUCHIc2e68152016-08-16 13:47:36 -0700168 return flowEntries.computeIfAbsent(deviceId, k -> new ConcurrentHashMap<>());
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700169 }
170
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700171 private List<StoredFlowEntry> getFlowEntries(DeviceId deviceId, FlowId flowId) {
172 final ConcurrentMap<FlowId, List<StoredFlowEntry>> flowTable = getFlowTable(deviceId);
173 List<StoredFlowEntry> r = flowTable.get(flowId);
174 if (r == null) {
175 final List<StoredFlowEntry> concurrentlyAdded;
176 r = new CopyOnWriteArrayList<>();
177 concurrentlyAdded = flowTable.putIfAbsent(flowId, r);
178 if (concurrentlyAdded != null) {
179 return concurrentlyAdded;
180 }
181 }
182 return r;
183 }
184
185 private FlowEntry getFlowEntryInternal(DeviceId deviceId, FlowRule rule) {
186 List<StoredFlowEntry> fes = getFlowEntries(deviceId, rule.id());
187 for (StoredFlowEntry fe : fes) {
188 if (fe.equals(rule)) {
189 return fe;
190 }
191 }
192 return null;
tom9b4030d2014-10-06 10:39:03 -0700193 }
194
195 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700196 public FlowEntry getFlowEntry(FlowRule rule) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700197 return getFlowEntryInternal(rule.deviceId(), rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700198 }
199
200 @Override
201 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700202 // flatten and make iterator unmodifiable
203 return FluentIterable.from(getFlowTable(deviceId).values())
Sho SHIMIZU74626412015-09-11 11:46:27 -0700204 .transformAndConcat(Collections::unmodifiableList);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700205 }
206
207 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700208 public void storeFlowRule(FlowRule rule) {
209 storeFlowRuleInternal(rule);
alshabib219ebaa2014-09-22 15:41:24 -0700210 }
211
Madan Jampani117aaae2014-10-23 10:04:05 -0700212 private void storeFlowRuleInternal(FlowRule rule) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700213 StoredFlowEntry f = new DefaultFlowEntry(rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700214 final DeviceId did = f.deviceId();
215 final FlowId fid = f.id();
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700216 List<StoredFlowEntry> existing = getFlowEntries(did, fid);
217 synchronized (existing) {
218 for (StoredFlowEntry fe : existing) {
219 if (fe.equals(rule)) {
220 // was already there? ignore
Madan Jampani117aaae2014-10-23 10:04:05 -0700221 return;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700222 }
223 }
224 // new flow rule added
225 existing.add(f);
alshabiba68eb962014-09-24 20:34:13 -0700226 }
227 }
228
229 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700230 public void deleteFlowRule(FlowRule rule) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700231
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700232 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
alshabib2374fc92014-10-22 11:03:23 -0700233
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700234 synchronized (entries) {
235 for (StoredFlowEntry entry : entries) {
236 if (entry.equals(rule)) {
237 synchronized (entry) {
238 entry.setState(FlowEntryState.PENDING_REMOVE);
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700239 }
240 }
241 }
alshabib219ebaa2014-09-22 15:41:24 -0700242 }
Madan Jampani31961c12014-10-23 12:06:58 -0700243
244
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700245 //log.warn("Cannot find rule {}", rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700246 }
247
tombe988312014-09-19 18:38:47 -0700248 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700249 public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700250 // check if this new rule is an update to an existing entry
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700251 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
252 synchronized (entries) {
253 for (StoredFlowEntry stored : entries) {
254 if (stored.equals(rule)) {
255 synchronized (stored) {
Brian O'Connora3e5cd52015-12-05 15:59:19 -0800256 //FIXME modification of "stored" flow entry outside of flow table
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700257 stored.setBytes(rule.bytes());
258 stored.setLife(rule.life());
259 stored.setPackets(rule.packets());
260 if (stored.state() == FlowEntryState.PENDING_ADD) {
261 stored.setState(FlowEntryState.ADDED);
262 // TODO: Do we need to change `rule` state?
263 return new FlowRuleEvent(Type.RULE_ADDED, rule);
264 }
265 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
266 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700267 }
alshabibba5ac482014-10-02 17:15:20 -0700268 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700269 }
alshabib219ebaa2014-09-22 15:41:24 -0700270
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700271 // should not reach here
272 // storeFlowRule was expected to be called
273 log.error("FlowRule was not found in store {} to update", rule);
274
alshabib8ca53902014-10-07 13:11:17 -0700275 //flowEntries.put(did, rule);
alshabibba5ac482014-10-02 17:15:20 -0700276 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700277 }
278
tombe988312014-09-19 18:38:47 -0700279 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700280 public FlowRuleEvent removeFlowRule(FlowEntry rule) {
alshabib1c319ff2014-10-04 20:29:09 -0700281 // This is where one could mark a rule as removed and still keep it in the store.
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700282 final DeviceId did = rule.deviceId();
283
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700284 List<StoredFlowEntry> entries = getFlowEntries(did, rule.id());
285 synchronized (entries) {
286 if (entries.remove(rule)) {
287 return new FlowRuleEvent(RULE_REMOVED, rule);
288 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700289 }
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700290 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700291 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700292
293 @Override
Charles Chan93fa7272016-01-26 22:27:02 -0800294 public FlowRuleEvent pendingFlowRule(FlowEntry rule) {
295 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
296 synchronized (entries) {
297 for (StoredFlowEntry entry : entries) {
298 if (entry.equals(rule) &&
299 entry.state() != FlowEntryState.PENDING_ADD) {
300 synchronized (entry) {
301 entry.setState(FlowEntryState.PENDING_ADD);
302 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
303 }
304 }
305 }
306 }
307 return null;
308 }
309
Yuta HIGUCHIc2e68152016-08-16 13:47:36 -0700310 @Override
Charles Chan0c7c43b2016-01-14 17:39:20 -0800311 public void purgeFlowRule(DeviceId deviceId) {
312 flowEntries.remove(deviceId);
313 }
314
Charles Chan93fa7272016-01-26 22:27:02 -0800315 @Override
Victor Silva139bca42016-08-18 11:46:35 -0300316 public void purgeFlowRules() {
317 flowEntries.clear();
318 }
319
320 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800321 public void storeBatch(
322 FlowRuleBatchOperation operation) {
Brian O'Connor427a1762014-11-19 18:40:32 -0800323 List<FlowRuleBatchEntry> toAdd = new ArrayList<>();
324 List<FlowRuleBatchEntry> toRemove = new ArrayList<>();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800325
326 for (FlowRuleBatchEntry entry : operation.getOperations()) {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800327 final FlowRule flowRule = entry.target();
328 if (entry.operator().equals(FlowRuleOperation.ADD)) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800329 if (!getFlowEntries(flowRule.deviceId(), flowRule.id()).contains(flowRule)) {
330 storeFlowRule(flowRule);
Brian O'Connor427a1762014-11-19 18:40:32 -0800331 toAdd.add(entry);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800332 }
Sho SHIMIZUaba9d002015-01-29 14:51:04 -0800333 } else if (entry.operator().equals(FlowRuleOperation.REMOVE)) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800334 if (getFlowEntries(flowRule.deviceId(), flowRule.id()).contains(flowRule)) {
335 deleteFlowRule(flowRule);
Brian O'Connor427a1762014-11-19 18:40:32 -0800336 toRemove.add(entry);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800337 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700338 } else {
339 throw new UnsupportedOperationException("Unsupported operation type");
340 }
341 }
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800342
343 if (toAdd.isEmpty() && toRemove.isEmpty()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800344 notifyDelegate(FlowRuleBatchEvent.completed(
345 new FlowRuleBatchRequest(operation.id(), Collections.emptySet()),
346 new CompletedBatchOperation(true, Collections.emptySet(),
347 operation.deviceId())));
348 return;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800349 }
350
351 SettableFuture<CompletedBatchOperation> r = SettableFuture.create();
352 final int batchId = localBatchIdGen.incrementAndGet();
353
354 pendingFutures.put(batchId, r);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800355
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800356 toAdd.addAll(toRemove);
357 notifyDelegate(FlowRuleBatchEvent.requested(
358 new FlowRuleBatchRequest(batchId, Sets.newHashSet(toAdd)), operation.deviceId()));
359
Madan Jampani117aaae2014-10-23 10:04:05 -0700360 }
361
362 @Override
363 public void batchOperationComplete(FlowRuleBatchEvent event) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800364 final Long batchId = event.subject().batchId();
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800365 SettableFuture<CompletedBatchOperation> future
366 = pendingFutures.getIfPresent(batchId);
367 if (future != null) {
368 future.set(event.result());
369 pendingFutures.invalidate(batchId);
370 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700371 notifyDelegate(event);
372 }
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -0800373
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700374 @Override
375 public FlowRuleEvent updateTableStatistics(DeviceId deviceId,
376 List<TableStatisticsEntry> tableStats) {
377 deviceTableStats.put(deviceId, tableStats);
378 return null;
379 }
380
381 @Override
382 public Iterable<TableStatisticsEntry> getTableStatistics(DeviceId deviceId) {
383 List<TableStatisticsEntry> tableStats = deviceTableStats.get(deviceId);
384 if (tableStats == null) {
385 return Collections.emptyList();
386 }
387 return ImmutableList.copyOf(tableStats);
388 }
sangyun-hanc1806862016-04-05 11:42:03 +0900389
Patryk Konopka7e40c012017-06-06 13:38:06 +0200390 @Override
391 public long getActiveFlowRuleCount(DeviceId deviceId) {
392 return Streams.stream(getTableStatistics(deviceId))
393 .mapToLong(TableStatisticsEntry::activeFlowEntries)
394 .sum();
395 }
396
sangyun-hanc1806862016-04-05 11:42:03 +0900397 private static final class TimeoutFuture
398 implements RemovalListener<Integer, SettableFuture<CompletedBatchOperation>> {
399 @Override
400 public void onRemoval(RemovalNotification<Integer, SettableFuture<CompletedBatchOperation>> notification) {
401 // wrapping in ExecutionException to support Future.get
402 if (notification.wasEvicted()) {
403 notification.getValue()
404 .setException(new ExecutionException("Timed out",
405 new TimeoutException()));
406 }
407 }
408 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700409}