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.p4runtime.ctl; |
| 18 | |
| 19 | import org.onosproject.net.PortNumber; |
| 20 | import org.onosproject.net.pi.runtime.PiMulticastGroupEntry; |
| 21 | import org.onosproject.net.pi.runtime.PiPreReplica; |
| 22 | import p4.v1.P4RuntimeOuterClass.MulticastGroupEntry; |
| 23 | import p4.v1.P4RuntimeOuterClass.Replica; |
| 24 | |
Carmelo Cascone | 03ae0ac | 2018-10-11 08:31:59 -0700 | [diff] [blame] | 25 | import static java.lang.String.format; |
| 26 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 27 | /** |
| 28 | * A coded of {@link PiMulticastGroupEntry} to P4Runtime MulticastGroupEntry |
| 29 | * messages, and vice versa. |
| 30 | */ |
| 31 | final class MulticastGroupEntryCodec { |
| 32 | |
| 33 | private MulticastGroupEntryCodec() { |
| 34 | // Hides constructor. |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Returns a P4Runtime MulticastGroupEntry message equivalent to the given |
| 39 | * PiMulticastGroupEntry. |
| 40 | * |
| 41 | * @param piEntry PiMulticastGroupEntry |
| 42 | * @return P4Runtime MulticastGroupEntry message |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 43 | * @throws CodecException if the PiMulticastGroupEntry cannot be encoded. |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 44 | */ |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 45 | static MulticastGroupEntry encode(PiMulticastGroupEntry piEntry) throws CodecException { |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 46 | final MulticastGroupEntry.Builder msgBuilder = MulticastGroupEntry.newBuilder(); |
| 47 | msgBuilder.setMulticastGroupId(piEntry.groupId()); |
Carmelo Cascone | 03ae0ac | 2018-10-11 08:31:59 -0700 | [diff] [blame] | 48 | for (PiPreReplica replica : piEntry.replicas()) { |
| 49 | final int p4PortId; |
| 50 | try { |
| 51 | p4PortId = Math.toIntExact(replica.egressPort().toLong()); |
| 52 | } catch (ArithmeticException e) { |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 53 | throw new CodecException(format( |
Carmelo Cascone | 03ae0ac | 2018-10-11 08:31:59 -0700 | [diff] [blame] | 54 | "Cannot cast 64bit port value '%s' to 32bit", |
| 55 | replica.egressPort())); |
| 56 | } |
| 57 | msgBuilder.addReplicas( |
| 58 | Replica.newBuilder() |
| 59 | .setEgressPort(p4PortId) |
| 60 | .setInstance(replica.instanceId()) |
| 61 | .build()); |
| 62 | } |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 63 | return msgBuilder.build(); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns a PiMulticastGroupEntry equivalent to the given P4Runtime |
| 68 | * MulticastGroupEntry message. |
| 69 | * |
| 70 | * @param msg P4Runtime MulticastGroupEntry message |
| 71 | * @return PiMulticastGroupEntry |
| 72 | */ |
| 73 | static PiMulticastGroupEntry decode(MulticastGroupEntry msg) { |
| 74 | final PiMulticastGroupEntry.Builder piEntryBuilder = PiMulticastGroupEntry.builder(); |
| 75 | piEntryBuilder.withGroupId(msg.getMulticastGroupId()); |
| 76 | msg.getReplicasList().stream() |
| 77 | .map(r -> new PiPreReplica( |
| 78 | PortNumber.portNumber(r.getEgressPort()), r.getInstance())) |
| 79 | .forEach(piEntryBuilder::addReplica); |
| 80 | return piEntryBuilder.build(); |
| 81 | } |
| 82 | } |