blob: 7402b142c50e0e45a3a4b558bd8a6184e4925e44 [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
19import org.onosproject.net.PortNumber;
20import org.onosproject.net.pi.model.PiPipeconf;
21import org.onosproject.net.pi.runtime.PiPreReplica;
22import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser;
23import p4.v1.P4RuntimeOuterClass;
24
25import static java.lang.String.format;
26
27/**
28 * Codec for P4Runtime PRE Replica.
29 */
30public class PreReplicaCodec extends AbstractCodec<PiPreReplica,
31 P4RuntimeOuterClass.Replica, Object> {
32
33 @Override
34 protected P4RuntimeOuterClass.Replica encode(
35 PiPreReplica replica, Object ignore,
36 PiPipeconf pipeconf, P4InfoBrowser browser)
37 throws CodecException, P4InfoBrowser.NotFoundException {
38 final int p4PortId;
39 try {
40 p4PortId = Math.toIntExact(replica.egressPort().toLong());
41 } catch (ArithmeticException e) {
42 throw new CodecException(format(
43 "Cannot cast 64 bit port value '%s' to 32 bit",
44 replica.egressPort()));
45 }
46 return P4RuntimeOuterClass.Replica.newBuilder()
47 .setEgressPort(p4PortId)
48 .setInstance(replica.instanceId())
49 .build();
50 }
51
52 @Override
53 protected PiPreReplica decode(
54 P4RuntimeOuterClass.Replica message, Object ignore,
55 PiPipeconf pipeconf, P4InfoBrowser browser)
56 throws CodecException, P4InfoBrowser.NotFoundException {
57 return new PiPreReplica(
58 PortNumber.portNumber(message.getEgressPort()),
59 message.getInstance());
60 }
61}