Search in sources :

Example 1 with FlowUpdateFsm

use of org.openkilda.wfm.topology.flowhs.fsm.update.FlowUpdateFsm in project open-kilda by telstra.

the class RevertResourceAllocationAction method perform.

@Override
protected void perform(State from, State to, Event event, FlowUpdateContext context, FlowUpdateFsm stateMachine) {
    Flow flow = getFlow(stateMachine.getFlowId());
    Stream.of(stateMachine.getNewPrimaryResources(), stateMachine.getNewProtectedResources()).filter(Objects::nonNull).forEach(resources -> {
        transactionManager.doInTransaction(() -> resourcesManager.deallocatePathResources(resources));
        saveHistory(stateMachine, flow, resources);
    });
    stateMachine.getRejectedResources().forEach(flowResources -> {
        transactionManager.doInTransaction(() -> resourcesManager.deallocatePathResources(flowResources));
        saveHistory(stateMachine, flow, flowResources);
    });
    Stream.of(stateMachine.getNewPrimaryForwardPath(), stateMachine.getNewPrimaryReversePath(), stateMachine.getNewProtectedForwardPath(), stateMachine.getNewProtectedReversePath()).forEach(pathId -> flowPathRepository.remove(pathId).ifPresent(flowPath -> {
        updateIslsForFlowPath(flowPath);
        saveRemovalActionWithDumpToHistory(stateMachine, flow, flowPath);
    }));
    stateMachine.getRejectedPaths().stream().forEach(pathId -> flowPathRepository.remove(pathId).ifPresent(flowPath -> {
        updateIslsForFlowPath(flowPath);
        saveRemovalActionWithDumpToHistory(stateMachine, flow, flowPath);
    }));
    stateMachine.setNewPrimaryResources(null);
    stateMachine.setNewPrimaryForwardPath(null);
    stateMachine.setNewPrimaryReversePath(null);
    stateMachine.setNewProtectedResources(null);
    stateMachine.setNewProtectedForwardPath(null);
    stateMachine.setNewProtectedReversePath(null);
}
Also used : HistoryMapper(org.openkilda.wfm.share.mappers.HistoryMapper) FlowUpdateFsm(org.openkilda.wfm.topology.flowhs.fsm.update.FlowUpdateFsm) State(org.openkilda.wfm.topology.flowhs.fsm.update.FlowUpdateFsm.State) FlowResources(org.openkilda.wfm.share.flow.resources.FlowResources) String.format(java.lang.String.format) Event(org.openkilda.wfm.topology.flowhs.fsm.update.FlowUpdateFsm.Event) Objects(java.util.Objects) BaseFlowPathRemovalAction(org.openkilda.wfm.topology.flowhs.fsm.common.actions.BaseFlowPathRemovalAction) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) Flow(org.openkilda.model.Flow) FlowUpdateContext(org.openkilda.wfm.topology.flowhs.fsm.update.FlowUpdateContext) FlowDumpData(org.openkilda.wfm.share.history.model.FlowDumpData) FlowResourcesManager(org.openkilda.wfm.share.flow.resources.FlowResourcesManager) DumpType(org.openkilda.wfm.share.history.model.FlowDumpData.DumpType) PersistenceManager(org.openkilda.persistence.PersistenceManager) Flow(org.openkilda.model.Flow)

Example 2 with FlowUpdateFsm

use of org.openkilda.wfm.topology.flowhs.fsm.update.FlowUpdateFsm in project open-kilda by telstra.

the class AllocateProtectedResourcesAction method allocate.

