Search in sources :

Example 1 with EncapsulationResources

use of org.openkilda.wfm.share.flow.resources.EncapsulationResources in project open-kilda by telstra.

the class RemoveRulesAction method buildResources.

private FlowResources buildResources(Flow flow, FlowPath path, FlowPath oppositePath) {
    FlowPath forwardPath;
    FlowPath reversePath;
    if (path.isForward()) {
        forwardPath = path;
        reversePath = oppositePath;
    } else {
        forwardPath = oppositePath;
        reversePath = path;
    }
    EncapsulationResources encapsulationResources;
    if (!flow.isOneSwitchFlow()) {
        encapsulationResources = resourcesManager.getEncapsulationResources(forwardPath.getPathId(), reversePath.getPathId(), flow.getEncapsulationType()).orElse(null);
    } else {
        encapsulationResources = null;
    }
    return FlowResources.builder().unmaskedCookie(forwardPath.getCookie().getFlowEffectiveId()).forward(PathResources.builder().pathId(forwardPath.getPathId()).meterId(forwardPath.getMeterId()).encapsulationResources(encapsulationResources).build()).reverse(PathResources.builder().pathId(reversePath.getPathId()).meterId(reversePath.getMeterId()).encapsulationResources(encapsulationResources).build()).build();
}
Also used : EncapsulationResources(org.openkilda.wfm.share.flow.resources.EncapsulationResources) FlowPath(org.openkilda.model.FlowPath)

Example 2 with EncapsulationResources

use of org.openkilda.wfm.share.flow.resources.EncapsulationResources in project open-kilda by telstra.

the class CommandBuilderImpl method buildCommandsToSyncMissingRules.

