Search in sources :

Example 46 with FlowSegmentRequestFactory

use of org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory in project open-kilda by telstra.

the class OnReceivedValidateResponseAction 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 validated", format("The installed rule has been validated successfully: switch %s, cookie %s", command.getSwitchId(), command.getCookie()));
    } else {
        FlowErrorResponse errorResponse = (FlowErrorResponse) response;
        int retries = stateMachine.getRetriedCommands().getOrDefault(commandId, 0);
        if (retries < speakerCommandRetriesLimit && errorResponse.getErrorCode() != FlowErrorResponse.ErrorCode.MISSING_OF_FLOWS) {
            stateMachine.getRetriedCommands().put(commandId, ++retries);
            stateMachine.saveErrorToHistory(RULE_VALIDATION_FAILED_ACTION, format("Failed to validate non ingress rule: commandId %s, switch %s, cookie %s. Error %s. " + "Retrying (attempt %d)", commandId, errorResponse.getSwitchId(), command.getCookie(), errorResponse, retries));
            stateMachine.getCarrier().sendSpeakerRequest(command.makeVerifyRequest(commandId));
        } else {
            stateMachine.getPendingCommands().remove(commandId);
            stateMachine.saveErrorToHistory(RULE_VALIDATION_FAILED_ACTION, format("Failed to validate non ingress rule: commandId %s, switch %s, cookie %s. Error %s", commandId, errorResponse.getSwitchId(), command.getCookie(), errorResponse));
            stateMachine.getFailedValidationResponses().put(commandId, response);
        }
    }
    if (stateMachine.getPendingCommands().isEmpty()) {
        if (stateMachine.getFailedValidationResponses().isEmpty()) {
            log.debug("Installed rules have been validated for flow {}", stateMachine.getFlowId());
            stateMachine.fire(Event.RULES_VALIDATED);
        } else {
            stateMachine.saveErrorToHistory(format("Found missing rules or received error response(s) on %d validation commands", stateMachine.getFailedValidationResponses().size()));
            stateMachine.fire(Event.MISSING_RULE_FOUND);
        }
    }
}
Also used : FlowSegmentRequestFactory(org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory) FlowErrorResponse(org.openkilda.floodlight.flow.response.FlowErrorResponse) SpeakerFlowSegmentResponse(org.openkilda.floodlight.api.response.SpeakerFlowSegmentResponse) UUID(java.util.UUID)

Example 47 with FlowSegmentRequestFactory

use of org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory in project open-kilda by telstra.

the class EmitCommandRequestsAction method perform.

@Override
protected void perform(State from, State to, Event event, FlowMirrorPointDeleteContext context, FlowMirrorPointDeleteFsm stateMachine) {
    String flowId = stateMachine.getFlowId();
    Flow flow = getFlow(flowId);
    PathId flowPathId = stateMachine.getFlowPathId();
    SwitchId mirrorSwitchId = stateMachine.getMirrorSwitchId();
    FlowMirrorPoints mirrorPoints = flowMirrorPointsRepository.findByPathIdAndSwitchId(flowPathId, mirrorSwitchId).orElseThrow(() -> new FlowProcessingException(ErrorType.NOT_FOUND, format("Flow mirror points for flow path %s and mirror switch id %s not found", flowPathId, mirrorSwitchId)));
    FlowCommandBuilder commandBuilder = commandBuilderFactory.getBuilder(flow.getEncapsulationType());
    Collection<FlowSegmentRequestFactory> commands = buildCommands(commandBuilder, stateMachine, flow, mirrorPoints);
    // emitting
    SpeakerRequestEmitter requestEmitter;
    if (mirrorPoints.getMirrorPaths().isEmpty()) {
        requestEmitter = SpeakerRemoveSegmentEmitter.INSTANCE;
    } else {
        requestEmitter = SpeakerInstallSegmentEmitter.INSTANCE;
    }
    requestEmitter.emitBatch(stateMachine.getCarrier(), commands, stateMachine.getCommands());
    stateMachine.getCommands().forEach((key, value) -> stateMachine.getPendingCommands().put(key, value.getSwitchId()));
    if (commands.isEmpty()) {
        stateMachine.saveActionToHistory("No need to remove group");
    } else {
        stateMachine.saveActionToHistory("Commands for removing group have been sent");
    }
}
Also used : PathId(org.openkilda.model.PathId) SpeakerRequestEmitter(org.openkilda.wfm.topology.flowhs.utils.SpeakerRequestEmitter) FlowSegmentRequestFactory(org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory) FlowProcessingException(org.openkilda.wfm.topology.flowhs.exception.FlowProcessingException) FlowMirrorPoints(org.openkilda.model.FlowMirrorPoints) FlowCommandBuilder(org.openkilda.wfm.topology.flowhs.service.FlowCommandBuilder) SwitchId(org.openkilda.model.SwitchId) Flow(org.openkilda.model.Flow)

