use of org.openkilda.wfm.share.utils.rule.validation.SimpleSwitchRule in project open-kilda by telstra.
the class FlowValidationService method validateFlow.
/**
* Validate flow.
*/
public List<FlowValidationResponse> validateFlow(String flowId, List<SwitchFlowEntries> switchFlowEntries, List<SwitchMeterEntries> switchMeterEntries, List<SwitchGroupEntries> switchGroupEntries) throws FlowNotFoundException, SwitchNotFoundException {
Map<SwitchId, List<SimpleSwitchRule>> switchRules = new HashMap<>();
int rulesCount = 0;
int metersCount = 0;
for (SwitchFlowEntries switchRulesEntries : switchFlowEntries) {
SwitchMeterEntries switchMeters = switchMeterEntries.stream().filter(meterEntries -> switchRulesEntries.getSwitchId().equals(meterEntries.getSwitchId())).findFirst().orElse(null);
SwitchGroupEntries switchGroup = switchGroupEntries.stream().filter(groupEntries -> switchRulesEntries.getSwitchId().equals(groupEntries.getSwitchId())).findFirst().orElse(null);
List<SimpleSwitchRule> simpleSwitchRules = simpleSwitchRuleConverter.convertSwitchFlowEntriesToSimpleSwitchRules(switchRulesEntries, switchMeters, switchGroup);
switchRules.put(switchRulesEntries.getSwitchId(), simpleSwitchRules);
rulesCount += Optional.ofNullable(switchRulesEntries.getFlowEntries()).map(List::size).orElse(0);
metersCount += Optional.ofNullable(switchMeters).map(SwitchMeterEntries::getMeterEntries).map(List::size).orElse(0);
}
Optional<Flow> foundFlow = flowRepository.findById(flowId);
if (!foundFlow.isPresent()) {
throw new FlowNotFoundException(flowId);
}
Flow flow = foundFlow.get();
if (flow.getForwardPath() == null) {
throw new InvalidPathException(flowId, "Forward path was not returned.");
}
if (flow.getReversePath() == null) {
throw new InvalidPathException(flowId, "Reverse path was not returned.");
}
List<FlowValidationResponse> flowValidationResponse = new ArrayList<>();
List<SimpleSwitchRule> forwardRules = getSimpleSwitchRules(flow, flow.getForwardPath(), flow.getReversePath());
flowValidationResponse.add(compare(switchRules, forwardRules, flowId, rulesCount, metersCount));
List<SimpleSwitchRule> reverseRules = getSimpleSwitchRules(flow, flow.getReversePath(), flow.getForwardPath());
flowValidationResponse.add(compare(switchRules, reverseRules, flowId, rulesCount, metersCount));
if (flow.getProtectedForwardPath() != null) {
List<SimpleSwitchRule> forwardProtectedRules = getSimpleSwitchRules(flow, flow.getProtectedForwardPath(), flow.getProtectedReversePath());
flowValidationResponse.add(compare(switchRules, forwardProtectedRules, flowId, rulesCount, metersCount));
}
if (flow.getProtectedReversePath() != null) {
List<SimpleSwitchRule> reverseProtectedRules = getSimpleSwitchRules(flow, flow.getProtectedReversePath(), flow.getProtectedForwardPath());
flowValidationResponse.add(compare(switchRules, reverseProtectedRules, flowId, rulesCount, metersCount));
}
return flowValidationResponse;
}
use of org.openkilda.wfm.share.utils.rule.validation.SimpleSwitchRule in project open-kilda by telstra.
the class FlowValidationService method compare.
private FlowValidationResponse compare(Map<SwitchId, List<SimpleSwitchRule>> rulesPerSwitch, List<SimpleSwitchRule> rulesFromDb, String flowId, int totalSwitchRules, int metersCount) throws SwitchNotFoundException {
List<PathDiscrepancyEntity> discrepancies = new ArrayList<>();
List<Long> pktCounts = new ArrayList<>();
List<Long> byteCounts = new ArrayList<>();
boolean ingressMirrorFlowIsPresent = false;
boolean egressMirrorFlowIsPresent = false;
for (SimpleSwitchRule simpleRule : rulesFromDb) {
discrepancies.addAll(simpleSwitchRuleComparator.findDiscrepancy(simpleRule, rulesPerSwitch.get(simpleRule.getSwitchId()), pktCounts, byteCounts));
if (new FlowSegmentCookie(simpleRule.getCookie()).isMirror() && simpleRule.isIngressRule()) {
ingressMirrorFlowIsPresent = true;
}
if (new FlowSegmentCookie(simpleRule.getCookie()).isMirror() && simpleRule.isEgressRule()) {
egressMirrorFlowIsPresent = true;
}
}
int flowMetersCount = (int) rulesFromDb.stream().filter(rule -> rule.getMeterId() != null).count();
return FlowValidationResponse.builder().flowId(flowId).discrepancies(discrepancies).asExpected(discrepancies.isEmpty()).pktCounts(pktCounts).byteCounts(byteCounts).flowRulesTotal(rulesFromDb.size()).switchRulesTotal(totalSwitchRules).flowMetersTotal(flowMetersCount).switchMetersTotal(metersCount).ingressMirrorFlowIsPresent(ingressMirrorFlowIsPresent).egressMirrorFlowIsPresent(egressMirrorFlowIsPresent).build();
}
use of org.openkilda.wfm.share.utils.rule.validation.SimpleSwitchRule in project open-kilda by telstra.
the class YFlowValidationService method buildSimpleSwitchRules.
private List<SimpleSwitchRule> buildSimpleSwitchRules(Flow subFlow, SwitchId sharedEndpoint, MeterId sharedMeterId, PathId forwardId, PathId reverseId, SwitchId yPoint, MeterId yMeterId) {
EncapsulationId encapsulationId = flowResourcesManager.getEncapsulationResources(forwardId, reverseId, subFlow.getEncapsulationType()).map(EncapsulationResources::getEncapsulation).orElseThrow(() -> new IllegalStateException(String.format("Encapsulation id was not found, pathId: %s", forwardId)));
FlowPath forward = subFlow.getPath(forwardId).orElseThrow(() -> new IllegalStateException(String.format("Path was not found, pathId: %s", forwardId)));
FlowPath reverse = subFlow.getPath(reverseId).orElseThrow(() -> new IllegalStateException(String.format("Path was not found, pathId: %s", reverseId)));
if (reverse.getSrcSwitchId().equals(sharedEndpoint)) {
FlowPath tmp = reverse;
reverse = forward;
forward = tmp;
}
Collection<SimpleSwitchRule> ingressRules = simpleSwitchRuleConverter.buildIngressSimpleSwitchRules(subFlow, forward, encapsulationId, flowMeterMinBurstSizeInKbits, flowMeterBurstCoefficient);
// TODO: apply shared endpoint y-flow cookie flags & sharedMeterId
List<SimpleSwitchRule> result = new ArrayList<>(ingressRules);
List<PathSegment> orderedSegments = reverse.getSegments().stream().sorted(Comparator.comparingInt(PathSegment::getSeqId)).collect(Collectors.toList());
for (int i = 1; i < orderedSegments.size(); i++) {
PathSegment srcPathSegment = orderedSegments.get(i - 1);
PathSegment dstPathSegment = orderedSegments.get(i);
if (dstPathSegment.getSrcSwitch().equals(yPoint)) {
SimpleSwitchRule yPointTransitRules = simpleSwitchRuleConverter.buildTransitSimpleSwitchRule(subFlow, reverse, srcPathSegment, dstPathSegment, encapsulationId);
// TODO: apply shared endpoint y-flow cookie flags & yMeterId
result.add(yPointTransitRules);
break;
}
}
return result;
}
use of org.openkilda.wfm.share.utils.rule.validation.SimpleSwitchRule in project open-kilda by telstra.
the class YFlowValidationService method validateYFlowResources.
/**
* Validate y-flow.
*/
public YFlowDiscrepancyDto validateYFlowResources(String yFlowId, List<SwitchFlowEntries> actualSwitchFlowEntries, List<SwitchMeterEntries> actualSwitchMeterEntries) throws FlowNotFoundException, SwitchNotFoundException {
Map<SwitchId, List<SimpleSwitchRule>> actualRules = new HashMap<>();
for (SwitchFlowEntries switchRulesEntries : actualSwitchFlowEntries) {
SwitchMeterEntries switchMeters = actualSwitchMeterEntries.stream().filter(meterEntries -> switchRulesEntries.getSwitchId().equals(meterEntries.getSwitchId())).findFirst().orElse(null);
List<SimpleSwitchRule> simpleSwitchRules = simpleSwitchRuleConverter.convertSwitchFlowEntriesToSimpleSwitchRules(switchRulesEntries, switchMeters, null);
actualRules.put(switchRulesEntries.getSwitchId(), simpleSwitchRules);
}
YFlow yFlow = yFlowRepository.findById(yFlowId).orElseThrow(() -> new FlowNotFoundException(yFlowId));
List<SimpleSwitchRule> expectedRules = new ArrayList<>();
for (YSubFlow subFlow : yFlow.getSubFlows()) {
Flow flow = subFlow.getFlow();
expectedRules.addAll(buildSimpleSwitchRules(flow, yFlow.getSharedEndpoint().getSwitchId(), yFlow.getSharedEndpointMeterId(), flow.getForwardPathId(), flow.getReversePathId(), yFlow.getYPoint(), yFlow.getMeterId()));
if (flow.isAllocateProtectedPath()) {
if (flow.getProtectedForwardPathId() != null && flow.getProtectedReversePathId() != null) {
expectedRules.addAll(buildSimpleSwitchRules(flow, yFlow.getSharedEndpoint().getSwitchId(), yFlow.getSharedEndpointMeterId(), flow.getProtectedForwardPathId(), flow.getProtectedReversePathId(), yFlow.getProtectedPathYPoint(), yFlow.getProtectedPathMeterId()));
} else {
log.warn("Sub-flow {} of y-flow {} has no expected protected paths", flow.getFlowId(), yFlowId);
}
}
}
List<PathDiscrepancyEntity> discrepancies = new ArrayList<>();
for (SimpleSwitchRule simpleRule : expectedRules) {
discrepancies.addAll(simpleSwitchRuleComparator.findDiscrepancy(simpleRule, actualRules.get(simpleRule.getSwitchId())));
}
return YFlowDiscrepancyDto.builder().discrepancies(discrepancies).asExpected(discrepancies.isEmpty()).build();
}
use of org.openkilda.wfm.share.utils.rule.validation.SimpleSwitchRule in project open-kilda by telstra.
the class SimpleSwitchRuleComparator method findDiscrepancy.
/**
* Compare expected and actual rules and gather discrepancy list.
*/
public List<PathDiscrepancyEntity> findDiscrepancy(SimpleSwitchRule expected, List<SimpleSwitchRule> actual, List<Long> pktCounts, List<Long> byteCounts) throws SwitchNotFoundException {
List<PathDiscrepancyEntity> discrepancies = new ArrayList<>();
SimpleSwitchRule matched = findMatched(expected, actual);
if (matched == null) {
discrepancies.add(new PathDiscrepancyEntity(String.valueOf(expected), "all", String.valueOf(expected), ""));
pktCounts.add(-1L);
byteCounts.add(-1L);
} else {
discrepancies.addAll(getRuleDiscrepancies(expected, matched));
pktCounts.add(matched.getPktCount());
byteCounts.add(matched.getByteCount());
}
return discrepancies;
}
Aggregations