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 | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 29 | import org.onosproject.net.pi.runtime.PiCloneSessionEntry; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 30 | import org.onosproject.net.pi.runtime.PiMulticastGroupEntry; |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 31 | import org.onosproject.net.pi.runtime.PiPreEntry; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 32 | import org.onosproject.net.pi.runtime.PiPreReplica; |
| 33 | import org.onosproject.net.pi.service.PiTranslationException; |
| 34 | |
| 35 | import java.util.Collection; |
| 36 | import java.util.List; |
| 37 | import java.util.Map; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 38 | import java.util.Optional; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 39 | import java.util.Set; |
| 40 | import java.util.stream.Collectors; |
| 41 | |
| 42 | import static com.google.common.base.Preconditions.checkNotNull; |
| 43 | import static java.lang.String.format; |
| 44 | |
| 45 | /** |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 46 | * Implementation of replication group translation logic. |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 47 | */ |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 48 | final class PiReplicationGroupTranslatorImpl { |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 49 | |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 50 | private PiReplicationGroupTranslatorImpl() { |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 51 | // Hides constructor. |
| 52 | } |
| 53 | |
| 54 | /** |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 55 | * Returns a PI PRE entry equivalent to the given group, for the given |
| 56 | * pipeconf and device. |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 57 | * <p> |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 58 | * The passed group is expected to have type {@link GroupDescription.Type#ALL} |
| 59 | * or {@link GroupDescription.Type#CLONE}. |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 60 | * |
| 61 | * @param group group |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 62 | * @param pipeconf pipeconf |
| 63 | * @param device device |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 64 | * @return PI PRE entry |
| 65 | * @throws PiTranslationException if the group cannot be translated |
| 66 | */ |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 67 | static PiPreEntry translate(Group group, PiPipeconf pipeconf, Device device) |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 68 | throws PiTranslationException { |
| 69 | |
| 70 | checkNotNull(group); |
| 71 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 72 | final List<Instruction> instructions = group.buckets().buckets().stream() |
| 73 | .flatMap(b -> b.treatment().allInstructions().stream()) |
| 74 | .collect(Collectors.toList()); |
| 75 | |
| 76 | final boolean hasNonOutputInstr = instructions.stream() |
| 77 | .anyMatch(i -> !i.type().equals(Instruction.Type.OUTPUT)); |
| 78 | |
| 79 | if (instructions.size() != group.buckets().buckets().size() |
| 80 | || hasNonOutputInstr) { |
| 81 | throw new PiTranslationException( |
| 82 | "support only groups with just one OUTPUT instruction per bucket"); |
| 83 | } |
| 84 | |
| 85 | final List<OutputInstruction> outInstructions = instructions.stream() |
| 86 | .map(i -> (OutputInstruction) i) |
| 87 | .collect(Collectors.toList()); |
| 88 | |
Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 89 | switch (group.type()) { |
| 90 | case ALL: |
| 91 | return PiMulticastGroupEntry.builder() |
| 92 | .withGroupId(group.id().id()) |
| 93 | .addReplicas(getReplicas(outInstructions, device)) |
| 94 | .build(); |
| 95 | case CLONE: |
| 96 | return PiCloneSessionEntry.builder() |
| 97 | .withSessionId(group.id().id()) |
| 98 | .addReplicas(getReplicas(outInstructions, device)) |
| 99 | .build(); |
| 100 | default: |
| 101 | throw new PiTranslationException(format( |
| 102 | "group type %s not supported", group.type())); |
| 103 | } |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 106 | private static Set<PiPreReplica> getReplicas( |
| 107 | Collection<OutputInstruction> instructions, Device device) |
| 108 | throws PiTranslationException { |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 109 | // Account for multiple replicas for the same port. |
| 110 | final Map<PortNumber, Set<PiPreReplica>> replicaMap = Maps.newHashMap(); |
| 111 | final List<PortNumber> ports = instructions.stream() |
| 112 | .map(OutputInstruction::port) |
| 113 | .collect(Collectors.toList()); |
| 114 | for (PortNumber port : ports) { |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 115 | if (port.isLogical()) { |
| 116 | port = logicalToPipelineSpecific(port, device); |
| 117 | } |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 118 | // Use incremental instance IDs for replicas of the same port. |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 119 | replicaMap.putIfAbsent(port, Sets.newHashSet()); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 120 | replicaMap.get(port).add( |
| 121 | new PiPreReplica(port, replicaMap.get(port).size() + 1)); |
| 122 | } |
| 123 | return replicaMap.values().stream() |
| 124 | .flatMap(Collection::stream) |
| 125 | .collect(Collectors.toSet()); |
| 126 | } |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 127 | |
| 128 | private static PortNumber logicalToPipelineSpecific( |
| 129 | PortNumber logicalPort, Device device) |
| 130 | throws PiTranslationException { |
| 131 | if (!device.is(PiPipelineInterpreter.class)) { |
| 132 | throw new PiTranslationException( |
| 133 | "missing interpreter, cannot map logical port " + logicalPort.toString()); |
| 134 | } |
| 135 | final PiPipelineInterpreter interpreter = device.as(PiPipelineInterpreter.class); |
| 136 | Optional<Integer> mappedPort = interpreter.mapLogicalPortNumber(logicalPort); |
| 137 | if (!mappedPort.isPresent()) { |
| 138 | throw new PiTranslationException( |
| 139 | "interpreter cannot map logical port " + logicalPort.toString()); |
| 140 | } |
| 141 | return PortNumber.portNumber(mappedPort.get()); |
| 142 | } |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 143 | } |