Search in sources :

Example 1 with SimpleSwitchRule

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;
}
Also used : FlowNotFoundException(org.openkilda.wfm.error.FlowNotFoundException) SwitchFlowEntries(org.openkilda.messaging.info.rule.SwitchFlowEntries) HashMap(java.util.HashMap) SwitchMeterEntries(org.openkilda.messaging.info.meter.SwitchMeterEntries) ArrayList(java.util.ArrayList) SwitchId(org.openkilda.model.SwitchId) FlowValidationResponse(org.openkilda.messaging.info.flow.FlowValidationResponse) InvalidPathException(java.nio.file.InvalidPathException) Flow(org.openkilda.model.Flow) SimpleSwitchRule(org.openkilda.wfm.share.utils.rule.validation.SimpleSwitchRule) SwitchGroupEntries(org.openkilda.messaging.info.rule.SwitchGroupEntries) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with SimpleSwitchRule

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();
}
Also used : FlowSegmentCookie(org.openkilda.model.cookie.FlowSegmentCookie) SimpleSwitchRule(org.openkilda.wfm.share.utils.rule.validation.SimpleSwitchRule) PathDiscrepancyEntity(org.openkilda.messaging.info.flow.PathDiscrepancyEntity) ArrayList(java.util.ArrayList)

Example 3 with SimpleSwitchRule

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;
}
Also used : SimpleSwitchRule(org.openkilda.wfm.share.utils.rule.validation.SimpleSwitchRule) EncapsulationId(org.openkilda.model.EncapsulationId) ArrayList(java.util.ArrayList) PathSegment(org.openkilda.model.PathSegment) FlowPath(org.openkilda.model.FlowPath)

Example 4 with SimpleSwitchRule

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();
}
Also used : YFlow(org.openkilda.model.YFlow) FlowNotFoundException(org.openkilda.wfm.error.FlowNotFoundException) SwitchFlowEntries(org.openkilda.messaging.info.rule.SwitchFlowEntries) HashMap(java.util.HashMap) SwitchMeterEntries(org.openkilda.messaging.info.meter.SwitchMeterEntries) ArrayList(java.util.ArrayList) PathDiscrepancyEntity(org.openkilda.messaging.info.flow.PathDiscrepancyEntity) SwitchId(org.openkilda.model.SwitchId) YSubFlow(org.openkilda.model.YSubFlow) Flow(org.openkilda.model.Flow) YFlow(org.openkilda.model.YFlow) YSubFlow(org.openkilda.model.YSubFlow) SimpleSwitchRule(org.openkilda.wfm.share.utils.rule.validation.SimpleSwitchRule) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with SimpleSwitchRule

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;
}
Also used : SimpleSwitchRule(org.openkilda.wfm.share.utils.rule.validation.SimpleSwitchRule) PathDiscrepancyEntity(org.openkilda.messaging.info.flow.PathDiscrepancyEntity) ArrayList(java.util.ArrayList)

Aggregations

ArrayList (java.util.ArrayList)5 SimpleSwitchRule (org.openkilda.wfm.share.utils.rule.validation.SimpleSwitchRule)5 PathDiscrepancyEntity (org.openkilda.messaging.info.flow.PathDiscrepancyEntity)3 HashMap (java.util.HashMap)2 List (java.util.List)2 SwitchMeterEntries (org.openkilda.messaging.info.meter.SwitchMeterEntries)2 SwitchFlowEntries (org.openkilda.messaging.info.rule.SwitchFlowEntries)2 Flow (org.openkilda.model.Flow)2 SwitchId (org.openkilda.model.SwitchId)2 FlowNotFoundException (org.openkilda.wfm.error.FlowNotFoundException)2 InvalidPathException (java.nio.file.InvalidPathException)1 FlowValidationResponse (org.openkilda.messaging.info.flow.FlowValidationResponse)1 SwitchGroupEntries (org.openkilda.messaging.info.rule.SwitchGroupEntries)1 EncapsulationId (org.openkilda.model.EncapsulationId)1 FlowPath (org.openkilda.model.FlowPath)1 PathSegment (org.openkilda.model.PathSegment)1 YFlow (org.openkilda.model.YFlow)1 YSubFlow (org.openkilda.model.YSubFlow)1 FlowSegmentCookie (org.openkilda.model.cookie.FlowSegmentCookie)1