Search in sources :

Example 21 with YSubFlow

use of org.openkilda.model.YSubFlow in project open-kilda by telstra.

the class OnSubFlowAllocatedAction method performWithResponse.

@Override
protected Optional<Message> performWithResponse(State from, State to, Event event, YFlowCreateContext context, YFlowCreateFsm stateMachine) {
    String subFlowId = context.getSubFlowId();
    if (!stateMachine.isCreatingSubFlow(subFlowId)) {
        throw new IllegalStateException("Received an event for non-pending sub-flow " + subFlowId);
    }
    String yFlowId = stateMachine.getYFlowId();
    stateMachine.saveActionToHistory("Creating a sub-flow", format("Allocated resources for sub-flow %s of y-flow %s", subFlowId, yFlowId));
    stateMachine.addAllocatedSubFlow(subFlowId);
    SubFlowDto subFlowDto = stateMachine.getTargetFlow().getSubFlows().stream().filter(f -> f.getFlowId().equals(subFlowId)).findAny().orElseThrow(() -> new FlowProcessingException(ErrorType.INTERNAL_ERROR, format("Can't find definition of created sub-flow %s", subFlowId)));
    SubFlowSharedEndpointEncapsulation sharedEndpoint = subFlowDto.getSharedEndpoint();
    FlowEndpoint endpoint = subFlowDto.getEndpoint();
    log.debug("Start creating sub-flow references from {} to y-flow {}", subFlowId, stateMachine.getYFlowId());
    YFlow result = transactionManager.doInTransaction(() -> {
        YFlow yFlow = yFlowRepository.findById(yFlowId).orElseThrow(() -> new FlowProcessingException(ErrorType.INTERNAL_ERROR, format("Y-flow %s not found", yFlowId)));
        Flow flow = flowRepository.findById(subFlowId).orElseThrow(() -> new FlowProcessingException(ErrorType.INTERNAL_ERROR, format("Flow %s not found", subFlowId)));
        YSubFlow subFlow = YSubFlow.builder().yFlow(yFlow).flow(flow).sharedEndpointVlan(sharedEndpoint.getVlanId()).sharedEndpointInnerVlan(sharedEndpoint.getInnerVlanId()).endpointSwitchId(endpoint.getSwitchId()).endpointPort(endpoint.getPortNumber()).endpointVlan(endpoint.getOuterVlanId()).endpointInnerVlan(endpoint.getInnerVlanId()).build();
        yFlow.addSubFlow(subFlow);
        return yFlow;
    });
    if (subFlowId.equals(stateMachine.getMainAffinityFlowId())) {
        stateMachine.getRequestedFlows().forEach(requestedFlow -> {
            String requestedFlowId = requestedFlow.getFlowId();
            if (!requestedFlowId.equals(subFlowId)) {
                stateMachine.addSubFlow(requestedFlowId);
                stateMachine.addCreatingSubFlow(requestedFlowId);
                stateMachine.notifyEventListeners(listener -> listener.onSubFlowProcessingStart(yFlowId, requestedFlowId));
                CommandContext flowContext = stateMachine.getCommandContext().fork(requestedFlowId);
                requestedFlow.setAffinityFlowId(stateMachine.getMainAffinityFlowId());
                flowCreateService.startFlowCreation(flowContext, requestedFlow, yFlowId);
            }
        });
    }
    if (stateMachine.getAllocatedSubFlows().size() == stateMachine.getSubFlows().size()) {
        return Optional.of(buildResponseMessage(result, stateMachine.getCommandContext()));
    } else {
        return Optional.empty();
    }
}
Also used : YFlow(org.openkilda.model.YFlow) FlowEndpoint(org.openkilda.model.FlowEndpoint) CommandContext(org.openkilda.wfm.CommandContext) FlowProcessingException(org.openkilda.wfm.topology.flowhs.exception.FlowProcessingException) SubFlowSharedEndpointEncapsulation(org.openkilda.messaging.command.yflow.SubFlowSharedEndpointEncapsulation) SubFlowDto(org.openkilda.messaging.command.yflow.SubFlowDto) YSubFlow(org.openkilda.model.YSubFlow) Flow(org.openkilda.model.Flow) YFlow(org.openkilda.model.YFlow) YSubFlow(org.openkilda.model.YSubFlow)

Example 22 with YSubFlow

use of org.openkilda.model.YSubFlow in project open-kilda by telstra.

the class OnSubFlowAllocatedAction method convertToYFlowDto.

