Search in sources :

Example 11 with FlowSegmentRequestFactory

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

the class EmitIngressRulesVerifyRequestsAction method perform.

@Override
public void perform(State from, State to, Event event, FlowRerouteContext context, FlowRerouteFsm stateMachine) {
    Map<UUID, FlowSegmentRequestFactory> requestsStorage = stateMachine.getIngressCommands();
    List<FlowSegmentRequestFactory> requestFactories = new ArrayList<>(requestsStorage.values());
    requestsStorage.clear();
    stateMachine.clearPendingAndRetriedAndFailedCommands();
    for (FlowSegmentRequestFactory factory : requestFactories) {
        FlowSegmentRequest request = factory.makeVerifyRequest(commandIdGenerator.generate());
        // TODO ensure no conflicts
        requestsStorage.put(request.getCommandId(), factory);
        stateMachine.getCarrier().sendSpeakerRequest(request);
    }
    requestsStorage.forEach((key, value) -> stateMachine.addPendingCommand(key, value.getSwitchId()));
    stateMachine.saveActionToHistory("Started validation of installed ingress rules");
}
Also used : FlowSegmentRequestFactory(org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory) FlowSegmentRequest(org.openkilda.floodlight.api.request.FlowSegmentRequest) ArrayList(java.util.ArrayList) UUID(java.util.UUID)

Example 12 with FlowSegmentRequestFactory

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

the class InstallIngressRulesAction method perform.

@Override
protected void perform(State from, State to, Event event, FlowRerouteContext context, FlowRerouteFsm stateMachine) {
    String flowId = stateMachine.getFlowId();
    Flow flow = getFlow(flowId);
    // Detach the entity to avoid propagation to the database.
    flowRepository.detach(flow);
    if (stateMachine.getNewEncapsulationType() != null) {
        // This is for commandBuilder.buildIngressOnly() to use proper (updated) encapsulation type.
        flow.setEncapsulationType(stateMachine.getNewEncapsulationType());
    }
    FlowCommandBuilder commandBuilder = commandBuilderFactory.getBuilder(flow.getEncapsulationType());
    Collection<FlowSegmentRequestFactory> requestFactories = new ArrayList<>();
    if (stateMachine.getNewPrimaryForwardPath() != null && stateMachine.getNewPrimaryReversePath() != null) {
        FlowPath newForward = getFlowPath(flow, stateMachine.getNewPrimaryForwardPath());
        FlowPath newReverse = getFlowPath(flow, stateMachine.getNewPrimaryReversePath());
        SpeakerRequestBuildContext speakerContext = buildBaseSpeakerContextForInstall(newForward.getSrcSwitchId(), newReverse.getSrcSwitchId());
        requestFactories.addAll(commandBuilder.buildIngressOnly(stateMachine.getCommandContext(), flow, newForward, newReverse, speakerContext));
    }
    // Installation of ingress rules for protected paths is skipped. These paths are activated on swap.
    stateMachine.clearPendingAndRetriedAndFailedCommands();
    if (requestFactories.isEmpty()) {
        stateMachine.saveActionToHistory("No need to install ingress rules");
        stateMachine.fire(Event.INGRESS_IS_SKIPPED);
    } else {
        Map<UUID, FlowSegmentRequestFactory> requestsStorage = stateMachine.getIngressCommands();
        for (FlowSegmentRequestFactory factory : requestFactories) {
            FlowSegmentRequest request = factory.makeInstallRequest(commandIdGenerator.generate());
            requestsStorage.put(request.getCommandId(), factory);
            stateMachine.getCarrier().sendSpeakerRequest(request);
        }
        requestsStorage.forEach((key, value) -> stateMachine.addPendingCommand(key, value.getSwitchId()));
        stateMachine.saveActionToHistory("Commands for installing ingress rules have been sent");
    }
}
Also used : FlowSegmentRequestFactory(org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory) FlowSegmentRequest(org.openkilda.floodlight.api.request.FlowSegmentRequest) ArrayList(java.util.ArrayList) FlowCommandBuilder(org.openkilda.wfm.topology.flowhs.service.FlowCommandBuilder) FlowPath(org.openkilda.model.FlowPath) SpeakerRequestBuildContext(org.openkilda.wfm.share.model.SpeakerRequestBuildContext) UUID(java.util.UUID) Flow(org.openkilda.model.Flow)

Example 13 with FlowSegmentRequestFactory

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

