use of org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory in project open-kilda by telstra.
the class EmitIngressRulesVerifyRequestsAction method perform.
@Override
public void perform(State from, State to, Event event, FlowRerouteContext context, FlowRerouteFsm stateMachine) {
Map<UUID, FlowSegmentRequestFactory> requestsStorage = stateMachine.getIngressCommands();
List<FlowSegmentRequestFactory> requestFactories = new ArrayList<>(requestsStorage.values());
requestsStorage.clear();
stateMachine.clearPendingAndRetriedAndFailedCommands();
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 ingress rules");
}
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, FlowRerouteContext context, FlowRerouteFsm stateMachine) {
String flowId = stateMachine.getFlowId();
Flow flow = getFlow(flowId);
// Detach the entity to avoid propagation to the database.
flowRepository.detach(flow);
if (stateMachine.getNewEncapsulationType() != null) {
// This is for commandBuilder.buildIngressOnly() to use proper (updated) encapsulation type.
flow.setEncapsulationType(stateMachine.getNewEncapsulationType());
}
FlowCommandBuilder commandBuilder = commandBuilderFactory.getBuilder(flow.getEncapsulationType());
Collection<FlowSegmentRequestFactory> requestFactories = new ArrayList<>();
if (stateMachine.getNewPrimaryForwardPath() != null && stateMachine.getNewPrimaryReversePath() != null) {
FlowPath newForward = getFlowPath(flow, stateMachine.getNewPrimaryForwardPath());
FlowPath newReverse = getFlowPath(flow, stateMachine.getNewPrimaryReversePath());
SpeakerRequestBuildContext speakerContext = buildBaseSpeakerContextForInstall(newForward.getSrcSwitchId(), newReverse.getSrcSwitchId());
requestFactories.addAll(commandBuilder.buildIngressOnly(stateMachine.getCommandContext(), flow, newForward, newReverse, speakerContext));
}
// Installation of ingress rules for protected paths is skipped. These paths are activated on swap.
stateMachine.clearPendingAndRetriedAndFailedCommands();
if (requestFactories.isEmpty()) {
stateMachine.saveActionToHistory("No need to install ingress rules");
stateMachine.fire(Event.INGRESS_IS_SKIPPED);
} else {
Map<UUID, FlowSegmentRequestFactory> requestsStorage = stateMachine.getIngressCommands();
for (FlowSegmentRequestFactory factory : requestFactories) {
FlowSegmentRequest request = factory.makeInstallRequest(commandIdGenerator.generate());
requestsStorage.put(request.getCommandId(), factory);
stateMachine.getCarrier().sendSpeakerRequest(request);
}
requestsStorage.forEach((key, value) -> stateMachine.addPendingCommand(key, value.getSwitchId()));
stateMachine.saveActionToHistory("Commands for installing ingress rules have been sent");
}
}
use of org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory in project open-kilda by telstra.
the class OnReceivedInstallResponseAction method perform.
@Override
protected void perform(State from, State to, Event event, FlowRerouteContext context, FlowRerouteFsm stateMachine) {
SpeakerFlowSegmentResponse response = context.getSpeakerFlowResponse();
UUID commandId = response.getCommandId();
FlowSegmentRequestFactory command = stateMachine.getInstallCommand(commandId);
if (!stateMachine.getPendingCommands().containsKey(commandId) || command == null) {
log.info("Received a response for unexpected command: {}", response);
return;
}
MeterRegistryHolder.getRegistry().ifPresent(registry -> {
if (response.getRequestCreateTime() > 0) {
long roundtrip = Duration.between(Instant.ofEpochMilli(response.getRequestCreateTime()), Instant.now()).toNanos();
if (roundtrip > 0) {
registry.timer("fsm.install_command.roundtrip").record(roundtrip, TimeUnit.NANOSECONDS);
}
}
if (response.getExecutionTime() > 0) {
registry.timer("fsm.install_command.floodlight_execution").record(response.getExecutionTime(), TimeUnit.NANOSECONDS);
}
});
if (response.isSuccess()) {
stateMachine.removePendingCommand(commandId);
stateMachine.saveActionToHistory("Rule was installed", format("The rule was installed: switch %s, cookie %s", response.getSwitchId(), command.getCookie()));
} else {
FlowErrorResponse errorResponse = (FlowErrorResponse) response;
int attempt = stateMachine.doRetryForCommand(commandId);
if (attempt <= speakerCommandRetriesLimit) {
stateMachine.saveErrorToHistory("Failed to install rule", format("Failed to install the rule: commandId %s, switch %s, cookie %s. Error %s. " + "Retrying (attempt %d)", commandId, errorResponse.getSwitchId(), command.getCookie(), errorResponse, attempt));
stateMachine.getCarrier().sendSpeakerRequest(command.makeInstallRequest(commandId));
} else {
stateMachine.removePendingCommand(commandId);
stateMachine.saveErrorToHistory("Failed to install rule", format("Failed to install the rule: commandId %s, switch %s, cookie %s. Error: %s", commandId, errorResponse.getSwitchId(), command.getCookie(), errorResponse));
stateMachine.addFailedCommand(commandId, errorResponse);
}
}
if (stateMachine.getPendingCommands().isEmpty()) {
if (stateMachine.getFailedCommands().isEmpty()) {
log.debug("Received responses for all pending install commands of the flow {}", stateMachine.getFlowId());
stateMachine.fire(Event.RULES_INSTALLED);
} else {
String errorMessage = format("Received error response(s) for %d install commands", stateMachine.getFailedCommands().size());
stateMachine.saveErrorToHistory(errorMessage);
stateMachine.fireError(errorMessage);
}
}
}
use of org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory in project open-kilda by telstra.
the class InstallNonIngressRulesAction method perform.
@Override
protected void perform(State from, State to, Event event, FlowRerouteContext context, FlowRerouteFsm stateMachine) {
String flowId = stateMachine.getFlowId();
Flow flow = getFlow(flowId);
// Detach the entity to avoid propagation to the database.
flowRepository.detach(flow);
if (stateMachine.getNewEncapsulationType() != null) {
// This is for commandBuilder.buildAllExceptIngress() to use proper (updated) encapsulation type.
flow.setEncapsulationType(stateMachine.getNewEncapsulationType());
}
FlowCommandBuilder commandBuilder = commandBuilderFactory.getBuilder(flow.getEncapsulationType());
Collection<FlowSegmentRequestFactory> requestFactories = new ArrayList<>();
if (stateMachine.getNewPrimaryForwardPath() != null && stateMachine.getNewPrimaryReversePath() != null) {
FlowPath newForward = getFlowPath(flow, stateMachine.getNewPrimaryForwardPath());
FlowPath newReverse = getFlowPath(flow, stateMachine.getNewPrimaryReversePath());
requestFactories.addAll(commandBuilder.buildAllExceptIngress(stateMachine.getCommandContext(), flow, newForward, newReverse));
}
if (stateMachine.getNewProtectedForwardPath() != null && stateMachine.getNewProtectedReversePath() != null) {
FlowPath newForward = getFlowPath(flow, stateMachine.getNewProtectedForwardPath());
FlowPath newReverse = getFlowPath(flow, stateMachine.getNewProtectedReversePath());
requestFactories.addAll(commandBuilder.buildAllExceptIngress(stateMachine.getCommandContext(), flow, newForward, newReverse));
}
stateMachine.clearPendingAndRetriedAndFailedCommands();
if (requestFactories.isEmpty()) {
stateMachine.saveActionToHistory("No need to install non ingress rules");
stateMachine.fire(Event.RULES_INSTALLED);
} else {
Map<UUID, FlowSegmentRequestFactory> requestsStorage = stateMachine.getNonIngressCommands();
for (FlowSegmentRequestFactory factory : requestFactories) {
FlowSegmentRequest request = factory.makeInstallRequest(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("Commands for installing non ingress rules have been sent");
}
}
use of org.openkilda.floodlight.api.request.factory.FlowSegmentRequestFactory in project open-kilda by telstra.
the class ValidateIngressRulesAction method perform.
@Override
protected void perform(State from, State to, Event event, FlowPathSwapContext context, FlowPathSwapFsm stateMachine) {
SpeakerResponse response = context.getSpeakerResponse();
UUID commandId = response.getCommandId();
FlowSegmentRequestFactory command = stateMachine.getIngressCommands().get(commandId);
if (!stateMachine.getPendingCommands().containsKey(commandId) || command == null) {
log.info("Received a response for unexpected command: {}", response);
return;
}
if (response.isSuccess()) {
stateMachine.removePendingCommand(commandId);
stateMachine.saveActionToHistory("Rule was validated", format("The ingress rule has been validated successfully: switch %s, cookie %s", command.getSwitchId(), command.getCookie()));
} else {
FlowErrorResponse errorResponse = (FlowErrorResponse) response;
int attempt = stateMachine.doRetryForCommand(commandId);
if (attempt <= speakerCommandRetriesLimit && errorResponse.getErrorCode() != FlowErrorResponse.ErrorCode.MISSING_OF_FLOWS) {
stateMachine.saveErrorToHistory("Rule validation failed", format("Failed to validate the ingress rule: commandId %s, switch %s, cookie %s. Error %s. " + "Retrying (attempt %d)", commandId, errorResponse.getSwitchId(), command.getCookie(), errorResponse, attempt));
stateMachine.getCarrier().sendSpeakerRequest(command.makeInstallRequest(commandId));
} else {
stateMachine.removePendingCommand(commandId);
stateMachine.saveErrorToHistory("Rule validation failed", format("Failed to validate the ingress rule: commandId %s, switch %s, cookie %s. Error %s", commandId, errorResponse.getSwitchId(), command.getCookie(), errorResponse));
stateMachine.getFailedValidationResponses().put(commandId, response);
}
}
if (stateMachine.getPendingCommands().isEmpty()) {
if (stateMachine.getFailedValidationResponses().isEmpty()) {
log.debug("Ingress rules have been validated for flow {}", stateMachine.getFlowId());
stateMachine.fire(Event.RULES_VALIDATED);
} else {
stateMachine.saveErrorToHistory(format("Found missing rules or received error response(s) on %d validation commands", stateMachine.getFailedValidationResponses().size()));
stateMachine.fire(Event.MISSING_RULE_FOUND);
}
}
}
Aggregations