Search in sources :

Example 6 with ImmutablePair

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

the class FlowCache method buildFlow.

/**
 * Builds new forward and reverse flow pair.
 *
 * @param flow  source flow
 * @param path  flow path
 * @param cache resource cache
 * @return new forward and reverse flow pair
 */
public ImmutablePair<Flow, Flow> buildFlow(final Flow flow, ImmutablePair<PathInfoData, PathInfoData> path, ResourceCache cache) {
    String timestamp = Utils.getIsoTimestamp();
    int cookie = cache.allocateCookie();
    Flow forward = new Flow(flow.getFlowId(), flow.getBandwidth(), flow.isIgnoreBandwidth(), cookie | ResourceCache.FORWARD_FLOW_COOKIE_MASK, flow.getDescription(), timestamp, flow.getSourceSwitch(), flow.getDestinationSwitch(), flow.getSourcePort(), flow.getDestinationPort(), flow.getSourceVlan(), flow.getDestinationVlan(), cache.allocateMeterId(flow.getSourceSwitch()), cache.allocateVlanId(), path.getLeft(), FlowState.ALLOCATED);
    Flow reverse = new Flow(flow.getFlowId(), flow.getBandwidth(), flow.isIgnoreBandwidth(), cookie | ResourceCache.REVERSE_FLOW_COOKIE_MASK, flow.getDescription(), timestamp, flow.getDestinationSwitch(), flow.getSourceSwitch(), flow.getDestinationPort(), flow.getSourcePort(), flow.getDestinationVlan(), flow.getSourceVlan(), cache.allocateMeterId(flow.getDestinationSwitch()), cache.allocateVlanId(), path.getRight(), FlowState.ALLOCATED);
    return new ImmutablePair<>(forward, reverse);
}
Also used : ImmutablePair(org.openkilda.messaging.model.ImmutablePair) Flow(org.openkilda.messaging.model.Flow)

Example 7 with ImmutablePair

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

the class NeoDriver method getPath.

/**
 * {@inheritDoc}
 */
@Override
public ImmutablePair<PathInfoData, PathInfoData> getPath(Flow flow, Strategy strategy) throws UnroutablePathException {
    long latency = 0L;
    List<PathNode> forwardNodes = new LinkedList<>();
    List<PathNode> reverseNodes = new LinkedList<>();
    if (!flow.isOneSwitchFlow()) {
        Statement statement = getPathQuery(flow, strategy);
        logger.debug("QUERY: {}", statement.toString());
        try (Session session = driver.session()) {
            StatementResult result = session.run(statement);
            try {
                Record record = result.next();
                LinkedList<Relationship> isls = new LinkedList<>();
                record.get(0).asPath().relationships().forEach(isls::add);
                int seqId = 0;
                for (Relationship isl : isls) {
                    latency += isl.get("latency").asLong();
                    forwardNodes.add(new PathNode(isl.get("src_switch").asString(), isl.get("src_port").asInt(), seqId, isl.get("latency").asLong()));
                    seqId++;
                    forwardNodes.add(new PathNode(isl.get("dst_switch").asString(), isl.get("dst_port").asInt(), seqId, 0L));
                    seqId++;
                }
                seqId = 0;
                Collections.reverse(isls);
                for (Relationship isl : isls) {
                    reverseNodes.add(new PathNode(isl.get("dst_switch").asString(), isl.get("dst_port").asInt(), seqId, isl.get("latency").asLong()));
                    seqId++;
                    reverseNodes.add(new PathNode(isl.get("src_switch").asString(), isl.get("src_port").asInt(), seqId, 0L));
                    seqId++;
                }
            } catch (NoSuchRecordException e) {
                throw new UnroutablePathException(flow);
            }
        }
    } else {
        logger.info("No path computation for one-switch flow");
    }
    return new ImmutablePair<>(new PathInfoData(latency, forwardNodes), new PathInfoData(latency, reverseNodes));
}
Also used : StatementResult(org.neo4j.driver.v1.StatementResult) Statement(org.neo4j.driver.v1.Statement) PathNode(org.openkilda.messaging.info.event.PathNode) PathInfoData(org.openkilda.messaging.info.event.PathInfoData) ImmutablePair(org.openkilda.messaging.model.ImmutablePair) Relationship(org.neo4j.driver.v1.types.Relationship) Record(org.neo4j.driver.v1.Record) NoSuchRecordException(org.neo4j.driver.v1.exceptions.NoSuchRecordException) Session(org.neo4j.driver.v1.Session)

