blob: 315e2697cea1661d18e3dff1f4f6affb3c04d8a7 [file] [log] [blame]
Carmelo Cascone9db4d5c2019-04-16 17:36:33 -07001/*
2 * Copyright 2019-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.codec;
18
Yi Tsengbac5f692022-02-03 15:33:55 -080019import com.google.common.primitives.UnsignedInteger;
Carmelo Cascone9db4d5c2019-04-16 17:36:33 -070020import org.onosproject.net.PortNumber;
21import org.onosproject.net.pi.model.PiPipeconf;
22import org.onosproject.net.pi.runtime.PiPreReplica;
23import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser;
24import p4.v1.P4RuntimeOuterClass;
25
26import static java.lang.String.format;
27
28/**
29 * Codec for P4Runtime PRE Replica.
30 */
31public class PreReplicaCodec extends AbstractCodec<PiPreReplica,
32 P4RuntimeOuterClass.Replica, Object> {
33
34 @Override
35 protected P4RuntimeOuterClass.Replica encode(
36 PiPreReplica replica, Object ignore,
37 PiPipeconf pipeconf, P4InfoBrowser browser)
38 throws CodecException, P4InfoBrowser.NotFoundException {
39 final int p4PortId;
40 try {
Yi Tsengbac5f692022-02-03 15:33:55 -080041 UnsignedInteger egressPort = UnsignedInteger.valueOf(replica.egressPort().toLong());
42 p4PortId = egressPort.intValue();
43 } catch (IllegalArgumentException e) {
Carmelo Cascone9db4d5c2019-04-16 17:36:33 -070044 throw new CodecException(format(
45 "Cannot cast 64 bit port value '%s' to 32 bit",
46 replica.egressPort()));
47 }
48 return P4RuntimeOuterClass.Replica.newBuilder()
49 .setEgressPort(p4PortId)
50 .setInstance(replica.instanceId())
51 .build();
52 }
53
54 @Override
55 protected PiPreReplica decode(
56 P4RuntimeOuterClass.Replica message, Object ignore,
57 PiPipeconf pipeconf, P4InfoBrowser browser)
58 throws CodecException, P4InfoBrowser.NotFoundException {
59 return new PiPreReplica(
60 PortNumber.portNumber(message.getEgressPort()),
61 message.getInstance());
62 }
63}