@Override
public List<BaseFlow> buildCommandsToSyncMissingRules(SwitchId switchId, List<Long> switchRules) {
    List<BaseFlow> commands = new ArrayList<>(buildInstallDefaultRuleCommands(switchId, switchRules));
    commands.addAll(buildInstallFlowSharedRuleCommands(switchId, switchRules));
    flowPathRepository.findBySegmentDestSwitch(switchId).forEach(flowPath -> {
        FlowSegmentCookie mirrorCookie = flowPath.getCookie().toBuilder().mirror(true).build();
        boolean switchRulesContainsFlowPathCookie = switchRules.contains(flowPath.getCookie().getValue());
        boolean switchRulesContainsMirrorCookie = switchRules.contains(mirrorCookie.getValue());
        if (switchRulesContainsFlowPathCookie || switchRulesContainsMirrorCookie) {
            PathSegment segment = flowPath.getSegments().stream().filter(pathSegment -> pathSegment.getDestSwitchId().equals(switchId)).findAny().orElseThrow(() -> new IllegalStateException(format("PathSegment not found, path %s, switch %s", flowPath, switchId)));
            log.info("Rule {} is to be (re)installed on switch {}", flowPath.getCookie(), switchId);
            commands.addAll(buildInstallCommandFromSegment(flowPath, segment, switchRulesContainsFlowPathCookie, switchRulesContainsMirrorCookie));
        }
    });
    SwitchProperties switchProperties = getSwitchProperties(switchId);
    flowPathRepository.findByEndpointSwitch(switchId).forEach(flowPath -> {
        FlowSegmentCookie mirrorCookie = flowPath.getCookie().toBuilder().mirror(true).build();
        boolean switchRulesContainsFlowPathCookie = switchRules.contains(flowPath.getCookie().getValue());
        boolean switchRulesContainsMirrorCookie = switchRules.contains(mirrorCookie.getValue());
        if (switchRulesContainsFlowPathCookie || switchRulesContainsMirrorCookie) {
            Flow flow = getFlow(flowPath);
            if (flowPath.isOneSwitchFlow()) {
                log.info("One-switch flow {} is to be (re)installed on switch {}", flowPath.getCookie(), switchId);
                SwitchId swId = flowPath.isForward() ? flow.getDestSwitchId() : flow.getSrcSwitchId();
                int port = flowPath.isForward() ? flow.getDestPort() : flow.getSrcPort();
                if (switchRulesContainsMirrorCookie) {
                    MirrorConfig mirrorConfig = makeMirrorConfig(flowPath, swId, port);
                    commands.add(flowCommandFactory.makeOneSwitchMirrorRule(flow, flowPath, mirrorConfig));
                }
                if (switchRulesContainsFlowPathCookie) {
                    commands.add(flowCommandFactory.makeOneSwitchRule(flow, flowPath));
                }
            } else if (flowPath.getSrcSwitchId().equals(switchId)) {
                log.info("Ingress flow {} is to be (re)installed on switch {}", flowPath.getCookie(), switchId);
                if (flowPath.getSegments().isEmpty()) {
                    log.warn("Output port was not found for ingress flow rule");
                } else {
                    PathSegment foundIngressSegment = flowPath.getSegments().get(0);
                    EncapsulationResources encapsulationResources = getEncapsulationResources(flowPath, flow);
                    if (switchRulesContainsMirrorCookie) {
                        MirrorConfig mirrorConfig = makeMirrorConfig(flowPath, foundIngressSegment.getSrcSwitchId(), foundIngressSegment.getSrcPort());
                        commands.add(flowCommandFactory.buildInstallIngressMirrorFlow(flow, flowPath, foundIngressSegment.getSrcPort(), encapsulationResources, foundIngressSegment.isSrcWithMultiTable(), mirrorConfig));
                    }
                    if (switchRulesContainsFlowPathCookie) {
                        commands.add(flowCommandFactory.buildInstallIngressFlow(flow, flowPath, foundIngressSegment.getSrcPort(), encapsulationResources, foundIngressSegment.isSrcWithMultiTable()));
                    }
                }
            }
        }
        long server42Cookie = flowPath.getCookie().toBuilder().type(CookieType.SERVER_42_FLOW_RTT_INGRESS).build().getValue();
        if (switchRules.contains(server42Cookie) && !flowPath.isOneSwitchFlow() && flowPath.getSrcSwitchId().equals(switchId)) {
            log.info("Ingress server 42 flow {} is to be (re)installed on switch {}", server42Cookie, switchId);
            if (flowPath.getSegments().isEmpty()) {
                log.warn("Output port was not found for server 42 ingress flow rule {}", server42Cookie);
            } else {
                Flow flow = getFlow(flowPath);
                PathSegment foundIngressSegment = flowPath.getSegments().get(0);
                EncapsulationResources encapsulationResources = getEncapsulationResources(flowPath, flow);
                commands.add(flowCommandFactory.buildInstallServer42IngressFlow(flow, flowPath, foundIngressSegment.getSrcPort(), switchProperties.getServer42Port(), switchProperties.getServer42MacAddress(), encapsulationResources, foundIngressSegment.isSrcWithMultiTable()));
            }
        }
        long loopCookie = flowPath.getCookie().toBuilder().looped(true).build().getValue();
        if (switchRules.contains(loopCookie)) {
            log.info("Loop rule with cookie {} is to be reinstalled on switch {}", loopCookie, switchId);
            Flow flow = getFlow(flowPath);
            EncapsulationResources encapsulationResources = getEncapsulationResources(flowPath, flow);
            if (flowPath.getSrcSwitch().getSwitchId().equals(switchId)) {
                boolean srcWithMultiTable = flowPath.getSegments().get(0).isSrcWithMultiTable();
                commands.add(flowCommandFactory.buildInstallIngressLoopFlow(flow, flowPath, encapsulationResources, srcWithMultiTable));
            } else {
                PathSegment lastSegment = flowPath.getSegments().get(flowPath.getSegments().size() - 1);
                boolean destWithMultiTable = lastSegment.isDestWithMultiTable();
                commands.add(flowCommandFactory.buildInstallTransitLoopFlow(flow, flowPath, lastSegment.getDestPort(), encapsulationResources, destWithMultiTable));
            }
        }
    });
    return commands;
}
Also used : FlowSegmentCookie(org.openkilda.model.cookie.FlowSegmentCookie) BaseFlow(org.openkilda.messaging.command.flow.BaseFlow) ArrayList(java.util.ArrayList) SwitchId(org.openkilda.model.SwitchId) PathSegment(org.openkilda.model.PathSegment) InstallServer42Flow(org.openkilda.messaging.command.flow.InstallServer42Flow) BaseFlow(org.openkilda.messaging.command.flow.BaseFlow) Flow(org.openkilda.model.Flow) RemoveFlow(org.openkilda.messaging.command.flow.RemoveFlow) BaseInstallFlow(org.openkilda.messaging.command.flow.BaseInstallFlow) InstallSharedFlow(org.openkilda.messaging.command.flow.InstallSharedFlow) EncapsulationResources(org.openkilda.wfm.share.flow.resources.EncapsulationResources) MirrorConfig(org.openkilda.model.MirrorConfig) SwitchProperties(org.openkilda.model.SwitchProperties)

