Search in sources :

Example 1 with TransactionRequired

use of org.openkilda.persistence.tx.TransactionRequired in project open-kilda by telstra.

the class FermaPathSegmentRepository method updateFailedStatus.

@Override
@TransactionRequired
public void updateFailedStatus(FlowPath path, PathSegment segment, boolean failed) {
    PathSegment segmentToUpdate;
    if (segment.getData() instanceof PathSegmentFrame) {
        segmentToUpdate = segment;
    } else {
        segmentToUpdate = path.getSegments().stream().filter(pathSegment -> pathSegment.getSrcSwitchId().equals(segment.getSrcSwitchId()) && pathSegment.getSrcPort() == segment.getSrcPort() && pathSegment.getDestSwitchId().equals(segment.getDestSwitchId()) && pathSegment.getDestPort() == segment.getDestPort()).findAny().orElse(null);
    }
    if (segmentToUpdate == null) {
        throw new PersistenceException(format("PathSegment not found to be updated: %s_%d - %s_%d. Path id: %s.", segment.getSrcSwitchId(), segment.getSrcPort(), segment.getDestSwitchId(), segment.getDestPort(), path.getPathId()));
    }
    segmentToUpdate.setFailed(failed);
}
Also used : PathSegment(org.openkilda.model.PathSegment) FlowPath(org.openkilda.model.FlowPath) TransactionRequired(org.openkilda.persistence.tx.TransactionRequired) PathSegmentData(org.openkilda.model.PathSegment.PathSegmentData) PathSegmentFrame(org.openkilda.persistence.ferma.frames.PathSegmentFrame) PersistenceException(org.openkilda.persistence.exceptions.PersistenceException) FermaPersistentImplementation(org.openkilda.persistence.ferma.FermaPersistentImplementation) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) List(java.util.List) PathSegmentRepository(org.openkilda.persistence.repositories.PathSegmentRepository) IslRepository(org.openkilda.persistence.repositories.IslRepository) Optional(java.util.Optional) Comparator(java.util.Comparator) PathIdConverter(org.openkilda.persistence.ferma.frames.converters.PathIdConverter) PathId(org.openkilda.model.PathId) PathSegmentFrame(org.openkilda.persistence.ferma.frames.PathSegmentFrame) PersistenceException(org.openkilda.persistence.exceptions.PersistenceException) PathSegment(org.openkilda.model.PathSegment) TransactionRequired(org.openkilda.persistence.tx.TransactionRequired)

Example 2 with TransactionRequired

use of org.openkilda.persistence.tx.TransactionRequired 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 3 with TransactionRequired

use of org.openkilda.persistence.tx.TransactionRequired 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)

Example 4 with TransactionRequired

use of org.openkilda.persistence.tx.TransactionRequired in project open-kilda by telstra.

the class MeterPool method allocate.

/**
 * Allocates a meter for the flow path.
 */
@TransactionRequired
public MeterId allocate(SwitchId switchId, String flowId, PathId pathId) {
    MeterId nextMeter = nextMeters.get(switchId);
    if (nextMeter != null && nextMeter.getValue() > 0) {
        if (nextMeter.compareTo(maxMeterId) <= 0 && !flowMeterRepository.exists(switchId, nextMeter)) {
            addMeter(flowId, pathId, switchId, nextMeter);
            nextMeters.put(switchId, new MeterId(nextMeter.getValue() + 1));
            return nextMeter;
        } else {
            nextMeters.remove(switchId);
        }
    }
    // The pool requires (re-)initialization.
    if (!nextMeters.containsKey(switchId)) {
        long numOfPools = (maxMeterId.getValue() - minMeterId.getValue()) / poolSize;
        if (numOfPools > 1) {
            long poolToTake = Math.abs(new Random().nextInt()) % numOfPools;
            Optional<MeterId> availableMeter = flowMeterRepository.findFirstUnassignedMeter(switchId, new MeterId(minMeterId.getValue() + poolToTake * poolSize), new MeterId(minMeterId.getValue() + (poolToTake + 1) * poolSize - 1));
            if (availableMeter.isPresent()) {
                nextMeter = availableMeter.get();
                addMeter(flowId, pathId, switchId, nextMeter);
                nextMeters.put(switchId, new MeterId(nextMeter.getValue() + 1));
                return nextMeter;
            }
        }
        // The pool requires full scan.
        nextMeter = new MeterId(-1);
        nextMeters.put(switchId, nextMeter);
    }
    if (nextMeter != null && nextMeter.getValue() == -1) {
        Optional<MeterId> availableMeter = flowMeterRepository.findFirstUnassignedMeter(switchId, minMeterId, maxMeterId);
        if (availableMeter.isPresent()) {
            nextMeter = availableMeter.get();
            addMeter(flowId, pathId, switchId, nextMeter);
            nextMeters.put(switchId, new MeterId(nextMeter.getValue() + 1));
            return nextMeter;
        }
    }
    throw new ResourceNotAvailableException(format("No meter available for switch %s", switchId));
}
Also used : Random(java.util.Random) MeterId(org.openkilda.model.MeterId) TransactionRequired(org.openkilda.persistence.tx.TransactionRequired)

