Search in sources :

Example 1 with ResourceNotAvailableException

use of org.openkilda.wfm.share.flow.resources.ResourceNotAvailableException in project open-kilda by telstra.

the class TransitVlanPool method allocate.

@TransactionRequired
private TransitVlanEncapsulation allocate(Flow flow, PathId pathId) {
    if (nextVlan > 0) {
        if (nextVlan <= maxTransitVlan && !transitVlanRepository.exists(nextVlan)) {
            return addVlan(flow, pathId, nextVlan++);
        } else {
            nextVlan = 0;
        }
    }
    // The pool requires (re-)initialization.
    if (nextVlan == 0) {
        long numOfPools = (maxTransitVlan - minTransitVlan) / poolSize;
        if (numOfPools > 1) {
            long poolToTake = Math.abs(new Random().nextInt()) % numOfPools;
            Optional<Integer> availableVlan = transitVlanRepository.findFirstUnassignedVlan(minTransitVlan + (int) poolToTake * poolSize, minTransitVlan + (int) (poolToTake + 1) * poolSize - 1);
            if (availableVlan.isPresent()) {
                nextVlan = availableVlan.get();
                return addVlan(flow, pathId, nextVlan++);
            }
        }
        // The pool requires full scan.
        nextVlan = -1;
    }
    if (nextVlan == -1) {
        Optional<Integer> availableVlan = transitVlanRepository.findFirstUnassignedVlan(minTransitVlan, minTransitVlan);
        if (availableVlan.isPresent()) {
            nextVlan = availableVlan.get();
            return addVlan(flow, pathId, nextVlan++);
        }
    }
    throw new ResourceNotAvailableException("No vlan available");
}
Also used : Random(java.util.Random) ResourceNotAvailableException(org.openkilda.wfm.share.flow.resources.ResourceNotAvailableException) TransactionRequired(org.openkilda.persistence.tx.TransactionRequired)

Example 2 with ResourceNotAvailableException

use of org.openkilda.wfm.share.flow.resources.ResourceNotAvailableException in project open-kilda by telstra.

the class VxlanPool method allocate.

@TransactionRequired
private VxlanEncapsulation allocate(Flow flow, PathId pathId) {
    if (nextVxlan > 0) {
        if (nextVxlan <= maxVxlan && !vxlanRepository.exists(nextVxlan)) {
            return addVxlan(flow, pathId, nextVxlan++);
        } else {
            nextVxlan = 0;
        }
    }
    // The pool requires (re-)initialization.
    if (nextVxlan == 0) {
        long numOfPools = (maxVxlan - minVxlan) / poolSize;
        if (numOfPools > 1) {
            long poolToTake = Math.abs(new Random().nextInt()) % numOfPools;
            Optional<Integer> availableVxlan = vxlanRepository.findFirstUnassignedVxlan(minVxlan + (int) poolToTake * poolSize, minVxlan + (int) (poolToTake + 1) * poolSize - 1);
            if (availableVxlan.isPresent()) {
                nextVxlan = availableVxlan.get();
                return addVxlan(flow, pathId, nextVxlan++);
            }
        }
        // The pool requires full scan.
        nextVxlan = -1;
    }
    if (nextVxlan == -1) {
        Optional<Integer> availableVxlan = vxlanRepository.findFirstUnassignedVxlan(minVxlan, maxVxlan);
        if (availableVxlan.isPresent()) {
            nextVxlan = availableVxlan.get();
            return addVxlan(flow, pathId, nextVxlan++);
        }
    }
    throw new ResourceNotAvailableException("No vxlan available");
}
Also used : Random(java.util.Random) ResourceNotAvailableException(org.openkilda.wfm.share.flow.resources.ResourceNotAvailableException) TransactionRequired(org.openkilda.persistence.tx.TransactionRequired)

Aggregations

Random (java.util.Random)2 TransactionRequired (org.openkilda.persistence.tx.TransactionRequired)2 ResourceNotAvailableException (org.openkilda.wfm.share.flow.resources.ResourceNotAvailableException)2