Search in sources :

Example 1 with SwitchMeterEntries

use of org.openkilda.messaging.info.meter.SwitchMeterEntries 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 SwitchMeterEntries

use of org.openkilda.messaging.info.meter.SwitchMeterEntries in project open-kilda by telstra.

the class OnReceivedYFlowResourcesAction method perform.

@Override
public void perform(State from, State to, Event event, YFlowValidationContext context, YFlowValidationFsm stateMachine) {
    MessageData data = context.getSpeakerResponse();
    if (data instanceof SwitchFlowEntries) {
        SwitchFlowEntries switchFlowEntries = (SwitchFlowEntries) data;
        log.info("Switch rules received for switch {}", switchFlowEntries.getSwitchId());
        stateMachine.getReceivedRules().add(switchFlowEntries);
        stateMachine.setAwaitingRules(stateMachine.getAwaitingRules() - 1);
        checkOfCompleteDataCollection(stateMachine);
    } else if (data instanceof SwitchMeterEntries) {
        SwitchMeterEntries switchMeterEntries = (SwitchMeterEntries) data;
        log.info("Switch meters received for switch {}", switchMeterEntries.getSwitchId());
        stateMachine.getReceivedMeters().add(switchMeterEntries);
        stateMachine.setAwaitingMeters(stateMachine.getAwaitingMeters() - 1);
        checkOfCompleteDataCollection(stateMachine);
    } else if (data instanceof SwitchMeterUnsupported) {
        SwitchMeterUnsupported meterUnsupported = (SwitchMeterUnsupported) data;
        log.info("Meters unsupported for switch {}", meterUnsupported.getSwitchId());
        stateMachine.getReceivedMeters().add(SwitchMeterEntries.builder().switchId(meterUnsupported.getSwitchId()).meterEntries(Collections.emptyList()).build());
        stateMachine.setAwaitingMeters(stateMachine.getAwaitingMeters() - 1);
        checkOfCompleteDataCollection(stateMachine);
    } else if (data instanceof ErrorData) {
        ErrorData errorData = (ErrorData) data;
        String errorMessage = format("%s : %s", errorData.getErrorMessage(), errorData.getErrorDescription());
        stateMachine.fireError(errorMessage);
    } else {
        String errorMessage = format("Unhandled message : %s", data);
        stateMachine.fireError(errorMessage);
    }
}
Also used : SwitchFlowEntries(org.openkilda.messaging.info.rule.SwitchFlowEntries) SwitchMeterUnsupported(org.openkilda.messaging.info.meter.SwitchMeterUnsupported) MessageData(org.openkilda.messaging.MessageData) SwitchMeterEntries(org.openkilda.messaging.info.meter.SwitchMeterEntries) ErrorData(org.openkilda.messaging.error.ErrorData)

Example 3 with SwitchMeterEntries

use of org.openkilda.messaging.info.meter.SwitchMeterEntries in project open-kilda by telstra.

the class SwitchFlowEntriesBuilder method getSwitchMeterEntries.

/**
 * Construct a list of {@link SwitchMeterEntries} that corresponds to the builder's flow.
 */
public List<SwitchMeterEntries> getSwitchMeterEntries() {
    List<SwitchMeterEntries> switchMeterEntries = new ArrayList<>();
    FlowPath forwardPath = flow.getForwardPath();
    switchMeterEntries.add(SwitchMeterEntries.builder().switchId(flow.getSrcSwitchId()).meterEntries(Collections.singletonList(MeterEntry.builder().meterId(forwardPath.getMeterId().getValue()).rate(forwardPath.getBandwidth()).burstSize(Meter.calculateBurstSize(forwardPath.getBandwidth(), MIN_BURST_SIZE_IN_KBITS, BURST_COEFFICIENT, "")).flags(Meter.getMeterKbpsFlags()).build())).build());
    FlowPath reversePath = flow.getReversePath();
    switchMeterEntries.add(SwitchMeterEntries.builder().switchId(flow.getDestSwitchId()).meterEntries(Collections.singletonList(MeterEntry.builder().meterId(reversePath.getMeterId().getValue()).rate(reversePath.getBandwidth()).burstSize(Meter.calculateBurstSize(reversePath.getBandwidth(), MIN_BURST_SIZE_IN_KBITS, BURST_COEFFICIENT, "")).flags(Meter.getMeterKbpsFlags()).build())).build());
    FlowPath protectedForwardPath = flow.getProtectedForwardPath();
    if (protectedForwardPath != null) {
        switchMeterEntries.add(SwitchMeterEntries.builder().switchId(flow.getSrcSwitchId()).meterEntries(Collections.singletonList(MeterEntry.builder().meterId(protectedForwardPath.getMeterId().getValue()).rate(protectedForwardPath.getBandwidth()).burstSize(Meter.calculateBurstSize(protectedForwardPath.getBandwidth(), MIN_BURST_SIZE_IN_KBITS, BURST_COEFFICIENT, "")).flags(Meter.getMeterKbpsFlags()).build())).build());
    }
    FlowPath protectedReversePath = flow.getProtectedReversePath();
    if (protectedReversePath != null) {
        switchMeterEntries.add(SwitchMeterEntries.builder().switchId(flow.getDestSwitchId()).meterEntries(Collections.singletonList(MeterEntry.builder().meterId(protectedReversePath.getMeterId().getValue()).rate(protectedReversePath.getBandwidth()).burstSize(Meter.calculateBurstSize(protectedReversePath.getBandwidth(), MIN_BURST_SIZE_IN_KBITS, BURST_COEFFICIENT, "")).flags(Meter.getMeterKbpsFlags()).build())).build());
    }
    return switchMeterEntries;
}
Also used : SwitchMeterEntries(org.openkilda.messaging.info.meter.SwitchMeterEntries) ArrayList(java.util.ArrayList) FlowPath(org.openkilda.model.FlowPath)

