Search in sources :

Example 6 with CacheException

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;
}
Also used : CacheException(org.openkilda.messaging.error.CacheException) SwitchInfoData(org.openkilda.messaging.info.event.SwitchInfoData)

Example 7 with CacheException

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()));
}
Also used : ImmutablePair(org.openkilda.messaging.model.ImmutablePair) CacheException(org.openkilda.messaging.error.CacheException) SwitchInfoData(org.openkilda.messaging.info.event.SwitchInfoData)

Example 8 with CacheException

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

Example 9 with CacheException

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;
}
Also used : CacheException(org.openkilda.messaging.error.CacheException) SwitchInfoData(org.openkilda.messaging.info.event.SwitchInfoData)

Example 10 with CacheException

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;
}
Also used : CacheException(org.openkilda.messaging.error.CacheException) SwitchInfoData(org.openkilda.messaging.info.event.SwitchInfoData)

Aggregations

CacheException (org.openkilda.messaging.error.CacheException)11 SwitchInfoData (org.openkilda.messaging.info.event.SwitchInfoData)8 IslInfoData (org.openkilda.messaging.info.event.IslInfoData)4 IOException (java.io.IOException)2 InfoMessage (org.openkilda.messaging.info.InfoMessage)2 ImmutablePair (org.openkilda.messaging.model.ImmutablePair)2 MutableNetwork (com.google.common.graph.MutableNetwork)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Values (org.apache.storm.tuple.Values)1 BaseMessage (org.openkilda.messaging.BaseMessage)1 Message (org.openkilda.messaging.Message)1