use of org.openkilda.wfm.topology.flowhs.fsm.yflow.update.YFlowUpdateFsm in project open-kilda by telstra.
the class YFlowUpdateService method handleAsyncResponse.
/**
* Handles async response from worker.
*
* @param key command identifier.
*/
public void handleAsyncResponse(@NonNull String key, @NonNull SpeakerResponse speakerResponse) throws UnknownKeyException {
log.debug("Received flow command response {}", speakerResponse);
YFlowUpdateFsm fsm = fsmRegister.getFsmByKey(key).orElseThrow(() -> new UnknownKeyException(key));
if (speakerResponse instanceof SpeakerFlowSegmentResponse) {
SpeakerFlowSegmentResponse response = (SpeakerFlowSegmentResponse) speakerResponse;
String flowId = response.getMetadata().getFlowId();
if (fsm.getUpdatingSubFlows().contains(flowId)) {
flowUpdateService.handleAsyncResponseByFlowId(flowId, response);
}
} else if (speakerResponse instanceof SpeakerCommandResponse) {
SpeakerCommandResponse response = (SpeakerCommandResponse) speakerResponse;
YFlowUpdateContext context = YFlowUpdateContext.builder().speakerResponse(response).build();
fsmExecutor.fire(fsm, Event.RESPONSE_RECEIVED, context);
} else {
log.debug("Received unexpected speaker response: {}", speakerResponse);
}
// After handling an event by FlowUpdate service, we should propagate execution to the FSM.
if (!fsm.isTerminated()) {
fsmExecutor.fire(fsm, Event.NEXT);
}
removeIfFinished(fsm, key);
}
use of org.openkilda.wfm.topology.flowhs.fsm.yflow.update.YFlowUpdateFsm in project open-kilda by telstra.
the class RevertSubFlowsAction method perform.
@Override
public void perform(State from, State to, Event event, YFlowUpdateContext context, YFlowUpdateFsm stateMachine) {
stateMachine.clearUpdatingSubFlows();
stateMachine.clearAllocatedSubFlows();
stateMachine.clearFailedSubFlows();
String yFlowId = stateMachine.getYFlowId();
Collection<RequestedFlow> originalFlows = YFlowRequestMapper.INSTANCE.toRequestedFlows(stateMachine.getOriginalFlow());
stateMachine.setRequestedFlows(originalFlows);
log.debug("Start reverting {} sub-flows for y-flow {}", originalFlows.size(), stateMachine.getYFlowId());
originalFlows.stream().filter(originalFlow -> originalFlow.getFlowId().equals(stateMachine.getMainAffinityFlowId())).forEach(originalFlow -> {
String subFlowId = originalFlow.getFlowId();
CommandContext flowContext = stateMachine.getCommandContext().fork(subFlowId);
stateMachine.addUpdatingSubFlow(subFlowId);
stateMachine.notifyEventListeners(listener -> listener.onSubFlowProcessingStart(yFlowId, subFlowId));
flowUpdateService.startFlowUpdating(flowContext, originalFlow, yFlowId);
});
}
use of org.openkilda.wfm.topology.flowhs.fsm.yflow.update.YFlowUpdateFsm in project open-kilda by telstra.
the class OnReceivedInstallResponseAction method perform.
@Override
public void perform(State from, State to, Event event, YFlowUpdateContext context, YFlowUpdateFsm stateMachine) {
SpeakerCommandResponse response = context.getSpeakerResponse();
UUID commandId = response.getCommandId();
if (!stateMachine.hasPendingCommand(commandId)) {
log.info("Received a response for unexpected command: {}", response);
return;
}
if (response.isSuccess()) {
stateMachine.removePendingCommand(commandId);
stateMachine.saveActionToHistory("Rule was installed", format("The rule was installed: switch %s", response.getSwitchId()));
} else {
Optional<InstallSpeakerCommandsRequest> request = stateMachine.getInstallSpeakerCommand(commandId);
int attempt = stateMachine.doRetryForCommand(commandId);
if (attempt <= speakerCommandRetriesLimit && request.isPresent()) {
response.getFailedCommandIds().forEach((uuid, message) -> stateMachine.saveErrorToHistory(FAILED_TO_INSTALL_RULE_ACTION, format("Failed to install the rule: commandId %s, ruleId %s, switch %s. " + "Error: %s. Retrying (attempt %d)", commandId, uuid, response.getSwitchId(), message, attempt)));
Set<UUID> failedUuids = response.getFailedCommandIds().keySet();
InstallSpeakerCommandsRequest installRequest = request.get();
List<OfCommand> commands = installRequest.getCommands().stream().filter(command -> command instanceof FlowCommand && failedUuids.contains(((FlowCommand) command).getData().getUuid()) || command instanceof MeterCommand && failedUuids.contains(((MeterCommand) command).getData().getUuid()) || command instanceof GroupCommand && failedUuids.contains(((GroupCommand) command).getData().getUuid())).collect(Collectors.toList());
stateMachine.getCarrier().sendSpeakerRequest(installRequest.toBuilder().commands(commands).build());
} else {
stateMachine.addFailedCommand(commandId, response);
stateMachine.removePendingCommand(commandId);
response.getFailedCommandIds().forEach((uuid, message) -> stateMachine.saveErrorToHistory(FAILED_TO_INSTALL_RULE_ACTION, format("Failed to install the rule: commandId %s, ruleId %s, switch %s. Error: %s", commandId, uuid, response.getSwitchId(), message)));
}
}
if (stateMachine.getPendingCommands().isEmpty()) {
if (stateMachine.getFailedCommands().isEmpty()) {
log.debug("Received responses for all pending install commands of the y-flow {}", stateMachine.getYFlowId());
stateMachine.fire(Event.ALL_YFLOW_METERS_INSTALLED);
} else {
String errorMessage = format("Received error response(s) for %d install commands", stateMachine.getFailedCommands().size());
stateMachine.saveErrorToHistory(errorMessage);
stateMachine.fireError(errorMessage);
}
}
}
use of org.openkilda.wfm.topology.flowhs.fsm.yflow.update.YFlowUpdateFsm in project open-kilda by telstra.
the class OnReceivedRemoveResponseAction method perform.
@Override
public void perform(State from, State to, Event event, YFlowUpdateContext context, YFlowUpdateFsm stateMachine) {
SpeakerCommandResponse response = context.getSpeakerResponse();
UUID commandId = response.getCommandId();
if (!stateMachine.hasPendingCommand(commandId)) {
log.info("Received a response for unexpected command: {}", response);
return;
}
if (response.isSuccess()) {
stateMachine.removePendingCommand(commandId);
stateMachine.saveActionToHistory("Rule was deleted", format("The rule was removed: switch %s", response.getSwitchId()));
} else {
Optional<DeleteSpeakerCommandsRequest> request = stateMachine.getDeleteSpeakerCommand(commandId);
int attempt = stateMachine.doRetryForCommand(commandId);
if (attempt <= speakerCommandRetriesLimit && request.isPresent()) {
response.getFailedCommandIds().forEach((uuid, message) -> stateMachine.saveErrorToHistory(FAILED_TO_REMOVE_RULE_ACTION, format("Failed to remove the rule: commandId %s, ruleId %s, switch %s. " + "Error: %s. Retrying (attempt %d)", commandId, uuid, response.getSwitchId(), message, attempt)));
Set<UUID> failedUuids = response.getFailedCommandIds().keySet();
DeleteSpeakerCommandsRequest deleteRequest = request.get();
List<OfCommand> commands = deleteRequest.getCommands().stream().filter(command -> command instanceof FlowCommand && failedUuids.contains(((FlowCommand) command).getData().getUuid()) || command instanceof MeterCommand && failedUuids.contains(((MeterCommand) command).getData().getUuid()) || command instanceof GroupCommand && failedUuids.contains(((GroupCommand) command).getData().getUuid())).collect(Collectors.toList());
stateMachine.getCarrier().sendSpeakerRequest(deleteRequest.toBuilder().commands(commands).build());
} else {
stateMachine.addFailedCommand(commandId, response);
stateMachine.removePendingCommand(commandId);
response.getFailedCommandIds().forEach((uuid, message) -> stateMachine.saveErrorToHistory(FAILED_TO_REMOVE_RULE_ACTION, format("Failed to remove the rule: commandId %s, ruleId %s, switch %s. Error: %s", commandId, uuid, response.getSwitchId(), message)));
}
}
if (stateMachine.getPendingCommands().isEmpty()) {
if (stateMachine.getFailedCommands().isEmpty()) {
log.debug("Received responses for all pending remove commands of the y-flow {}", stateMachine.getYFlowId());
stateMachine.fire(Event.YPOINT_METERS_REMOVED);
} else {
String errorMessage = format("Received error response(s) for %d remove commands", stateMachine.getFailedCommands().size());
stateMachine.saveErrorToHistory(errorMessage);
stateMachine.fireError(errorMessage);
}
}
}
use of org.openkilda.wfm.topology.flowhs.fsm.yflow.update.YFlowUpdateFsm in project open-kilda by telstra.
the class YFlowUpdateService method handleRequest.
/**
* Handles request for y-flow updating.
*
* @param key command identifier.
* @param request request data.
*/
public void handleRequest(@NonNull String key, @NonNull CommandContext commandContext, @NonNull YFlowRequest request) throws DuplicateKeyException {
String yFlowId = request.getYFlowId();
log.debug("Handling y-flow update request with key {} and flow ID: {}", key, request.getYFlowId());
if (fsmRegister.hasRegisteredFsmWithKey(key)) {
throw new DuplicateKeyException(key, "There's another active FSM with the same key");
}
if (fsmRegister.hasRegisteredFsmWithFlowId(yFlowId)) {
sendErrorResponseToNorthbound(ErrorType.ALREADY_EXISTS, "Could not update y-flow", format("Y-flow %s is already updating now", yFlowId), commandContext);
log.error("Attempt to create a FSM with key {}, while there's another active FSM for the same yFlowId {}.", key, yFlowId);
return;
}
if (yFlowId == null) {
yFlowId = generateFlowId(prefixForGeneratedYFlowId);
request.setYFlowId(yFlowId);
}
request.getSubFlows().forEach(subFlow -> {
if (subFlow.getFlowId() == null) {
subFlow.setFlowId(generateFlowId(prefixForGeneratedSubFlowId));
}
});
if (request.getEncapsulationType() == null) {
request.setEncapsulationType(kildaConfigurationRepository.getOrDefault().getFlowEncapsulationType());
}
if (request.getPathComputationStrategy() == null) {
request.setPathComputationStrategy(kildaConfigurationRepository.getOrDefault().getPathComputationStrategy());
}
YFlowUpdateFsm fsm = fsmFactory.newInstance(commandContext, yFlowId, eventListeners);
fsmRegister.registerFsm(key, fsm);
YFlowUpdateContext context = YFlowUpdateContext.builder().targetFlow(request).build();
fsm.start(context);
fsmExecutor.fire(fsm, Event.NEXT, context);
removeIfFinished(fsm, key);
}
Aggregations