Example 48 with FlowSegmentRequestFactory

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, FlowRerouteContext context, FlowRerouteFsm stateMachine) {
    FlowSegmentRequestFactory requestFactory;
    for (UUID commandId : stateMachine.getPendingCommands().keySet()) {
        requestFactory = stateMachine.getRemoveCommands().get(commandId);
        if (requestFactory != null) {
            stateMachine.saveErrorToHistory("Command is not finished yet", format("Completing the reroute operation although the remove command may not be " + "finished yet: commandId %s, switch %s, cookie %s", commandId, requestFactory.getSwitchId(), requestFactory.getCookie()));
        } else {
            requestFactory = stateMachine.getIngressCommands().get(commandId);
            stateMachine.saveErrorToHistory("Command is not finished yet", format("Completing the reroute operation although the install command may not be " + "finished yet: commandId %s, switch %s, cookie %s", commandId, requestFactory.getSwitchId(), requestFactory.getCookie()));
        }
    }
    for (SpeakerResponse errorResponse : stateMachine.getFailedCommands().values()) {
        log.warn("Receive error response from {} for command {}: {}", errorResponse.getSwitchId(), errorResponse.getCommandId(), errorResponse);
    }
    log.debug("Abandoning all pending commands: {}", stateMachine.getPendingCommands());
    stateMachine.clearPendingCommands();
}
Also used : FlowSegmentRequestFactory(org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory) SpeakerResponse(org.openkilda.floodlight.api.response.SpeakerResponse) UUID(java.util.UUID)