the class OnReceivedInstallResponseAction 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.getInstallCommand(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.install_command.roundtrip").record(roundtrip, TimeUnit.NANOSECONDS);
            }
        }
        if (response.getExecutionTime() > 0) {
            registry.timer("fsm.install_command.floodlight_execution").record(response.getExecutionTime(), TimeUnit.NANOSECONDS);
        }
    });
    if (response.isSuccess()) {
        stateMachine.removePendingCommand(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 attempt = stateMachine.doRetryForCommand(commandId);
        if (attempt <= speakerCommandRetriesLimit) {
            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(), command.getCookie(), errorResponse, attempt));
            stateMachine.getCarrier().sendSpeakerRequest(command.makeInstallRequest(commandId));
        } else {
            stateMachine.removePendingCommand(commandId);
            stateMachine.saveErrorToHistory("Failed to install rule", format("Failed to install the rule: commandId %s, switch %s, cookie %s. Error: %s", commandId, errorResponse.getSwitchId(), command.getCookie(), errorResponse));
            stateMachine.addFailedCommand(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_INSTALLED);
        } else {
            String errorMessage = format("Received error response(s) for %d 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 14 with FlowSegmentRequestFactory

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

the class InstallNonIngressRulesAction method perform.

@Override
protected void perform(State from, State to, Event event, FlowRerouteContext context, FlowRerouteFsm stateMachine) {
    String flowId = stateMachine.getFlowId();
    Flow flow = getFlow(flowId);
    // Detach the entity to avoid propagation to the database.
    flowRepository.detach(flow);
    if (stateMachine.getNewEncapsulationType() != null) {
        // This is for commandBuilder.buildAllExceptIngress() to use proper (updated) encapsulation type.
        flow.setEncapsulationType(stateMachine.getNewEncapsulationType());
    }
    FlowCommandBuilder commandBuilder = commandBuilderFactory.getBuilder(flow.getEncapsulationType());
    Collection<FlowSegmentRequestFactory> requestFactories = new ArrayList<>();
    if (stateMachine.getNewPrimaryForwardPath() != null && stateMachine.getNewPrimaryReversePath() != null) {
        FlowPath newForward = getFlowPath(flow, stateMachine.getNewPrimaryForwardPath());
        FlowPath newReverse = getFlowPath(flow, stateMachine.getNewPrimaryReversePath());
        requestFactories.addAll(commandBuilder.buildAllExceptIngress(stateMachine.getCommandContext(), flow, newForward, newReverse));
    }
    if (stateMachine.getNewProtectedForwardPath() != null && stateMachine.getNewProtectedReversePath() != null) {
        FlowPath newForward = getFlowPath(flow, stateMachine.getNewProtectedForwardPath());
        FlowPath newReverse = getFlowPath(flow, stateMachine.getNewProtectedReversePath());
        requestFactories.addAll(commandBuilder.buildAllExceptIngress(stateMachine.getCommandContext(), flow, newForward, newReverse));
    }
    stateMachine.clearPendingAndRetriedAndFailedCommands();
    if (requestFactories.isEmpty()) {
        stateMachine.saveActionToHistory("No need to install non ingress rules");
        stateMachine.fire(Event.RULES_INSTALLED);
    } else {
        Map<UUID, FlowSegmentRequestFactory> requestsStorage = stateMachine.getNonIngressCommands();
        for (FlowSegmentRequestFactory factory : requestFactories) {
            FlowSegmentRequest request = factory.makeInstallRequest(commandIdGenerator.generate());
            // TODO ensure no conflicts
            requestsStorage.put(request.getCommandId(), factory);
            stateMachine.getCarrier().sendSpeakerRequest(request);
        }
        requestsStorage.forEach((key, value) -> stateMachine.addPendingCommand(key, value.getSwitchId()));
        stateMachine.saveActionToHistory("Commands for installing non ingress rules have been sent");
    }
}
Also used : FlowSegmentRequestFactory(org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory) FlowSegmentRequest(org.openkilda.floodlight.api.request.FlowSegmentRequest) ArrayList(java.util.ArrayList) FlowCommandBuilder(org.openkilda.wfm.topology.flowhs.service.FlowCommandBuilder) FlowPath(org.openkilda.model.FlowPath) UUID(java.util.UUID) Flow(org.openkilda.model.Flow)

Example 15 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, FlowPathSwapContext context, FlowPathSwapFsm stateMachine) {
    SpeakerResponse response = context.getSpeakerResponse();
    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", 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 {
            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) SpeakerResponse(org.openkilda.floodlight.api.response.SpeakerResponse) 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