Example 3 with EncapsulationResources

use of org.openkilda.wfm.share.flow.resources.EncapsulationResources in project open-kilda by telstra.

the class CommandBuilderImpl method buildGroupInstallContexts.

@Override
public List<GroupInstallContext> buildGroupInstallContexts(SwitchId switchId, List<Integer> groupIds) {
    List<GroupInstallContext> groupInstallContexts = new ArrayList<>();
    Map<PathId, MirrorGroup> mirrorGroups = new HashMap<>();
    groupIds.stream().map(GroupId::new).map(mirrorGroupRepository::findByGroupId).flatMap(o -> o.map(Stream::of).orElseGet(Stream::empty)).forEach(mirrorGroup -> mirrorGroups.put(mirrorGroup.getPathId(), mirrorGroup));
    flowPathRepository.findBySegmentDestSwitch(switchId).forEach(flowPath -> {
        if (mirrorGroups.containsKey(flowPath.getPathId())) {
            PathSegment segment = flowPath.getSegments().stream().filter(pathSegment -> pathSegment.getDestSwitchId().equals(switchId)).findAny().orElseThrow(() -> new IllegalStateException(format("PathSegment not found, path %s, switch %s", flowPath, switchId)));
            Flow flow = flowPath.getFlow();
            if (segment.getDestSwitchId().equals(flowPath.getDestSwitchId())) {
                MirrorConfig mirrorConfig = makeMirrorConfig(flowPath, flow.getDestSwitchId(), flow.getDestPort());
                groupInstallContexts.add(GroupInstallContext.builder().mirrorConfig(mirrorConfig).build());
                log.info("Group {} is to be (re)installed on switch {}", mirrorConfig.getGroupId(), switchId);
            }
        }
    });
    flowPathRepository.findByEndpointSwitch(switchId).forEach(flowPath -> {
        if (mirrorGroups.containsKey(flowPath.getPathId())) {
            Flow flow = getFlow(flowPath);
            if (flowPath.isOneSwitchFlow()) {
                SwitchId swId = flowPath.isForward() ? flow.getDestSwitchId() : flow.getSrcSwitchId();
                int port = flowPath.isForward() ? flow.getDestPort() : flow.getSrcPort();
                MirrorConfig mirrorConfig = makeMirrorConfig(flowPath, swId, port);
                groupInstallContexts.add(GroupInstallContext.builder().mirrorConfig(mirrorConfig).build());
                log.info("Group {} is to be (re)installed on switch {}", mirrorConfig.getGroupId(), switchId);
            } else if (flowPath.getSrcSwitchId().equals(switchId)) {
                if (flowPath.getSegments().isEmpty()) {
                    log.warn("Output port was not found for mirror config");
                } else {
                    PathSegment foundIngressSegment = flowPath.getSegments().get(0);
                    MirrorConfig mirrorConfig = makeMirrorConfig(flowPath, foundIngressSegment.getSrcSwitchId(), foundIngressSegment.getSrcPort());
                    EncapsulationResources encapsulation = getEncapsulationResources(flowPath, flow);
                    groupInstallContexts.add(GroupInstallContext.builder().mirrorConfig(mirrorConfig).encapsulation(new FlowTransitEncapsulation(encapsulation.getTransitEncapsulationId(), encapsulation.getEncapsulationType())).egressSwitchId(flowPath.getDestSwitchId()).build());
                    log.info("Group {} is to be (re)installed on switch {}", mirrorConfig.getGroupId(), switchId);
                }
            }
        }
    });
    return groupInstallContexts;
}
Also used : ModifyDefaultMeterForSwitchManagerRequest(org.openkilda.messaging.command.flow.ModifyDefaultMeterForSwitchManagerRequest) MirrorConfig(org.openkilda.model.MirrorConfig) NoArgGenerator(com.fasterxml.uuid.NoArgGenerator) EncapsulationResources(org.openkilda.wfm.share.flow.resources.EncapsulationResources) InstallServer42Flow(org.openkilda.messaging.command.flow.InstallServer42Flow) DeleteRulesCriteria(org.openkilda.messaging.command.switches.DeleteRulesCriteria) FlowPath(org.openkilda.model.FlowPath) BaseFlow(org.openkilda.messaging.command.flow.BaseFlow) FlowResourcesConfig(org.openkilda.wfm.share.flow.resources.FlowResourcesConfig) SERVER_42_FLOW_RTT_OUTPUT_VXLAN_COOKIE(org.openkilda.model.cookie.Cookie.SERVER_42_FLOW_RTT_OUTPUT_VXLAN_COOKIE) NumberUtils(org.apache.commons.lang.math.NumberUtils) LogicalPortInfoEntry(org.openkilda.messaging.info.switches.LogicalPortInfoEntry) ReinstallDefaultFlowForSwitchManagerRequest(org.openkilda.messaging.command.flow.ReinstallDefaultFlowForSwitchManagerRequest) Flow(org.openkilda.model.Flow) MirrorGroup(org.openkilda.model.MirrorGroup) Map(java.util.Map) FlowRepository(org.openkilda.persistence.repositories.FlowRepository) ModifyFlowMeterForSwitchManagerRequest(org.openkilda.messaging.command.flow.ModifyFlowMeterForSwitchManagerRequest) RemoveFlow(org.openkilda.messaging.command.flow.RemoveFlow) DeleteLogicalPortRequest(org.openkilda.messaging.command.grpc.DeleteLogicalPortRequest) SwitchProperties(org.openkilda.model.SwitchProperties) IpSocketAddress(org.openkilda.model.IpSocketAddress) FlowPathRepository(org.openkilda.persistence.repositories.FlowPathRepository) NonNull(lombok.NonNull) FlowMirrorPoints(org.openkilda.model.FlowMirrorPoints) Set(java.util.Set) CookieType(org.openkilda.model.cookie.CookieBase.CookieType) InstallServer42FlowBuilder(org.openkilda.messaging.command.flow.InstallServer42Flow.InstallServer42FlowBuilder) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) ReinstallServer42FlowForSwitchManagerRequest(org.openkilda.messaging.command.flow.ReinstallServer42FlowForSwitchManagerRequest) MeterInfoEntry(org.openkilda.messaging.info.switches.MeterInfoEntry) SwitchPropertiesRepository(org.openkilda.persistence.repositories.SwitchPropertiesRepository) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Stream(java.util.stream.Stream) FlowInstructions(org.openkilda.messaging.info.rule.FlowInstructions) Optional(java.util.Optional) FlowCommandFactory(org.openkilda.wfm.share.flow.service.FlowCommandFactory) SwitchRepository(org.openkilda.persistence.repositories.SwitchRepository) PathSegment(org.openkilda.model.PathSegment) FlowTransitEncapsulation(org.openkilda.model.FlowTransitEncapsulation) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SwitchNotFoundException(org.openkilda.wfm.topology.switchmanager.error.SwitchNotFoundException) BaseInstallFlow(org.openkilda.messaging.command.flow.BaseInstallFlow) CreateLogicalPortRequest(org.openkilda.messaging.command.grpc.CreateLogicalPortRequest) Cookie(org.openkilda.model.cookie.Cookie) LogicalPortMapper(org.openkilda.wfm.topology.switchmanager.mappers.LogicalPortMapper) FlowSegmentCookie(org.openkilda.model.cookie.FlowSegmentCookie) PersistenceManager(org.openkilda.persistence.PersistenceManager) CommandBuilder(org.openkilda.wfm.topology.switchmanager.service.CommandBuilder) PathId(org.openkilda.model.PathId) SERVER_42_FLOW_RTT_OUTPUT_VLAN_COOKIE(org.openkilda.model.cookie.Cookie.SERVER_42_FLOW_RTT_OUTPUT_VLAN_COOKIE) SharedSegmentType(org.openkilda.model.cookie.FlowSharedSegmentCookie.SharedSegmentType) FlowEncapsulationType(org.openkilda.model.FlowEncapsulationType) Switch(org.openkilda.model.Switch) GroupInstallContext(org.openkilda.wfm.topology.switchmanager.model.GroupInstallContext) SERVER_42_ISL_RTT_OUTPUT_COOKIE(org.openkilda.model.cookie.Cookie.SERVER_42_ISL_RTT_OUTPUT_COOKIE) FlowEntry(org.openkilda.messaging.info.rule.FlowEntry) PortColourCookie(org.openkilda.model.cookie.PortColourCookie) FlowSharedSegmentCookie(org.openkilda.model.cookie.FlowSharedSegmentCookie) FlowApplyActions(org.openkilda.messaging.info.rule.FlowApplyActions) MirrorGroupRepository(org.openkilda.persistence.repositories.MirrorGroupRepository) InstallSharedFlow(org.openkilda.messaging.command.flow.InstallSharedFlow) SwitchId(org.openkilda.model.SwitchId) MirrorConfigData(org.openkilda.model.MirrorConfig.MirrorConfigData) FlowResourcesManager(org.openkilda.wfm.share.flow.resources.FlowResourcesManager) Generators(com.fasterxml.uuid.Generators) VisibleForTesting(com.google.common.annotations.VisibleForTesting) FlowMatchField(org.openkilda.messaging.info.rule.FlowMatchField) GroupId(org.openkilda.model.GroupId) Collections(java.util.Collections) MirrorGroup(org.openkilda.model.MirrorGroup) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FlowTransitEncapsulation(org.openkilda.model.FlowTransitEncapsulation) SwitchId(org.openkilda.model.SwitchId) PathSegment(org.openkilda.model.PathSegment) InstallServer42Flow(org.openkilda.messaging.command.flow.InstallServer42Flow) BaseFlow(org.openkilda.messaging.command.flow.BaseFlow) Flow(org.openkilda.model.Flow) RemoveFlow(org.openkilda.messaging.command.flow.RemoveFlow) BaseInstallFlow(org.openkilda.messaging.command.flow.BaseInstallFlow) InstallSharedFlow(org.openkilda.messaging.command.flow.InstallSharedFlow) PathId(org.openkilda.model.PathId) GroupInstallContext(org.openkilda.wfm.topology.switchmanager.model.GroupInstallContext) EncapsulationResources(org.openkilda.wfm.share.flow.resources.EncapsulationResources) MirrorConfig(org.openkilda.model.MirrorConfig) Stream(java.util.stream.Stream)

