use of org.openkilda.model.MeterId in project open-kilda by telstra.
the class OfInstructionsConverter method convertToRuleManagerInstructions.
/**
* Convert instructions.
*/
public Instructions convertToRuleManagerInstructions(List<OFInstruction> ofInstructions) {
InstructionsBuilder builder = Instructions.builder();
for (OFInstruction ofInstruction : ofInstructions) {
if (ofInstruction instanceof OFInstructionApplyActions) {
List<OFAction> ofActions = ((OFInstructionApplyActions) ofInstruction).getActions();
List<Action> actions = ofActions.stream().map(this::convertToRuleManagerAction).collect(Collectors.toList());
builder.applyActions(actions);
} else if (ofInstruction instanceof OFInstructionWriteActions) {
List<OFAction> ofActions = ((OFInstructionWriteActions) ofInstruction).getActions();
Set<Action> actions = ofActions.stream().map(this::convertToRuleManagerAction).collect(Collectors.toSet());
builder.writeActions(actions);
} else if (ofInstruction instanceof OFInstructionMeter) {
final long meterId = ((OFInstructionMeter) ofInstruction).getMeterId();
builder.goToMeter(new MeterId(meterId));
} else if (ofInstruction instanceof OFInstructionGotoTable) {
final short tableId = ((OFInstructionGotoTable) ofInstruction).getTableId().getValue();
builder.goToTable(OfTable.fromInt(tableId));
} else if (ofInstruction instanceof OFInstructionWriteMetadata) {
final long metadata = ((OFInstructionWriteMetadata) ofInstruction).getMetadata().getValue();
final long metadataMask = ((OFInstructionWriteMetadata) ofInstruction).getMetadataMask().getValue();
builder.writeMetadata(new OfMetadata(metadata, metadataMask));
}
}
return builder.build();
}
use of org.openkilda.model.MeterId in project open-kilda by telstra.
the class DeallocateYFlowResourcesAction method perform.
@Override
public void perform(State from, State to, Event event, YFlowDeleteContext context, YFlowDeleteFsm stateMachine) {
notifyStats(stateMachine);
String yFlowId = stateMachine.getYFlowId();
Optional<YFlowResources> oldResources = Optional.ofNullable(stateMachine.getOldResources());
Optional<EndpointResources> sharedEndpointResources = oldResources.map(YFlowResources::getSharedEndpointResources);
Optional<MeterId> sharedEndpointMeterId = sharedEndpointResources.map(EndpointResources::getMeterId);
if (sharedEndpointMeterId.isPresent()) {
MeterId meterId = sharedEndpointMeterId.get();
SwitchId endpoint = sharedEndpointResources.get().getEndpoint();
resourcesManager.deallocateMeter(endpoint, meterId);
// reset to avoid false notifications in {@link HandleNotRemovedResourcesAction}.
oldResources.get().setSharedEndpointResources(null);
stateMachine.saveActionToHistory("The meter was deallocated", format("The meter %s / %s was deallocated", endpoint, meterId));
} else {
log.debug("No meter was allocated for y-flow {} (shared endpoint)", yFlowId);
}
Optional<EndpointResources> mainResources = oldResources.map(YFlowResources::getMainPathYPointResources);
Optional<MeterId> mainMeterId = mainResources.map(EndpointResources::getMeterId);
if (mainMeterId.isPresent()) {
MeterId meterId = mainMeterId.get();
SwitchId endpoint = mainResources.get().getEndpoint();
resourcesManager.deallocateMeter(endpoint, meterId);
// reset to avoid false notifications in {@link HandleNotRemovedResourcesAction}.
oldResources.get().setMainPathYPointResources(null);
stateMachine.saveActionToHistory("The meter was deallocated", format("The meter %s / %s was deallocated", endpoint, meterId));
} else {
log.debug("No meter was allocated for y-flow {} (main paths)", yFlowId);
}
Optional<EndpointResources> protectedResources = oldResources.map(YFlowResources::getProtectedPathYPointResources);
Optional<MeterId> protectedMeterId = protectedResources.map(EndpointResources::getMeterId);
if (protectedMeterId.isPresent()) {
MeterId meterId = protectedMeterId.get();
SwitchId endpoint = protectedResources.get().getEndpoint();
resourcesManager.deallocateMeter(endpoint, meterId);
// reset to avoid false notifications in {@link HandleNotRemovedResourcesAction}.
oldResources.get().setProtectedPathYPointResources(null);
stateMachine.saveActionToHistory("The meter was deallocated", format("The meter %s / %s was deallocated", endpoint, meterId));
} else {
log.debug("No meter was allocated for y-flow {} (protected paths)", yFlowId);
}
}
use of org.openkilda.model.MeterId in project open-kilda by telstra.
the class IdProvider method provideMeterId.
public MeterId provideMeterId(SwitchId switchId) {
Integer counter = meterCounters.getOrDefault(switchId, MeterId.MIN_FLOW_METER_ID);
MeterId meterId = new MeterId(counter);
meterCounters.put(switchId, counter + 1);
return meterId;
}
use of org.openkilda.model.MeterId 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.model.MeterId in project open-kilda by telstra.
the class DeallocateYFlowResourcesAction method deallocateYFlowResources.
protected void deallocateYFlowResources(YFlowResources resources, T stateMachine) {
String yFlowId = stateMachine.getYFlowId();
if (resources == null) {
log.debug("No resources were allocated for y-flow {}", yFlowId);
return;
}
stateMachine.sendRemoveStatsNotification(resources);
Optional<EndpointResources> sharedEndpointResources = ofNullable(resources.getSharedEndpointResources());
Optional<MeterId> sharedEndpointMeterId = sharedEndpointResources.map(EndpointResources::getMeterId);
if (sharedEndpointMeterId.isPresent()) {
MeterId meterId = sharedEndpointMeterId.get();
SwitchId endpoint = sharedEndpointResources.get().getEndpoint();
resourcesManager.deallocateMeter(endpoint, meterId);
stateMachine.saveActionToHistory("The meter was deallocated", format("The meter %s / %s was deallocated", endpoint, meterId));
} else {
log.debug("No meter was allocated for y-flow {} (shared endpoint)", yFlowId);
}
Optional<EndpointResources> mainResources = ofNullable(resources.getMainPathYPointResources());
Optional<MeterId> mainMeterId = mainResources.map(EndpointResources::getMeterId);
if (mainMeterId.isPresent()) {
MeterId meterId = mainMeterId.get();
SwitchId yPoint = mainResources.get().getEndpoint();
resourcesManager.deallocateMeter(yPoint, meterId);
stateMachine.saveActionToHistory("The meter was deallocated", format("The meter %s / %s was deallocated", yPoint, meterId));
} else {
log.debug("No meter was allocated for y-flow {} (main paths)", yFlowId);
}
Optional<EndpointResources> protectedResources = ofNullable(resources.getProtectedPathYPointResources());
Optional<MeterId> protectedMeterId = protectedResources.map(EndpointResources::getMeterId);
if (protectedMeterId.isPresent()) {
MeterId meterId = protectedMeterId.get();
SwitchId yPoint = protectedResources.get().getEndpoint();
resourcesManager.deallocateMeter(yPoint, meterId);
stateMachine.saveActionToHistory("The meter was deallocated", format("The meter %s / %s was deallocated", yPoint, meterId));
} else {
log.debug("No meter was allocated for y-flow {} (main paths)", yFlowId);
}
}
Aggregations