Example 8 with ImmutablePair

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

the class TopologyHelp method GetFlow.

public static ImmutablePair<Flow, Flow> GetFlow(String flowId) {
    System.out.println("\n==> Topology-Engine Get Flow");
    Client client = ClientBuilder.newClient(new ClientConfig());
    Response response = client.target(topologyEndpoint).path("/api/v1/topology/flows/").path(flowId).request(MediaType.APPLICATION_JSON).get();
    int status = response.getStatus();
    if (status != 200) {
        System.out.println(String.format("====> Error: Topology-Engine Get Flow = %s", response.readEntity(MessageError.class)));
        return null;
    }
    ImmutablePair<Flow, Flow> result = response.readEntity(new GenericType<ImmutablePair<Flow, Flow>>() {
    });
    System.out.println(String.format("====> Topology-Engine Get Flow = %s", result));
    return result;
}
Also used : Response(javax.ws.rs.core.Response) ImmutablePair(org.openkilda.messaging.model.ImmutablePair) Client(javax.ws.rs.client.Client) ClientConfig(org.glassfish.jersey.client.ClientConfig) DefaultParameters.topologyEndpoint(org.openkilda.DefaultParameters.topologyEndpoint) DefaultParameters.mininetEndpoint(org.openkilda.DefaultParameters.mininetEndpoint) Flow(org.openkilda.messaging.model.Flow)

Example 9 with ImmutablePair

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

the class PathComputerMock method getPath.

@Override
public ImmutablePair<PathInfoData, PathInfoData> getPath(Flow flow, Strategy strategy) {
    /*
         * TODO: Implement other strategies? Default is HOPS ...
         * TODO: Is PathComputerMock necessary, since we can embed Neo4J?
         */
    SwitchInfoData source = network.nodes().stream().filter(sw -> sw.getSwitchId().equals(flow.getSourceSwitch())).findFirst().orElse(null);
    if (source == null) {
        throw new CacheException(ErrorType.NOT_FOUND, "Can not find path", String.format("Error: No node found source=%s", flow.getSourceSwitch()));
    }
    SwitchInfoData destination = network.nodes().stream().filter(sw -> sw.getSwitchId().equals(flow.getDestinationSwitch())).findFirst().orElse(null);
    if (destination == null) {
        throw new CacheException(ErrorType.NOT_FOUND, "Can not find path", String.format("Error: No node found destination=%s", flow.getDestinationSwitch()));
    }
    return new ImmutablePair<>(path(source, destination, flow.getBandwidth()), path(destination, source, flow.getBandwidth()));
}
Also used : ImmutablePair(org.openkilda.messaging.model.ImmutablePair) CacheException(org.openkilda.messaging.error.CacheException) SwitchInfoData(org.openkilda.messaging.info.event.SwitchInfoData)

Example 10 with ImmutablePair

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

the class PathComputerMock method path.

