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);
}
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));
}
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;
}
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()));
}
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;
}
Aggregations