private YFlowDto convertToYFlowDto(YFlow yFlow) {
    Flow mainAffinityFlow = yFlow.getSubFlows().stream().map(YSubFlow::getFlow).filter(flow -> flow.getFlowId().equals(flow.getAffinityGroupId())).findFirst().orElseThrow(() -> new FlowProcessingException(ErrorType.INTERNAL_ERROR, "Main affinity flow not found"));
    Collection<Flow> diverseWithFlow = getDiverseWithFlow(mainAffinityFlow);
    Set<String> diverseFlows = diverseWithFlow.stream().filter(flow -> flow.getYFlowId() == null).map(Flow::getFlowId).collect(Collectors.toSet());
    Set<String> diverseYFlows = diverseWithFlow.stream().map(Flow::getYFlowId).filter(Objects::nonNull).collect(Collectors.toSet());
    return YFlowMapper.INSTANCE.toYFlowDto(yFlow, diverseFlows, diverseYFlows);
}
Also used : FlowProcessingException(org.openkilda.wfm.topology.flowhs.exception.FlowProcessingException) Flow(org.openkilda.model.Flow) YFlow(org.openkilda.model.YFlow) YSubFlow(org.openkilda.model.YSubFlow)

Example 23 with YSubFlow

use of org.openkilda.model.YSubFlow in project open-kilda by telstra.

the class RerouteServiceTest method setup.

