use of org.onosproject.p4runtime.api.P4RuntimeWriteClient.WriteResponse in project onos by opennetworkinglab.
the class P4RuntimeFlowRuleProgrammable method processFlowRules.
private Collection<FlowRule> processFlowRules(Collection<FlowRule> rules, Operation driverOperation) {
if (!setupBehaviour("processFlowRules()") || rules.isEmpty()) {
return Collections.emptyList();
}
// Created batched write request.
final WriteRequest request = client.write(p4DeviceId, pipeconf);
// For each rule, translate to PI and append to write request.
final Map<PiHandle, FlowRule> handleToRuleMap = Maps.newHashMap();
final List<FlowRule> skippedRules = Lists.newArrayList();
final CompletableFuture<WriteResponse> futureResponse;
WRITE_LOCKS.get(deviceId).lock();
try {
for (FlowRule rule : rules) {
// Translate.
final PiTableEntry entry;
try {
entry = translator.translate(rule, pipeconf);
} catch (PiTranslationException e) {
log.warn("Unable to translate flow rule for pipeconf '{}': {} [{}]", pipeconf.id(), e.getMessage(), rule);
// Next rule.
continue;
}
final PiTableEntryHandle handle = entry.handle(deviceId);
handleToRuleMap.put(handle, rule);
// Update translation store.
if (driverOperation.equals(APPLY)) {
translator.learn(handle, new PiTranslatedEntity<>(rule, entry, handle));
} else {
translator.forget(handle);
}
// Append entry to batched write request (returns false), or skip (true)
if (appendEntryToWriteRequestOrSkip(request, handle, entry, driverOperation)) {
skippedRules.add(rule);
}
}
if (request.pendingUpdates().isEmpty()) {
// All good. No need to write on device.
return rules;
}
// Update mirror.
tableMirror.applyWriteRequest(request);
// Async submit request to server.
futureResponse = request.submit();
} finally {
WRITE_LOCKS.get(deviceId).unlock();
}
// Wait for response.
final WriteResponse response = Futures.getUnchecked(futureResponse);
// Derive successfully applied flow rule from response.
final List<FlowRule> appliedRules = getAppliedFlowRules(response, handleToRuleMap, driverOperation);
// Return skipped and applied rules.
return ImmutableList.<FlowRule>builder().addAll(skippedRules).addAll(appliedRules).build();
}
Aggregations