Search in sources :

Example 6 with EndpointResources

use of org.openkilda.wfm.topology.flowhs.model.yflow.YFlowResources.EndpointResources in project open-kilda by telstra.

the class AllocateYFlowResourcesAction method perform.

@Override
public void perform(S from, S to, E event, C context, T stateMachine) {
    try {
        String yFlowId = stateMachine.getYFlowId();
        YFlowResources newResources;
        // This could be a retry.
        if (stateMachine.getNewResources() != null) {
            newResources = stateMachine.getNewResources();
        } else {
            newResources = new YFlowResources();
            stateMachine.setNewResources(newResources);
        }
        YFlow yFlow = getYFlow(yFlowId);
        SwitchId sharedEndpoint = yFlow.getSharedEndpoint().getSwitchId();
        if (newResources.getSharedEndpointResources() == null) {
            EndpointResources sharedEndpointResources = allocateMeterAsEndpointResources(yFlowId, sharedEndpoint, yFlow.getMaximumBandwidth());
            newResources.setSharedEndpointResources(sharedEndpointResources);
            stateMachine.saveActionToHistory("A new meter was allocated for the y-flow shared endpoint", format("A new meter %s / %s was allocated", sharedEndpointResources.getMeterId(), sharedEndpointResources.getEndpoint()));
        }
        if (newResources.getMainPathYPointResources() == null) {
            List<FlowPath> subFlowsReversePaths = new ArrayList<>();
            yFlow.getSubFlows().forEach(subFlow -> {
                Flow flow = subFlow.getFlow();
                FlowPath path = flow.getReversePath();
                if (path == null) {
                    throw new FlowProcessingException(ErrorType.INTERNAL_ERROR, format("Missing a reverse path for %s sub-flow", flow.getFlowId()));
                } else if (!flow.isOneSwitchFlow()) {
                    subFlowsReversePaths.add(path);
                }
            });
            EndpointResources yPointResources = allocateYPointResources(yFlowId, sharedEndpoint, yFlow.getMaximumBandwidth(), subFlowsReversePaths.toArray(new FlowPath[0]));
            newResources.setMainPathYPointResources(yPointResources);
            stateMachine.saveActionToHistory("A new meter was allocated for the y-flow y-point", format("A new meter %s / %s was allocated", yPointResources.getMeterId(), yPointResources.getEndpoint()));
        }
        if (yFlow.isAllocateProtectedPath() && newResources.getProtectedPathYPointResources() == null) {
            List<FlowPath> subFlowsReversePaths = new ArrayList<>();
            yFlow.getSubFlows().forEach(subFlow -> {
                Flow flow = subFlow.getFlow();
                FlowPath path = flow.getProtectedReversePath();
                if (path == null) {
                    if (flow.getStatus() == FlowStatus.UP) {
                        throw new FlowProcessingException(ErrorType.INTERNAL_ERROR, format("Missing a protected path for %s sub-flow", flow.getFlowId()));
                    } else {
                        log.warn("Sub-flow {} has no expected protected path and status {}", flow.getFlowId(), flow.getStatus());
                    }
                } else if (!flow.isOneSwitchFlow()) {
                    subFlowsReversePaths.add(path);
                }
            });
            if (subFlowsReversePaths.size() > 1) {
                EndpointResources yPointResources = allocateYPointResources(yFlowId, sharedEndpoint, yFlow.getMaximumBandwidth(), subFlowsReversePaths.toArray(new FlowPath[0]));
                newResources.setProtectedPathYPointResources(yPointResources);
                stateMachine.saveActionToHistory("A new meter was allocated for the y-flow protected path y-point", format("A new meter %s / %s was allocated", yPointResources.getMeterId(), yPointResources.getEndpoint()));
            } else {
                stateMachine.saveActionToHistory("Skip meter allocation for the y-flow protected path y-point", "Y-flow protected path y-point can't be found - sub-flow(s) lacks a protected path");
            }
        }
        transactionManager.doInTransaction(() -> {
            YFlow yFlowToUpdate = getYFlow(yFlowId);
            yFlowToUpdate.setYPoint(newResources.getMainPathYPointResources().getEndpoint());
            yFlowToUpdate.setMeterId(newResources.getMainPathYPointResources().getMeterId());
            if (newResources.getProtectedPathYPointResources() != null) {
                yFlowToUpdate.setProtectedPathYPoint(newResources.getProtectedPathYPointResources().getEndpoint());
                yFlowToUpdate.setProtectedPathMeterId(newResources.getProtectedPathYPointResources().getMeterId());
            } else {
                yFlowToUpdate.setProtectedPathYPoint(null);
                yFlowToUpdate.setProtectedPathMeterId(null);
            }
            yFlowToUpdate.setSharedEndpointMeterId(newResources.getSharedEndpointResources().getMeterId());
        });
        notifyStats(stateMachine, newResources);
    } catch (ResourceAllocationException ex) {
        String errorMessage = format("Failed to allocate y-flow resources. %s", ex.getMessage());
        stateMachine.saveErrorToHistory(errorMessage, ex);
        stateMachine.fireError(errorMessage);
    }
}
Also used : YFlow(org.openkilda.model.YFlow) YFlowResources(org.openkilda.wfm.topology.flowhs.model.yflow.YFlowResources) FlowProcessingException(org.openkilda.wfm.topology.flowhs.exception.FlowProcessingException) ArrayList(java.util.ArrayList) SwitchId(org.openkilda.model.SwitchId) EndpointResources(org.openkilda.wfm.topology.flowhs.model.yflow.YFlowResources.EndpointResources) FlowPath(org.openkilda.model.FlowPath) ResourceAllocationException(org.openkilda.wfm.share.flow.resources.ResourceAllocationException) Flow(org.openkilda.model.Flow) YFlow(org.openkilda.model.YFlow)

Example 7 with EndpointResources

use of org.openkilda.wfm.topology.flowhs.model.yflow.YFlowResources.EndpointResources 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

EndpointResources (org.openkilda.wfm.topology.flowhs.model.yflow.YFlowResources.EndpointResources)7 YFlowResources (org.openkilda.wfm.topology.flowhs.model.yflow.YFlowResources)6 SwitchId (org.openkilda.model.SwitchId)4 MeterId (org.openkilda.model.MeterId)3 ArrayList (java.util.ArrayList)1 Flow (org.openkilda.model.Flow)1 FlowPath (org.openkilda.model.FlowPath)1 YFlow (org.openkilda.model.YFlow)1 ResourceAllocationException (org.openkilda.wfm.share.flow.resources.ResourceAllocationException)1 FlowProcessingException (org.openkilda.wfm.topology.flowhs.exception.FlowProcessingException)1