Search in sources :

Example 41 with AvailableNetwork

use of org.openkilda.pce.impl.AvailableNetwork in project open-kilda by telstra.

the class AvailableNetworkFactoryTest method shouldBuildAvailableNetworkUsingCostStrategy.

@Test
public void shouldBuildAvailableNetworkUsingCostStrategy() throws RecoverableException {
    Flow flow = getFlow(false);
    IslImmutableView isl = getIslView(flow);
    when(config.getNetworkStrategy()).thenReturn("COST");
    when(islRepository.findActiveByBandwidthAndEncapsulationType(flow.getBandwidth(), flow.getEncapsulationType())).thenReturn(Collections.singletonList(isl));
    AvailableNetwork availableNetwork = availableNetworkFactory.getAvailableNetwork(flow, Collections.emptyList());
    assertAvailableNetworkIsCorrect(isl, availableNetwork);
}
Also used : IslImmutableView(org.openkilda.persistence.repositories.IslRepository.IslImmutableView) AvailableNetwork(org.openkilda.pce.impl.AvailableNetwork) Flow(org.openkilda.model.Flow) Test(org.junit.Test)

Example 42 with AvailableNetwork

use of org.openkilda.pce.impl.AvailableNetwork in project open-kilda by telstra.

the class BestWeightAndShortestPathFinderTest method shouldChooseCheaperOverTooDeepForReverseOrderMaxWeightStrategy.

@Test
public void shouldChooseCheaperOverTooDeepForReverseOrderMaxWeightStrategy() throws UnroutableFlowException {
    AvailableNetwork network = buildLongAndExpensivePathsNetwork();
    BestWeightAndShortestPathFinder pathFinder = new BestWeightAndShortestPathFinder(2);
    Pair<List<Edge>, List<Edge>> pairPath = pathFinder.findPathWithWeightCloseToMaxWeight(network, SWITCH_ID_3, SWITCH_ID_1, WEIGHT_FUNCTION, Long.MAX_VALUE, Long.MAX_VALUE).getFoundPath();
    List<Edge> fpath = pairPath.getLeft();
    assertThat(fpath, Matchers.hasSize(2));
    assertEquals(SWITCH_ID_2, fpath.get(1).getSrcSwitch().getSwitchId());
    List<Edge> rpath = pairPath.getRight();
    assertThat(rpath, Matchers.hasSize(2));
    assertEquals(SWITCH_ID_2, rpath.get(0).getDestSwitch().getSwitchId());
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) AvailableNetwork(org.openkilda.pce.impl.AvailableNetwork) Edge(org.openkilda.pce.model.Edge) Test(org.junit.Test)

Example 43 with AvailableNetwork

use of org.openkilda.pce.impl.AvailableNetwork in project open-kilda by telstra.

the class AvailableNetworkFactory method getAvailableNetwork.

/**
 * Gets a {@link AvailableNetwork}.
 *
 * @param flow                      the flow, for which {@link AvailableNetwork} is constructing.
 * @param reusePathsResources       reuse resources already allocated by {@param reusePathsResources} paths.
 * @return {@link AvailableNetwork} instance.
 */
