Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.net.pi.impl; |
| 18 | |
| 19 | import com.google.common.collect.Maps; |
| 20 | import com.google.common.collect.Sets; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 21 | import org.onosproject.net.Device; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 22 | import org.onosproject.net.PortNumber; |
| 23 | import org.onosproject.net.flow.instructions.Instruction; |
| 24 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
| 25 | import org.onosproject.net.group.Group; |
| 26 | import org.onosproject.net.group.GroupDescription; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 27 | import org.onosproject.net.pi.model.PiPipeconf; |
| 28 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 29 | import org.onosproject.net.pi.runtime.PiMulticastGroupEntry; |
| 30 | import org.onosproject.net.pi.runtime.PiPreReplica; |
| 31 | import org.onosproject.net.pi.service.PiTranslationException; |
| 32 | |
| 33 | import java.util.Collection; |
| 34 | import java.util.List; |
| 35 | import java.util.Map; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 36 | import java.util.Optional; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 37 | import java.util.Set; |
| 38 | import java.util.stream.Collectors; |
| 39 | |
| 40 | import static com.google.common.base.Preconditions.checkNotNull; |
| 41 | import static java.lang.String.format; |
| 42 | |
| 43 | /** |
| 44 | * Implementation of multicast group translation logic. |
| 45 | */ |
| 46 | final class PiMulticastGroupTranslatorImpl { |
| 47 | |
| 48 | private PiMulticastGroupTranslatorImpl() { |
| 49 | // Hides constructor. |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Returns a PI PRE multicast group entry equivalent to the given group, for |
| 54 | * the given pipeconf and device. |
| 55 | * <p> |
| 56 | * The passed group is expected to have type {@link GroupDescription.Type#ALL}. |
| 57 | * |
| 58 | * @param group group |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 59 | * @param pipeconf pipeconf |
| 60 | * @param device device |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 61 | * @return PI PRE entry |
| 62 | * @throws PiTranslationException if the group cannot be translated |
| 63 | */ |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 64 | static PiMulticastGroupEntry translate(Group group, PiPipeconf pipeconf, Device device) |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 65 | throws PiTranslationException { |
| 66 | |
| 67 | checkNotNull(group); |
| 68 | |
| 69 | if (!group.type().equals(GroupDescription.Type.ALL)) { |
| 70 | throw new PiTranslationException(format( |
| 71 | "group type %s not supported", group.type())); |
| 72 | } |
| 73 | |
| 74 | final List<Instruction> instructions = group.buckets().buckets().stream() |
| 75 | .flatMap(b -> b.treatment().allInstructions().stream()) |
| 76 | .collect(Collectors.toList()); |
| 77 | |
| 78 | final boolean hasNonOutputInstr = instructions.stream() |
| 79 | .anyMatch(i -> !i.type().equals(Instruction.Type.OUTPUT)); |
| 80 | |
| 81 | if (instructions.size() != group.buckets().buckets().size() |
| 82 | || hasNonOutputInstr) { |
| 83 | throw new PiTranslationException( |
| 84 | "support only groups with just one OUTPUT instruction per bucket"); |
| 85 | } |
| 86 | |
| 87 | final List<OutputInstruction> outInstructions = instructions.stream() |
| 88 | .map(i -> (OutputInstruction) i) |
| 89 | .collect(Collectors.toList()); |
| 90 | |
| 91 | return PiMulticastGroupEntry.builder() |
| 92 | .withGroupId(group.id().id()) |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 93 | .addReplicas(getReplicas(outInstructions, device)) |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 94 | .build(); |
| 95 | } |
| 96 | |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 97 | private static Set<PiPreReplica> getReplicas( |
| 98 | Collection<OutputInstruction> instructions, Device device) |
| 99 | throws PiTranslationException { |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 100 | // Account for multiple replicas for the same port. |
| 101 | final Map<PortNumber, Set<PiPreReplica>> replicaMap = Maps.newHashMap(); |
| 102 | final List<PortNumber> ports = instructions.stream() |
| 103 | .map(OutputInstruction::port) |
| 104 | .collect(Collectors.toList()); |
| 105 | for (PortNumber port : ports) { |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 106 | if (port.isLogical()) { |
| 107 | port = logicalToPipelineSpecific(port, device); |
| 108 | } |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 109 | // Use incremental instance IDs for replicas of the same port. |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 110 | replicaMap.putIfAbsent(port, Sets.newHashSet()); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 111 | replicaMap.get(port).add( |
| 112 | new PiPreReplica(port, replicaMap.get(port).size() + 1)); |
| 113 | } |
| 114 | return replicaMap.values().stream() |
| 115 | .flatMap(Collection::stream) |
| 116 | .collect(Collectors.toSet()); |
| 117 | } |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 118 | |
| 119 | private static PortNumber logicalToPipelineSpecific( |
| 120 | PortNumber logicalPort, Device device) |
| 121 | throws PiTranslationException { |
| 122 | if (!device.is(PiPipelineInterpreter.class)) { |
| 123 | throw new PiTranslationException( |
| 124 | "missing interpreter, cannot map logical port " + logicalPort.toString()); |
| 125 | } |
| 126 | final PiPipelineInterpreter interpreter = device.as(PiPipelineInterpreter.class); |
| 127 | Optional<Integer> mappedPort = interpreter.mapLogicalPortNumber(logicalPort); |
| 128 | if (!mappedPort.isPresent()) { |
| 129 | throw new PiTranslationException( |
| 130 | "interpreter cannot map logical port " + logicalPort.toString()); |
| 131 | } |
| 132 | return PortNumber.portNumber(mappedPort.get()); |
| 133 | } |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 134 | } |