@Before
public void setup() throws Throwable {
    doAnswer(invocation -> {
        TransactionCallbackWithoutResult<?> arg = invocation.getArgument(0);
        arg.doInTransaction();
        return null;
    }).when(transactionManager).doInTransaction(Mockito.<TransactionCallbackWithoutResult<?>>any());
    doAnswer(invocation -> {
        TransactionCallback<?, ?> arg = invocation.getArgument(0);
        return arg.doInTransaction();
    }).when(transactionManager).doInTransaction(Mockito.<TransactionCallback<?, ?>>any());
    pinnedFlow = Flow.builder().flowId(FLOW_ID).srcSwitch(SWITCH_A).destSwitch(SWITCH_C).pinned(true).build();
    FlowPath pinnedFlowForwardPath = FlowPath.builder().pathId(new PathId("1")).srcSwitch(SWITCH_A).destSwitch(SWITCH_C).cookie(new FlowSegmentCookie(FlowPathDirection.FORWARD, 1)).build();
    List<PathSegment> pinnedFlowForwardSegments = new ArrayList<>();
    pinnedFlowForwardSegments.add(PathSegment.builder().pathId(pinnedFlowForwardPath.getPathId()).srcSwitch(SWITCH_A).srcPort(1).destSwitch(SWITCH_B).destPort(1).build());
    pinnedFlowForwardSegments.add(PathSegment.builder().pathId(pinnedFlowForwardPath.getPathId()).srcSwitch(SWITCH_B).srcPort(2).destSwitch(SWITCH_C).destPort(1).build());
    pinnedFlowForwardPath.setSegments(pinnedFlowForwardSegments);
    FlowPath pinnedFlowReversePath = FlowPath.builder().pathId(new PathId("2")).srcSwitch(SWITCH_C).destSwitch(SWITCH_A).cookie(new FlowSegmentCookie(FlowPathDirection.REVERSE, 2)).build();
    List<PathSegment> pinnedFlowReverseSegments = new ArrayList<>();
    pinnedFlowReverseSegments.add(PathSegment.builder().pathId(pinnedFlowReversePath.getPathId()).srcSwitch(SWITCH_C).srcPort(1).destSwitch(SWITCH_B).destPort(2).build());
    pinnedFlowReverseSegments.add(PathSegment.builder().pathId(pinnedFlowReversePath.getPathId()).srcSwitch(SWITCH_B).srcPort(1).destSwitch(SWITCH_A).destPort(1).build());
    pinnedFlowReversePath.setSegments(pinnedFlowReverseSegments);
    pinnedFlow.setForwardPath(pinnedFlowForwardPath);
    pinnedFlow.setReversePath(pinnedFlowReversePath);
    regularFlow = Flow.builder().flowId(FLOW_ID).srcSwitch(SWITCH_A).destSwitch(SWITCH_C).pinned(false).priority(2).build();
    FlowPath regularFlowForwardPath = FlowPath.builder().pathId(new PathId("3")).srcSwitch(SWITCH_A).destSwitch(SWITCH_C).cookie(new FlowSegmentCookie(FlowPathDirection.FORWARD, 3)).status(FlowPathStatus.ACTIVE).build();
    List<PathSegment> unpinnedFlowForwardSegments = new ArrayList<>();
    unpinnedFlowForwardSegments.add(PathSegment.builder().pathId(regularFlowForwardPath.getPathId()).srcSwitch(SWITCH_A).srcPort(1).destSwitch(SWITCH_B).destPort(1).build());
    unpinnedFlowForwardSegments.add(PathSegment.builder().pathId(regularFlowForwardPath.getPathId()).srcSwitch(SWITCH_B).srcPort(2).destSwitch(SWITCH_C).destPort(1).build());
    regularFlowForwardPath.setSegments(unpinnedFlowForwardSegments);
    FlowPath regularFlowReversePath = FlowPath.builder().pathId(new PathId("4")).srcSwitch(SWITCH_C).destSwitch(SWITCH_A).cookie(new FlowSegmentCookie(FlowPathDirection.REVERSE, 3)).status(FlowPathStatus.ACTIVE).build();
    List<PathSegment> unpinnedFlowReverseSegments = new ArrayList<>();
    unpinnedFlowReverseSegments.add(PathSegment.builder().pathId(regularFlowReversePath.getPathId()).srcSwitch(SWITCH_C).srcPort(1).destSwitch(SWITCH_B).destPort(2).build());
    unpinnedFlowReverseSegments.add(PathSegment.builder().pathId(regularFlowReversePath.getPathId()).srcSwitch(SWITCH_B).srcPort(1).destSwitch(SWITCH_A).destPort(1).build());
    regularFlowReversePath.setSegments(unpinnedFlowReverseSegments);
    regularFlow.setForwardPath(regularFlowForwardPath);
    regularFlow.setReversePath(regularFlowReversePath);
    oneSwitchFlow = Flow.builder().flowId(FLOW_ID).srcSwitch(SWITCH_A).destSwitch(SWITCH_A).build();
    FlowPath oneSwitchFlowForwardPath = FlowPath.builder().pathId(new PathId("5")).srcSwitch(SWITCH_A).destSwitch(SWITCH_A).cookie(new FlowSegmentCookie(FlowPathDirection.FORWARD, 4)).status(FlowPathStatus.ACTIVE).build();
    FlowPath oneSwitchFlowReversePath = FlowPath.builder().pathId(new PathId("6")).srcSwitch(SWITCH_A).destSwitch(SWITCH_A).cookie(new FlowSegmentCookie(FlowPathDirection.REVERSE, 4)).status(FlowPathStatus.ACTIVE).build();
    oneSwitchFlow.setForwardPath(oneSwitchFlowForwardPath);
    oneSwitchFlow.setReversePath(oneSwitchFlowReversePath);
    regularYFlow = YFlow.builder().yFlowId(YFLOW_ID).priority(2).sharedEndpoint(new SharedEndpoint(SWITCH_A.getSwitchId(), 10)).build();
    FlowPath regularYFlowForwardPath = FlowPath.builder().pathId(new PathId("3")).srcSwitch(SWITCH_A).destSwitch(SWITCH_C).cookie(new FlowSegmentCookie(FlowPathDirection.FORWARD, 3)).status(FlowPathStatus.ACTIVE).build();
    regularYFlowForwardPath.setSegments(unpinnedFlowForwardSegments);
    FlowPath regularYFlowReversePath = FlowPath.builder().pathId(new PathId("4")).srcSwitch(SWITCH_C).destSwitch(SWITCH_A).cookie(new FlowSegmentCookie(FlowPathDirection.REVERSE, 3)).status(FlowPathStatus.ACTIVE).build();
    regularYFlowReversePath.setSegments(unpinnedFlowReverseSegments);
    subFlow = Flow.builder().flowId(SUB_YFLOW_ID).srcSwitch(SWITCH_A).destSwitch(SWITCH_C).pinned(false).priority(2).yFlowId(YFLOW_ID).yFlow(regularYFlow).build();
    subFlow.setForwardPath(regularYFlowForwardPath);
    subFlow.setReversePath(regularYFlowReversePath);
    Set<YSubFlow> subFlows = Collections.singleton(YSubFlow.builder().yFlow(regularYFlow).flow(subFlow).build());
    regularYFlow.setSubFlows(subFlows);
}
Also used : PathId(org.openkilda.model.PathId) FlowSegmentCookie(org.openkilda.model.cookie.FlowSegmentCookie) ArrayList(java.util.ArrayList) SharedEndpoint(org.openkilda.model.YFlow.SharedEndpoint) PathSegment(org.openkilda.model.PathSegment) FlowPath(org.openkilda.model.FlowPath) YSubFlow(org.openkilda.model.YSubFlow) Before(org.junit.Before)

