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