private PathInfoData path(SwitchInfoData srcSwitch, SwitchInfoData dstSwitch, int bandwidth) {
    System.out.println("Get Path By Switch Instances " + bandwidth + ": " + srcSwitch + " - " + dstSwitch);
    LinkedList<IslInfoData> islInfoDataLinkedList = new LinkedList<>();
    List<PathNode> nodes = new ArrayList<>();
    PathInfoData path = new PathInfoData(0L, nodes);
    if (srcSwitch.equals(dstSwitch)) {
        return path;
    }
    Set<SwitchInfoData> nodesToProcess = new HashSet<>(network.nodes());
    Set<SwitchInfoData> nodesWereProcess = new HashSet<>();
    Map<SwitchInfoData, ImmutablePair<SwitchInfoData, IslInfoData>> predecessors = new HashMap<>();
    Map<SwitchInfoData, Long> distances = network.nodes().stream().collect(Collectors.toMap(k -> k, v -> Long.MAX_VALUE));
    distances.put(srcSwitch, 0L);
    while (!nodesToProcess.isEmpty()) {
        SwitchInfoData source = nodesToProcess.stream().min(Comparator.comparingLong(distances::get)).orElseThrow(() -> new CacheException(ErrorType.NOT_FOUND, "Can not find path", "Error: No nodes to process left"));
        nodesToProcess.remove(source);
        nodesWereProcess.add(source);
        for (SwitchInfoData target : network.successors(source)) {
            if (!nodesWereProcess.contains(target)) {
                IslInfoData edge = network.edgesConnecting(source, target).stream().filter(isl -> isl.getAvailableBandwidth() >= bandwidth).findFirst().orElseThrow(() -> new CacheException(ErrorType.NOT_FOUND, "Can not find path", "Error: No enough bandwidth"));
                Long distance = distances.get(source) + getWeight(edge);
                if (distances.get(target) >= distance) {
                    distances.put(target, distance);
                    nodesToProcess.add(target);
                    predecessors.put(target, new ImmutablePair<>(source, edge));
                }
            }
        }
    }
    ImmutablePair<SwitchInfoData, IslInfoData> nextHop = predecessors.get(dstSwitch);
    if (nextHop == null) {
        return null;
    }
    islInfoDataLinkedList.add(nextHop.getRight());
    while (predecessors.get(nextHop.getLeft()) != null) {
        nextHop = predecessors.get(nextHop.getLeft());
        islInfoDataLinkedList.add(nextHop.getRight());
    }
    Collections.reverse(islInfoDataLinkedList);
    int i = 0;
    for (IslInfoData isl : islInfoDataLinkedList) {
        collect(isl, path, i);
        i += 2;
    }
    updatePathBandwidth(path, bandwidth, islInfoDataLinkedList);
    return path;
}
Also used : Flow(org.openkilda.messaging.model.Flow) IslInfoData(org.openkilda.messaging.info.event.IslInfoData) ImmutablePair(org.openkilda.messaging.model.ImmutablePair) ErrorType(org.openkilda.messaging.error.ErrorType) Set(java.util.Set) CacheException(org.openkilda.messaging.error.CacheException) HashMap(java.util.HashMap) PathNode(org.openkilda.messaging.info.event.PathNode) Collectors(java.util.stream.Collectors) PathInfoData(org.openkilda.messaging.info.event.PathInfoData) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) Map(java.util.Map) MutableNetwork(com.google.common.graph.MutableNetwork) Comparator(java.util.Comparator) LinkedList(java.util.LinkedList) Collections(java.util.Collections) SwitchInfoData(org.openkilda.messaging.info.event.SwitchInfoData) HashMap(java.util.HashMap) CacheException(org.openkilda.messaging.error.CacheException) ArrayList(java.util.ArrayList) PathNode(org.openkilda.messaging.info.event.PathNode) LinkedList(java.util.LinkedList) PathInfoData(org.openkilda.messaging.info.event.PathInfoData) ImmutablePair(org.openkilda.messaging.model.ImmutablePair) IslInfoData(org.openkilda.messaging.info.event.IslInfoData) SwitchInfoData(org.openkilda.messaging.info.event.SwitchInfoData) HashSet(java.util.HashSet)

Aggregations

ImmutablePair (org.openkilda.messaging.model.ImmutablePair)12 Flow (org.openkilda.messaging.model.Flow)7 PathInfoData (org.openkilda.messaging.info.event.PathInfoData)5 And (cucumber.api.java.en.And)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Set (java.util.Set)2 Test (org.junit.Test)2 CacheException (org.openkilda.messaging.error.CacheException)2 PathNode (org.openkilda.messaging.info.event.PathNode)2 SwitchInfoData (org.openkilda.messaging.info.event.SwitchInfoData)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ContiguousSet (com.google.common.collect.ContiguousSet)1 DiscreteDomain (com.google.common.collect.DiscreteDomain)1 Range (com.google.common.collect.Range)1 RangeSet (com.google.common.collect.RangeSet)1 TreeRangeSet (com.google.common.collect.TreeRangeSet)1 MutableNetwork (com.google.common.graph.MutableNetwork)1