Example 49 with FlowSegmentRequestFactory

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, FlowRerouteContext context, FlowRerouteFsm stateMachine) {
    SpeakerFlowSegmentResponse response = context.getSpeakerFlowResponse();
    UUID commandId = response.getCommandId();
    FlowSegmentRequestFactory removeCommand = stateMachine.getRemoveCommands().get(commandId);
    FlowSegmentRequestFactory installCommand = stateMachine.getInstallCommand(commandId);
    if (!stateMachine.getPendingCommands().containsKey(commandId) || (removeCommand == null && installCommand == null)) {
        log.info("Received a response for unexpected command: {}", response);
        return;
    }
    MeterRegistryHolder.getRegistry().ifPresent(registry -> {
        if (response.getRequestCreateTime() > 0) {
            long roundtrip = Duration.between(Instant.ofEpochMilli(response.getRequestCreateTime()), Instant.now()).toNanos();
            if (roundtrip > 0) {
                registry.timer("fsm.remove_command.roundtrip").record(roundtrip, TimeUnit.NANOSECONDS);
            }
        }
        if (response.getExecutionTime() > 0) {
            registry.timer("fsm.remove_command.floodlight_execution").record(response.getExecutionTime(), TimeUnit.NANOSECONDS);
        }
    });
    if (response.isSuccess()) {
        stateMachine.removePendingCommand(commandId);
        if (removeCommand != null) {
            stateMachine.saveActionToHistory("Rule was deleted", format("The rule was removed: switch %s, cookie %s", response.getSwitchId(), removeCommand.getCookie()));
        } else {
            stateMachine.saveActionToHistory("Rule was re-installed (reverted)", format("The rule was installed: switch %s, cookie %s", response.getSwitchId(), installCommand.getCookie()));
        }
    } else {
        FlowErrorResponse errorResponse = (FlowErrorResponse) response;
        int attempt = stateMachine.doRetryForCommand(commandId);
        if (attempt <= speakerCommandRetriesLimit) {
            if (removeCommand != null) {
                stateMachine.saveErrorToHistory("Failed to remove 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 {
                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 {
            stateMachine.removePendingCommand(commandId);
            if (removeCommand != null) {
                stateMachine.saveErrorToHistory("Failed to remove rule", format("Failed to remove the rule: commandId %s, switch %s, cookie %s. Error: %s", commandId, errorResponse.getSwitchId(), removeCommand.getCookie(), errorResponse));
            } else {
                stateMachine.saveErrorToHistory("Failed to re-install rule", format("Failed to install the rule: commandId %s, switch %s, cookie %s. Error: %s", commandId, errorResponse.getSwitchId(), installCommand.getCookie(), errorResponse));
            }
            stateMachine.addFailedCommand(commandId, errorResponse);
        }
    }
    if (stateMachine.getPendingCommands().isEmpty()) {
        if (stateMachine.getFailedCommands().isEmpty()) {
            log.debug("Received responses for all pending remove / re-install commands of the flow {}", stateMachine.getFlowId());
            stateMachine.fire(Event.RULES_REMOVED);
        } else {
            String errorMessage = format("Received error response(s) for %d remove / re-install commands", stateMachine.getFailedCommands().size());
            stateMachine.saveErrorToHistory(errorMessage);
            stateMachine.fireError(errorMessage);
        }
    }
}
Also used : FlowSegmentRequestFactory(org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory) FlowErrorResponse(org.openkilda.floodlight.flow.response.FlowErrorResponse) SpeakerFlowSegmentResponse(org.openkilda.floodlight.api.response.SpeakerFlowSegmentResponse) UUID(java.util.UUID)

Example 50 with FlowSegmentRequestFactory

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, FlowPathSwapContext context, FlowPathSwapFsm stateMachine) {
    for (UUID commandId : stateMachine.getPendingCommands().keySet()) {
        FlowSegmentRequestFactory removeCommand = stateMachine.getRemoveCommands().get(commandId);
        if (removeCommand != null) {
            stateMachine.saveErrorToHistory("Command is not finished yet", format("Completing the path swap operation although the remove command may not be " + "finished yet: commandId %s, switch %s, cookie %s", commandId, removeCommand.getSwitchId(), removeCommand.getCookie()));
        } else {
            FlowSegmentRequestFactory ingressRule = stateMachine.getIngressCommands().get(commandId);
            stateMachine.saveErrorToHistory("Command is not finished yet", format("Completing the path swap operation although the install command may not be " + "finished yet: commandId %s, switch %s, rule %s", commandId, ingressRule.getSwitchId(), ingressRule));
        }
    }
    log.debug("Abandoning all pending commands: {}", stateMachine.getPendingCommands());
    stateMachine.clearPendingCommands();
}
Also used : FlowSegmentRequestFactory(org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory) UUID(java.util.UUID)

Aggregations

FlowSegmentRequestFactory (org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory)53 UUID (java.util.UUID)33 Flow (org.openkilda.model.Flow)21 ArrayList (java.util.ArrayList)19 FlowErrorResponse (org.openkilda.floodlight.flow.response.FlowErrorResponse)15 SpeakerFlowSegmentResponse (org.openkilda.floodlight.api.response.SpeakerFlowSegmentResponse)14 FlowCommandBuilder (org.openkilda.wfm.topology.flowhs.service.FlowCommandBuilder)14 FlowPath (org.openkilda.model.FlowPath)12 IngressFlowSegmentRequestFactory (org.openkilda.floodlight.api.request.factory.IngressFlowSegmentRequestFactory)11 SpeakerRequestBuildContext (org.openkilda.wfm.share.model.SpeakerRequestBuildContext)11 FlowSegmentRequest (org.openkilda.floodlight.api.request.FlowSegmentRequest)9 EgressMirrorFlowSegmentRequestFactory (org.openkilda.floodlight.api.request.factory.EgressMirrorFlowSegmentRequestFactory)7 EgressFlowSegmentRequestFactory (org.openkilda.floodlight.api.request.factory.EgressFlowSegmentRequestFactory)6 IngressMirrorFlowSegmentRequestFactory (org.openkilda.floodlight.api.request.factory.IngressMirrorFlowSegmentRequestFactory)6 TransitFlowSegmentRequestFactory (org.openkilda.floodlight.api.request.factory.TransitFlowSegmentRequestFactory)6 Switch (org.openkilda.model.Switch)5 FlowSegmentCookie (org.openkilda.model.cookie.FlowSegmentCookie)5 MirrorContext (org.openkilda.wfm.share.model.MirrorContext)5 Test (org.junit.Test)4 SpeakerResponse (org.openkilda.floodlight.api.response.SpeakerResponse)4