blob: 3d524c7dbd6db05f1002e6a4ad05499fc2ea804d [file] [log] [blame]
Carmelo Cascone58136812018-07-19 03:40:16 +02001/*
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
17package org.onosproject.net.pi.impl;
18
19import com.google.common.collect.Maps;
20import com.google.common.collect.Sets;
21import org.onosproject.net.PortNumber;
22import org.onosproject.net.flow.instructions.Instruction;
23import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
24import org.onosproject.net.group.Group;
25import org.onosproject.net.group.GroupDescription;
26import org.onosproject.net.pi.runtime.PiMulticastGroupEntry;
27import org.onosproject.net.pi.runtime.PiPreReplica;
28import org.onosproject.net.pi.service.PiTranslationException;
29
30import java.util.Collection;
31import java.util.List;
32import java.util.Map;
33import java.util.Set;
34import java.util.stream.Collectors;
35
36import static com.google.common.base.Preconditions.checkNotNull;
37import static java.lang.String.format;
38
39/**
40 * Implementation of multicast group translation logic.
41 */
42final 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}