use of org.openkilda.floodlight.api.response.SpeakerFlowSegmentResponse in project open-kilda by telstra.
the class OnReceivedInstallResponseAction 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 ? "installed" : "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) {
FlowSegmentRequestFactory flowSegmentRequest = stateMachine.getInstallCommand(commandId);
if (flowSegmentRequest != null && response instanceof FlowErrorResponse) {
FlowErrorResponse errorResponse = (FlowErrorResponse) response;
stateMachine.saveErrorToHistory("Failed to install rule", format("Failed to install the rule: commandId %s, switch %s, cookie %s. Error %s. " + "Retrying (attempt %d)", commandId, errorResponse.getSwitchId(), flowSegmentRequest.getCookie(), errorResponse, attempt));
stateMachine.getCarrier().sendSpeakerRequest(flowSegmentRequest.makeInstallRequest(commandId));
} else if (response instanceof SpeakerCommandResponse) {
String commandName = isInstallCommand ? "install" : "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 ? "install" : "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_INSTALLED);
} 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.response.SpeakerFlowSegmentResponse in project open-kilda by telstra.
the class OnReceivedInstallResponseAction method perform.
@Override
protected void perform(State from, State to, Event event, FlowMirrorPointCreateContext context, FlowMirrorPointCreateFsm stateMachine) {
SpeakerFlowSegmentResponse response = context.getSpeakerFlowResponse();
UUID commandId = response.getCommandId();
FlowSegmentRequestFactory command = stateMachine.getCommands().get(commandId);
if (!stateMachine.getPendingCommands().containsKey(commandId) || command == null) {
log.info("Received a response for unexpected command: {}", response);
return;
}
if (response.isSuccess()) {
stateMachine.getPendingCommands().remove(commandId);
stateMachine.saveActionToHistory("Rule was installed", format("The rule was installed: switch %s, cookie %s", response.getSwitchId(), command.getCookie()));
} else {
FlowErrorResponse errorResponse = (FlowErrorResponse) response;
int retries = stateMachine.getRetriedCommands().getOrDefault(commandId, 0);
if (retries < speakerCommandRetriesLimit) {
stateMachine.getRetriedCommands().put(commandId, ++retries);
stateMachine.saveErrorToHistory(FAILED_TO_INSTALL_RULE_ACTION, format("Failed to install the rule: commandId %s, switch %s, cookie %s. Error %s. " + "Retrying (attempt %d)", commandId, errorResponse.getSwitchId(), command.getCookie(), errorResponse, retries));
stateMachine.getCarrier().sendSpeakerRequest(command.makeInstallRequest(commandId));
} else {
stateMachine.getPendingCommands().remove(commandId);
stateMachine.saveErrorToHistory(FAILED_TO_INSTALL_RULE_ACTION, format("Failed to install the rule: commandId %s, switch %s, cookie %s. Error: %s", commandId, errorResponse.getSwitchId(), command.getCookie(), errorResponse));
stateMachine.getFailedCommands().put(commandId, errorResponse);
}
}
if (stateMachine.getPendingCommands().isEmpty()) {
if (stateMachine.getFailedCommands().isEmpty()) {
log.debug("Received responses for all pending install commands of the flow {}", stateMachine.getFlowId());
stateMachine.fire(Event.RULES_UPDATED);
} else {
String errorMessage = format("Received error response(s) for %d install commands", stateMachine.getFailedCommands().size());
stateMachine.saveErrorToHistory(errorMessage);
stateMachine.fireError(errorMessage);
}
}
}
Aggregations