Example 5 with TransactionRequired

use of org.openkilda.persistence.tx.TransactionRequired in project open-kilda by telstra.

the class CookiePool method allocate.

/**
 * Allocates a cookie for the flow.
 *
 * @return unmasked allocated cookie.
 */
@TransactionRequired
public long allocate(String flowId) {
    if (nextCookie > 0) {
        if (nextCookie <= maxCookie && !flowCookieRepository.exists(nextCookie)) {
            addCookie(flowId, nextCookie);
            return nextCookie++;
        } else {
            nextCookie = 0;
        }
    }
    // The pool requires (re-)initialization.
    if (nextCookie == 0) {
        long numOfPools = (maxCookie - minCookie) / poolSize;
        if (numOfPools > 1) {
            long poolToTake = Math.abs(new Random().nextInt()) % numOfPools;
            Optional<Long> availableCookie = flowCookieRepository.findFirstUnassignedCookie(minCookie + poolToTake * poolSize, minCookie + (poolToTake + 1) * poolSize - 1);
            if (availableCookie.isPresent()) {
                nextCookie = availableCookie.get();
                addCookie(flowId, nextCookie);
                return nextCookie++;
            }
        }
        // The pool requires full scan.
        nextCookie = -1;
    }
    if (nextCookie == -1) {
        Optional<Long> availableCookie = flowCookieRepository.findFirstUnassignedCookie(minCookie, maxCookie);
        if (availableCookie.isPresent()) {
            nextCookie = availableCookie.get();
            addCookie(flowId, nextCookie);
            return nextCookie++;
        }
    }
    throw new ResourceNotAvailableException("No cookie available");
}
Also used : Random(java.util.Random) TransactionRequired(org.openkilda.persistence.tx.TransactionRequired)

Aggregations

TransactionRequired (org.openkilda.persistence.tx.TransactionRequired)6 Random (java.util.Random)5 ResourceNotAvailableException (org.openkilda.wfm.share.flow.resources.ResourceNotAvailableException)2 String.format (java.lang.String.format)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 FlowPath (org.openkilda.model.FlowPath)1 GroupId (org.openkilda.model.GroupId)1 MeterId (org.openkilda.model.MeterId)1 MirrorGroup (org.openkilda.model.MirrorGroup)1 PathId (org.openkilda.model.PathId)1 PathSegment (org.openkilda.model.PathSegment)1 PathSegmentData (org.openkilda.model.PathSegment.PathSegmentData)1 PersistenceException (org.openkilda.persistence.exceptions.PersistenceException)1 FermaPersistentImplementation (org.openkilda.persistence.ferma.FermaPersistentImplementation)1 PathSegmentFrame (org.openkilda.persistence.ferma.frames.PathSegmentFrame)1 PathIdConverter (org.openkilda.persistence.ferma.frames.converters.PathIdConverter)1 IslRepository (org.openkilda.persistence.repositories.IslRepository)1