blob: 5f55c1fc50e93647e3286c9bdccaeda56b6d4526 [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.p4runtime.ctl;
18
19import org.onosproject.net.PortNumber;
20import org.onosproject.net.pi.runtime.PiMulticastGroupEntry;
21import org.onosproject.net.pi.runtime.PiPreReplica;
22import p4.v1.P4RuntimeOuterClass.MulticastGroupEntry;
23import p4.v1.P4RuntimeOuterClass.Replica;
24
Carmelo Cascone03ae0ac2018-10-11 08:31:59 -070025import static java.lang.String.format;
26
Carmelo Cascone58136812018-07-19 03:40:16 +020027/**
28 * A coded of {@link PiMulticastGroupEntry} to P4Runtime MulticastGroupEntry
29 * messages, and vice versa.
30 */
31final 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 Cascone99c59db2019-01-17 15:39:35 -080043 * @throws CodecException if the PiMulticastGroupEntry cannot be encoded.
Carmelo Cascone58136812018-07-19 03:40:16 +020044 */
Carmelo Cascone99c59db2019-01-17 15:39:35 -080045 static MulticastGroupEntry encode(PiMulticastGroupEntry piEntry) throws CodecException {
Carmelo Cascone58136812018-07-19 03:40:16 +020046 final MulticastGroupEntry.Builder msgBuilder = MulticastGroupEntry.newBuilder();
47 msgBuilder.setMulticastGroupId(piEntry.groupId());
Carmelo Cascone03ae0ac2018-10-11 08:31:59 -070048 for (PiPreReplica replica : piEntry.replicas()) {
49 final int p4PortId;
50 try {
51 p4PortId = Math.toIntExact(replica.egressPort().toLong());
52 } catch (ArithmeticException e) {
Carmelo Cascone99c59db2019-01-17 15:39:35 -080053 throw new CodecException(format(
Carmelo Cascone03ae0ac2018-10-11 08:31:59 -070054 "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 Cascone58136812018-07-19 03:40:16 +020063 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}