Search in sources :

Example 41 with FlowSegmentRequestFactory

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

the class SpeakerFlowSegmentRequestBuilder method makePathRequests.

@SuppressWarnings("squid:S00107")
private List<FlowSegmentRequestFactory> makePathRequests(@NonNull Flow flow, @NonNull FlowPath path, CommandContext context, FlowTransitEncapsulation encapsulation, boolean doIngress, boolean doTransit, boolean doEgress, RulesContext rulesContext, MirrorContext mirrorContext) {
    final FlowSideAdapter ingressSide = FlowSideAdapter.makeIngressAdapter(flow, path);
    final FlowSideAdapter egressSide = FlowSideAdapter.makeEgressAdapter(flow, path);
    final List<FlowSegmentRequestFactory> requests = new ArrayList<>();
    PathSegment lastSegment = null;
    for (PathSegment segment : path.getSegments()) {
        if (lastSegment == null) {
            if (doIngress) {
                requests.addAll(makeIngressSegmentRequests(context, path, encapsulation, ingressSide, segment, egressSide, rulesContext, mirrorContext));
                if (ingressLoopRuleRequired(flow, ingressSide)) {
                    requests.addAll(makeLoopRequests(context, path, encapsulation, ingressSide, egressSide, segment));
                }
            }
        } else {
            if (doTransit) {
                requests.add(makeTransitSegmentRequest(context, path, encapsulation, lastSegment, segment));
            }
        }
        lastSegment = segment;
    }
    if (lastSegment != null) {
        if (doEgress) {
            requests.addAll(makeEgressSegmentRequests(context, path, encapsulation, lastSegment, egressSide, ingressSide, mirrorContext));
        }
    } else if (flow.isOneSwitchFlow()) {
        // one switch flow (path without path segments)
        requests.addAll(makeOneSwitchRequest(context, path, ingressSide, egressSide, rulesContext, mirrorContext));
        if (singleSwitchLoopRuleRequired(flow)) {
            requests.add(makeSingleSwitchIngressLoopRequest(context, path, ingressSide));
        }
    }
    return requests;
}
Also used : IngressMirrorFlowSegmentRequestFactory(org.openkilda.floodlight.api.request.factory.IngressMirrorFlowSegmentRequestFactory) IngressFlowSegmentRequestFactory(org.openkilda.floodlight.api.request.factory.IngressFlowSegmentRequestFactory) EgressMirrorFlowSegmentRequestFactory(org.openkilda.floodlight.api.request.factory.EgressMirrorFlowSegmentRequestFactory) EgressFlowSegmentRequestFactory(org.openkilda.floodlight.api.request.factory.EgressFlowSegmentRequestFactory) TransitFlowSegmentRequestFactory(org.openkilda.floodlight.api.request.factory.TransitFlowSegmentRequestFactory) FlowSegmentRequestFactory(org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory) ArrayList(java.util.ArrayList) FlowSideAdapter(org.openkilda.adapter.FlowSideAdapter) PathSegment(org.openkilda.model.PathSegment)

Example 42 with FlowSegmentRequestFactory

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

the class EmitNonIngressRulesVerifyRequestsAction method perform.

