Search in sources :

Example 6 with CommandContext

use of org.openkilda.wfm.CommandContext in project open-kilda by telstra.

the class EmitCommandRequestsAction method buildCommands.

private Collection<FlowSegmentRequestFactory> buildCommands(FlowCommandBuilder commandBuilder, FlowMirrorPointDeleteFsm stateMachine, Flow flow, FlowMirrorPoints mirrorPoints) {
    FlowPath path = mirrorPoints.getFlowPath();
    FlowPath oppositePath = path.isForward() ? flow.getReversePath() : flow.getForwardPath();
    CommandContext context = stateMachine.getCommandContext();
    SpeakerRequestBuildContext speakerContext = buildBaseSpeakerContextForInstall(path.getSrcSwitchId(), path.getDestSwitchId());
    if (mirrorPoints.getMirrorSwitchId().equals(path.getSrcSwitchId())) {
        return new ArrayList<>(commandBuilder.buildIngressOnlyOneDirection(context, flow, path, oppositePath, speakerContext.getForward(), MirrorContext.builder().buildMirrorFactoryOnly(true).build()));
    } else if (mirrorPoints.getMirrorSwitchId().equals(path.getDestSwitchId())) {
        return new ArrayList<>(commandBuilder.buildEgressOnlyOneDirection(context, flow, path, oppositePath, MirrorContext.builder().buildMirrorFactoryOnly(true).build()));
    }
    return Collections.emptyList();
}
Also used : CommandContext(org.openkilda.wfm.CommandContext) ArrayList(java.util.ArrayList) FlowPath(org.openkilda.model.FlowPath) SpeakerRequestBuildContext(org.openkilda.wfm.share.model.SpeakerRequestBuildContext)

Example 7 with CommandContext

use of org.openkilda.wfm.CommandContext in project open-kilda by telstra.

the class ValidateRequestAction method performWithResponse.

@Override
protected Optional<Message> performWithResponse(State from, State to, Event event, FlowMirrorPointDeleteContext context, FlowMirrorPointDeleteFsm stateMachine) {
    String flowId = stateMachine.getFlowId();
    PathId mirrorPathId = new PathId(context.getFlowMirrorPointId());
    stateMachine.setMirrorPathId(mirrorPathId);
    dashboardLogger.onFlowMirrorPointDelete(flowId, context.getFlowMirrorPointId());
    FlowMirrorPointResponse response = transactionManager.doInTransaction(() -> {
        Flow foundFlow = getFlow(flowId);
        if (foundFlow.getStatus() == FlowStatus.IN_PROGRESS) {
            throw new FlowProcessingException(ErrorType.REQUEST_INVALID, format("Flow %s is in progress now", flowId));
        }
        stateMachine.setFlowStatus(foundFlow.getStatus());
        flowRepository.updateStatus(flowId, FlowStatus.IN_PROGRESS);
        FlowMirrorPath flowMirrorPath = flowMirrorPathRepository.findById(mirrorPathId).orElseThrow(() -> new FlowProcessingException(ErrorType.NOT_FOUND, format("Flow mirror point %s not found", mirrorPathId)));
        if (flowMirrorPath.getStatus() == FlowPathStatus.IN_PROGRESS) {
            throw new FlowProcessingException(ErrorType.REQUEST_INVALID, format("Flow mirror point %s is in progress now", mirrorPathId));
        }
        stateMachine.setOriginalFlowMirrorPathStatus(flowMirrorPath.getStatus());
        flowMirrorPathRepository.updateStatus(mirrorPathId, FlowPathStatus.IN_PROGRESS);
        String direction = flowMirrorPath.getFlowMirrorPoints().getFlowPath().isForward() ? "forward" : "reverse";
        return FlowMirrorPointResponse.builder().flowId(foundFlow.getFlowId()).mirrorPointId(flowMirrorPath.getPathId().getId()).mirrorPointDirection(direction).mirrorPointSwitchId(flowMirrorPath.getMirrorSwitchId()).sinkEndpoint(FlowEndpoint.builder().switchId(flowMirrorPath.getEgressSwitchId()).portNumber(flowMirrorPath.getEgressPort()).innerVlanId(flowMirrorPath.getEgressInnerVlan()).outerVlanId(flowMirrorPath.getEgressOuterVlan()).build()).build();
    });
    stateMachine.saveNewEventToHistory("Flow was validated successfully", FlowEventData.Event.FLOW_MIRROR_POINT_DELETE);
    CommandContext commandContext = stateMachine.getCommandContext();
    return Optional.of(new InfoMessage(response, commandContext.getCreateTime(), commandContext.getCorrelationId()));
}
Also used : PathId(org.openkilda.model.PathId) FlowMirrorPointResponse(org.openkilda.messaging.info.flow.FlowMirrorPointResponse) CommandContext(org.openkilda.wfm.CommandContext) FlowProcessingException(org.openkilda.wfm.topology.flowhs.exception.FlowProcessingException) InfoMessage(org.openkilda.messaging.info.InfoMessage) FlowMirrorPath(org.openkilda.model.FlowMirrorPath) Flow(org.openkilda.model.Flow)