Example 4 with SwitchMeterEntries

use of org.openkilda.messaging.info.meter.SwitchMeterEntries in project open-kilda by telstra.

the class FlowValidationHubService method handleAsyncResponse.

/**
 * Handles async response from worker.
 *
 * @param key command identifier.
 */
public void handleAsyncResponse(@NonNull String key, @NonNull MessageData data) throws UnknownKeyException {
    log.debug("Received command response {}", data);
    FlowValidationFsm fsm = fsmRegister.getFsmByKey(key).orElseThrow(() -> new UnknownKeyException(key));
    if (data instanceof SwitchFlowEntries) {
        fsmExecutor.fire(fsm, Event.RULES_RECEIVED, data);
    } else if (data instanceof SwitchMeterEntries) {
        fsmExecutor.fire(fsm, Event.METERS_RECEIVED, data);
    } else if (data instanceof SwitchMeterUnsupported) {
        SwitchMeterUnsupported meterUnsupported = (SwitchMeterUnsupported) data;
        log.info("Key: {}; Meters unsupported for switch '{};", key, meterUnsupported.getSwitchId());
        fsmExecutor.fire(fsm, Event.METERS_RECEIVED, SwitchMeterEntries.builder().switchId(meterUnsupported.getSwitchId()).meterEntries(Collections.emptyList()).build());
    } else if (data instanceof SwitchGroupEntries) {
        fsmExecutor.fire(fsm, Event.GROUPS_RECEIVED, data);
    } else if (data instanceof ErrorData) {
        fsmExecutor.fire(fsm, Event.ERROR, data);
    } else {
        log.warn("Key: {}; Unhandled message {}", key, data);
    }
    removeIfFinished(fsm, key);
}
Also used : SwitchFlowEntries(org.openkilda.messaging.info.rule.SwitchFlowEntries) SwitchMeterUnsupported(org.openkilda.messaging.info.meter.SwitchMeterUnsupported) SwitchMeterEntries(org.openkilda.messaging.info.meter.SwitchMeterEntries) SwitchGroupEntries(org.openkilda.messaging.info.rule.SwitchGroupEntries) FlowValidationFsm(org.openkilda.wfm.topology.flowhs.fsm.validation.FlowValidationFsm) ErrorData(org.openkilda.messaging.error.ErrorData) UnknownKeyException(org.openkilda.wfm.topology.flowhs.exception.UnknownKeyException)

Example 5 with SwitchMeterEntries

use of org.openkilda.messaging.info.meter.SwitchMeterEntries in project open-kilda by telstra.

the class FlowValidationHubServiceTest method testMainPath.