@Override
public void perform(State from, State to, Event event, FlowRerouteContext context, FlowRerouteFsm stateMachine) {
    Map<UUID, FlowSegmentRequestFactory> requestsStorage = stateMachine.getNonIngressCommands();
    List<FlowSegmentRequestFactory> requestFactories = new ArrayList<>(requestsStorage.values());
    requestsStorage.clear();
    stateMachine.clearPendingAndRetriedAndFailedCommands();
    if (requestFactories.isEmpty()) {
        stateMachine.saveActionToHistory("No need to validate non ingress rules");
        stateMachine.fire(Event.RULES_VALIDATED);
    } else {
        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 non 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 43 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, FlowPathSwapContext context, FlowPathSwapFsm stateMachine) {
    String flowId = stateMachine.getFlowId();
    Flow flow = getFlow(flowId);
    FlowCommandBuilder commandBuilder = commandBuilderFactory.getBuilder(flow.getEncapsulationType());
    FlowPath newPrimaryForward = flow.getForwardPath();
    FlowPath newPrimaryReverse = flow.getReversePath();
    SpeakerRequestBuildContext speakerContext = buildBaseSpeakerContextForInstall(flow.getSrcSwitchId(), flow.getDestSwitchId());
    Collection<FlowSegmentRequestFactory> commands = new ArrayList<>(commandBuilder.buildIngressOnly(stateMachine.getCommandContext(), flow, newPrimaryForward, newPrimaryReverse, speakerContext));
    commands.addAll(commandBuilder.buildEgressOnly(stateMachine.getCommandContext(), flow, newPrimaryForward, newPrimaryReverse).stream().filter(f -> f instanceof TransitFlowLoopSegmentRequestFactory || f instanceof EgressMirrorFlowSegmentRequestFactory).collect(Collectors.toList()));
    // Installation of ingress rules for protected paths is skipped. These paths are activated on swap.
    stateMachine.clearPendingAndRetriedAndFailedCommands();
    SpeakerInstallSegmentEmitter.INSTANCE.emitBatch(stateMachine.getCarrier(), commands, stateMachine.getIngressCommands());
    stateMachine.getIngressCommands().forEach((key, value) -> stateMachine.addPendingCommand(key, value.getSwitchId()));
    stateMachine.saveActionToHistory("Commands for installing ingress rules have been sent");
}
Also used : EgressMirrorFlowSegmentRequestFactory(org.openkilda.floodlight.api.request.factory.EgressMirrorFlowSegmentRequestFactory) FlowSegmentRequestFactory(org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory) TransitFlowLoopSegmentRequestFactory(org.openkilda.floodlight.api.request.factory.TransitFlowLoopSegmentRequestFactory) EgressMirrorFlowSegmentRequestFactory(org.openkilda.floodlight.api.request.factory.EgressMirrorFlowSegmentRequestFactory) ArrayList(java.util.ArrayList) FlowCommandBuilder(org.openkilda.wfm.topology.flowhs.service.FlowCommandBuilder) FlowPath(org.openkilda.model.FlowPath) SpeakerRequestBuildContext(org.openkilda.wfm.share.model.SpeakerRequestBuildContext) Flow(org.openkilda.model.Flow)

Example 44 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, FlowPathSwapContext context, FlowPathSwapFsm stateMachine) {
    SpeakerResponse response = context.getSpeakerResponse();
    UUID commandId = response.getCommandId();
    if (!stateMachine.hasPendingCommand(commandId)) {
        log.info("Received a response for unexpected command: {}", response);
        return;
    }
    boolean isInstallCommand = stateMachine.getInstallCommand(commandId) != null || stateMachine.getInstallSpeakerCommand(commandId).isPresent();
    if (response.isSuccess()) {
        stateMachine.removePendingCommand(commandId);
        String commandName = isInstallCommand ? "re-installed (reverted)" : "deleted";
        if (response instanceof SpeakerFlowSegmentResponse) {
            stateMachine.saveActionToHistory("Rule was " + commandName, format("The rule was %s: switch %s, cookie %s", commandName, response.getSwitchId(), ((SpeakerFlowSegmentResponse) response).getCookie()));
        } else {
            stateMachine.saveActionToHistory("Rule was " + commandName, format("The rule was %s: switch %s", commandName, response.getSwitchId()));
        }
    } else {
        int attempt = stateMachine.doRetryForCommand(commandId);
        if (attempt <= speakerCommandRetriesLimit) {
            if (response instanceof FlowErrorResponse) {
                FlowErrorResponse errorResponse = (FlowErrorResponse) response;
                if (isInstallCommand) {
                    FlowSegmentRequestFactory installCommand = stateMachine.getInstallCommand(commandId);
                    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 {
                    FlowSegmentRequestFactory removeCommand = stateMachine.getRemoveCommand(commandId);
                    stateMachine.saveErrorToHistory("Failed to delete 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 if (response instanceof SpeakerCommandResponse) {
                String commandName = isInstallCommand ? "re-install (revert)" : "delete";
                SpeakerCommandResponse speakerCommandResponse = (SpeakerCommandResponse) response;
                speakerCommandResponse.getFailedCommandIds().forEach((uuid, message) -> stateMachine.saveErrorToHistory("Failed to " + commandName + " rule", format("Failed to %s the rule: commandId %s, ruleId %s, switch %s. " + "Error %s. Retrying (attempt %d)", commandName, commandId, uuid, response.getSwitchId(), message, attempt)));
                Set<UUID> failedUuids = speakerCommandResponse.getFailedCommandIds().keySet();
                stateMachine.getInstallSpeakerCommand(commandId).ifPresent(command -> stateMachine.getCarrier().sendSpeakerRequest(command.toBuilder().commands(filterOfCommands(command.getCommands(), failedUuids)).build()));
                stateMachine.getDeleteSpeakerCommand(commandId).ifPresent(command -> stateMachine.getCarrier().sendSpeakerRequest(command.toBuilder().commands(filterOfCommands(command.getCommands(), failedUuids)).build()));
            } else {
                log.warn("Received a unknown response: {}", response);
                return;
            }
        } else {
            stateMachine.addFailedCommand(commandId, response);
            stateMachine.removePendingCommand(commandId);
            String commandName = isInstallCommand ? "re-install (revert)" : "delete";
            if (response instanceof FlowErrorResponse) {
                stateMachine.saveErrorToHistory("Failed to " + commandName + " rule", format("Failed to %s the rule: commandId %s, switch %s, cookie %s. Error %s.", commandName, commandId, response.getSwitchId(), ((FlowErrorResponse) response).getCookie(), response));
            } else if (response instanceof SpeakerCommandResponse) {
                ((SpeakerCommandResponse) response).getFailedCommandIds().forEach((uuid, message) -> stateMachine.saveErrorToHistory("Failed to " + commandName + " rule", format("Failed to %s the rule: commandId %s, ruleId %s, switch %s. Error %s.", commandName, commandId, uuid, response.getSwitchId(), message)));
            } else {
                log.warn("Received a unknown response: {}", response);
                return;
            }
        }
    }
    if (stateMachine.getPendingCommands().isEmpty()) {
        if (stateMachine.getFailedCommands().isEmpty()) {
            log.debug("Received responses for all pending install / delete commands of the flow {}", stateMachine.getFlowId());
            stateMachine.fire(Event.RULES_REMOVED);
        } else {
            String errorMessage = format("Received error response(s) for %d install / delete commands", stateMachine.getFailedCommands().size());
            stateMachine.saveErrorToHistory(errorMessage);
            stateMachine.fireError(errorMessage);
        }
    }
}
Also used : FlowPathSwapFsm(org.openkilda.wfm.topology.flowhs.fsm.pathswap.FlowPathSwapFsm) SpeakerFlowSegmentResponse(org.openkilda.floodlight.api.response.SpeakerFlowSegmentResponse) FlowErrorResponse(org.openkilda.floodlight.flow.response.FlowErrorResponse) State(org.openkilda.wfm.topology.flowhs.fsm.pathswap.FlowPathSwapFsm.State) Set(java.util.Set) Event(org.openkilda.wfm.topology.flowhs.fsm.pathswap.FlowPathSwapFsm.Event) UUID(java.util.UUID) String.format(java.lang.String.format) SpeakerCommandResponse(org.openkilda.floodlight.api.response.rulemanager.SpeakerCommandResponse) Slf4j(lombok.extern.slf4j.Slf4j) SpeakerResponse(org.openkilda.floodlight.api.response.SpeakerResponse) FlowSegmentRequestFactory(org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory) FlowPathSwapContext(org.openkilda.wfm.topology.flowhs.fsm.pathswap.FlowPathSwapContext) FlowSegmentRequestFactory(org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory) Set(java.util.Set) FlowErrorResponse(org.openkilda.floodlight.flow.response.FlowErrorResponse) SpeakerFlowSegmentResponse(org.openkilda.floodlight.api.response.SpeakerFlowSegmentResponse) SpeakerCommandResponse(org.openkilda.floodlight.api.response.rulemanager.SpeakerCommandResponse) SpeakerResponse(org.openkilda.floodlight.api.response.SpeakerResponse) UUID(java.util.UUID)

Example 45 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, FlowMirrorPointDeleteContext context, FlowMirrorPointDeleteFsm stateMachine) {
    for (UUID commandId : stateMachine.getPendingCommands().keySet()) {
        FlowSegmentRequestFactory command = stateMachine.getCommands().get(commandId);
        if (command != null) {
            stateMachine.saveErrorToHistory("Command is not finished yet", format("Completing the removal operation although the command may not be " + "finished yet: commandId %s, switch %s, cookie %s", commandId, command.getSwitchId(), command.getCookie()));
        }
    }
    log.debug("Abandoning all pending commands: {}", stateMachine.getPendingCommands());
    stateMachine.getPendingCommands().clear();
}
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