Search in sources :

Example 1 with YFlowRerouteContext

use of org.openkilda.wfm.topology.flowhs.fsm.yflow.reroute.YFlowRerouteContext in project open-kilda by telstra.

the class OnReceivedInstallResponseAction method perform.

@Override
public void perform(State from, State to, Event event, YFlowRerouteContext context, YFlowRerouteFsm 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 installed", format("The rule was installed: switch %s", response.getSwitchId()));
    } else {
        Optional<InstallSpeakerCommandsRequest> request = stateMachine.getInstallSpeakerCommand(commandId);
        int attempt = stateMachine.doRetryForCommand(commandId);
        if (attempt <= speakerCommandRetriesLimit && request.isPresent()) {
            response.getFailedCommandIds().forEach((uuid, message) -> stateMachine.saveErrorToHistory(FAILED_TO_INSTALL_RULE_ACTION, format("Failed to install 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();
            InstallSpeakerCommandsRequest installRequest = request.get();
            List<OfCommand> commands = installRequest.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(installRequest.toBuilder().commands(commands).build());
        } else {
            stateMachine.addFailedCommand(commandId, response);
            stateMachine.removePendingCommand(commandId);
            response.getFailedCommandIds().forEach((uuid, message) -> stateMachine.saveErrorToHistory(FAILED_TO_INSTALL_RULE_ACTION, format("Failed to install 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 install commands of the y-flow {}", stateMachine.getYFlowId());
            stateMachine.fire(Event.YFLOW_METERS_INSTALLED);
        } else {
            String errorMessage = format("Received error response(s) for %d install 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) State(org.openkilda.wfm.topology.flowhs.fsm.yflow.reroute.YFlowRerouteFsm.State) 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) OfCommand(org.openkilda.floodlight.api.request.rulemanager.OfCommand) SpeakerCommandResponse(org.openkilda.floodlight.api.response.rulemanager.SpeakerCommandResponse) YFlowRerouteContext(org.openkilda.wfm.topology.flowhs.fsm.yflow.reroute.YFlowRerouteContext) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) InstallSpeakerCommandsRequest(org.openkilda.floodlight.api.request.rulemanager.InstallSpeakerCommandsRequest) HistoryRecordingAction(org.openkilda.wfm.topology.flowhs.fsm.common.actions.HistoryRecordingAction) Optional(java.util.Optional) Event(org.openkilda.wfm.topology.flowhs.fsm.yflow.reroute.YFlowRerouteFsm.Event) GroupCommand(org.openkilda.floodlight.api.request.rulemanager.GroupCommand) YFlowRerouteFsm(org.openkilda.wfm.topology.flowhs.fsm.yflow.reroute.YFlowRerouteFsm) InstallSpeakerCommandsRequest(org.openkilda.floodlight.api.request.rulemanager.InstallSpeakerCommandsRequest) 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)

Example 2 with YFlowRerouteContext

use of org.openkilda.wfm.topology.flowhs.fsm.yflow.reroute.YFlowRerouteContext in project open-kilda by telstra.

the class OnReceivedRemoveResponseAction method perform.

@Override
public void perform(State from, State to, Event event, YFlowRerouteContext context, YFlowRerouteFsm 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 the y-flow {}", stateMachine.getYFlowId());
            stateMachine.fire(Event.YPOINT_METER_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) State(org.openkilda.wfm.topology.flowhs.fsm.yflow.reroute.YFlowRerouteFsm.State) 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) OfCommand(org.openkilda.floodlight.api.request.rulemanager.OfCommand) DeleteSpeakerCommandsRequest(org.openkilda.floodlight.api.request.rulemanager.DeleteSpeakerCommandsRequest) SpeakerCommandResponse(org.openkilda.floodlight.api.response.rulemanager.SpeakerCommandResponse) YFlowRerouteContext(org.openkilda.wfm.topology.flowhs.fsm.yflow.reroute.YFlowRerouteContext) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) HistoryRecordingAction(org.openkilda.wfm.topology.flowhs.fsm.common.actions.HistoryRecordingAction) Optional(java.util.Optional) Event(org.openkilda.wfm.topology.flowhs.fsm.yflow.reroute.YFlowRerouteFsm.Event) GroupCommand(org.openkilda.floodlight.api.request.rulemanager.GroupCommand) YFlowRerouteFsm(org.openkilda.wfm.topology.flowhs.fsm.yflow.reroute.YFlowRerouteFsm) 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)

Example 3 with YFlowRerouteContext

use of org.openkilda.wfm.topology.flowhs.fsm.yflow.reroute.YFlowRerouteContext in project open-kilda by telstra.

the class YFlowRerouteService method handleRequest.

/**
 * Handles request for y-flow rerouting.
 *
 * @param key command identifier.
 * @param request request data.
 */
public void handleRequest(@NonNull String key, @NonNull CommandContext commandContext, @NonNull YFlowRerouteRequest request) throws DuplicateKeyException {
    String yFlowId = request.getYFlowId();
    log.debug("Handling y-flow reroute request with key {} and flow ID: {}", key, request.getYFlowId());
    if (fsmRegister.hasRegisteredFsmWithKey(key)) {
        throw new DuplicateKeyException(key, "There's another active FSM with the same key");
    }
    if (fsmRegister.hasRegisteredFsmWithFlowId(yFlowId)) {
        sendErrorResponseToNorthbound(ErrorType.ALREADY_EXISTS, "Could not reroute y-flow", format("Y-flow %s is already rerouting now", yFlowId), commandContext);
        return;
    }
    YFlowRerouteFsm fsm = fsmFactory.newInstance(commandContext, request.getYFlowId(), eventListeners);
    fsmRegister.registerFsm(key, fsm);
    YFlowRerouteContext context = YFlowRerouteContext.builder().rerouteRequest(request).build();
    fsm.start(context);
    fsmExecutor.fire(fsm, Event.NEXT, context);
    removeIfFinished(fsm, key);
}
Also used : YFlowRerouteFsm(org.openkilda.wfm.topology.flowhs.fsm.yflow.reroute.YFlowRerouteFsm) YFlowRerouteContext(org.openkilda.wfm.topology.flowhs.fsm.yflow.reroute.YFlowRerouteContext) DuplicateKeyException(org.openkilda.wfm.topology.flowhs.exception.DuplicateKeyException)

Example 4 with YFlowRerouteContext

use of org.openkilda.wfm.topology.flowhs.fsm.yflow.reroute.YFlowRerouteContext in project open-kilda by telstra.

the class YFlowRerouteService method handleAsyncResponse.

/**
 * Handles async response from worker.
 *
 * @param key command identifier.
 */
public void handleAsyncResponse(@NonNull String key, @NonNull SpeakerResponse speakerResponse) throws UnknownKeyException {
    log.debug("Received flow command response {}", speakerResponse);
    YFlowRerouteFsm fsm = fsmRegister.getFsmByKey(key).orElseThrow(() -> new UnknownKeyException(key));
    if (speakerResponse instanceof SpeakerFlowSegmentResponse) {
        SpeakerFlowSegmentResponse response = (SpeakerFlowSegmentResponse) speakerResponse;
        String flowId = response.getMetadata().getFlowId();
        if (fsm.getReroutingSubFlows().contains(flowId)) {
            flowRerouteService.handleAsyncResponseByFlowId(flowId, response);
        }
    } else if (speakerResponse instanceof SpeakerCommandResponse) {
        SpeakerCommandResponse response = (SpeakerCommandResponse) speakerResponse;
        YFlowRerouteContext context = YFlowRerouteContext.builder().speakerResponse(response).build();
        fsmExecutor.fire(fsm, Event.RESPONSE_RECEIVED, context);
    } else {
        log.debug("Received unexpected speaker response: {}", speakerResponse);
    }
    // After handling an event by FlowReroute service, we should propagate execution to the FSM.
    if (!fsm.isTerminated()) {
        fsmExecutor.fire(fsm, Event.NEXT);
    }
    removeIfFinished(fsm, key);
}
Also used : YFlowRerouteFsm(org.openkilda.wfm.topology.flowhs.fsm.yflow.reroute.YFlowRerouteFsm) YFlowRerouteContext(org.openkilda.wfm.topology.flowhs.fsm.yflow.reroute.YFlowRerouteContext) SpeakerFlowSegmentResponse(org.openkilda.floodlight.api.response.SpeakerFlowSegmentResponse) SpeakerCommandResponse(org.openkilda.floodlight.api.response.rulemanager.SpeakerCommandResponse) UnknownKeyException(org.openkilda.wfm.topology.flowhs.exception.UnknownKeyException)

Example 5 with YFlowRerouteContext

use of org.openkilda.wfm.topology.flowhs.fsm.yflow.reroute.YFlowRerouteContext in project open-kilda by telstra.

the class StartReroutingYFlowAction method perform.

@Override
protected void perform(State from, State to, Event event, YFlowRerouteContext context, YFlowRerouteFsm stateMachine) {
    String yFlowId = stateMachine.getYFlowId();
    List<FlowPath> flowPaths = transactionManager.doInTransaction(() -> {
        YFlow yFlow = getYFlow(yFlowId);
        saveOldResources(stateMachine, yFlow);
        stateMachine.setDeleteOldYFlowCommands(buildYFlowDeleteCommands(yFlow, stateMachine.getCommandContext()));
        SwitchId sharedSwitchId = yFlow.getSharedEndpoint().getSwitchId();
        return yFlow.getSubFlows().stream().map(YSubFlow::getFlow).flatMap(flow -> Stream.of(flow.getForwardPath(), flow.getReversePath())).filter(path -> sharedSwitchId.equals(path.getSrcSwitchId())).collect(Collectors.toList());
    });
    stateMachine.setOldYFlowPathCookies(flowPaths.stream().map(FlowPath::getCookie).map(FlowSegmentCookie::getValue).collect(Collectors.toList()));
    List<PathSegment> sharedPathSegments = IntersectionComputer.calculatePathIntersectionFromSource(flowPaths);
    PathInfoData sharedPath = FlowPathMapper.INSTANCE.map(sharedPathSegments);
    stateMachine.setOldSharedPath(sharedPath);
    List<SubFlowPathDto> subFlowPathDtos = flowPaths.stream().map(flowPath -> new SubFlowPathDto(flowPath.getFlowId(), FlowPathMapper.INSTANCE.map(flowPath))).sorted(Comparator.comparing(SubFlowPathDto::getFlowId)).collect(Collectors.toList());
    stateMachine.setOldSubFlowPathDtos(subFlowPathDtos);
}
Also used : YFlow(org.openkilda.model.YFlow) EndpointResources(org.openkilda.wfm.topology.flowhs.model.yflow.YFlowResources.EndpointResources) PathSegment(org.openkilda.model.PathSegment) YSubFlow(org.openkilda.model.YSubFlow) FlowPath(org.openkilda.model.FlowPath) State(org.openkilda.wfm.topology.flowhs.fsm.yflow.reroute.YFlowRerouteFsm.State) YFlowResources(org.openkilda.wfm.topology.flowhs.model.yflow.YFlowResources) FlowPathMapper(org.openkilda.wfm.share.mappers.FlowPathMapper) YFlowRuleManagerProcessingAction(org.openkilda.wfm.topology.flowhs.fsm.common.actions.YFlowRuleManagerProcessingAction) Collectors(java.util.stream.Collectors) PathInfoData(org.openkilda.messaging.info.event.PathInfoData) RuleManager(org.openkilda.rulemanager.RuleManager) YFlowRerouteContext(org.openkilda.wfm.topology.flowhs.fsm.yflow.reroute.YFlowRerouteContext) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Stream(java.util.stream.Stream) SwitchId(org.openkilda.model.SwitchId) IntersectionComputer(org.openkilda.wfm.share.service.IntersectionComputer) SubFlowPathDto(org.openkilda.messaging.command.yflow.SubFlowPathDto) YFlow(org.openkilda.model.YFlow) Event(org.openkilda.wfm.topology.flowhs.fsm.yflow.reroute.YFlowRerouteFsm.Event) FlowSegmentCookie(org.openkilda.model.cookie.FlowSegmentCookie) PersistenceManager(org.openkilda.persistence.PersistenceManager) Comparator(java.util.Comparator) YFlowRerouteFsm(org.openkilda.wfm.topology.flowhs.fsm.yflow.reroute.YFlowRerouteFsm) PathInfoData(org.openkilda.messaging.info.event.PathInfoData) FlowSegmentCookie(org.openkilda.model.cookie.FlowSegmentCookie) SwitchId(org.openkilda.model.SwitchId) PathSegment(org.openkilda.model.PathSegment) FlowPath(org.openkilda.model.FlowPath) SubFlowPathDto(org.openkilda.messaging.command.yflow.SubFlowPathDto)

Aggregations

YFlowRerouteContext (org.openkilda.wfm.topology.flowhs.fsm.yflow.reroute.YFlowRerouteContext)5 YFlowRerouteFsm (org.openkilda.wfm.topology.flowhs.fsm.yflow.reroute.YFlowRerouteFsm)5 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 Slf4j (lombok.extern.slf4j.Slf4j)3 SpeakerCommandResponse (org.openkilda.floodlight.api.response.rulemanager.SpeakerCommandResponse)3 Event (org.openkilda.wfm.topology.flowhs.fsm.yflow.reroute.YFlowRerouteFsm.Event)3 State (org.openkilda.wfm.topology.flowhs.fsm.yflow.reroute.YFlowRerouteFsm.State)3 String.format (java.lang.String.format)2 Optional (java.util.Optional)2 Set (java.util.Set)2 UUID (java.util.UUID)2 FlowCommand (org.openkilda.floodlight.api.request.rulemanager.FlowCommand)2 GroupCommand (org.openkilda.floodlight.api.request.rulemanager.GroupCommand)2 MeterCommand (org.openkilda.floodlight.api.request.rulemanager.MeterCommand)2 OfCommand (org.openkilda.floodlight.api.request.rulemanager.OfCommand)2 HistoryRecordingAction (org.openkilda.wfm.topology.flowhs.fsm.common.actions.HistoryRecordingAction)2 Comparator (java.util.Comparator)1 Stream (java.util.stream.Stream)1 DeleteSpeakerCommandsRequest (org.openkilda.floodlight.api.request.rulemanager.DeleteSpeakerCommandsRequest)1