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; |
| 21 | import org.onosproject.net.PortNumber; |
| 22 | import org.onosproject.net.flow.instructions.Instruction; |
| 23 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
| 24 | import org.onosproject.net.group.Group; |
| 25 | import org.onosproject.net.group.GroupDescription; |
| 26 | import org.onosproject.net.pi.runtime.PiMulticastGroupEntry; |
| 27 | import org.onosproject.net.pi.runtime.PiPreReplica; |
| 28 | import org.onosproject.net.pi.service.PiTranslationException; |
| 29 | |
| 30 | import java.util.Collection; |
| 31 | import java.util.List; |
| 32 | import java.util.Map; |
| 33 | import java.util.Set; |
| 34 | import java.util.stream.Collectors; |
| 35 | |
| 36 | import static com.google.common.base.Preconditions.checkNotNull; |
| 37 | import static java.lang.String.format; |
| 38 | |
| 39 | /** |
| 40 | * Implementation of multicast group translation logic. |
| 41 | */ |
| 42 | final class PiMulticastGroupTranslatorImpl { |
| 43 | |
| 44 | private PiMulticastGroupTranslatorImpl() { |
| 45 | // Hides constructor. |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Returns a PI PRE multicast group entry equivalent to the given group, for |
| 50 | * the given pipeconf and device. |
| 51 | * <p> |
| 52 | * The passed group is expected to have type {@link GroupDescription.Type#ALL}. |
| 53 | * |
| 54 | * @param group group |
| 55 | * @return PI PRE entry |
| 56 | * @throws PiTranslationException if the group cannot be translated |
| 57 | */ |
| 58 | static PiMulticastGroupEntry translate(Group group) |
| 59 | throws PiTranslationException { |
| 60 | |
| 61 | checkNotNull(group); |
| 62 | |
| 63 | if (!group.type().equals(GroupDescription.Type.ALL)) { |
| 64 | throw new PiTranslationException(format( |
| 65 | "group type %s not supported", group.type())); |
| 66 | } |
| 67 | |
| 68 | final List<Instruction> instructions = group.buckets().buckets().stream() |
| 69 | .flatMap(b -> b.treatment().allInstructions().stream()) |
| 70 | .collect(Collectors.toList()); |
| 71 | |
| 72 | final boolean hasNonOutputInstr = instructions.stream() |
| 73 | .anyMatch(i -> !i.type().equals(Instruction.Type.OUTPUT)); |
| 74 | |
| 75 | if (instructions.size() != group.buckets().buckets().size() |
| 76 | || hasNonOutputInstr) { |
| 77 | throw new PiTranslationException( |
| 78 | "support only groups with just one OUTPUT instruction per bucket"); |
| 79 | } |
| 80 | |
| 81 | final List<OutputInstruction> outInstructions = instructions.stream() |
| 82 | .map(i -> (OutputInstruction) i) |
| 83 | .collect(Collectors.toList()); |
| 84 | |
| 85 | return PiMulticastGroupEntry.builder() |
| 86 | .withGroupId(group.id().id()) |
| 87 | .addReplicas(getReplicas(outInstructions)) |
| 88 | .build(); |
| 89 | } |
| 90 | |
| 91 | private static Set<PiPreReplica> getReplicas(Collection<OutputInstruction> instructions) { |
| 92 | // Account for multiple replicas for the same port. |
| 93 | final Map<PortNumber, Set<PiPreReplica>> replicaMap = Maps.newHashMap(); |
| 94 | final List<PortNumber> ports = instructions.stream() |
| 95 | .map(OutputInstruction::port) |
| 96 | .collect(Collectors.toList()); |
| 97 | for (PortNumber port : ports) { |
| 98 | replicaMap.putIfAbsent(port, Sets.newHashSet()); |
| 99 | // Use incremental instance IDs for replicas of the same port. |
| 100 | replicaMap.get(port).add( |
| 101 | new PiPreReplica(port, replicaMap.get(port).size() + 1)); |
| 102 | } |
| 103 | return replicaMap.values().stream() |
| 104 | .flatMap(Collection::stream) |
| 105 | .collect(Collectors.toSet()); |
| 106 | } |
| 107 | } |