@Test
public void testMainPath() throws DuplicateKeyException, UnknownKeyException {
    FlowValidationHubCarrier carrier = new FlowValidationHubCarrier() {

        @Override
        public void sendSpeakerRequest(String flowId, CommandData commandData) {
            assertTrue(commandData instanceof DumpRulesForFlowHsRequest || commandData instanceof DumpMetersForFlowHsRequest || commandData instanceof DumpGroupsForFlowHsRequest);
            List<SwitchId> switchIds = Lists.newArrayList(TEST_SWITCH_ID_A, TEST_SWITCH_ID_B, TEST_SWITCH_ID_C, TEST_SWITCH_ID_E);
            if (commandData instanceof DumpRulesForFlowHsRequest) {
                assertTrue(switchIds.contains(((DumpRulesForFlowHsRequest) commandData).getSwitchId()));
            } else if (commandData instanceof DumpMetersForFlowHsRequest) {
                assertTrue(switchIds.contains(((DumpMetersForFlowHsRequest) commandData).getSwitchId()));
            } else {
                assertTrue(switchIds.contains(((DumpGroupsForFlowHsRequest) commandData).getSwitchId()));
            }
        }

        @Override
        public void sendNorthboundResponse(List<? extends InfoData> message) {
            assertEquals(4, message.size());
            try {
                assertEquals(flowValidationService.validateFlow(TEST_FLOW_ID_A, getSwitchFlowEntriesWithTransitVlan(), getSwitchMeterEntries(), getSwitchGroupEntries()), message);
            } catch (FlowNotFoundException | SwitchNotFoundException e) {
            // tested in the FlowValidationServiceTest
            }
        }

        @Override
        public void sendNorthboundResponse(Message message) {
            fail();
        }

        @Override
        public void cancelTimeoutCallback(String key) {
            assertEquals(TEST_KEY, key);
        }

        @Override
        public void sendInactive() {
        }
    };
    flowValidationHubService = new FlowValidationHubService(carrier, persistenceManager, flowResourcesManager, MIN_BURST_SIZE_IN_KBITS, BURST_COEFFICIENT);
    buildTransitVlanFlow("");
    flowValidationHubService.handleFlowValidationRequest(TEST_KEY, new CommandContext(), new FlowValidationRequest(TEST_FLOW_ID_A));
    for (SwitchFlowEntries switchFlowEntries : getSwitchFlowEntriesWithTransitVlan()) {
        flowValidationHubService.handleAsyncResponse(TEST_KEY, switchFlowEntries);
    }
    for (SwitchMeterEntries switchMeterEntries : getSwitchMeterEntries()) {
        flowValidationHubService.handleAsyncResponse(TEST_KEY, switchMeterEntries);
    }
    for (SwitchGroupEntries switchGroupEntries : getSwitchGroupEntries()) {
        flowValidationHubService.handleAsyncResponse(TEST_KEY, switchGroupEntries);
    }
}
Also used : DumpGroupsForFlowHsRequest(org.openkilda.messaging.command.switches.DumpGroupsForFlowHsRequest) FlowNotFoundException(org.openkilda.wfm.error.FlowNotFoundException) SwitchFlowEntries(org.openkilda.messaging.info.rule.SwitchFlowEntries) Message(org.openkilda.messaging.Message) ErrorMessage(org.openkilda.messaging.error.ErrorMessage) CommandContext(org.openkilda.wfm.CommandContext) SwitchMeterEntries(org.openkilda.messaging.info.meter.SwitchMeterEntries) SwitchId(org.openkilda.model.SwitchId) SwitchNotFoundException(org.openkilda.wfm.error.SwitchNotFoundException) DumpMetersForFlowHsRequest(org.openkilda.messaging.command.switches.DumpMetersForFlowHsRequest) FlowValidationRequest(org.openkilda.messaging.command.flow.FlowValidationRequest) InfoData(org.openkilda.messaging.info.InfoData) DumpRulesForFlowHsRequest(org.openkilda.messaging.command.switches.DumpRulesForFlowHsRequest) SwitchGroupEntries(org.openkilda.messaging.info.rule.SwitchGroupEntries) List(java.util.List) CommandData(org.openkilda.messaging.command.CommandData) Test(org.junit.Test)

Aggregations

SwitchMeterEntries (org.openkilda.messaging.info.meter.SwitchMeterEntries)13 SwitchFlowEntries (org.openkilda.messaging.info.rule.SwitchFlowEntries)6 SwitchId (org.openkilda.model.SwitchId)5 List (java.util.List)4 SwitchMeterUnsupported (org.openkilda.messaging.info.meter.SwitchMeterUnsupported)4 SwitchGroupEntries (org.openkilda.messaging.info.rule.SwitchGroupEntries)4 ArrayList (java.util.ArrayList)3 ErrorData (org.openkilda.messaging.error.ErrorData)3 ErrorMessage (org.openkilda.messaging.error.ErrorMessage)3 InfoData (org.openkilda.messaging.info.InfoData)3 FlowNotFoundException (org.openkilda.wfm.error.FlowNotFoundException)3 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 SwitchNotFoundException (org.openkilda.floodlight.error.SwitchNotFoundException)2 SwitchOperationException (org.openkilda.floodlight.error.SwitchOperationException)2 UnsupportedSwitchOperationException (org.openkilda.floodlight.error.UnsupportedSwitchOperationException)2 ISwitchManager (org.openkilda.floodlight.switchmanager.ISwitchManager)2 Message (org.openkilda.messaging.Message)2 CommandData (org.openkilda.messaging.command.CommandData)2 FlowValidationRequest (org.openkilda.messaging.command.flow.FlowValidationRequest)2