Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.p4runtime.ctl.codec; |
| 18 | |
| 19 | import org.onosproject.net.PortNumber; |
| 20 | import org.onosproject.net.pi.model.PiPipeconf; |
| 21 | import org.onosproject.net.pi.runtime.PiPreReplica; |
| 22 | import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser; |
| 23 | import p4.v1.P4RuntimeOuterClass; |
| 24 | |
| 25 | import static java.lang.String.format; |
| 26 | |
| 27 | /** |
| 28 | * Codec for P4Runtime PRE Replica. |
| 29 | */ |
| 30 | public 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 | } |