blob: 4175489381f2ecf86165098f1eae5cfca8d04a8e [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
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020019import com.google.common.collect.ImmutableList;
20import com.google.common.collect.Lists;
Carmelo Cascone33b27bc2018-09-09 22:56:14 -070021import com.google.common.util.concurrent.Striped;
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -080022import org.onlab.util.SharedExecutors;
23import org.onosproject.drivers.p4runtime.mirror.P4RuntimeTableMirror;
24import org.onosproject.drivers.p4runtime.mirror.TimedEntry;
Frank Wang0e805082017-07-21 14:37:35 +080025import org.onosproject.net.flow.DefaultFlowEntry;
26import org.onosproject.net.flow.FlowEntry;
27import org.onosproject.net.flow.FlowRule;
28import org.onosproject.net.flow.FlowRuleProgrammable;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020029import org.onosproject.net.pi.model.PiPipelineModel;
Carmelo Cascone87892e22017-11-13 16:01:29 -080030import org.onosproject.net.pi.model.PiTableId;
Carmelo Cascone7f75be42017-09-07 14:37:02 +020031import org.onosproject.net.pi.runtime.PiCounterCellData;
32import org.onosproject.net.pi.runtime.PiCounterCellId;
Frank Wang0e805082017-07-21 14:37:35 +080033import org.onosproject.net.pi.runtime.PiTableEntry;
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -080034import org.onosproject.net.pi.runtime.PiTableEntryHandle;
35import org.onosproject.net.pi.service.PiFlowRuleTranslator;
36import org.onosproject.net.pi.service.PiTranslatedEntity;
Carmelo Cascone326ad2d2017-11-28 18:09:13 -080037import org.onosproject.net.pi.service.PiTranslationException;
Frank Wang0e805082017-07-21 14:37:35 +080038import org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType;
Frank Wang0e805082017-07-21 14:37:35 +080039
40import java.util.Collection;
41import java.util.Collections;
Manjunath Vanaraj59ad6572017-12-26 11:10:57 +053042import java.util.List;
Carmelo Cascone3da671a2018-02-12 10:43:35 -080043import java.util.Map;
Carmelo Cascone26600972018-09-10 00:23:20 -070044import java.util.Objects;
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -080045import java.util.Optional;
Carmelo Cascone7f75be42017-09-07 14:37:02 +020046import java.util.Set;
Carmelo Casconee5b28722018-06-22 17:28:28 +020047import java.util.concurrent.CompletableFuture;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020048import java.util.concurrent.locks.Lock;
Carmelo Casconefe99be92017-09-11 21:55:54 +020049import java.util.stream.Collectors;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020050
51import static com.google.common.collect.Lists.newArrayList;
Andrea Campanella0288c872017-08-07 18:32:51 +020052import static org.onosproject.drivers.p4runtime.P4RuntimeFlowRuleProgrammable.Operation.APPLY;
53import static org.onosproject.drivers.p4runtime.P4RuntimeFlowRuleProgrammable.Operation.REMOVE;
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020054import static org.onosproject.net.flow.FlowEntry.FlowEntryState.ADDED;
Carmelo Cascone87892e22017-11-13 16:01:29 -080055import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.DELETE;
56import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.INSERT;
57import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.MODIFY;
Frank Wang0e805082017-07-21 14:37:35 +080058
59/**
Carmelo Casconee3a7c742017-09-01 01:25:52 +020060 * Implementation of the flow rule programmable behaviour for P4Runtime.
Frank Wang0e805082017-07-21 14:37:35 +080061 */
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -080062public class P4RuntimeFlowRuleProgrammable
63 extends AbstractP4RuntimeHandlerBehaviour
64 implements FlowRuleProgrammable {
Frank Wang0e805082017-07-21 14:37:35 +080065
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -080066 // When updating an existing rule, if true, we issue a DELETE operation
67 // before inserting the new one, otherwise we issue a MODIFY operation. This
68 // is useful fore devices that do not support MODIFY operations for table
69 // entries.
Carmelo Cascone3da671a2018-02-12 10:43:35 -080070 private static final String DELETE_BEFORE_UPDATE = "tableDeleteBeforeUpdate";
71 private static final boolean DEFAULT_DELETE_BEFORE_UPDATE = false;
Carmelo Cascone2308e522017-08-25 02:35:12 +020072
Carmelo Cascone81929aa2018-04-07 01:38:55 -070073 // If true, we ignore re-installing rules that already exist in the
74 // device mirror, i.e. same match key and action.
Carmelo Cascone3da671a2018-02-12 10:43:35 -080075 private static final String IGNORE_SAME_ENTRY_UPDATE = "tableIgnoreSameEntryUpdate";
76 private static final boolean DEFAULT_IGNORE_SAME_ENTRY_UPDATE = false;
Carmelo Cascone2308e522017-08-25 02:35:12 +020077
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -080078 // If true, we avoid querying the device and return what's already known by
79 // the ONOS store.
Carmelo Cascone3da671a2018-02-12 10:43:35 -080080 private static final String READ_FROM_MIRROR = "tableReadFromMirror";
81 private static final boolean DEFAULT_READ_FROM_MIRROR = false;
Carmelo Casconefe99be92017-09-11 21:55:54 +020082
Carmelo Cascone255125d2018-04-11 14:03:22 -070083 // If true, we read counters when reading table entries (if table has
84 // counters). Otherwise, we don't.
85 private static final String SUPPORT_TABLE_COUNTERS = "supportTableCounters";
86 private static final boolean DEFAULT_SUPPORT_TABLE_COUNTERS = true;
87
Carmelo Cascone3da671a2018-02-12 10:43:35 -080088 // If true, we read all direct counters of a table with one request.
89 // Otherwise, we send as many requests as the number of table entries.
90 private static final String READ_ALL_DIRECT_COUNTERS = "tableReadAllDirectCounters";
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -080091 // FIXME: set to true as soon as the feature is implemented in P4Runtime.
Carmelo Cascone3da671a2018-02-12 10:43:35 -080092 private static final boolean DEFAULT_READ_ALL_DIRECT_COUNTERS = false;
Carmelo Cascone7f75be42017-09-07 14:37:02 +020093
Manjunath Vanaraj59ad6572017-12-26 11:10:57 +053094 // Needed to synchronize operations over the same table entry.
Carmelo Cascone33b27bc2018-09-09 22:56:14 -070095 private static final Striped<Lock> ENTRY_LOCKS = Striped.lock(30);
96
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +020097 private PiPipelineModel pipelineModel;
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -080098 private P4RuntimeTableMirror tableMirror;
99 private PiFlowRuleTranslator translator;
Frank Wang0e805082017-07-21 14:37:35 +0800100
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200101 @Override
102 protected boolean setupBehaviour() {
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200103
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200104 if (!super.setupBehaviour()) {
Frank Wang0e805082017-07-21 14:37:35 +0800105 return false;
106 }
107
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200108 pipelineModel = pipeconf.pipelineModel();
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800109 tableMirror = handler().get(P4RuntimeTableMirror.class);
110 translator = piTranslationService.flowRuleTranslator();
Frank Wang0e805082017-07-21 14:37:35 +0800111 return true;
112 }
113
114 @Override
115 public Collection<FlowEntry> getFlowEntries() {
116
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200117 if (!setupBehaviour()) {
Frank Wang0e805082017-07-21 14:37:35 +0800118 return Collections.emptyList();
119 }
120
Carmelo Cascone3da671a2018-02-12 10:43:35 -0800121 if (driverBoolProperty(READ_FROM_MIRROR, DEFAULT_READ_FROM_MIRROR)) {
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800122 return getFlowEntriesFromMirror();
Carmelo Casconefe99be92017-09-11 21:55:54 +0200123 }
124
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800125 final ImmutableList.Builder<FlowEntry> result = ImmutableList.builder();
126 final List<PiTableEntry> inconsistentEntries = Lists.newArrayList();
Frank Wang0e805082017-07-21 14:37:35 +0800127
Carmelo Casconee5b28722018-06-22 17:28:28 +0200128 // Read table entries.
Carmelo Casconee5b28722018-06-22 17:28:28 +0200129 // TODO: ONOS-7596 read counters with table entries
Carmelo Cascone158b8c42018-07-04 19:42:37 +0200130 final Collection<PiTableEntry> installedEntries = getFutureWithDeadline(
131 client.dumpAllTables(pipeconf), "dumping all tables",
Carmelo Cascone33b27bc2018-09-09 22:56:14 -0700132 Collections.emptyList())
133 // Filter out entries from constant table.
134 .stream()
135 .filter(e -> !tableIsConstant(e.table()))
136 .collect(Collectors.toList());
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200137
Carmelo Casconee5b28722018-06-22 17:28:28 +0200138 if (installedEntries.isEmpty()) {
139 return Collections.emptyList();
140 }
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200141
Carmelo Casconee5b28722018-06-22 17:28:28 +0200142 // Read table direct counters (if any).
143 final Map<PiTableEntry, PiCounterCellData> counterCellMap =
144 readEntryCounters(installedEntries);
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200145
Carmelo Casconee5b28722018-06-22 17:28:28 +0200146 // Forge flow entries with counter values.
147 for (PiTableEntry installedEntry : installedEntries) {
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800148
Carmelo Casconee5b28722018-06-22 17:28:28 +0200149 final FlowEntry flowEntry = forgeFlowEntry(
150 installedEntry, counterCellMap.get(installedEntry));
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200151
Carmelo Casconee5b28722018-06-22 17:28:28 +0200152 if (flowEntry == null) {
153 // Entry is on device but unknown to translation service or
154 // device mirror. Inconsistent. Mark for removal.
155 // TODO: make this behaviour configurable
156 // In some cases it's fine for the device to have rules
157 // that were not installed by us.
158 inconsistentEntries.add(installedEntry);
159 } else {
160 result.add(flowEntry);
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200161 }
162 }
163
164 if (inconsistentEntries.size() > 0) {
Carmelo Cascone33b27bc2018-09-09 22:56:14 -0700165 // Trigger clean up of inconsistent entries.
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800166 SharedExecutors.getSingleThreadExecutor().execute(
167 () -> cleanUpInconsistentEntries(inconsistentEntries));
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200168 }
169
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800170 return result.build();
Frank Wang0e805082017-07-21 14:37:35 +0800171 }
172
173 @Override
174 public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) {
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200175 return processFlowRules(rules, APPLY);
Frank Wang0e805082017-07-21 14:37:35 +0800176 }
177
178 @Override
179 public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) {
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200180 return processFlowRules(rules, REMOVE);
Frank Wang0e805082017-07-21 14:37:35 +0800181 }
182
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800183 private FlowEntry forgeFlowEntry(PiTableEntry entry,
184 PiCounterCellData cellData) {
185 final PiTableEntryHandle handle = PiTableEntryHandle
186 .of(deviceId, entry);
187 final Optional<PiTranslatedEntity<FlowRule, PiTableEntry>>
188 translatedEntity = translator.lookup(handle);
189 final TimedEntry<PiTableEntry> timedEntry = tableMirror.get(handle);
190
191 if (!translatedEntity.isPresent()) {
Carmelo Cascone26600972018-09-10 00:23:20 -0700192 log.warn("Table entry handle not found in translation store: {}", handle);
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800193 return null;
194 }
195
196 if (timedEntry == null) {
Carmelo Cascone26600972018-09-10 00:23:20 -0700197 log.warn("Table entry handle not found in device mirror: {}", handle);
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800198 return null;
199 }
200
201 if (cellData != null) {
202 return new DefaultFlowEntry(translatedEntity.get().original(),
Carmelo Cascone81929aa2018-04-07 01:38:55 -0700203 ADDED, timedEntry.lifeSec(), cellData.packets(),
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800204 cellData.bytes());
205 } else {
206 return new DefaultFlowEntry(translatedEntity.get().original(),
207 ADDED, timedEntry.lifeSec(), 0, 0);
208 }
209 }
210
211 private Collection<FlowEntry> getFlowEntriesFromMirror() {
212 return tableMirror.getAll(deviceId).stream()
213 .map(timedEntry -> forgeFlowEntry(
214 timedEntry.entry(), null))
Carmelo Cascone26600972018-09-10 00:23:20 -0700215 .filter(Objects::nonNull)
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800216 .collect(Collectors.toList());
217 }
218
219 private void cleanUpInconsistentEntries(Collection<PiTableEntry> piEntries) {
Carmelo Cascone26600972018-09-10 00:23:20 -0700220 log.warn("Found {} inconsistent table entries on {}, removing them...",
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800221 piEntries.size(), deviceId);
222 piEntries.forEach(entry -> {
223 log.debug(entry.toString());
Carmelo Cascone26600972018-09-10 00:23:20 -0700224 final PiTableEntryHandle handle = PiTableEntryHandle.of(deviceId, entry);
225 ENTRY_LOCKS.get(handle).lock();
226 try {
227 applyEntry(handle, entry, null, REMOVE);
228 } finally {
229 ENTRY_LOCKS.get(handle).unlock();
230 }
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800231 });
232 }
233
234 private Collection<FlowRule> processFlowRules(Collection<FlowRule> rules,
235 Operation driverOperation) {
Frank Wang0e805082017-07-21 14:37:35 +0800236
Carmelo Casconee3a7c742017-09-01 01:25:52 +0200237 if (!setupBehaviour()) {
Frank Wang0e805082017-07-21 14:37:35 +0800238 return Collections.emptyList();
239 }
240
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800241 final ImmutableList.Builder<FlowRule> result = ImmutableList.builder();
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200242
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800243 // TODO: send writes in bulk (e.g. all entries to insert, modify or delete).
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200244 // Instead of calling the client for each one of them.
245
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800246 for (FlowRule ruleToApply : rules) {
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200247
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800248 final PiTableEntry piEntryToApply;
Frank Wang0e805082017-07-21 14:37:35 +0800249 try {
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800250 piEntryToApply = translator.translate(ruleToApply, pipeconf);
Carmelo Cascone326ad2d2017-11-28 18:09:13 -0800251 } catch (PiTranslationException e) {
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800252 log.warn("Unable to translate flow rule for pipeconf '{}': {} - {}",
253 pipeconf.id(), e.getMessage(), ruleToApply);
254 // Next rule.
255 continue;
Frank Wang0e805082017-07-21 14:37:35 +0800256 }
Frank Wang0e805082017-07-21 14:37:35 +0800257
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800258 final PiTableEntryHandle handle = PiTableEntryHandle
259 .of(deviceId, piEntryToApply);
Frank Wang0e805082017-07-21 14:37:35 +0800260
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800261 // Serialize operations over the same match key/table/device ID.
Carmelo Cascone33b27bc2018-09-09 22:56:14 -0700262 ENTRY_LOCKS.get(handle).lock();
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200263 try {
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800264 if (applyEntry(handle, piEntryToApply,
265 ruleToApply, driverOperation)) {
266 result.add(ruleToApply);
Frank Wang0e805082017-07-21 14:37:35 +0800267 }
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200268 } finally {
Carmelo Cascone33b27bc2018-09-09 22:56:14 -0700269 ENTRY_LOCKS.get(handle).unlock();
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200270 }
271 }
272
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800273 return result.build();
274 }
275
276 /**
277 * Applies the given entry to the device, and returns true if the operation
278 * was successful, false otherwise.
279 */
280 private boolean applyEntry(PiTableEntryHandle handle,
281 PiTableEntry piEntryToApply,
282 FlowRule ruleToApply,
283 Operation driverOperation) {
284 // Depending on the driver operation, and if a matching rule exists on
285 // the device, decide which P4 Runtime write operation to perform for
286 // this entry.
287 final TimedEntry<PiTableEntry> piEntryOnDevice = tableMirror.get(handle);
288 final WriteOperationType p4Operation;
289 if (driverOperation == APPLY) {
290 if (piEntryOnDevice == null) {
291 // Entry is first-timer.
292 p4Operation = INSERT;
293 } else {
Carmelo Cascone3da671a2018-02-12 10:43:35 -0800294 if (driverBoolProperty(IGNORE_SAME_ENTRY_UPDATE,
295 DEFAULT_IGNORE_SAME_ENTRY_UPDATE)
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800296 && piEntryToApply.action().equals(piEntryOnDevice.entry().action())) {
297 log.debug("Ignoring re-apply of existing entry: {}", piEntryToApply);
298 p4Operation = null;
Carmelo Cascone3da671a2018-02-12 10:43:35 -0800299 } else if (driverBoolProperty(DELETE_BEFORE_UPDATE,
300 DEFAULT_DELETE_BEFORE_UPDATE)) {
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800301 // Some devices return error when updating existing
302 // entries. If requested, remove entry before
303 // re-inserting the modified one.
304 applyEntry(handle, piEntryOnDevice.entry(), null, REMOVE);
305 p4Operation = INSERT;
306 } else {
307 p4Operation = MODIFY;
308 }
309 }
310 } else {
311 p4Operation = DELETE;
312 }
313
314 if (p4Operation != null) {
315 if (writeEntry(piEntryToApply, p4Operation)) {
316 updateStores(handle, piEntryToApply, ruleToApply, p4Operation);
317 return true;
318 } else {
319 return false;
320 }
321 } else {
322 // If no operation, let's pretend we applied the rule to the device.
323 return true;
324 }
325 }
326
327 /**
328 * Performs a write operation on the device.
329 */
330 private boolean writeEntry(PiTableEntry entry,
331 WriteOperationType p4Operation) {
Carmelo Casconee5b28722018-06-22 17:28:28 +0200332 final CompletableFuture<Boolean> future = client.writeTableEntries(
333 newArrayList(entry), p4Operation, pipeconf);
334 final Boolean success = getFutureWithDeadline(
335 future, "performing table " + p4Operation.name(), null);
336 if (success == null) {
337 // Error logged by getFutureWithDeadline();
338 return false;
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800339 }
Carmelo Casconee5b28722018-06-22 17:28:28 +0200340 if (!success) {
341 log.warn("Unable to {} table entry in {}: {}",
342 p4Operation.name(), deviceId, entry);
343 }
344 return success;
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800345 }
346
347 private void updateStores(PiTableEntryHandle handle,
348 PiTableEntry entry,
349 FlowRule rule,
350 WriteOperationType p4Operation) {
351 switch (p4Operation) {
352 case INSERT:
353 case MODIFY:
354 tableMirror.put(handle, entry);
355 translator.learn(handle, new PiTranslatedEntity<>(rule, entry, handle));
356 break;
357 case DELETE:
358 tableMirror.remove(handle);
359 translator.forget(handle);
360 break;
361 default:
362 throw new IllegalArgumentException(
363 "Unknown operation " + p4Operation.name());
364 }
365 }
366
367 private Map<PiTableEntry, PiCounterCellData> readEntryCounters(
Carmelo Cascone255125d2018-04-11 14:03:22 -0700368 Collection<PiTableEntry> tableEntries) {
369 if (!driverBoolProperty(SUPPORT_TABLE_COUNTERS,
Carmelo Casconee5b28722018-06-22 17:28:28 +0200370 DEFAULT_SUPPORT_TABLE_COUNTERS)
371 || tableEntries.isEmpty()) {
Carmelo Cascone255125d2018-04-11 14:03:22 -0700372 return Collections.emptyMap();
373 }
374
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800375 Collection<PiCounterCellData> cellDatas;
Carmelo Casconee5b28722018-06-22 17:28:28 +0200376
377 if (driverBoolProperty(READ_ALL_DIRECT_COUNTERS,
378 DEFAULT_READ_ALL_DIRECT_COUNTERS)) {
379 // FIXME: read counters when dumping table entries ONOS-7596
380 cellDatas = Collections.emptyList();
381 } else {
382 Set<PiCounterCellId> cellIds = tableEntries.stream()
383 .filter(e -> tableHasCounter(e.table()))
384 .map(PiCounterCellId::ofDirect)
385 .collect(Collectors.toSet());
386 cellDatas = getFutureWithDeadline(client.readCounterCells(cellIds, pipeconf),
387 "reading table counters", Collections.emptyList());
Carmelo Cascone6a0b5a32017-11-20 23:08:32 -0800388 }
Carmelo Casconee5b28722018-06-22 17:28:28 +0200389 return cellDatas.stream()
390 .collect(Collectors.toMap(c -> c.cellId().tableEntry(), c -> c));
391
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200392 }
393
Carmelo Cascone255125d2018-04-11 14:03:22 -0700394 private boolean tableHasCounter(PiTableId tableId) {
395 return pipelineModel.table(tableId).isPresent() &&
396 !pipelineModel.table(tableId).get().counters().isEmpty();
397 }
398
Carmelo Cascone33b27bc2018-09-09 22:56:14 -0700399 private boolean tableIsConstant(PiTableId tableId) {
400 return pipelineModel.table(tableId).isPresent() &&
401 pipelineModel.table(tableId).get().isConstantTable();
402 }
403
Carmelo Cascone0b22d8f2017-07-31 07:22:27 +0200404 enum Operation {
405 APPLY, REMOVE
Frank Wang0e805082017-07-21 14:37:35 +0800406 }
Carmelo Cascone87892e22017-11-13 16:01:29 -0800407}