Example 24 with YSubFlow

use of org.openkilda.model.YSubFlow in project open-kilda by telstra.

the class YFlowValidationHubServiceTest method shouldValidateAndFailIfSubFlowHasMissingRule.

@Test
public void shouldValidateAndFailIfSubFlowHasMissingRule() throws DuplicateKeyException {
    // given
    String yFlowId = "test_y_flow_1";
    YFlow yFlow = createYFlowViaTransit(yFlowId);
    YSubFlow failedSubFlow = yFlow.getSubFlows().stream().findFirst().orElseThrow(IllegalStateException::new);
    Flow failedFlow = failedSubFlow.getFlow();
    YFlowSwitchFlowEntriesBuilder flowEntriesBuilder = new YFlowSwitchFlowEntriesBuilder(yFlow, persistenceManager.getRepositoryFactory().createTransitVlanRepository(), persistenceManager.getRepositoryFactory().createVxlanRepository());
    Map<SwitchId, Collection<FlowEntry>> flowEntries = flowEntriesBuilder.getFlowEntries();
    flowEntries.forEach((s, f) -> f.removeIf(entry -> entry.getCookie() == failedFlow.getForwardPath().getCookie().getValue()));
    Map<SwitchId, Collection<MeterEntry>> meterEntries = flowEntriesBuilder.getMeterEntries();
    Map<SwitchId, Collection<GroupEntry>> groupEntries = flowEntriesBuilder.getGroupEntries();
    YFlowValidationHubService service = makeYFlowValidationHubService();
    service.handleRequest(yFlow.getYFlowId(), new CommandContext(), yFlow.getYFlowId());
    // when
    handleSpeakerRequests(service, yFlowId, flowEntries, meterEntries, groupEntries);
    // then
    YFlowValidationResponse response = getNorthboundResponse(yFlowValidationHubCarrier);
    assertFalse(response.isAsExpected());
    assertFalse(response.getYFlowValidationResult().isAsExpected());
    response.getSubFlowValidationResults().forEach(result -> assertTrue(result.getFlowId().equals(failedFlow.getFlowId()) || result.getAsExpected()));
    assertEquals(1, response.getSubFlowValidationResults().stream().filter(r -> !r.getAsExpected()).count());
}
Also used : YFlow(org.openkilda.model.YFlow) InfoMessage(org.openkilda.messaging.info.InfoMessage) Flow(org.openkilda.model.Flow) Pair(org.apache.commons.lang3.tuple.Pair) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) FlowValidationHubCarrier(org.openkilda.wfm.topology.flowhs.service.FlowValidationHubCarrier) Assert.fail(org.junit.Assert.fail) SwitchFlowEntries(org.openkilda.messaging.info.rule.SwitchFlowEntries) YFlowValidationResponse(org.openkilda.messaging.command.yflow.YFlowValidationResponse) SwitchGroupEntries(org.openkilda.messaging.info.rule.SwitchGroupEntries) YFlowSwitchFlowEntriesBuilder(org.openkilda.wfm.topology.flowhs.fsm.yflow.validation.YFlowSwitchFlowEntriesBuilder) YSubFlow(org.openkilda.model.YSubFlow) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) InfoData(org.openkilda.messaging.info.InfoData) CommandContext(org.openkilda.wfm.CommandContext) GroupEntry(org.openkilda.messaging.info.rule.GroupEntry) SwitchMeterEntries(org.openkilda.messaging.info.meter.SwitchMeterEntries) BURST_COEFFICIENT(org.openkilda.wfm.topology.flowhs.fsm.validation.SwitchFlowEntriesBuilder.BURST_COEFFICIENT) Assert.assertFalse(org.junit.Assert.assertFalse) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CommandData(org.openkilda.messaging.command.CommandData) Message(org.openkilda.messaging.Message) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) MeterEntry(org.openkilda.messaging.info.meter.MeterEntry) ArrayList(java.util.ArrayList) DumpMetersForFlowHsRequest(org.openkilda.messaging.command.switches.DumpMetersForFlowHsRequest) ArgumentCaptor(org.mockito.ArgumentCaptor) YFlow(org.openkilda.model.YFlow) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) DumpGroupsForFlowHsRequest(org.openkilda.messaging.command.switches.DumpGroupsForFlowHsRequest) DuplicateKeyException(org.openkilda.wfm.topology.flowhs.exception.DuplicateKeyException) Assert.assertNotNull(org.junit.Assert.assertNotNull) ErrorType(org.openkilda.messaging.error.ErrorType) FlowEntry(org.openkilda.messaging.info.rule.FlowEntry) Assert.assertTrue(org.junit.Assert.assertTrue) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.verify(org.mockito.Mockito.verify) FlowValidationHubService(org.openkilda.wfm.topology.flowhs.service.FlowValidationHubService) DumpRulesForFlowHsRequest(org.openkilda.messaging.command.switches.DumpRulesForFlowHsRequest) SwitchId(org.openkilda.model.SwitchId) MIN_BURST_SIZE_IN_KBITS(org.openkilda.wfm.topology.flowhs.fsm.validation.SwitchFlowEntriesBuilder.MIN_BURST_SIZE_IN_KBITS) YFlowValidationService(org.openkilda.wfm.topology.flowhs.fsm.yflow.validation.YFlowValidationService) AbstractYFlowTest(org.openkilda.wfm.topology.flowhs.service.AbstractYFlowTest) Assert.assertEquals(org.junit.Assert.assertEquals) YFlowValidationResponse(org.openkilda.messaging.command.yflow.YFlowValidationResponse) CommandContext(org.openkilda.wfm.CommandContext) SwitchId(org.openkilda.model.SwitchId) YSubFlow(org.openkilda.model.YSubFlow) Flow(org.openkilda.model.Flow) YSubFlow(org.openkilda.model.YSubFlow) YFlow(org.openkilda.model.YFlow) Collection(java.util.Collection) YFlowSwitchFlowEntriesBuilder(org.openkilda.wfm.topology.flowhs.fsm.yflow.validation.YFlowSwitchFlowEntriesBuilder) Test(org.junit.Test) AbstractYFlowTest(org.openkilda.wfm.topology.flowhs.service.AbstractYFlowTest)