@Override
protected void allocate(FlowUpdateFsm stateMachine) throws RecoverableException, UnroutableFlowException, ResourceAllocationException {
    String flowId = stateMachine.getFlowId();
    Set<String> flowIds = Sets.newHashSet(flowId);
    if (stateMachine.getBulkUpdateFlowIds() != null) {
        flowIds.addAll(stateMachine.getBulkUpdateFlowIds());
    }
    log.debug("Finding protected path ids for flows {}", flowIds);
    List<PathId> pathIdsToReuse = new ArrayList<>(flowPathRepository.findActualPathIdsByFlowIds(flowIds));
    pathIdsToReuse.addAll(stateMachine.getRejectedPaths());
    Flow tmpFlow = getFlow(flowId);
    FlowPathPair oldPaths = new FlowPathPair(tmpFlow.getProtectedForwardPath(), tmpFlow.getProtectedReversePath());
    FlowPath primaryForward = getFlowPath(tmpFlow, stateMachine.getNewPrimaryForwardPath());
    FlowPath primaryReverse = getFlowPath(tmpFlow, stateMachine.getNewPrimaryReversePath());
    Predicate<GetPathsResult> testNonOverlappingPath = path -> (primaryForward == null || !flowPathBuilder.arePathsOverlapped(path.getForward(), primaryForward)) && (primaryReverse == null || !flowPathBuilder.arePathsOverlapped(path.getReverse(), primaryReverse));
    PathId newForwardPathId = resourcesManager.generatePathId(flowId);
    PathId newReversePathId = resourcesManager.generatePathId(flowId);
    log.debug("Finding a new protected path for flow {}", flowId);
    GetPathsResult allocatedPaths = allocatePathPair(tmpFlow, newForwardPathId, newReversePathId, false, pathIdsToReuse, oldPaths, true, stateMachine.getSharedBandwidthGroupId(), testNonOverlappingPath);
    if (allocatedPaths == null) {
        throw new ResourceAllocationException("Unable to allocate a path");
    }
    if (!testNonOverlappingPath.test(allocatedPaths)) {
        stateMachine.saveActionToHistory("Couldn't find non overlapping protected path");
    } else {
        log.debug("New protected paths have been allocated: {}", allocatedPaths);
        stateMachine.setNewProtectedForwardPath(newForwardPathId);
        stateMachine.setNewProtectedReversePath(newReversePathId);
        stateMachine.setBackUpProtectedPathComputationWayUsed(allocatedPaths.isBackUpPathComputationWayUsed());
        log.debug("Allocating resources for a new protected path of flow {}", flowId);
        FlowResources flowResources = allocateFlowResources(tmpFlow, newForwardPathId, newReversePathId);
        stateMachine.setNewProtectedResources(flowResources);
        FlowPathPair createdPaths = createFlowPathPair(flowId, flowResources, allocatedPaths, false, stateMachine.getSharedBandwidthGroupId());
        log.debug("New protected path has been created: {}", createdPaths);
        saveAllocationActionWithDumpsToHistory(stateMachine, tmpFlow, "protected", createdPaths);
    }
}
Also used : BaseResourceAllocationAction(org.openkilda.wfm.topology.flowhs.fsm.common.actions.BaseResourceAllocationAction) FlowPath(org.openkilda.model.FlowPath) ResourceAllocationException(org.openkilda.wfm.share.flow.resources.ResourceAllocationException) RecoverableException(org.openkilda.pce.exception.RecoverableException) ArrayList(java.util.ArrayList) UnroutableFlowException(org.openkilda.pce.exception.UnroutableFlowException) Flow(org.openkilda.model.Flow) FlowPathPair(org.openkilda.wfm.topology.flow.model.FlowPathPair) PersistenceManager(org.openkilda.persistence.PersistenceManager) PathId(org.openkilda.model.PathId) FlowOperationsDashboardLogger(org.openkilda.wfm.share.logger.FlowOperationsDashboardLogger) ErrorType(org.openkilda.messaging.error.ErrorType) Predicate(java.util.function.Predicate) FlowUpdateFsm(org.openkilda.wfm.topology.flowhs.fsm.update.FlowUpdateFsm) Set(java.util.Set) State(org.openkilda.wfm.topology.flowhs.fsm.update.FlowUpdateFsm.State) FlowResources(org.openkilda.wfm.share.flow.resources.FlowResources) Sets(com.google.common.collect.Sets) Event(org.openkilda.wfm.topology.flowhs.fsm.update.FlowUpdateFsm.Event) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) FlowUpdateContext(org.openkilda.wfm.topology.flowhs.fsm.update.FlowUpdateContext) PathComputer(org.openkilda.pce.PathComputer) FlowResourcesManager(org.openkilda.wfm.share.flow.resources.FlowResourcesManager) GetPathsResult(org.openkilda.pce.GetPathsResult) FlowResources(org.openkilda.wfm.share.flow.resources.FlowResources) ArrayList(java.util.ArrayList) Flow(org.openkilda.model.Flow) FlowPathPair(org.openkilda.wfm.topology.flow.model.FlowPathPair) GetPathsResult(org.openkilda.pce.GetPathsResult) PathId(org.openkilda.model.PathId) FlowPath(org.openkilda.model.FlowPath) ResourceAllocationException(org.openkilda.wfm.share.flow.resources.ResourceAllocationException)

Example 3 with FlowUpdateFsm

use of org.openkilda.wfm.topology.flowhs.fsm.update.FlowUpdateFsm in project open-kilda by telstra.

the class FlowUpdateService method startFlowUpdating.

