use of org.openkilda.messaging.error.CacheException in project open-kilda by telstra.
the class NetworkCache method updateSwitch.
/**
* Updates {@link SwitchInfoData} instance.
*
* @param newSwitch {@link SwitchInfoData} instance
* @return {@link SwitchInfoData} instance before update
* @throws CacheException if {@link SwitchInfoData} instance with specified id does not exist
*/
public SwitchInfoData updateSwitch(SwitchInfoData newSwitch) throws CacheException {
String switchId = newSwitch.getSwitchId();
logger.debug("Update {} switch with {} parameters", switchId, newSwitch);
SwitchInfoData oldSwitch = switchPool.remove(switchId);
if (oldSwitch == null) {
throw new CacheException(ErrorType.NOT_FOUND, "Can not update switch", String.format("Switch %s not found", switchId));
}
newSwitch.copyTimeTag(oldSwitch);
newSwitch.setUpdatedInCacheNow();
network.removeNode(oldSwitch);
network.addNode(newSwitch);
switchPool.put(switchId, newSwitch);
return newSwitch;
}
use of org.openkilda.messaging.error.CacheException 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.error.CacheException 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;
}
use of org.openkilda.messaging.error.CacheException in project open-kilda by telstra.
the class NetworkCache method getSwitch.
/**
* Gets {@link SwitchInfoData} instance.
*
* @param switchId {@link SwitchInfoData} instance id
* @return {@link SwitchInfoData} instance with specified {@link SwitchInfoData} instance id
* @throws CacheException if {@link SwitchInfoData} instance with specified id does not exist
*/
public SwitchInfoData getSwitch(String switchId) throws CacheException {
logger.debug("Get {} switch", switchId);
SwitchInfoData node = switchPool.get(switchId);
if (node == null) {
throw new CacheException(ErrorType.NOT_FOUND, "Can not get switch", String.format("Switch %s not found", switchId));
}
return node;
}
use of org.openkilda.messaging.error.CacheException in project open-kilda by telstra.
the class NetworkCache method createSwitch.
/**
* Creates {@link SwitchInfoData} instance.
*
* @param newSwitch {@link SwitchInfoData} instance
* @return created {@link SwitchInfoData} instance
* @throws CacheException if {@link SwitchInfoData} instance with specified id already exists
*/
public SwitchInfoData createSwitch(SwitchInfoData newSwitch) throws CacheException {
String switchId = newSwitch.getSwitchId();
logger.debug("Create {} switch with {} parameters", switchId, newSwitch);
SwitchInfoData oldSwitch = switchPool.get(switchId);
if (oldSwitch != null) {
throw new CacheException(ErrorType.ALREADY_EXISTS, "Can not create switch", String.format("Switch %s already exists", switchId));
}
newSwitch.setCreatedInCacheNow();
network.addNode(newSwitch);
switchPool.put(switchId, newSwitch);
return newSwitch;
}
Aggregations