Example 8 with CommandContext

use of org.openkilda.wfm.CommandContext in project open-kilda by telstra.

the class CreateSubFlowsAction method perform.

@Override
public void perform(State from, State to, Event event, YFlowCreateContext context, YFlowCreateFsm stateMachine) {
    String yFlowId = stateMachine.getYFlowId();
    Collection<RequestedFlow> requestedFlows = YFlowRequestMapper.INSTANCE.toRequestedFlows(stateMachine.getTargetFlow());
    stateMachine.setRequestedFlows(requestedFlows);
    log.debug("Start creating {} sub-flows for y-flow {}", requestedFlows.size(), yFlowId);
    stateMachine.clearCreatingSubFlows();
    requestedFlows.stream().findFirst().ifPresent(requestedFlow -> {
        String subFlowId = requestedFlow.getFlowId();
        stateMachine.setMainAffinityFlowId(subFlowId);
        stateMachine.addSubFlow(subFlowId);
        stateMachine.addCreatingSubFlow(subFlowId);
        stateMachine.notifyEventListeners(listener -> listener.onSubFlowProcessingStart(yFlowId, subFlowId));
        CommandContext flowContext = stateMachine.getCommandContext().fork(subFlowId);
        requestedFlow.setDiverseFlowId(stateMachine.getDiverseFlowId());
        flowCreateService.startFlowCreation(flowContext, requestedFlow, yFlowId);
    });
}
Also used : CommandContext(org.openkilda.wfm.CommandContext) RequestedFlow(org.openkilda.wfm.topology.flowhs.model.RequestedFlow)

Example 9 with CommandContext

use of org.openkilda.wfm.CommandContext in project open-kilda by telstra.

the class OnFinishedWithErrorAction method performWithResponse.

@Override
protected Optional<Message> performWithResponse(State from, State to, Event event, FlowSwapEndpointsContext context, FlowSwapEndpointsFsm stateMachine) {
    ErrorData data;
    if (Event.TIMEOUT.equals(event)) {
        data = new ErrorData(ErrorType.OPERATION_TIMED_OUT, getGenericErrorMessage(), "Flow swap endpoints failed by timeout");
    } else if (Event.NEXT.equals(event)) {
        List<String> flows = stateMachine.getFlowResponses().stream().map(FlowResponse::getPayload).map(FlowDto::getFlowId).collect(Collectors.toList());
        data = new ErrorData(ErrorType.UPDATE_FAILURE, getGenericErrorMessage(), format("Reverted flows: %s", flows));
    } else {
        data = (ErrorData) context.getResponse();
    }
    if (!Event.VALIDATION_ERROR.equals(event)) {
        saveActionToHistory(stateMachine, stateMachine.getFirstFlowId(), stateMachine.getSecondFlowId(), data.getErrorDescription());
        saveActionToHistory(stateMachine, stateMachine.getSecondFlowId(), stateMachine.getFirstFlowId(), data.getErrorDescription());
    }
    updateFlowsStatuses(stateMachine);
    CommandContext commandContext = stateMachine.getCommandContext();
    return Optional.of(new ErrorMessage(data, commandContext.getCreateTime(), commandContext.getCorrelationId()));
}
Also used : FlowDto(org.openkilda.messaging.model.FlowDto) CommandContext(org.openkilda.wfm.CommandContext) List(java.util.List) ErrorMessage(org.openkilda.messaging.error.ErrorMessage) ErrorData(org.openkilda.messaging.error.ErrorData)

Example 10 with CommandContext

use of org.openkilda.wfm.CommandContext in project open-kilda by telstra.

the class OnSubFlowAllocatedAction method buildRerouteResponseMessage.