private void startFlowUpdating(String key, CommandContext commandContext, RequestedFlow request, boolean doNotRevert, Set<String> bulkUpdateFlowIds, String sharedBandwidthGroupId) throws DuplicateKeyException {
    String flowId = request.getFlowId();
    log.debug("Handling flow update request with key {} and flow ID: {}", key, request.getFlowId());
    if (fsmRegister.hasRegisteredFsmWithKey(key)) {
        throw new DuplicateKeyException(key, "There's another active FSM with the same key");
    }
    if (fsmRegister.hasRegisteredFsmWithFlowId(flowId)) {
        sendErrorResponseToNorthbound(ErrorType.REQUEST_INVALID, "Could not update flow", format("Flow %s is updating now", flowId), commandContext);
        log.error("Attempt to create a FSM with key {}, while there's another active FSM for the same flowId {}.", key, flowId);
        return;
    }
    FlowUpdateFsm fsm = fsmFactory.newInstance(request.getFlowId(), commandContext, eventListeners);
    fsm.setSharedBandwidthGroupId(sharedBandwidthGroupId);
    fsmRegister.registerFsm(key, fsm);
    if (request.getFlowEncapsulationType() == null) {
        request.setFlowEncapsulationType(kildaConfigurationRepository.getOrDefault().getFlowEncapsulationType());
    }
    FlowUpdateContext context = FlowUpdateContext.builder().targetFlow(request).doNotRevert(doNotRevert).bulkUpdateFlowIds(bulkUpdateFlowIds).build();
    fsmExecutor.fire(fsm, Event.NEXT, context);
    removeIfFinished(fsm, key);
}
Also used : FlowUpdateFsm(org.openkilda.wfm.topology.flowhs.fsm.update.FlowUpdateFsm) FlowUpdateContext(org.openkilda.wfm.topology.flowhs.fsm.update.FlowUpdateContext) DuplicateKeyException(org.openkilda.wfm.topology.flowhs.exception.DuplicateKeyException)

Example 4 with FlowUpdateFsm

use of org.openkilda.wfm.topology.flowhs.fsm.update.FlowUpdateFsm in project open-kilda by telstra.

the class FlowUpdateService method handleAsyncResponse.

/**
 * Handles async response from worker.
 *
 * @param key command identifier.
 */
public void handleAsyncResponse(@NonNull String key, @NonNull SpeakerFlowSegmentResponse flowResponse) throws UnknownKeyException {
    log.debug("Received flow command response {}", flowResponse);
    FlowUpdateFsm fsm = fsmRegister.getFsmByKey(key).orElseThrow(() -> new UnknownKeyException(key));
    FlowUpdateContext context = FlowUpdateContext.builder().speakerFlowResponse(flowResponse).build();
    if (flowResponse instanceof FlowErrorResponse) {
        fsmExecutor.fire(fsm, Event.ERROR_RECEIVED, context);
    } else {
        fsmExecutor.fire(fsm, Event.RESPONSE_RECEIVED, context);
    }
    removeIfFinished(fsm, key);
}
Also used : FlowErrorResponse(org.openkilda.floodlight.flow.response.FlowErrorResponse) FlowUpdateFsm(org.openkilda.wfm.topology.flowhs.fsm.update.FlowUpdateFsm) FlowUpdateContext(org.openkilda.wfm.topology.flowhs.fsm.update.FlowUpdateContext) UnknownKeyException(org.openkilda.wfm.topology.flowhs.exception.UnknownKeyException)

Example 5 with FlowUpdateFsm

use of org.openkilda.wfm.topology.flowhs.fsm.update.FlowUpdateFsm in project open-kilda by telstra.

the class FlowUpdateService method handleTimeout.

/**
 * Handles timeout case.
 *
 * @param key command identifier.
 */
public void handleTimeout(@NonNull String key) throws UnknownKeyException {
    log.debug("Handling timeout for {}", key);
    FlowUpdateFsm fsm = fsmRegister.getFsmByKey(key).orElseThrow(() -> new UnknownKeyException(key));
    fsmExecutor.fire(fsm, Event.TIMEOUT);
    removeIfFinished(fsm, key);
}
Also used : FlowUpdateFsm(org.openkilda.wfm.topology.flowhs.fsm.update.FlowUpdateFsm) UnknownKeyException(org.openkilda.wfm.topology.flowhs.exception.UnknownKeyException)

Aggregations

FlowUpdateFsm (org.openkilda.wfm.topology.flowhs.fsm.update.FlowUpdateFsm)5 FlowUpdateContext (org.openkilda.wfm.topology.flowhs.fsm.update.FlowUpdateContext)4 Slf4j (lombok.extern.slf4j.Slf4j)2 Flow (org.openkilda.model.Flow)2 PersistenceManager (org.openkilda.persistence.PersistenceManager)2 FlowResources (org.openkilda.wfm.share.flow.resources.FlowResources)2 FlowResourcesManager (org.openkilda.wfm.share.flow.resources.FlowResourcesManager)2 UnknownKeyException (org.openkilda.wfm.topology.flowhs.exception.UnknownKeyException)2 Event (org.openkilda.wfm.topology.flowhs.fsm.update.FlowUpdateFsm.Event)2 State (org.openkilda.wfm.topology.flowhs.fsm.update.FlowUpdateFsm.State)2 Sets (com.google.common.collect.Sets)1 String.format (java.lang.String.format)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Objects (java.util.Objects)1 Set (java.util.Set)1 Predicate (java.util.function.Predicate)1 Stream (java.util.stream.Stream)1 FlowErrorResponse (org.openkilda.floodlight.flow.response.FlowErrorResponse)1 ErrorType (org.openkilda.messaging.error.ErrorType)1