blob: a51c4ad610097aef92198cf285335e230687753e [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present 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;
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;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070027
tom85c612b2014-09-19 19:25:47 -070028import org.apache.felix.scr.annotations.Activate;
29import org.apache.felix.scr.annotations.Component;
30import org.apache.felix.scr.annotations.Deactivate;
sangyun-hanc1806862016-04-05 11:42:03 +090031import org.apache.felix.scr.annotations.Modified;
32import org.apache.felix.scr.annotations.Property;
tom85c612b2014-09-19 19:25:47 -070033import org.apache.felix.scr.annotations.Service;
sangyun-hanc1806862016-04-05 11:42:03 +090034import org.onlab.util.Tools;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.DeviceId;
36import org.onosproject.net.flow.CompletedBatchOperation;
37import org.onosproject.net.flow.DefaultFlowEntry;
38import org.onosproject.net.flow.FlowEntry;
39import org.onosproject.net.flow.FlowEntry.FlowEntryState;
40import org.onosproject.net.flow.FlowId;
41import org.onosproject.net.flow.FlowRule;
42import org.onosproject.net.flow.FlowRuleBatchEntry;
43import org.onosproject.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
44import org.onosproject.net.flow.FlowRuleBatchEvent;
45import org.onosproject.net.flow.FlowRuleBatchOperation;
46import org.onosproject.net.flow.FlowRuleBatchRequest;
47import org.onosproject.net.flow.FlowRuleEvent;
48import org.onosproject.net.flow.FlowRuleEvent.Type;
49import org.onosproject.net.flow.FlowRuleStore;
50import org.onosproject.net.flow.FlowRuleStoreDelegate;
51import org.onosproject.net.flow.StoredFlowEntry;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070052import org.onosproject.net.flow.TableStatisticsEntry;
Brian O'Connorabafb502014-12-02 22:26:20 -080053import org.onosproject.store.AbstractStore;
sangyun-hanc1806862016-04-05 11:42:03 +090054import org.osgi.service.component.ComponentContext;
tom85c612b2014-09-19 19:25:47 -070055import org.slf4j.Logger;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070056
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080057import java.util.ArrayList;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070058import java.util.Collections;
sangyun-hanc1806862016-04-05 11:42:03 +090059import java.util.Dictionary;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070060import java.util.List;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070061import java.util.concurrent.ConcurrentHashMap;
62import java.util.concurrent.ConcurrentMap;
63import java.util.concurrent.CopyOnWriteArrayList;
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -080064import java.util.concurrent.ExecutionException;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080065import java.util.concurrent.TimeUnit;
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -080066import java.util.concurrent.TimeoutException;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080067import java.util.concurrent.atomic.AtomicInteger;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070068
Brian O'Connorabafb502014-12-02 22:26:20 -080069import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070070import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070071
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070072/**
73 * Manages inventory of flow rules using trivial in-memory implementation.
74 */
tom85c612b2014-09-19 19:25:47 -070075@Component(immediate = true)
76@Service
tomc78acee2014-09-24 15:16:55 -070077public class SimpleFlowRuleStore
Madan Jampani117aaae2014-10-23 10:04:05 -070078 extends AbstractStore<FlowRuleBatchEvent, FlowRuleStoreDelegate>
tomc78acee2014-09-24 15:16:55 -070079 implements FlowRuleStore {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070080
tom85c612b2014-09-19 19:25:47 -070081 private final Logger log = getLogger(getClass());
82
alshabiba68eb962014-09-24 20:34:13 -070083
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070084 // inner Map is Device flow table
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070085 // inner Map value (FlowId synonym list) must be synchronized before modifying
86 private final ConcurrentMap<DeviceId, ConcurrentMap<FlowId, List<StoredFlowEntry>>>
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070087 flowEntries = new ConcurrentHashMap<>();
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070088
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070089 private final ConcurrentMap<DeviceId, List<TableStatisticsEntry>>
90 deviceTableStats = new ConcurrentHashMap<>();
91
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080092 private final AtomicInteger localBatchIdGen = new AtomicInteger();
93
sangyun-hanc1806862016-04-05 11:42:03 +090094 private static final int DEFAULT_PENDING_FUTURE_TIMEOUT_MINUTES = 5;
95 @Property(name = "pendingFutureTimeoutMinutes", intValue = DEFAULT_PENDING_FUTURE_TIMEOUT_MINUTES,
96 label = "Expiration time after an entry is created that it should be automatically removed")
97 private int pendingFutureTimeoutMinutes = DEFAULT_PENDING_FUTURE_TIMEOUT_MINUTES;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080098
99 private Cache<Integer, SettableFuture<CompletedBatchOperation>> pendingFutures =
100 CacheBuilder.newBuilder()
101 .expireAfterWrite(pendingFutureTimeoutMinutes, TimeUnit.MINUTES)
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -0800102 .removalListener(new TimeoutFuture())
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800103 .build();
104
tom85c612b2014-09-19 19:25:47 -0700105 @Activate
106 public void activate() {
107 log.info("Started");
108 }
109
110 @Deactivate
111 public void deactivate() {
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700112 deviceTableStats.clear();
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700113 flowEntries.clear();
tom85c612b2014-09-19 19:25:47 -0700114 log.info("Stopped");
115 }
116
sangyun-hanc1806862016-04-05 11:42:03 +0900117 @Modified
118 public void modified(ComponentContext context) {
119
120 readComponentConfiguration(context);
121
122 // Reset Cache and copy all.
123 Cache<Integer, SettableFuture<CompletedBatchOperation>> prevFutures = pendingFutures;
124 pendingFutures = CacheBuilder.newBuilder()
125 .expireAfterWrite(pendingFutureTimeoutMinutes, TimeUnit.MINUTES)
126 .removalListener(new TimeoutFuture())
127 .build();
128
129 pendingFutures.putAll(prevFutures.asMap());
130 }
131
alshabiba68eb962014-09-24 20:34:13 -0700132
tombe988312014-09-19 18:38:47 -0700133 @Override
tom9b4030d2014-10-06 10:39:03 -0700134 public int getFlowRuleCount() {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700135 int sum = 0;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700136 for (ConcurrentMap<FlowId, List<StoredFlowEntry>> ft : flowEntries.values()) {
137 for (List<StoredFlowEntry> fes : ft.values()) {
138 sum += fes.size();
139 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700140 }
141 return sum;
142 }
143
sangyun-hanc1806862016-04-05 11:42:03 +0900144 /**
145 * Extracts properties from the component configuration context.
146 *
147 * @param context the component context
148 */
149 private void readComponentConfiguration(ComponentContext context) {
150 Dictionary<?, ?> properties = context.getProperties();
151
152 Integer newPendingFutureTimeoutMinutes =
153 Tools.getIntegerProperty(properties, "pendingFutureTimeoutMinutes");
154 if (newPendingFutureTimeoutMinutes == null) {
155 pendingFutureTimeoutMinutes = DEFAULT_PENDING_FUTURE_TIMEOUT_MINUTES;
156 log.info("Pending future timeout is not configured, " +
157 "using current value of {}", pendingFutureTimeoutMinutes);
158 } else {
159 pendingFutureTimeoutMinutes = newPendingFutureTimeoutMinutes;
160 log.info("Configured. Pending future timeout is configured to {}",
161 pendingFutureTimeoutMinutes);
162 }
163 }
164
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700165 /**
166 * Returns the flow table for specified device.
167 *
168 * @param deviceId identifier of the device
169 * @return Map representing Flow Table of given device.
170 */
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700171 private ConcurrentMap<FlowId, List<StoredFlowEntry>> getFlowTable(DeviceId deviceId) {
Yuta HIGUCHIc2e68152016-08-16 13:47:36 -0700172 return flowEntries.computeIfAbsent(deviceId, k -> new ConcurrentHashMap<>());
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700173 }
174
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700175 private List<StoredFlowEntry> getFlowEntries(DeviceId deviceId, FlowId flowId) {
176 final ConcurrentMap<FlowId, List<StoredFlowEntry>> flowTable = getFlowTable(deviceId);
177 List<StoredFlowEntry> r = flowTable.get(flowId);
178 if (r == null) {
179 final List<StoredFlowEntry> concurrentlyAdded;
180 r = new CopyOnWriteArrayList<>();
181 concurrentlyAdded = flowTable.putIfAbsent(flowId, r);
182 if (concurrentlyAdded != null) {
183 return concurrentlyAdded;
184 }
185 }
186 return r;
187 }
188
189 private FlowEntry getFlowEntryInternal(DeviceId deviceId, FlowRule rule) {
190 List<StoredFlowEntry> fes = getFlowEntries(deviceId, rule.id());
191 for (StoredFlowEntry fe : fes) {
192 if (fe.equals(rule)) {
193 return fe;
194 }
195 }
196 return null;
tom9b4030d2014-10-06 10:39:03 -0700197 }
198
199 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700200 public FlowEntry getFlowEntry(FlowRule rule) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700201 return getFlowEntryInternal(rule.deviceId(), rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700202 }
203
204 @Override
205 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700206 // flatten and make iterator unmodifiable
207 return FluentIterable.from(getFlowTable(deviceId).values())
Sho SHIMIZU74626412015-09-11 11:46:27 -0700208 .transformAndConcat(Collections::unmodifiableList);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700209 }
210
211 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700212 public void storeFlowRule(FlowRule rule) {
213 storeFlowRuleInternal(rule);
alshabib219ebaa2014-09-22 15:41:24 -0700214 }
215
Madan Jampani117aaae2014-10-23 10:04:05 -0700216 private void storeFlowRuleInternal(FlowRule rule) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700217 StoredFlowEntry f = new DefaultFlowEntry(rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700218 final DeviceId did = f.deviceId();
219 final FlowId fid = f.id();
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700220 List<StoredFlowEntry> existing = getFlowEntries(did, fid);
221 synchronized (existing) {
222 for (StoredFlowEntry fe : existing) {
223 if (fe.equals(rule)) {
224 // was already there? ignore
Madan Jampani117aaae2014-10-23 10:04:05 -0700225 return;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700226 }
227 }
228 // new flow rule added
229 existing.add(f);
alshabiba68eb962014-09-24 20:34:13 -0700230 }
231 }
232
233 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700234 public void deleteFlowRule(FlowRule rule) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700235
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700236 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
alshabib2374fc92014-10-22 11:03:23 -0700237
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700238 synchronized (entries) {
239 for (StoredFlowEntry entry : entries) {
240 if (entry.equals(rule)) {
241 synchronized (entry) {
242 entry.setState(FlowEntryState.PENDING_REMOVE);
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700243 }
244 }
245 }
alshabib219ebaa2014-09-22 15:41:24 -0700246 }
Madan Jampani31961c12014-10-23 12:06:58 -0700247
248
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700249 //log.warn("Cannot find rule {}", rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700250 }
251
tombe988312014-09-19 18:38:47 -0700252 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700253 public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700254 // check if this new rule is an update to an existing entry
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700255 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
256 synchronized (entries) {
257 for (StoredFlowEntry stored : entries) {
258 if (stored.equals(rule)) {
259 synchronized (stored) {
Brian O'Connora3e5cd52015-12-05 15:59:19 -0800260 //FIXME modification of "stored" flow entry outside of flow table
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700261 stored.setBytes(rule.bytes());
262 stored.setLife(rule.life());
263 stored.setPackets(rule.packets());
264 if (stored.state() == FlowEntryState.PENDING_ADD) {
265 stored.setState(FlowEntryState.ADDED);
266 // TODO: Do we need to change `rule` state?
267 return new FlowRuleEvent(Type.RULE_ADDED, rule);
268 }
269 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
270 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700271 }
alshabibba5ac482014-10-02 17:15:20 -0700272 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700273 }
alshabib219ebaa2014-09-22 15:41:24 -0700274
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700275 // should not reach here
276 // storeFlowRule was expected to be called
277 log.error("FlowRule was not found in store {} to update", rule);
278
alshabib8ca53902014-10-07 13:11:17 -0700279 //flowEntries.put(did, rule);
alshabibba5ac482014-10-02 17:15:20 -0700280 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700281 }
282
tombe988312014-09-19 18:38:47 -0700283 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700284 public FlowRuleEvent removeFlowRule(FlowEntry rule) {
alshabib1c319ff2014-10-04 20:29:09 -0700285 // This is where one could mark a rule as removed and still keep it in the store.
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700286 final DeviceId did = rule.deviceId();
287
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700288 List<StoredFlowEntry> entries = getFlowEntries(did, rule.id());
289 synchronized (entries) {
290 if (entries.remove(rule)) {
291 return new FlowRuleEvent(RULE_REMOVED, rule);
292 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700293 }
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700294 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700295 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700296
297 @Override
Charles Chan93fa7272016-01-26 22:27:02 -0800298 public FlowRuleEvent pendingFlowRule(FlowEntry rule) {
299 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
300 synchronized (entries) {
301 for (StoredFlowEntry entry : entries) {
302 if (entry.equals(rule) &&
303 entry.state() != FlowEntryState.PENDING_ADD) {
304 synchronized (entry) {
305 entry.setState(FlowEntryState.PENDING_ADD);
306 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
307 }
308 }
309 }
310 }
311 return null;
312 }
313
Yuta HIGUCHIc2e68152016-08-16 13:47:36 -0700314 @Override
Charles Chan0c7c43b2016-01-14 17:39:20 -0800315 public void purgeFlowRule(DeviceId deviceId) {
316 flowEntries.remove(deviceId);
317 }
318
Charles Chan93fa7272016-01-26 22:27:02 -0800319 @Override
Victor Silva139bca42016-08-18 11:46:35 -0300320 public void purgeFlowRules() {
321 flowEntries.clear();
322 }
323
324 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800325 public void storeBatch(
326 FlowRuleBatchOperation operation) {
Brian O'Connor427a1762014-11-19 18:40:32 -0800327 List<FlowRuleBatchEntry> toAdd = new ArrayList<>();
328 List<FlowRuleBatchEntry> toRemove = new ArrayList<>();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800329
330 for (FlowRuleBatchEntry entry : operation.getOperations()) {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800331 final FlowRule flowRule = entry.target();
332 if (entry.operator().equals(FlowRuleOperation.ADD)) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800333 if (!getFlowEntries(flowRule.deviceId(), flowRule.id()).contains(flowRule)) {
334 storeFlowRule(flowRule);
Brian O'Connor427a1762014-11-19 18:40:32 -0800335 toAdd.add(entry);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800336 }
Sho SHIMIZUaba9d002015-01-29 14:51:04 -0800337 } else if (entry.operator().equals(FlowRuleOperation.REMOVE)) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800338 if (getFlowEntries(flowRule.deviceId(), flowRule.id()).contains(flowRule)) {
339 deleteFlowRule(flowRule);
Brian O'Connor427a1762014-11-19 18:40:32 -0800340 toRemove.add(entry);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800341 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700342 } else {
343 throw new UnsupportedOperationException("Unsupported operation type");
344 }
345 }
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800346
347 if (toAdd.isEmpty() && toRemove.isEmpty()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800348 notifyDelegate(FlowRuleBatchEvent.completed(
349 new FlowRuleBatchRequest(operation.id(), Collections.emptySet()),
350 new CompletedBatchOperation(true, Collections.emptySet(),
351 operation.deviceId())));
352 return;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800353 }
354
355 SettableFuture<CompletedBatchOperation> r = SettableFuture.create();
356 final int batchId = localBatchIdGen.incrementAndGet();
357
358 pendingFutures.put(batchId, r);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800359
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800360 toAdd.addAll(toRemove);
361 notifyDelegate(FlowRuleBatchEvent.requested(
362 new FlowRuleBatchRequest(batchId, Sets.newHashSet(toAdd)), operation.deviceId()));
363
Madan Jampani117aaae2014-10-23 10:04:05 -0700364 }
365
366 @Override
367 public void batchOperationComplete(FlowRuleBatchEvent event) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800368 final Long batchId = event.subject().batchId();
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800369 SettableFuture<CompletedBatchOperation> future
370 = pendingFutures.getIfPresent(batchId);
371 if (future != null) {
372 future.set(event.result());
373 pendingFutures.invalidate(batchId);
374 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700375 notifyDelegate(event);
376 }
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -0800377
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700378 @Override
379 public FlowRuleEvent updateTableStatistics(DeviceId deviceId,
380 List<TableStatisticsEntry> tableStats) {
381 deviceTableStats.put(deviceId, tableStats);
382 return null;
383 }
384
385 @Override
386 public Iterable<TableStatisticsEntry> getTableStatistics(DeviceId deviceId) {
387 List<TableStatisticsEntry> tableStats = deviceTableStats.get(deviceId);
388 if (tableStats == null) {
389 return Collections.emptyList();
390 }
391 return ImmutableList.copyOf(tableStats);
392 }
sangyun-hanc1806862016-04-05 11:42:03 +0900393
Patryk Konopka7e40c012017-06-06 13:38:06 +0200394 @Override
395 public long getActiveFlowRuleCount(DeviceId deviceId) {
396 return Streams.stream(getTableStatistics(deviceId))
397 .mapToLong(TableStatisticsEntry::activeFlowEntries)
398 .sum();
399 }
400
sangyun-hanc1806862016-04-05 11:42:03 +0900401 private static final class TimeoutFuture
402 implements RemovalListener<Integer, SettableFuture<CompletedBatchOperation>> {
403 @Override
404 public void onRemoval(RemovalNotification<Integer, SettableFuture<CompletedBatchOperation>> notification) {
405 // wrapping in ExecutionException to support Future.get
406 if (notification.wasEvicted()) {
407 notification.getValue()
408 .setException(new ExecutionException("Timed out",
409 new TimeoutException()));
410 }
411 }
412 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700413}