Example 4 with EncapsulationResources

use of org.openkilda.wfm.share.flow.resources.EncapsulationResources in project open-kilda by telstra.

the class CommandBuilderImpl method buildInstallCommandFromSegment.

private List<BaseInstallFlow> buildInstallCommandFromSegment(FlowPath flowPath, PathSegment segment, boolean switchRulesContainsFlowPathCookie, boolean switchRulesContainsMirrorCookie) {
    if (segment.getSrcSwitchId().equals(segment.getDestSwitchId())) {
        log.warn("One-switch flow segment {} is provided", flowPath.getCookie());
        return new ArrayList<>();
    }
    Optional<Flow> foundFlow = flowRepository.findById(flowPath.getFlow().getFlowId());
    if (!foundFlow.isPresent()) {
        log.warn("Flow with id {} was not found", flowPath.getFlow().getFlowId());
        return new ArrayList<>();
    }
    Flow flow = foundFlow.get();
    EncapsulationResources encapsulationResources = getEncapsulationResources(flowPath, flow);
    if (segment.getDestSwitchId().equals(flowPath.getDestSwitchId())) {
        List<BaseInstallFlow> commands = new ArrayList<>();
        if (switchRulesContainsMirrorCookie) {
            MirrorConfig mirrorConfig = makeMirrorConfig(flowPath, flow.getDestSwitchId(), flow.getDestPort());
            commands.add(flowCommandFactory.buildInstallEgressMirrorFlow(flowPath, segment.getDestPort(), encapsulationResources, segment.isDestWithMultiTable(), mirrorConfig));
        }
        if (switchRulesContainsFlowPathCookie) {
            commands.add(flowCommandFactory.buildInstallEgressFlow(flowPath, segment.getDestPort(), encapsulationResources, segment.isDestWithMultiTable()));
        }
        return commands;
    } else {
        int segmentIdx = flowPath.getSegments().indexOf(segment);
        if (segmentIdx < 0 || segmentIdx + 1 == flowPath.getSegments().size()) {
            log.warn("Paired segment for switch {} and cookie {} has not been found", segment.getDestSwitchId(), flowPath.getCookie());
            return new ArrayList<>();
        }
        PathSegment foundPairedFlowSegment = flowPath.getSegments().get(segmentIdx + 1);
        return Collections.singletonList(flowCommandFactory.buildInstallTransitFlow(flowPath, segment.getDestSwitchId(), segment.getDestPort(), foundPairedFlowSegment.getSrcPort(), encapsulationResources, segment.isDestWithMultiTable()));
    }
}
Also used : EncapsulationResources(org.openkilda.wfm.share.flow.resources.EncapsulationResources) MirrorConfig(org.openkilda.model.MirrorConfig) ArrayList(java.util.ArrayList) BaseInstallFlow(org.openkilda.messaging.command.flow.BaseInstallFlow) PathSegment(org.openkilda.model.PathSegment) InstallServer42Flow(org.openkilda.messaging.command.flow.InstallServer42Flow) BaseFlow(org.openkilda.messaging.command.flow.BaseFlow) Flow(org.openkilda.model.Flow) RemoveFlow(org.openkilda.messaging.command.flow.RemoveFlow) BaseInstallFlow(org.openkilda.messaging.command.flow.BaseInstallFlow) InstallSharedFlow(org.openkilda.messaging.command.flow.InstallSharedFlow)

Aggregations

EncapsulationResources (org.openkilda.wfm.share.flow.resources.EncapsulationResources)4 ArrayList (java.util.ArrayList)3 BaseFlow (org.openkilda.messaging.command.flow.BaseFlow)3 BaseInstallFlow (org.openkilda.messaging.command.flow.BaseInstallFlow)3 InstallServer42Flow (org.openkilda.messaging.command.flow.InstallServer42Flow)3 InstallSharedFlow (org.openkilda.messaging.command.flow.InstallSharedFlow)3 RemoveFlow (org.openkilda.messaging.command.flow.RemoveFlow)3 Flow (org.openkilda.model.Flow)3 MirrorConfig (org.openkilda.model.MirrorConfig)3 PathSegment (org.openkilda.model.PathSegment)3 FlowPath (org.openkilda.model.FlowPath)2 SwitchId (org.openkilda.model.SwitchId)2 SwitchProperties (org.openkilda.model.SwitchProperties)2 FlowSegmentCookie (org.openkilda.model.cookie.FlowSegmentCookie)2 Generators (com.fasterxml.uuid.Generators)1 NoArgGenerator (com.fasterxml.uuid.NoArgGenerator)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 String.format (java.lang.String.format)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1