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 com.google.protobuf.ByteString; |
Daniele Moro | 7aa13e6 | 2021-02-23 15:28:07 +0100 | [diff] [blame^] | 20 | import org.onlab.util.ImmutableByteSequence; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 21 | import org.onosproject.net.pi.model.PiPacketMetadataId; |
| 22 | import org.onosproject.net.pi.model.PiPipeconf; |
| 23 | import org.onosproject.net.pi.runtime.PiPacketMetadata; |
| 24 | import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser; |
| 25 | import p4.config.v1.P4InfoOuterClass; |
| 26 | import p4.v1.P4RuntimeOuterClass; |
| 27 | |
| 28 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
| 29 | |
| 30 | /** |
| 31 | * Coded for P4Runtime PacketMetadata. The metadata is expected to be a Preamble |
| 32 | * of a P4Info.ControllerPacketMetadata message. |
| 33 | */ |
| 34 | public final class PacketMetadataCodec |
| 35 | extends AbstractCodec<PiPacketMetadata, |
| 36 | P4RuntimeOuterClass.PacketMetadata, P4InfoOuterClass.Preamble> { |
| 37 | |
| 38 | @Override |
| 39 | protected P4RuntimeOuterClass.PacketMetadata encode( |
| 40 | PiPacketMetadata piEntity, P4InfoOuterClass.Preamble ctrlPktMetaPreamble, |
| 41 | PiPipeconf pipeconf, P4InfoBrowser browser) |
| 42 | throws P4InfoBrowser.NotFoundException { |
| 43 | final int metadataId = browser |
| 44 | .packetMetadatas(ctrlPktMetaPreamble.getId()) |
| 45 | .getByName(piEntity.id().id()).getId(); |
| 46 | return P4RuntimeOuterClass.PacketMetadata.newBuilder() |
| 47 | .setMetadataId(metadataId) |
| 48 | .setValue(ByteString.copyFrom(piEntity.value().asReadOnlyBuffer())) |
| 49 | .build(); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | protected PiPacketMetadata decode( |
| 54 | P4RuntimeOuterClass.PacketMetadata message, |
| 55 | P4InfoOuterClass.Preamble ctrlPktMetaPreamble, |
| 56 | PiPipeconf pipeconf, P4InfoBrowser browser) |
| 57 | throws P4InfoBrowser.NotFoundException { |
Daniele Moro | 7aa13e6 | 2021-02-23 15:28:07 +0100 | [diff] [blame^] | 58 | final P4InfoOuterClass.ControllerPacketMetadata.Metadata packetMetadata = |
| 59 | browser.packetMetadatas(ctrlPktMetaPreamble.getId()) |
| 60 | .getById(message.getMetadataId()); |
| 61 | final ImmutableByteSequence value; |
| 62 | if (browser.isTypeString(packetMetadata.getTypeName())) { |
| 63 | value = copyFrom(new String(message.getValue().toByteArray())); |
| 64 | } else { |
| 65 | value = copyFrom(message.getValue().asReadOnlyByteBuffer()); |
| 66 | } |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 67 | return PiPacketMetadata.builder() |
Daniele Moro | 7aa13e6 | 2021-02-23 15:28:07 +0100 | [diff] [blame^] | 68 | .withId(PiPacketMetadataId.of(packetMetadata.getName())) |
| 69 | .withValue(value) |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 70 | .build(); |
| 71 | } |
| 72 | } |