Search in sources :

Example 36 with MeterId

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();
}
Also used : OFInstructionMeter(org.projectfloodlight.openflow.protocol.instruction.OFInstructionMeter) MeterAction(org.openkilda.rulemanager.action.MeterAction) SwapFieldAction(org.openkilda.rulemanager.action.SwapFieldAction) OFAction(org.projectfloodlight.openflow.protocol.action.OFAction) GroupAction(org.openkilda.rulemanager.action.GroupAction) CopyFieldAction(org.openkilda.rulemanager.action.noviflow.CopyFieldAction) SetFieldAction(org.openkilda.rulemanager.action.SetFieldAction) Action(org.openkilda.rulemanager.action.Action) PushVxlanAction(org.openkilda.rulemanager.action.PushVxlanAction) PopVxlanAction(org.openkilda.rulemanager.action.PopVxlanAction) PushVlanAction(org.openkilda.rulemanager.action.PushVlanAction) PopVlanAction(org.openkilda.rulemanager.action.PopVlanAction) PortOutAction(org.openkilda.rulemanager.action.PortOutAction) Set(java.util.Set) OFInstructionGotoTable(org.projectfloodlight.openflow.protocol.instruction.OFInstructionGotoTable) OFAction(org.projectfloodlight.openflow.protocol.action.OFAction) OFInstructionWriteMetadata(org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteMetadata) InstructionsBuilder(org.openkilda.rulemanager.Instructions.InstructionsBuilder) MeterId(org.openkilda.model.MeterId) OFInstructionApplyActions(org.projectfloodlight.openflow.protocol.instruction.OFInstructionApplyActions) OfMetadata(org.openkilda.rulemanager.OfMetadata) OFInstruction(org.projectfloodlight.openflow.protocol.instruction.OFInstruction) List(java.util.List) ArrayList(java.util.ArrayList) OFInstructionWriteActions(org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteActions)

Example 37 with MeterId

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);
    }
}
Also used : YFlowResources(org.openkilda.wfm.topology.flowhs.model.yflow.YFlowResources) SwitchId(org.openkilda.model.SwitchId) EndpointResources(org.openkilda.wfm.topology.flowhs.model.yflow.YFlowResources.EndpointResources) MeterId(org.openkilda.model.MeterId)

Example 38 with MeterId

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;
}
Also used : MeterId(org.openkilda.model.MeterId)

Example 39 with 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));
}
Also used : Random(java.util.Random) MeterId(org.openkilda.model.MeterId) TransactionRequired(org.openkilda.persistence.tx.TransactionRequired)

Example 40 with MeterId

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);
    }
}
Also used : SwitchId(org.openkilda.model.SwitchId) EndpointResources(org.openkilda.wfm.topology.flowhs.model.yflow.YFlowResources.EndpointResources) MeterId(org.openkilda.model.MeterId)

Aggregations

MeterId (org.openkilda.model.MeterId)62 PathId (org.openkilda.model.PathId)20 Flow (org.openkilda.model.Flow)19 FlowPath (org.openkilda.model.FlowPath)18 FlowSegmentCookie (org.openkilda.model.cookie.FlowSegmentCookie)18 Test (org.junit.Test)17 SwitchId (org.openkilda.model.SwitchId)16 Switch (org.openkilda.model.Switch)11 ArrayList (java.util.ArrayList)10 PathSegment (org.openkilda.model.PathSegment)9 MeterSpeakerData (org.openkilda.rulemanager.MeterSpeakerData)7 List (java.util.List)6 ValidateMetersResult (org.openkilda.wfm.topology.switchmanager.model.ValidateMetersResult)6 FlowMeter (org.openkilda.model.FlowMeter)5 MeterConfig (org.openkilda.model.MeterConfig)5 YFlow (org.openkilda.model.YFlow)5 Instructions (org.openkilda.rulemanager.Instructions)5 PortOutAction (org.openkilda.rulemanager.action.PortOutAction)5 ValidationService (org.openkilda.wfm.topology.switchmanager.service.ValidationService)5 MessageContext (org.openkilda.messaging.MessageContext)4