Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [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.pi.model.PiPacketOperationType; |
| 20 | import org.onosproject.net.pi.model.PiPipeconf; |
| 21 | import org.onosproject.net.pi.runtime.PiPacketOperation; |
| 22 | import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser; |
| 23 | import p4.config.v1.P4InfoOuterClass; |
| 24 | import p4.v1.P4RuntimeOuterClass; |
| 25 | |
| 26 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
| 27 | import static org.onosproject.p4runtime.ctl.codec.Codecs.CODECS; |
| 28 | |
| 29 | /** |
| 30 | * Codec for P4Runtime PacketIn. Only decoding is supported, as encoding is not |
| 31 | * meaningful in this case (packet-ins are always generated by the server). |
| 32 | */ |
| 33 | public final class PacketInCodec |
| 34 | extends AbstractCodec<PiPacketOperation, |
| 35 | P4RuntimeOuterClass.PacketIn, Object> { |
| 36 | |
| 37 | private static final String PACKET_IN = "packet_in"; |
| 38 | |
| 39 | @Override |
| 40 | protected P4RuntimeOuterClass.PacketIn encode( |
| 41 | PiPacketOperation piEntity, Object ignored, PiPipeconf pipeconf, |
| 42 | P4InfoBrowser browser) |
| 43 | throws CodecException { |
| 44 | throw new CodecException("Encoding of packet-in is not supported"); |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | protected PiPacketOperation decode( |
| 49 | P4RuntimeOuterClass.PacketIn message, Object ignored, |
| 50 | PiPipeconf pipeconf, P4InfoBrowser browser) |
| 51 | throws CodecException, P4InfoBrowser.NotFoundException { |
| 52 | final P4InfoOuterClass.Preamble ctrlPktMetaPreamble = browser |
| 53 | .controllerPacketMetadatas() |
| 54 | .getByName(PACKET_IN) |
| 55 | .getPreamble(); |
| 56 | return PiPacketOperation.builder() |
| 57 | .withType(PiPacketOperationType.PACKET_IN) |
| 58 | .withMetadatas(CODECS.packetMetadata().decodeAll( |
| 59 | message.getMetadataList(), ctrlPktMetaPreamble, pipeconf)) |
| 60 | .withData(copyFrom(message.getPayload().asReadOnlyByteBuffer())) |
| 61 | .build(); |
| 62 | } |
| 63 | } |