public AvailableNetwork getAvailableNetwork(Flow flow, Collection<PathId> reusePathsResources) throws RecoverableException {
    BuildStrategy buildStrategy = BuildStrategy.from(config.getNetworkStrategy());
    AvailableNetwork network = new AvailableNetwork();
    try {
        // Reads all active links from the database and creates representation of the network.
        getAvailableIsls(buildStrategy, flow).forEach(link -> addIslAsEdge(link, network));
        if (!reusePathsResources.isEmpty() && !flow.isIgnoreBandwidth()) {
            reusePathsResources.stream().filter(pathId -> flowPathRepository.findById(pathId).map(path -> !path.isIgnoreBandwidth()).orElse(false)).forEach(pathId -> {
                // ISLs occupied by the flow (take the bandwidth already occupied by the flow into account).
                islRepository.findActiveByPathAndBandwidthAndEncapsulationType(pathId, flow.getBandwidth(), flow.getEncapsulationType()).forEach(link -> addIslAsEdge(link, network));
            });
        }
    } catch (PersistenceException e) {
        throw new RecoverableException("An error from the database", e);
    }
    if (flow.getDiverseGroupId() != null) {
        log.info("Filling AvailableNetwork diverse weights for group with id {}", flow.getDiverseGroupId());
        Collection<PathId> flowPaths = flowPathRepository.findPathIdsByFlowDiverseGroupId(flow.getDiverseGroupId());
        if (!reusePathsResources.isEmpty()) {
            flowPaths = flowPaths.stream().filter(s -> !reusePathsResources.contains(s)).collect(Collectors.toList());
        }
        Collection<PathId> affinityPathIds = flowPathRepository.findPathIdsByFlowAffinityGroupId(flow.getAffinityGroupId());
        flowPaths.forEach(pathId -> flowPathRepository.findById(pathId).filter(flowPath -> !affinityPathIds.contains(flowPath.getPathId()) || flowPath.getFlowId().equals(flow.getFlowId())).ifPresent(flowPath -> {
            network.processDiversitySegments(flowPath.getSegments(), flow);
            network.processDiversitySegmentsWithPop(flowPath.getSegments());
        }));
    }
    // The main flow should not take into account the rest of the flows in this affinity group.
    if (flow.getAffinityGroupId() != null && !flow.getAffinityGroupId().equals(flow.getFlowId())) {
        log.info("Filling AvailableNetwork affinity weights for group with id {}", flow.getAffinityGroupId());
        flowPathRepository.findByFlowId(flow.getAffinityGroupId()).stream().filter(flowPath -> !flowPath.isProtected()).filter(FlowPath::isForward).map(FlowPath::getSegments).forEach(network::processAffinitySegments);
    }
    return network;
}
Also used : IslImmutableView(org.openkilda.persistence.repositories.IslRepository.IslImmutableView) FlowPath(org.openkilda.model.FlowPath) FlowPathRepository(org.openkilda.persistence.repositories.FlowPathRepository) Collection(java.util.Collection) PersistenceException(org.openkilda.persistence.exceptions.PersistenceException) Collectors(java.util.stream.Collectors) RecoverableException(org.openkilda.pce.exception.RecoverableException) ArrayList(java.util.ArrayList) RepositoryFactory(org.openkilda.persistence.repositories.RepositoryFactory) Edge(org.openkilda.pce.model.Edge) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Flow(org.openkilda.model.Flow) Node(org.openkilda.pce.model.Node) IslRepository(org.openkilda.persistence.repositories.IslRepository) PathId(org.openkilda.model.PathId) AvailableNetwork(org.openkilda.pce.impl.AvailableNetwork) PathId(org.openkilda.model.PathId) PersistenceException(org.openkilda.persistence.exceptions.PersistenceException) AvailableNetwork(org.openkilda.pce.impl.AvailableNetwork) FlowPath(org.openkilda.model.FlowPath) RecoverableException(org.openkilda.pce.exception.RecoverableException)

Aggregations

AvailableNetwork (org.openkilda.pce.impl.AvailableNetwork)43 Test (org.junit.Test)28 ArrayList (java.util.ArrayList)24 List (java.util.List)23 Edge (org.openkilda.pce.model.Edge)13 SwitchId (org.openkilda.model.SwitchId)9 Flow (org.openkilda.model.Flow)7 IslImmutableView (org.openkilda.persistence.repositories.IslRepository.IslImmutableView)7 FindPathResult (org.openkilda.pce.model.FindPathResult)6 FlowPath (org.openkilda.model.FlowPath)3 PathId (org.openkilda.model.PathId)3 Collectors (java.util.stream.Collectors)2 Lists (com.google.common.collect.Lists)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Objects (java.util.Objects)1 IntStream (java.util.stream.IntStream)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Pair (org.apache.commons.lang3.tuple.Pair)1 CoreMatchers.equalTo (org.hamcrest.CoreMatchers.equalTo)1