Search in sources :

Example 6 with FlowSegmentRequestFactory

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

the class OnReceivedCommandResponseAction method perform.

@Override
protected void perform(State from, State to, Event event, FlowMirrorPointDeleteContext context, FlowMirrorPointDeleteFsm 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("Group id was removed", format("The group id was removed: 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_REMOVE_GROUP_ACTION, format("Failed to remove the group: 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_REMOVE_GROUP_ACTION, format("Failed to remove the group: 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 remove commands of the flow {}", stateMachine.getFlowId());
            stateMachine.fire(Event.GROUP_REMOVED);
        } else {
            String errorMessage = format("Received error response(s) for %d remove 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 7 with FlowSegmentRequestFactory

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

the class ValidateIngressRulesAction method perform.

@Override
protected void perform(State from, State to, Event event, FlowUpdateContext context, FlowUpdateFsm stateMachine) {
    SpeakerFlowSegmentResponse response = context.getSpeakerFlowResponse();
    UUID commandId = response.getCommandId();
    FlowSegmentRequestFactory command = stateMachine.getIngressCommands().get(commandId);
    if (!stateMachine.getPendingCommands().containsKey(commandId) || command == null) {
        log.info("Received a response for unexpected command: {}", response);
        return;
    }
    if (response.isSuccess()) {
        stateMachine.removePendingCommand(commandId);
        stateMachine.saveActionToHistory("Rule was validated", format("The ingress rule has been validated successfully: switch %s, cookie %s", command.getSwitchId(), command.getCookie()));
    } else {
        FlowErrorResponse errorResponse = (FlowErrorResponse) response;
        int attempt = stateMachine.doRetryForCommand(commandId);
        if (attempt <= speakerCommandRetriesLimit && errorResponse.getErrorCode() != FlowErrorResponse.ErrorCode.MISSING_OF_FLOWS) {
            stateMachine.saveErrorToHistory(RULE_VALIDATION_FAILED_ACTION, format("Failed to validate the ingress rule: commandId %s, switch %s, cookie %s. Error %s. " + "Retrying (attempt %d)", commandId, errorResponse.getSwitchId(), command.getCookie(), errorResponse, attempt));
            stateMachine.getCarrier().sendSpeakerRequest(command.makeInstallRequest(commandId));
        } else if (stateMachine.isDoNotRevert()) {
            stateMachine.removePendingCommand(commandId);
            stateMachine.saveErrorToHistory(RULE_VALIDATION_FAILED_ACTION, format("Failed to validate the ingress rule: commandId %s, switch %s, cookie %s. Error %s. " + "Skipping validation attempts", commandId, errorResponse.getSwitchId(), command.getCookie(), errorResponse));
            stateMachine.setNewFlowStatus(FlowStatus.DOWN);
            stateMachine.setErrorReason(RULE_VALIDATION_FAILED_ACTION);
        } else {
            stateMachine.removePendingCommand(commandId);
            stateMachine.saveErrorToHistory(RULE_VALIDATION_FAILED_ACTION, format("Failed to validate the 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("Ingress 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 8 with FlowSegmentRequestFactory

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

the class RevertNewRulesAction method perform.

@Override
protected void perform(State from, State to, Event event, FlowRerouteContext context, FlowRerouteFsm stateMachine) {
    Flow flow = getFlow(stateMachine.getFlowId());
    log.debug("Abandoning all pending commands: {}", stateMachine.getPendingCommands());
    stateMachine.clearPendingAndRetriedAndFailedCommands();
    FlowEncapsulationType encapsulationType = stateMachine.getNewEncapsulationType() != null ? stateMachine.getNewEncapsulationType() : flow.getEncapsulationType();
    FlowCommandBuilder commandBuilder = commandBuilderFactory.getBuilder(encapsulationType);
    Collection<FlowSegmentRequestFactory> installCommands = new ArrayList<>();
    // Reinstall old ingress rules that may be overridden by new ingress.
    if (stateMachine.getOldPrimaryForwardPath() != null && stateMachine.getOldPrimaryReversePath() != null) {
        FlowPath oldForward = getFlowPath(flow, stateMachine.getOldPrimaryForwardPath());
        FlowPath oldReverse = getFlowPath(flow, stateMachine.getOldPrimaryReversePath());
        SpeakerRequestBuildContext installContext = buildBaseSpeakerContextForInstall(oldForward.getSrcSwitchId(), oldReverse.getSrcSwitchId());
        installCommands.addAll(commandBuilder.buildIngressOnly(stateMachine.getCommandContext(), flow, oldForward, oldReverse, installContext));
    }
    // need to clean previous requests
    stateMachine.getIngressCommands().clear();
    SpeakerInstallSegmentEmitter.INSTANCE.emitBatch(stateMachine.getCarrier(), installCommands, stateMachine.getIngressCommands());
    stateMachine.getIngressCommands().forEach((key, value) -> stateMachine.addPendingCommand(key, value.getSwitchId()));
    // Remove possible installed flow segments
    MirrorContext mirrorContext = MirrorContext.builder().removeFlowOperation(true).build();
    Collection<FlowSegmentRequestFactory> removeCommands = new ArrayList<>();
    if (stateMachine.getNewPrimaryForwardPath() != null && stateMachine.getNewPrimaryReversePath() != null) {
        FlowPath newForward = getFlowPath(flow, stateMachine.getNewPrimaryForwardPath());
        FlowPath newReverse = getFlowPath(flow, stateMachine.getNewPrimaryReversePath());
        SpeakerRequestBuildContext speakerContext = buildSpeakerContextForRemovalIngressOnly(newForward.getSrcSwitchId(), newReverse.getSrcSwitchId());
        removeCommands.addAll(commandBuilder.buildAll(stateMachine.getCommandContext(), flow, newForward, newReverse, speakerContext, mirrorContext));
    }
    if (stateMachine.getNewProtectedForwardPath() != null && stateMachine.getNewProtectedReversePath() != null) {
        FlowPath newForward = getFlowPath(flow, stateMachine.getNewProtectedForwardPath());
        FlowPath newReverse = getFlowPath(flow, stateMachine.getNewProtectedReversePath());
        removeCommands.addAll(commandBuilder.buildAllExceptIngress(stateMachine.getCommandContext(), flow, newForward, newReverse, mirrorContext));
    }
    stateMachine.getRemoveCommands().clear();
    SpeakerRemoveSegmentEmitter.INSTANCE.emitBatch(stateMachine.getCarrier(), removeCommands, stateMachine.getRemoveCommands());
    stateMachine.getRemoveCommands().forEach((key, value) -> stateMachine.addPendingCommand(key, value.getSwitchId()));
    if (stateMachine.getPendingCommands().isEmpty()) {
        stateMachine.saveActionToHistory("No need to remove new rules or re-install original ingress rule");
        stateMachine.fire(Event.RULES_REMOVED);
    } else {
        stateMachine.saveActionToHistory("Commands for removing new rules and re-installing original ingress rule have been sent");
    }
}
Also used : FlowSegmentRequestFactory(org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory) MirrorContext(org.openkilda.wfm.share.model.MirrorContext) ArrayList(java.util.ArrayList) FlowCommandBuilder(org.openkilda.wfm.topology.flowhs.service.FlowCommandBuilder) FlowEncapsulationType(org.openkilda.model.FlowEncapsulationType) FlowPath(org.openkilda.model.FlowPath) SpeakerRequestBuildContext(org.openkilda.wfm.share.model.SpeakerRequestBuildContext) Flow(org.openkilda.model.Flow)

Example 9 with FlowSegmentRequestFactory

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

the class ValidateIngressRulesAction 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 command = stateMachine.getIngressCommands().get(commandId);
    if (!stateMachine.getPendingCommands().containsKey(commandId) || command == 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.validate_command.roundtrip").record(roundtrip, TimeUnit.NANOSECONDS);
            }
        }
        if (response.getExecutionTime() > 0) {
            registry.timer("fsm.validate_command.floodlight_execution").record(response.getExecutionTime(), TimeUnit.NANOSECONDS);
        }
    });
    if (response.isSuccess()) {
        stateMachine.removePendingCommand(commandId);
        stateMachine.saveActionToHistory("Rule was validated", format("The ingress rule has been validated successfully: switch %s, cookie %s", command.getSwitchId(), command.getCookie()));
    } else {
        FlowErrorResponse errorResponse = (FlowErrorResponse) response;
        int attempt = stateMachine.doRetryForCommand(commandId);
        if (attempt <= speakerCommandRetriesLimit && errorResponse.getErrorCode() == FlowErrorResponse.ErrorCode.MISSING_OF_FLOWS) {
            stateMachine.saveErrorToHistory("Rule validation failed", format("Failed to validate the ingress rule: commandId %s, switch %s, cookie %s. Error %s. " + "Retrying (attempt %d)", commandId, errorResponse.getSwitchId(), command.getCookie(), errorResponse, attempt));
            stateMachine.getCarrier().sendSpeakerRequest(command.makeVerifyRequest(commandId));
        } else {
            stateMachine.removePendingCommand(commandId);
            stateMachine.saveErrorToHistory("Rule validation failed", format("Failed to validate the 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("Ingress 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 10 with FlowSegmentRequestFactory

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

the class RemoveOldRulesAction method perform.

@Override
protected void perform(State from, State to, Event event, FlowUpdateContext context, FlowUpdateFsm stateMachine) {
    FlowEncapsulationType oldEncapsulationType = stateMachine.getOriginalFlow().getFlowEncapsulationType();
    FlowCommandBuilder commandBuilder = commandBuilderFactory.getBuilder(oldEncapsulationType);
    Collection<FlowSegmentRequestFactory> factories = new ArrayList<>();
    Flow originalFlow = getOriginalFlowWithPaths(stateMachine, stateMachine.getOriginalFlow());
    MirrorContext mirrorContext = MirrorContext.builder().removeFlowOperation(true).build();
    if (stateMachine.getEndpointUpdate().isPartialUpdate()) {
        SpeakerRequestBuildContext speakerContext = getSpeakerRequestBuildContext(stateMachine, false);
        FlowPath forward = getFlowPath(stateMachine.getOldPrimaryForwardPath());
        FlowPath reverse = getFlowPath(stateMachine.getOldPrimaryReversePath());
        switch(stateMachine.getEndpointUpdate()) {
            case SOURCE:
                factories.addAll(buildCommandsForSourceUpdate(commandBuilder, stateMachine, originalFlow, forward, reverse, speakerContext, mirrorContext.toBuilder().removeGroup(false).build()));
                break;
            case DESTINATION:
                factories.addAll(buildCommandsForDestinationUpdate(commandBuilder, stateMachine, originalFlow, forward, reverse, speakerContext, mirrorContext.toBuilder().removeGroup(false).build()));
                break;
            case BOTH:
            default:
                switch(stateMachine.getFlowLoopOperation()) {
                    case DELETE:
                        factories.addAll(commandBuilder.buildIngressOnly(stateMachine.getCommandContext(), originalFlow, forward, reverse, speakerContext).stream().filter(f -> f instanceof IngressFlowLoopSegmentRequestFactory).collect(Collectors.toList()));
                        break;
                    case CREATE:
                        // No rules removing required
                        break;
                    case NONE:
                    default:
                        factories.addAll(commandBuilder.buildIngressOnly(stateMachine.getCommandContext(), originalFlow, forward, reverse, speakerContext, mirrorContext.toBuilder().removeGroup(false).build()));
                        break;
                }
                break;
        }
    } else {
        SpeakerRequestBuildContext speakerContext = getSpeakerRequestBuildContext(stateMachine, true);
        if (stateMachine.getOldPrimaryForwardPath() != null) {
            FlowPath oldForward = getFlowPath(stateMachine.getOldPrimaryForwardPath());
            if (stateMachine.getOldPrimaryReversePath() != null) {
                FlowPath oldReverse = getFlowPath(stateMachine.getOldPrimaryReversePath());
                factories.addAll(commandBuilder.buildAll(stateMachine.getCommandContext(), originalFlow, oldForward, oldReverse, speakerContext, mirrorContext));
            } else {
                factories.addAll(commandBuilder.buildAll(stateMachine.getCommandContext(), originalFlow, oldForward, speakerContext, mirrorContext));
            }
        } else if (stateMachine.getOldPrimaryReversePath() != null) {
            FlowPath oldReverse = getFlowPath(stateMachine.getOldPrimaryReversePath());
            // swap contexts
            speakerContext.setForward(speakerContext.getReverse());
            speakerContext.setReverse(PathContext.builder().build());
            factories.addAll(commandBuilder.buildAll(stateMachine.getCommandContext(), originalFlow, oldReverse, speakerContext, mirrorContext));
        }
        if (stateMachine.getOldProtectedForwardPath() != null) {
            FlowPath oldForward = getFlowPath(stateMachine.getOldProtectedForwardPath());
            if (stateMachine.getOldProtectedReversePath() != null) {
                FlowPath oldReverse = getFlowPath(stateMachine.getOldProtectedReversePath());
                factories.addAll(commandBuilder.buildAllExceptIngress(stateMachine.getCommandContext(), originalFlow, oldForward, oldReverse, mirrorContext));
            } else {
                factories.addAll(commandBuilder.buildAllExceptIngress(stateMachine.getCommandContext(), originalFlow, oldForward, mirrorContext));
            }
        } else if (stateMachine.getOldProtectedReversePath() != null) {
            FlowPath oldReverse = getFlowPath(stateMachine.getOldProtectedReversePath());
            factories.addAll(commandBuilder.buildAllExceptIngress(stateMachine.getCommandContext(), originalFlow, oldReverse, mirrorContext));
        }
    }
    stateMachine.clearPendingAndRetriedAndFailedCommands();
    if (factories.isEmpty()) {
        stateMachine.saveActionToHistory("No need to remove old rules");
        stateMachine.fire(Event.RULES_REMOVED);
    } else {
        SpeakerRemoveSegmentEmitter.INSTANCE.emitBatch(stateMachine.getCarrier(), factories, stateMachine.getRemoveCommands());
        stateMachine.getRemoveCommands().forEach((key, value) -> stateMachine.addPendingCommand(key, value.getSwitchId()));
        stateMachine.saveActionToHistory("Remove commands for old rules have been sent");
    }
}
Also used : FlowSegmentRequestFactory(org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory) MirrorContext(org.openkilda.wfm.share.model.MirrorContext) IngressFlowLoopSegmentRequestFactory(org.openkilda.floodlight.api.request.factory.IngressFlowLoopSegmentRequestFactory) ArrayList(java.util.ArrayList) FlowCommandBuilder(org.openkilda.wfm.topology.flowhs.service.FlowCommandBuilder) FlowEncapsulationType(org.openkilda.model.FlowEncapsulationType) SpeakerRequestBuildContext(org.openkilda.wfm.share.model.SpeakerRequestBuildContext) FlowPath(org.openkilda.model.FlowPath) Flow(org.openkilda.model.Flow) RequestedFlow(org.openkilda.wfm.topology.flowhs.model.RequestedFlow)

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