Search in sources :

Example 1 with State

use of org.openkilda.wfm.topology.flowhs.fsm.yflow.delete.YFlowDeleteFsm.State in project open-kilda by telstra.

the class OnReceivedRemoveResponseAction method perform.

@Override
public void perform(State from, State to, Event event, YFlowDeleteContext context, YFlowDeleteFsm 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 y-flow {}", stateMachine.getYFlowId());
            stateMachine.fire(Event.ALL_YFLOW_METERS_REMOVED);
        } else {
            String errorMessage = format("Received error response(s) for %d remove commands", stateMachine.getFailedCommands().size());
            stateMachine.saveErrorToHistory(errorMessage);
            stateMachine.fireError(errorMessage);
        }
    }
}
Also used : OfCommand(org.openkilda.floodlight.api.request.rulemanager.OfCommand) FlowCommand(org.openkilda.floodlight.api.request.rulemanager.FlowCommand) Event(org.openkilda.wfm.topology.flowhs.fsm.yflow.delete.YFlowDeleteFsm.Event) Set(java.util.Set) UUID(java.util.UUID) MeterCommand(org.openkilda.floodlight.api.request.rulemanager.MeterCommand) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) YFlowDeleteContext(org.openkilda.wfm.topology.flowhs.fsm.yflow.delete.YFlowDeleteContext) OfCommand(org.openkilda.floodlight.api.request.rulemanager.OfCommand) DeleteSpeakerCommandsRequest(org.openkilda.floodlight.api.request.rulemanager.DeleteSpeakerCommandsRequest) SpeakerCommandResponse(org.openkilda.floodlight.api.response.rulemanager.SpeakerCommandResponse) State(org.openkilda.wfm.topology.flowhs.fsm.yflow.delete.YFlowDeleteFsm.State) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) HistoryRecordingAction(org.openkilda.wfm.topology.flowhs.fsm.common.actions.HistoryRecordingAction) YFlowDeleteFsm(org.openkilda.wfm.topology.flowhs.fsm.yflow.delete.YFlowDeleteFsm) Optional(java.util.Optional) GroupCommand(org.openkilda.floodlight.api.request.rulemanager.GroupCommand) FlowCommand(org.openkilda.floodlight.api.request.rulemanager.FlowCommand) SpeakerCommandResponse(org.openkilda.floodlight.api.response.rulemanager.SpeakerCommandResponse) GroupCommand(org.openkilda.floodlight.api.request.rulemanager.GroupCommand) MeterCommand(org.openkilda.floodlight.api.request.rulemanager.MeterCommand) UUID(java.util.UUID) DeleteSpeakerCommandsRequest(org.openkilda.floodlight.api.request.rulemanager.DeleteSpeakerCommandsRequest)

Aggregations

String.format (java.lang.String.format)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 Slf4j (lombok.extern.slf4j.Slf4j)1 DeleteSpeakerCommandsRequest (org.openkilda.floodlight.api.request.rulemanager.DeleteSpeakerCommandsRequest)1 FlowCommand (org.openkilda.floodlight.api.request.rulemanager.FlowCommand)1 GroupCommand (org.openkilda.floodlight.api.request.rulemanager.GroupCommand)1 MeterCommand (org.openkilda.floodlight.api.request.rulemanager.MeterCommand)1 OfCommand (org.openkilda.floodlight.api.request.rulemanager.OfCommand)1 SpeakerCommandResponse (org.openkilda.floodlight.api.response.rulemanager.SpeakerCommandResponse)1 HistoryRecordingAction (org.openkilda.wfm.topology.flowhs.fsm.common.actions.HistoryRecordingAction)1 YFlowDeleteContext (org.openkilda.wfm.topology.flowhs.fsm.yflow.delete.YFlowDeleteContext)1 YFlowDeleteFsm (org.openkilda.wfm.topology.flowhs.fsm.yflow.delete.YFlowDeleteFsm)1 Event (org.openkilda.wfm.topology.flowhs.fsm.yflow.delete.YFlowDeleteFsm.Event)1 State (org.openkilda.wfm.topology.flowhs.fsm.yflow.delete.YFlowDeleteFsm.State)1