use of org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory in project open-kilda by telstra.
the class SpeakerFlowSegmentRequestBuilder method makePathRequests.
@SuppressWarnings("squid:S00107")
private List<FlowSegmentRequestFactory> makePathRequests(@NonNull Flow flow, @NonNull FlowPath path, CommandContext context, FlowTransitEncapsulation encapsulation, boolean doIngress, boolean doTransit, boolean doEgress, RulesContext rulesContext, MirrorContext mirrorContext) {
final FlowSideAdapter ingressSide = FlowSideAdapter.makeIngressAdapter(flow, path);
final FlowSideAdapter egressSide = FlowSideAdapter.makeEgressAdapter(flow, path);
final List<FlowSegmentRequestFactory> requests = new ArrayList<>();
PathSegment lastSegment = null;
for (PathSegment segment : path.getSegments()) {
if (lastSegment == null) {
if (doIngress) {
requests.addAll(makeIngressSegmentRequests(context, path, encapsulation, ingressSide, segment, egressSide, rulesContext, mirrorContext));
if (ingressLoopRuleRequired(flow, ingressSide)) {
requests.addAll(makeLoopRequests(context, path, encapsulation, ingressSide, egressSide, segment));
}
}
} else {
if (doTransit) {
requests.add(makeTransitSegmentRequest(context, path, encapsulation, lastSegment, segment));
}
}
lastSegment = segment;
}
if (lastSegment != null) {
if (doEgress) {
requests.addAll(makeEgressSegmentRequests(context, path, encapsulation, lastSegment, egressSide, ingressSide, mirrorContext));
}
} else if (flow.isOneSwitchFlow()) {
// one switch flow (path without path segments)
requests.addAll(makeOneSwitchRequest(context, path, ingressSide, egressSide, rulesContext, mirrorContext));
if (singleSwitchLoopRuleRequired(flow)) {
requests.add(makeSingleSwitchIngressLoopRequest(context, path, ingressSide));
}
}
return requests;
}
use of org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory in project open-kilda by telstra.
the class EmitNonIngressRulesVerifyRequestsAction method perform.
@Override
public void perform(State from, State to, Event event, FlowRerouteContext context, FlowRerouteFsm stateMachine) {
Map<UUID, FlowSegmentRequestFactory> requestsStorage = stateMachine.getNonIngressCommands();
List<FlowSegmentRequestFactory> requestFactories = new ArrayList<>(requestsStorage.values());
requestsStorage.clear();
stateMachine.clearPendingAndRetriedAndFailedCommands();
if (requestFactories.isEmpty()) {
stateMachine.saveActionToHistory("No need to validate non ingress rules");
stateMachine.fire(Event.RULES_VALIDATED);
} else {
for (FlowSegmentRequestFactory factory : requestFactories) {
FlowSegmentRequest request = factory.makeVerifyRequest(commandIdGenerator.generate());
// TODO ensure no conflicts
requestsStorage.put(request.getCommandId(), factory);
stateMachine.getCarrier().sendSpeakerRequest(request);
}
requestsStorage.forEach((key, value) -> stateMachine.addPendingCommand(key, value.getSwitchId()));
stateMachine.saveActionToHistory("Started validation of installed non ingress rules");
}
}
use of org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory in project open-kilda by telstra.
the class InstallIngressRulesAction method perform.
@Override
protected void perform(State from, State to, Event event, FlowPathSwapContext context, FlowPathSwapFsm stateMachine) {
String flowId = stateMachine.getFlowId();
Flow flow = getFlow(flowId);
FlowCommandBuilder commandBuilder = commandBuilderFactory.getBuilder(flow.getEncapsulationType());
FlowPath newPrimaryForward = flow.getForwardPath();
FlowPath newPrimaryReverse = flow.getReversePath();
SpeakerRequestBuildContext speakerContext = buildBaseSpeakerContextForInstall(flow.getSrcSwitchId(), flow.getDestSwitchId());
Collection<FlowSegmentRequestFactory> commands = new ArrayList<>(commandBuilder.buildIngressOnly(stateMachine.getCommandContext(), flow, newPrimaryForward, newPrimaryReverse, speakerContext));
commands.addAll(commandBuilder.buildEgressOnly(stateMachine.getCommandContext(), flow, newPrimaryForward, newPrimaryReverse).stream().filter(f -> f instanceof TransitFlowLoopSegmentRequestFactory || f instanceof EgressMirrorFlowSegmentRequestFactory).collect(Collectors.toList()));
// Installation of ingress rules for protected paths is skipped. These paths are activated on swap.
stateMachine.clearPendingAndRetriedAndFailedCommands();
SpeakerInstallSegmentEmitter.INSTANCE.emitBatch(stateMachine.getCarrier(), commands, stateMachine.getIngressCommands());
stateMachine.getIngressCommands().forEach((key, value) -> stateMachine.addPendingCommand(key, value.getSwitchId()));
stateMachine.saveActionToHistory("Commands for installing ingress rules have been sent");
}
use of org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory in project open-kilda by telstra.
the class OnReceivedRemoveOrRevertResponseAction method perform.
@Override
protected void perform(State from, State to, Event event, FlowPathSwapContext context, FlowPathSwapFsm stateMachine) {
SpeakerResponse response = context.getSpeakerResponse();
UUID commandId = response.getCommandId();
if (!stateMachine.hasPendingCommand(commandId)) {
log.info("Received a response for unexpected command: {}", response);
return;
}
boolean isInstallCommand = stateMachine.getInstallCommand(commandId) != null || stateMachine.getInstallSpeakerCommand(commandId).isPresent();
if (response.isSuccess()) {
stateMachine.removePendingCommand(commandId);
String commandName = isInstallCommand ? "re-installed (reverted)" : "deleted";
if (response instanceof SpeakerFlowSegmentResponse) {
stateMachine.saveActionToHistory("Rule was " + commandName, format("The rule was %s: switch %s, cookie %s", commandName, response.getSwitchId(), ((SpeakerFlowSegmentResponse) response).getCookie()));
} else {
stateMachine.saveActionToHistory("Rule was " + commandName, format("The rule was %s: switch %s", commandName, response.getSwitchId()));
}
} else {
int attempt = stateMachine.doRetryForCommand(commandId);
if (attempt <= speakerCommandRetriesLimit) {
if (response instanceof FlowErrorResponse) {
FlowErrorResponse errorResponse = (FlowErrorResponse) response;
if (isInstallCommand) {
FlowSegmentRequestFactory installCommand = stateMachine.getInstallCommand(commandId);
stateMachine.saveErrorToHistory("Failed to re-install (revert) rule", format("Failed to install the rule: commandId %s, switch %s, cookie %s. Error %s. " + "Retrying (attempt %d)", commandId, errorResponse.getSwitchId(), installCommand.getCookie(), errorResponse, attempt));
stateMachine.getCarrier().sendSpeakerRequest(installCommand.makeInstallRequest(commandId));
} else {
FlowSegmentRequestFactory removeCommand = stateMachine.getRemoveCommand(commandId);
stateMachine.saveErrorToHistory("Failed to delete rule", format("Failed to remove the rule: commandId %s, switch %s, cookie %s. Error %s. " + "Retrying (attempt %d)", commandId, errorResponse.getSwitchId(), removeCommand.getCookie(), errorResponse, attempt));
stateMachine.getCarrier().sendSpeakerRequest(removeCommand.makeRemoveRequest(commandId));
}
} else if (response instanceof SpeakerCommandResponse) {
String commandName = isInstallCommand ? "re-install (revert)" : "delete";
SpeakerCommandResponse speakerCommandResponse = (SpeakerCommandResponse) response;
speakerCommandResponse.getFailedCommandIds().forEach((uuid, message) -> stateMachine.saveErrorToHistory("Failed to " + commandName + " rule", format("Failed to %s the rule: commandId %s, ruleId %s, switch %s. " + "Error %s. Retrying (attempt %d)", commandName, commandId, uuid, response.getSwitchId(), message, attempt)));
Set<UUID> failedUuids = speakerCommandResponse.getFailedCommandIds().keySet();
stateMachine.getInstallSpeakerCommand(commandId).ifPresent(command -> stateMachine.getCarrier().sendSpeakerRequest(command.toBuilder().commands(filterOfCommands(command.getCommands(), failedUuids)).build()));
stateMachine.getDeleteSpeakerCommand(commandId).ifPresent(command -> stateMachine.getCarrier().sendSpeakerRequest(command.toBuilder().commands(filterOfCommands(command.getCommands(), failedUuids)).build()));
} else {
log.warn("Received a unknown response: {}", response);
return;
}
} else {
stateMachine.addFailedCommand(commandId, response);
stateMachine.removePendingCommand(commandId);
String commandName = isInstallCommand ? "re-install (revert)" : "delete";
if (response instanceof FlowErrorResponse) {
stateMachine.saveErrorToHistory("Failed to " + commandName + " rule", format("Failed to %s the rule: commandId %s, switch %s, cookie %s. Error %s.", commandName, commandId, response.getSwitchId(), ((FlowErrorResponse) response).getCookie(), response));
} else if (response instanceof SpeakerCommandResponse) {
((SpeakerCommandResponse) response).getFailedCommandIds().forEach((uuid, message) -> stateMachine.saveErrorToHistory("Failed to " + commandName + " rule", format("Failed to %s the rule: commandId %s, ruleId %s, switch %s. Error %s.", commandName, commandId, uuid, response.getSwitchId(), message)));
} else {
log.warn("Received a unknown response: {}", response);
return;
}
}
}
if (stateMachine.getPendingCommands().isEmpty()) {
if (stateMachine.getFailedCommands().isEmpty()) {
log.debug("Received responses for all pending install / delete commands of the flow {}", stateMachine.getFlowId());
stateMachine.fire(Event.RULES_REMOVED);
} else {
String errorMessage = format("Received error response(s) for %d install / delete commands", stateMachine.getFailedCommands().size());
stateMachine.saveErrorToHistory(errorMessage);
stateMachine.fireError(errorMessage);
}
}
}
use of org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory in project open-kilda by telstra.
the class HandleNotCompletedCommandsAction method perform.
@Override
public void perform(State from, State to, Event event, FlowMirrorPointDeleteContext context, FlowMirrorPointDeleteFsm stateMachine) {
for (UUID commandId : stateMachine.getPendingCommands().keySet()) {
FlowSegmentRequestFactory command = stateMachine.getCommands().get(commandId);
if (command != null) {
stateMachine.saveErrorToHistory("Command is not finished yet", format("Completing the removal operation although the command may not be " + "finished yet: commandId %s, switch %s, cookie %s", commandId, command.getSwitchId(), command.getCookie()));
}
}
log.debug("Abandoning all pending commands: {}", stateMachine.getPendingCommands());
stateMachine.getPendingCommands().clear();
}
Aggregations