blob: 2b941cbac38b9cc69d26e9011b4d916f184ab8a8 [file] [log] [blame]
Frank Wang0e805082017-07-21 14:37:35 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Frank Wang0e805082017-07-21 14:37:35 +08003 *
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 */
16
Andrea Campanella0288c872017-08-07 18:32:51 +020017package org.onosproject.drivers.p4runtime;
Frank Wang0e805082017-07-21 14:37:35 +080018
Manjunath Vanaraj59ad6572017-12-26 11:10:57 +053019import com.google.common.cache.CacheBuilder;
20import com.google.common.cache.CacheLoader;
Carmelo Cascone3da671a2018-02-12 10:43:35 -080021import com.google.common.cache.LoadingCache;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020022import com.google.common.collect.ImmutableList;
23import com.google.common.collect.Lists;
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -080024import org.onlab.util.SharedExecutors;
25import org.onosproject.drivers.p4runtime.mirror.P4RuntimeTableMirror;
26import org.onosproject.drivers.p4runtime.mirror.TimedEntry;
Frank Wang0e805082017-07-21 14:37:35 +080027import org.onosproject.net.flow.DefaultFlowEntry;
28import org.onosproject.net.flow.FlowEntry;
29import org.onosproject.net.flow.FlowRule;
30import org.onosproject.net.flow.FlowRuleProgrammable;
Frank Wang0e805082017-07-21 14:37:35 +080031import org.onosproject.net.pi.model.PiPipelineInterpreter;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020032import org.onosproject.net.pi.model.PiPipelineModel;
Carmelo Cascone87892e22017-11-13 16:01:29 -080033import org.onosproject.net.pi.model.PiTableId;
Carmelo Cascone7f75be42017-09-07 14:37:02 +020034import org.onosproject.net.pi.runtime.PiCounterCellData;
35import org.onosproject.net.pi.runtime.PiCounterCellId;
Frank Wang0e805082017-07-21 14:37:35 +080036import org.onosproject.net.pi.runtime.PiTableEntry;
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -080037import org.onosproject.net.pi.runtime.PiTableEntryHandle;
38import org.onosproject.net.pi.service.PiFlowRuleTranslator;
39import org.onosproject.net.pi.service.PiTranslatedEntity;
Carmelo Cascone326ad2d2017-11-28 18:09:13 -080040import org.onosproject.net.pi.service.PiTranslationException;
Frank Wang0e805082017-07-21 14:37:35 +080041import org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType;
Frank Wang0e805082017-07-21 14:37:35 +080042
43import java.util.Collection;
44import java.util.Collections;
Manjunath Vanaraj59ad6572017-12-26 11:10:57 +053045import java.util.List;
Carmelo Cascone3da671a2018-02-12 10:43:35 -080046import java.util.Map;
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -080047import java.util.Optional;
Carmelo Cascone7f75be42017-09-07 14:37:02 +020048import java.util.Set;
Carmelo Casconee5b28722018-06-22 17:28:28 +020049import java.util.concurrent.CompletableFuture;
Manjunath Vanaraj59ad6572017-12-26 11:10:57 +053050import java.util.concurrent.TimeUnit;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020051import java.util.concurrent.locks.Lock;
52import java.util.concurrent.locks.ReentrantLock;
Carmelo Casconefe99be92017-09-11 21:55:54 +020053import java.util.stream.Collectors;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020054
55import static com.google.common.collect.Lists.newArrayList;
Andrea Campanella0288c872017-08-07 18:32:51 +020056import static org.onosproject.drivers.p4runtime.P4RuntimeFlowRuleProgrammable.Operation.APPLY;
57import static org.onosproject.drivers.p4runtime.P4RuntimeFlowRuleProgrammable.Operation.REMOVE;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020058import static org.onosproject.net.flow.FlowEntry.FlowEntryState.ADDED;
Carmelo Cascone87892e22017-11-13 16:01:29 -080059import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.DELETE;
60import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.INSERT;
61import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.MODIFY;
Frank Wang0e805082017-07-21 14:37:35 +080062
63/**
Carmelo Casconee3a7c742017-09-01 01:25:52 +020064 * Implementation of the flow rule programmable behaviour for P4Runtime.
Frank Wang0e805082017-07-21 14:37:35 +080065 */
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -080066public class P4RuntimeFlowRuleProgrammable
67 extends AbstractP4RuntimeHandlerBehaviour
68 implements FlowRuleProgrammable {
Frank Wang0e805082017-07-21 14:37:35 +080069
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -080070 // When updating an existing rule, if true, we issue a DELETE operation
71 // before inserting the new one, otherwise we issue a MODIFY operation. This
72 // is useful fore devices that do not support MODIFY operations for table
73 // entries.
Carmelo Cascone3da671a2018-02-12 10:43:35 -080074 private static final String DELETE_BEFORE_UPDATE = "tableDeleteBeforeUpdate";
75 private static final boolean DEFAULT_DELETE_BEFORE_UPDATE = false;
Carmelo Cascone2308e522017-08-25 02:35:12 +020076
Carmelo Cascone81929aa2018-04-07 01:38:55 -070077 // If true, we ignore re-installing rules that already exist in the
78 // device mirror, i.e. same match key and action.
Carmelo Cascone3da671a2018-02-12 10:43:35 -080079 private static final String IGNORE_SAME_ENTRY_UPDATE = "tableIgnoreSameEntryUpdate";
80 private static final boolean DEFAULT_IGNORE_SAME_ENTRY_UPDATE = false;
Carmelo Cascone2308e522017-08-25 02:35:12 +020081
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -080082 // If true, we avoid querying the device and return what's already known by
83 // the ONOS store.
Carmelo Cascone3da671a2018-02-12 10:43:35 -080084 private static final String READ_FROM_MIRROR = "tableReadFromMirror";
85 private static final boolean DEFAULT_READ_FROM_MIRROR = false;
Carmelo Casconefe99be92017-09-11 21:55:54 +020086
Carmelo Cascone255125d2018-04-11 14:03:22 -070087 // If true, we read counters when reading table entries (if table has
88 // counters). Otherwise, we don't.
89 private static final String SUPPORT_TABLE_COUNTERS = "supportTableCounters";
90 private static final boolean DEFAULT_SUPPORT_TABLE_COUNTERS = true;
91
Carmelo Cascone3da671a2018-02-12 10:43:35 -080092 // If true, we read all direct counters of a table with one request.
93 // Otherwise, we send as many requests as the number of table entries.
94 private static final String READ_ALL_DIRECT_COUNTERS = "tableReadAllDirectCounters";
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -080095 // FIXME: set to true as soon as the feature is implemented in P4Runtime.
Carmelo Cascone3da671a2018-02-12 10:43:35 -080096 private static final boolean DEFAULT_READ_ALL_DIRECT_COUNTERS = false;
Carmelo Cascone7f75be42017-09-07 14:37:02 +020097
Manjunath Vanaraj59ad6572017-12-26 11:10:57 +053098 private static final int TABLE_ENTRY_LOCK_EXPIRE_TIME_IN_MIN = 10;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020099
Manjunath Vanaraj59ad6572017-12-26 11:10:57 +0530100 // Needed to synchronize operations over the same table entry.
101 private static final LoadingCache<PiTableEntryHandle, Lock>
102 ENTRY_LOCKS = CacheBuilder.newBuilder()
103 .expireAfterAccess(TABLE_ENTRY_LOCK_EXPIRE_TIME_IN_MIN, TimeUnit.MINUTES)
104 .build(new CacheLoader<PiTableEntryHandle, Lock>() {
105 @Override
106 public Lock load(PiTableEntryHandle handle) {
107 return new ReentrantLock();
108 }
109 });
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200110 private PiPipelineModel pipelineModel;
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800111 private P4RuntimeTableMirror tableMirror;
112 private PiFlowRuleTranslator translator;
Frank Wang0e805082017-07-21 14:37:35 +0800113
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200114 @Override
115 protected boolean setupBehaviour() {
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200116
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200117 if (!super.setupBehaviour()) {
Frank Wang0e805082017-07-21 14:37:35 +0800118 return false;
119 }
120
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200121 if (!device.is(PiPipelineInterpreter.class)) {
122 log.warn("Unable to get interpreter of {}", deviceId);
Frank Wang0e805082017-07-21 14:37:35 +0800123 return false;
124 }
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200125 pipelineModel = pipeconf.pipelineModel();
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800126 tableMirror = handler().get(P4RuntimeTableMirror.class);
127 translator = piTranslationService.flowRuleTranslator();
Frank Wang0e805082017-07-21 14:37:35 +0800128 return true;
129 }
130
131 @Override
132 public Collection<FlowEntry> getFlowEntries() {
133
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200134 if (!setupBehaviour()) {
Frank Wang0e805082017-07-21 14:37:35 +0800135 return Collections.emptyList();
136 }
137
Carmelo Cascone3da671a2018-02-12 10:43:35 -0800138 if (driverBoolProperty(READ_FROM_MIRROR, DEFAULT_READ_FROM_MIRROR)) {
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800139 return getFlowEntriesFromMirror();
Carmelo Casconefe99be92017-09-11 21:55:54 +0200140 }
141
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800142 final ImmutableList.Builder<FlowEntry> result = ImmutableList.builder();
143 final List<PiTableEntry> inconsistentEntries = Lists.newArrayList();
Frank Wang0e805082017-07-21 14:37:35 +0800144
Carmelo Casconee5b28722018-06-22 17:28:28 +0200145 // Read table entries.
146 final Collection<PiTableEntry> installedEntries;
147 // TODO: ONOS-7596 read counters with table entries
148 installedEntries = getFutureWithDeadline(client.dumpAllTables(pipeconf),
149 "dumping tables", Collections.emptyList());
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200150
Carmelo Casconee5b28722018-06-22 17:28:28 +0200151 if (installedEntries.isEmpty()) {
152 return Collections.emptyList();
153 }
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200154
Carmelo Casconee5b28722018-06-22 17:28:28 +0200155 // Read table direct counters (if any).
156 final Map<PiTableEntry, PiCounterCellData> counterCellMap =
157 readEntryCounters(installedEntries);
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200158
Carmelo Casconee5b28722018-06-22 17:28:28 +0200159 // Forge flow entries with counter values.
160 for (PiTableEntry installedEntry : installedEntries) {
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800161
Carmelo Casconee5b28722018-06-22 17:28:28 +0200162 final FlowEntry flowEntry = forgeFlowEntry(
163 installedEntry, counterCellMap.get(installedEntry));
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200164
Carmelo Casconee5b28722018-06-22 17:28:28 +0200165 if (flowEntry == null) {
166 // Entry is on device but unknown to translation service or
167 // device mirror. Inconsistent. Mark for removal.
168 // TODO: make this behaviour configurable
169 // In some cases it's fine for the device to have rules
170 // that were not installed by us.
171 inconsistentEntries.add(installedEntry);
172 } else {
173 result.add(flowEntry);
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200174 }
175 }
176
177 if (inconsistentEntries.size() > 0) {
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800178 // Async clean up inconsistent entries.
179 SharedExecutors.getSingleThreadExecutor().execute(
180 () -> cleanUpInconsistentEntries(inconsistentEntries));
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200181 }
182
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800183 return result.build();
Frank Wang0e805082017-07-21 14:37:35 +0800184 }
185
186 @Override
187 public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) {
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200188 return processFlowRules(rules, APPLY);
Frank Wang0e805082017-07-21 14:37:35 +0800189 }
190
191 @Override
192 public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) {
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200193 return processFlowRules(rules, REMOVE);
Frank Wang0e805082017-07-21 14:37:35 +0800194 }
195
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800196 private FlowEntry forgeFlowEntry(PiTableEntry entry,
197 PiCounterCellData cellData) {
198 final PiTableEntryHandle handle = PiTableEntryHandle
199 .of(deviceId, entry);
200 final Optional<PiTranslatedEntity<FlowRule, PiTableEntry>>
201 translatedEntity = translator.lookup(handle);
202 final TimedEntry<PiTableEntry> timedEntry = tableMirror.get(handle);
203
204 if (!translatedEntity.isPresent()) {
205 log.debug("Handle not found in store: {}", handle);
206 return null;
207 }
208
209 if (timedEntry == null) {
210 log.debug("Handle not found in device mirror: {}", handle);
211 return null;
212 }
213
214 if (cellData != null) {
215 return new DefaultFlowEntry(translatedEntity.get().original(),
Carmelo Cascone81929aa2018-04-07 01:38:55 -0700216 ADDED, timedEntry.lifeSec(), cellData.packets(),
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800217 cellData.bytes());
218 } else {
219 return new DefaultFlowEntry(translatedEntity.get().original(),
220 ADDED, timedEntry.lifeSec(), 0, 0);
221 }
222 }
223
224 private Collection<FlowEntry> getFlowEntriesFromMirror() {
225 return tableMirror.getAll(deviceId).stream()
226 .map(timedEntry -> forgeFlowEntry(
227 timedEntry.entry(), null))
228 .collect(Collectors.toList());
229 }
230
231 private void cleanUpInconsistentEntries(Collection<PiTableEntry> piEntries) {
232 log.warn("Found {} entries from {} not on translation store, removing them...",
233 piEntries.size(), deviceId);
234 piEntries.forEach(entry -> {
235 log.debug(entry.toString());
236 applyEntry(PiTableEntryHandle.of(deviceId, entry),
237 entry, null, REMOVE);
238 });
239 }
240
241 private Collection<FlowRule> processFlowRules(Collection<FlowRule> rules,
242 Operation driverOperation) {
Frank Wang0e805082017-07-21 14:37:35 +0800243
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200244 if (!setupBehaviour()) {
Frank Wang0e805082017-07-21 14:37:35 +0800245 return Collections.emptyList();
246 }
247
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800248 final ImmutableList.Builder<FlowRule> result = ImmutableList.builder();
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200249
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800250 // TODO: send writes in bulk (e.g. all entries to insert, modify or delete).
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200251 // Instead of calling the client for each one of them.
252
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800253 for (FlowRule ruleToApply : rules) {
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200254
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800255 final PiTableEntry piEntryToApply;
Frank Wang0e805082017-07-21 14:37:35 +0800256 try {
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800257 piEntryToApply = translator.translate(ruleToApply, pipeconf);
Carmelo Cascone326ad2d2017-11-28 18:09:13 -0800258 } catch (PiTranslationException e) {
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800259 log.warn("Unable to translate flow rule for pipeconf '{}': {} - {}",
260 pipeconf.id(), e.getMessage(), ruleToApply);
261 // Next rule.
262 continue;
Frank Wang0e805082017-07-21 14:37:35 +0800263 }
Frank Wang0e805082017-07-21 14:37:35 +0800264
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800265 final PiTableEntryHandle handle = PiTableEntryHandle
266 .of(deviceId, piEntryToApply);
Frank Wang0e805082017-07-21 14:37:35 +0800267
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800268 // Serialize operations over the same match key/table/device ID.
Manjunath Vanaraj59ad6572017-12-26 11:10:57 +0530269 ENTRY_LOCKS.getUnchecked(handle).lock();
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200270 try {
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800271 if (applyEntry(handle, piEntryToApply,
272 ruleToApply, driverOperation)) {
273 result.add(ruleToApply);
Frank Wang0e805082017-07-21 14:37:35 +0800274 }
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200275 } finally {
Manjunath Vanaraj59ad6572017-12-26 11:10:57 +0530276 ENTRY_LOCKS.getUnchecked(handle).unlock();
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200277 }
278 }
279
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800280 return result.build();
281 }
282
283 /**
284 * Applies the given entry to the device, and returns true if the operation
285 * was successful, false otherwise.
286 */
287 private boolean applyEntry(PiTableEntryHandle handle,
288 PiTableEntry piEntryToApply,
289 FlowRule ruleToApply,
290 Operation driverOperation) {
291 // Depending on the driver operation, and if a matching rule exists on
292 // the device, decide which P4 Runtime write operation to perform for
293 // this entry.
294 final TimedEntry<PiTableEntry> piEntryOnDevice = tableMirror.get(handle);
295 final WriteOperationType p4Operation;
296 if (driverOperation == APPLY) {
297 if (piEntryOnDevice == null) {
298 // Entry is first-timer.
299 p4Operation = INSERT;
300 } else {
Carmelo Cascone3da671a2018-02-12 10:43:35 -0800301 if (driverBoolProperty(IGNORE_SAME_ENTRY_UPDATE,
302 DEFAULT_IGNORE_SAME_ENTRY_UPDATE)
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800303 && piEntryToApply.action().equals(piEntryOnDevice.entry().action())) {
304 log.debug("Ignoring re-apply of existing entry: {}", piEntryToApply);
305 p4Operation = null;
Carmelo Cascone3da671a2018-02-12 10:43:35 -0800306 } else if (driverBoolProperty(DELETE_BEFORE_UPDATE,
307 DEFAULT_DELETE_BEFORE_UPDATE)) {
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800308 // Some devices return error when updating existing
309 // entries. If requested, remove entry before
310 // re-inserting the modified one.
311 applyEntry(handle, piEntryOnDevice.entry(), null, REMOVE);
312 p4Operation = INSERT;
313 } else {
314 p4Operation = MODIFY;
315 }
316 }
317 } else {
318 p4Operation = DELETE;
319 }
320
321 if (p4Operation != null) {
322 if (writeEntry(piEntryToApply, p4Operation)) {
323 updateStores(handle, piEntryToApply, ruleToApply, p4Operation);
324 return true;
325 } else {
326 return false;
327 }
328 } else {
329 // If no operation, let's pretend we applied the rule to the device.
330 return true;
331 }
332 }
333
334 /**
335 * Performs a write operation on the device.
336 */
337 private boolean writeEntry(PiTableEntry entry,
338 WriteOperationType p4Operation) {
Carmelo Casconee5b28722018-06-22 17:28:28 +0200339 final CompletableFuture<Boolean> future = client.writeTableEntries(
340 newArrayList(entry), p4Operation, pipeconf);
341 final Boolean success = getFutureWithDeadline(
342 future, "performing table " + p4Operation.name(), null);
343 if (success == null) {
344 // Error logged by getFutureWithDeadline();
345 return false;
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800346 }
Carmelo Casconee5b28722018-06-22 17:28:28 +0200347 if (!success) {
348 log.warn("Unable to {} table entry in {}: {}",
349 p4Operation.name(), deviceId, entry);
350 }
351 return success;
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800352 }
353
354 private void updateStores(PiTableEntryHandle handle,
355 PiTableEntry entry,
356 FlowRule rule,
357 WriteOperationType p4Operation) {
358 switch (p4Operation) {
359 case INSERT:
360 case MODIFY:
361 tableMirror.put(handle, entry);
362 translator.learn(handle, new PiTranslatedEntity<>(rule, entry, handle));
363 break;
364 case DELETE:
365 tableMirror.remove(handle);
366 translator.forget(handle);
367 break;
368 default:
369 throw new IllegalArgumentException(
370 "Unknown operation " + p4Operation.name());
371 }
372 }
373
374 private Map<PiTableEntry, PiCounterCellData> readEntryCounters(
Carmelo Cascone255125d2018-04-11 14:03:22 -0700375 Collection<PiTableEntry> tableEntries) {
376 if (!driverBoolProperty(SUPPORT_TABLE_COUNTERS,
Carmelo Casconee5b28722018-06-22 17:28:28 +0200377 DEFAULT_SUPPORT_TABLE_COUNTERS)
378 || tableEntries.isEmpty()) {
Carmelo Cascone255125d2018-04-11 14:03:22 -0700379 return Collections.emptyMap();
380 }
381
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800382 Collection<PiCounterCellData> cellDatas;
Carmelo Casconee5b28722018-06-22 17:28:28 +0200383
384 if (driverBoolProperty(READ_ALL_DIRECT_COUNTERS,
385 DEFAULT_READ_ALL_DIRECT_COUNTERS)) {
386 // FIXME: read counters when dumping table entries ONOS-7596
387 cellDatas = Collections.emptyList();
388 } else {
389 Set<PiCounterCellId> cellIds = tableEntries.stream()
390 .filter(e -> tableHasCounter(e.table()))
391 .map(PiCounterCellId::ofDirect)
392 .collect(Collectors.toSet());
393 cellDatas = getFutureWithDeadline(client.readCounterCells(cellIds, pipeconf),
394 "reading table counters", Collections.emptyList());
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800395 }
Carmelo Casconee5b28722018-06-22 17:28:28 +0200396 return cellDatas.stream()
397 .collect(Collectors.toMap(c -> c.cellId().tableEntry(), c -> c));
398
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200399 }
400
Carmelo Cascone255125d2018-04-11 14:03:22 -0700401 private boolean tableHasCounter(PiTableId tableId) {
402 return pipelineModel.table(tableId).isPresent() &&
403 !pipelineModel.table(tableId).get().counters().isEmpty();
404 }
405
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200406 enum Operation {
407 APPLY, REMOVE
Frank Wang0e805082017-07-21 14:37:35 +0800408 }
Carmelo Cascone87892e22017-11-13 16:01:29 -0800409}