use of org.openkilda.wfm.topology.flowhs.fsm.reroute.FlowRerouteFsm in project open-kilda by telstra.
the class RevertResourceAllocationAction method perform.
@Override
protected void perform(State from, State to, Event event, FlowRerouteContext context, FlowRerouteFsm 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);
}
use of org.openkilda.wfm.topology.flowhs.fsm.reroute.FlowRerouteFsm in project open-kilda by telstra.
the class FlowRerouteService method startFlowRerouting.
private void startFlowRerouting(String key, FlowRerouteRequest reroute, CommandContext commandContext, String sharedBandwidthGroupId) {
String flowId = reroute.getFlowId();
log.debug("Handling flow reroute request with key {} and flow ID: {}", key, flowId);
try {
checkRequestsCollision(key, flowId);
} catch (Exception e) {
log.error(e.getMessage());
fsmRegister.getFsmByKey(key).ifPresent(fsm -> removeIfFinished(fsm, key));
return;
}
if (fsmRegister.hasRegisteredFsmWithFlowId(flowId)) {
carrier.sendRerouteResultStatus(flowId, new RerouteInProgressError(), commandContext.getCorrelationId());
return;
}
FlowRerouteFsm fsm = fsmFactory.newInstance(flowId, commandContext, eventListeners);
fsm.setSharedBandwidthGroupId(sharedBandwidthGroupId);
fsmRegister.registerFsm(key, fsm);
FlowRerouteContext context = FlowRerouteContext.builder().flowId(flowId).affectedIsl(reroute.getAffectedIsl()).forceReroute(reroute.isForce()).ignoreBandwidth(reroute.isIgnoreBandwidth()).effectivelyDown(reroute.isEffectivelyDown()).rerouteReason(reroute.getReason()).build();
fsmExecutor.fire(fsm, Event.NEXT, context);
removeIfFinished(fsm, key);
}
use of org.openkilda.wfm.topology.flowhs.fsm.reroute.FlowRerouteFsm in project open-kilda by telstra.
the class FlowRerouteService 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);
FlowRerouteFsm fsm = fsmRegister.getFsmByKey(key).orElseThrow(() -> new UnknownKeyException(key));
FlowRerouteContext context = FlowRerouteContext.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);
}
use of org.openkilda.wfm.topology.flowhs.fsm.reroute.FlowRerouteFsm in project open-kilda by telstra.
the class FlowRerouteService method handleTimeout.
/**
* Handles timeout case.
*
* @param key command identifier.
*/
public void handleTimeout(@NonNull String key) throws UnknownKeyException {
log.debug("Handling timeout for {}", key);
FlowRerouteFsm fsm = fsmRegister.getFsmByKey(key).orElseThrow(() -> new UnknownKeyException(key));
fsmExecutor.fire(fsm, Event.TIMEOUT);
removeIfFinished(fsm, key);
}
use of org.openkilda.wfm.topology.flowhs.fsm.reroute.FlowRerouteFsm in project open-kilda by telstra.
the class AllocateProtectedResourcesAction method allocate.
@TimedExecution("fsm.resource_allocation_protected")
@Override
protected void allocate(FlowRerouteFsm stateMachine) throws RecoverableException, UnroutableFlowException, ResourceAllocationException {
String flowId = stateMachine.getFlowId();
Flow tmpFlowCopy = getFlow(flowId);
// Detach the entity to avoid propagation to the database.
flowRepository.detach(tmpFlowCopy);
if (stateMachine.getNewEncapsulationType() != null) {
// This is for PCE to use proper (updated) encapsulation type.
tmpFlowCopy.setEncapsulationType(stateMachine.getNewEncapsulationType());
}
FlowPathPair oldPaths = new FlowPathPair(tmpFlowCopy.getProtectedForwardPath(), tmpFlowCopy.getProtectedReversePath());
FlowPath primaryForward = tmpFlowCopy.getPath(stateMachine.getNewPrimaryForwardPath()).orElse(tmpFlowCopy.getForwardPath());
FlowPath primaryReverse = tmpFlowCopy.getPath(stateMachine.getNewPrimaryReversePath()).orElse(tmpFlowCopy.getReversePath());
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);
List<PathId> pathsToReuse = Lists.newArrayList(tmpFlowCopy.getProtectedForwardPathId(), tmpFlowCopy.getProtectedReversePathId());
pathsToReuse.addAll(stateMachine.getRejectedPaths());
log.debug("Finding a new protected path for flow {}", flowId);
GetPathsResult allocatedPaths = allocatePathPair(tmpFlowCopy, newForwardPathId, newReversePathId, stateMachine.isIgnoreBandwidth(), pathsToReuse, oldPaths, stateMachine.isRecreateIfSamePath(), stateMachine.getSharedBandwidthGroupId(), testNonOverlappingPath);
if (allocatedPaths != null) {
stateMachine.setBackUpProtectedPathComputationWayUsed(allocatedPaths.isBackUpPathComputationWayUsed());
if (!testNonOverlappingPath.test(allocatedPaths)) {
stateMachine.saveActionToHistory("Couldn't find non overlapping protected path. Skipped creating it");
stateMachine.fireNoPathFound("Couldn't find non overlapping protected path");
} else {
log.debug("New protected paths have been allocated: {}", allocatedPaths);
stateMachine.setNewProtectedForwardPath(newForwardPathId);
stateMachine.setNewProtectedReversePath(newReversePathId);
log.debug("Allocating resources for a new protected path of flow {}", flowId);
FlowResources flowResources = allocateFlowResources(tmpFlowCopy, newForwardPathId, newReversePathId);
stateMachine.setNewProtectedResources(flowResources);
FlowPathPair createdPaths = createFlowPathPair(flowId, flowResources, allocatedPaths, stateMachine.isIgnoreBandwidth(), stateMachine.getSharedBandwidthGroupId());
log.debug("New protected paths have been created: {}", createdPaths);
saveAllocationActionWithDumpsToHistory(stateMachine, tmpFlowCopy, "protected", createdPaths);
}
} else {
stateMachine.saveActionToHistory("Found the same protected path. Skipped creating of it");
}
}
Aggregations