Example 25 with YSubFlow

use of org.openkilda.model.YSubFlow in project open-kilda by telstra.

the class YFlowUpdateServiceTest method verifyYFlowStatus.

protected YFlow verifyYFlowStatus(String yFlowId, FlowStatus expectedStatus, FlowStatus expectedFirstSubFlowStatus, FlowStatus expectedSecondSubFlowStatus) {
    YFlow flow = getYFlow(yFlowId);
    assertEquals(expectedStatus, flow.getStatus());
    Set<FlowStatus> expectedSubFlowStatuses = Stream.of(expectedFirstSubFlowStatus, expectedSecondSubFlowStatus).collect(Collectors.toSet());
    Set<FlowStatus> actualSubFlowStatuses = flow.getSubFlows().stream().map(YSubFlow::getFlow).map(Flow::getStatus).collect(Collectors.toSet());
    assertEquals(expectedSubFlowStatuses, actualSubFlowStatuses);
    return flow;
}
Also used : YFlow(org.openkilda.model.YFlow) FlowStatus(org.openkilda.model.FlowStatus) YSubFlow(org.openkilda.model.YSubFlow)

Aggregations

YSubFlow (org.openkilda.model.YSubFlow)26 YFlow (org.openkilda.model.YFlow)24 Flow (org.openkilda.model.Flow)18 FlowProcessingException (org.openkilda.wfm.topology.flowhs.exception.FlowProcessingException)11 SwitchId (org.openkilda.model.SwitchId)7 ArrayList (java.util.ArrayList)6 FlowEndpoint (org.openkilda.model.FlowEndpoint)6 FlowPath (org.openkilda.model.FlowPath)5 Collection (java.util.Collection)4 HashSet (java.util.HashSet)4 List (java.util.List)4 ErrorType (org.openkilda.messaging.error.ErrorType)4 PathSegment (org.openkilda.model.PathSegment)4 PersistenceManager (org.openkilda.persistence.PersistenceManager)4 CommandContext (org.openkilda.wfm.CommandContext)4 Collectors (java.util.stream.Collectors)3 Slf4j (lombok.extern.slf4j.Slf4j)3 SubFlowDto (org.openkilda.messaging.command.yflow.SubFlowDto)3 SubFlowSharedEndpointEncapsulation (org.openkilda.messaging.command.yflow.SubFlowSharedEndpointEncapsulation)3 String.format (java.lang.String.format)2