private Message buildRerouteResponseMessage(YFlowRerouteFsm stateMachine) {
    String yFlowId = stateMachine.getYFlowId();
    List<Long> oldFlowPathCookies = stateMachine.getOldYFlowPathCookies();
    List<FlowPath> flowPaths = transactionManager.doInTransaction(() -> {
        YFlow yflow = yFlowRepository.findById(yFlowId).orElseThrow(() -> new FlowProcessingException(ErrorType.NOT_FOUND, format("Y-flow %s not found", yFlowId)));
        SwitchId sharedSwitchId = yflow.getSharedEndpoint().getSwitchId();
        List<FlowPath> paths = new ArrayList<>();
        for (YSubFlow subFlow : yflow.getSubFlows()) {
            Flow flow = subFlow.getFlow();
            FlowPath flowPath = flow.getPaths().stream().filter(path -> sharedSwitchId.equals(path.getSrcSwitchId()) && !path.isProtected() && !oldFlowPathCookies.contains(path.getCookie().getValue())).findFirst().orElse(sharedSwitchId.equals(flow.getForwardPath().getSrcSwitchId()) ? flow.getForwardPath() : flow.getReversePath());
            paths.add(flowPath);
        }
        return paths;
    });
    List<PathSegment> sharedPathSegments = IntersectionComputer.calculatePathIntersectionFromSource(flowPaths);
    PathInfoData sharedPath = FlowPathMapper.INSTANCE.map(sharedPathSegments);
    List<SubFlowPathDto> subFlowPathDtos = flowPaths.stream().map(flowPath -> new SubFlowPathDto(flowPath.getFlowId(), FlowPathMapper.INSTANCE.map(flowPath))).sorted(Comparator.comparing(SubFlowPathDto::getFlowId)).collect(Collectors.toList());
    PathInfoData oldSharedPath = stateMachine.getOldSharedPath();
    List<SubFlowPathDto> oldSubFlowPathDtos = stateMachine.getOldSubFlowPathDtos();
    YFlowRerouteResponse response = new YFlowRerouteResponse(sharedPath, subFlowPathDtos, !(sharedPath.equals(oldSharedPath) && subFlowPathDtos.equals(oldSubFlowPathDtos)));
    CommandContext commandContext = stateMachine.getCommandContext();
    return new InfoMessage(response, commandContext.getCreateTime(), commandContext.getCorrelationId());
}
Also used : YFlow(org.openkilda.model.YFlow) CommandContext(org.openkilda.wfm.CommandContext) FlowProcessingException(org.openkilda.wfm.topology.flowhs.exception.FlowProcessingException) ArrayList(java.util.ArrayList) SwitchId(org.openkilda.model.SwitchId) PathSegment(org.openkilda.model.PathSegment) YSubFlow(org.openkilda.model.YSubFlow) Flow(org.openkilda.model.Flow) YFlow(org.openkilda.model.YFlow) YSubFlow(org.openkilda.model.YSubFlow) PathInfoData(org.openkilda.messaging.info.event.PathInfoData) YFlowRerouteResponse(org.openkilda.messaging.command.yflow.YFlowRerouteResponse) InfoMessage(org.openkilda.messaging.info.InfoMessage) FlowPath(org.openkilda.model.FlowPath) SubFlowPathDto(org.openkilda.messaging.command.yflow.SubFlowPathDto)

Aggregations

CommandContext (org.openkilda.wfm.CommandContext)95 Test (org.junit.Test)28 Values (org.apache.storm.tuple.Values)27 FlowSegmentRequest (org.openkilda.floodlight.api.request.FlowSegmentRequest)15 Flow (org.openkilda.model.Flow)15 AbstractYFlowTest (org.openkilda.wfm.topology.flowhs.service.AbstractYFlowTest)14 Tuple (org.apache.storm.tuple.Tuple)12 SwitchId (org.openkilda.model.SwitchId)11 YFlow (org.openkilda.model.YFlow)11 BaseSpeakerCommandsRequest (org.openkilda.floodlight.api.request.rulemanager.BaseSpeakerCommandsRequest)9 SpeakerResponse (org.openkilda.floodlight.api.response.SpeakerResponse)9 YFlowRequest (org.openkilda.messaging.command.yflow.YFlowRequest)9 InfoMessage (org.openkilda.messaging.info.InfoMessage)9 TupleImpl (org.apache.storm.tuple.TupleImpl)8 FlowPath (org.openkilda.model.FlowPath)8 ArrayList (java.util.ArrayList)7 FlowRerouteRequest (org.openkilda.messaging.command.flow.FlowRerouteRequest)7 FlowProcessingException (org.openkilda.wfm.topology.flowhs.exception.FlowProcessingException)7 UnknownKeyException (org.openkilda.wfm.topology.flowhs.exception.UnknownKeyException)7 List (java.util.List)6