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 | 787a064 | 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 | |
Yi Tseng | 3821399 | 2022-03-17 16:04:06 -0700 | [diff] [blame^] | 28 | import java.nio.ByteBuffer; |
| 29 | |
Daniele Moro | 8eb7c0a | 2021-05-17 14:49:31 +0200 | [diff] [blame] | 30 | import static org.onlab.util.ImmutableByteSequence.copyAndFit; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 31 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
| 32 | |
| 33 | /** |
| 34 | * Coded for P4Runtime PacketMetadata. The metadata is expected to be a Preamble |
| 35 | * of a P4Info.ControllerPacketMetadata message. |
| 36 | */ |
| 37 | public final class PacketMetadataCodec |
| 38 | extends AbstractCodec<PiPacketMetadata, |
| 39 | P4RuntimeOuterClass.PacketMetadata, P4InfoOuterClass.Preamble> { |
| 40 | |
| 41 | @Override |
| 42 | protected P4RuntimeOuterClass.PacketMetadata encode( |
| 43 | PiPacketMetadata piEntity, P4InfoOuterClass.Preamble ctrlPktMetaPreamble, |
| 44 | PiPipeconf pipeconf, P4InfoBrowser browser) |
| 45 | throws P4InfoBrowser.NotFoundException { |
Yi Tseng | 3821399 | 2022-03-17 16:04:06 -0700 | [diff] [blame^] | 46 | P4InfoOuterClass.ControllerPacketMetadata.Metadata packetMetadata = browser |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 47 | .packetMetadatas(ctrlPktMetaPreamble.getId()) |
Yi Tseng | 3821399 | 2022-03-17 16:04:06 -0700 | [diff] [blame^] | 48 | .getByName(piEntity.id().id()); |
| 49 | final ByteBuffer value; |
| 50 | if (browser.isTypeString(packetMetadata.getTypeName())) { |
| 51 | value = piEntity.value().asReadOnlyBuffer(); |
| 52 | } else { |
| 53 | value = piEntity.value().canonical().asReadOnlyBuffer(); |
| 54 | } |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 55 | return P4RuntimeOuterClass.PacketMetadata.newBuilder() |
Yi Tseng | 3821399 | 2022-03-17 16:04:06 -0700 | [diff] [blame^] | 56 | .setMetadataId(packetMetadata.getId()) |
| 57 | .setValue(ByteString.copyFrom(value)) |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 58 | .build(); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | protected PiPacketMetadata decode( |
| 63 | P4RuntimeOuterClass.PacketMetadata message, |
| 64 | P4InfoOuterClass.Preamble ctrlPktMetaPreamble, |
| 65 | PiPipeconf pipeconf, P4InfoBrowser browser) |
Daniele Moro | 8eb7c0a | 2021-05-17 14:49:31 +0200 | [diff] [blame] | 66 | throws P4InfoBrowser.NotFoundException, CodecException { |
| 67 | final P4InfoOuterClass.ControllerPacketMetadata.Metadata pktMeta = |
Daniele Moro | 787a064 | 2021-02-23 15:28:07 +0100 | [diff] [blame] | 68 | browser.packetMetadatas(ctrlPktMetaPreamble.getId()) |
Daniele Moro | 8eb7c0a | 2021-05-17 14:49:31 +0200 | [diff] [blame] | 69 | .getById(message.getMetadataId()); |
Daniele Moro | 787a064 | 2021-02-23 15:28:07 +0100 | [diff] [blame] | 70 | final ImmutableByteSequence value; |
Daniele Moro | 8eb7c0a | 2021-05-17 14:49:31 +0200 | [diff] [blame] | 71 | if (browser.isTypeString(pktMeta.getTypeName())) { |
Daniele Moro | 787a064 | 2021-02-23 15:28:07 +0100 | [diff] [blame] | 72 | value = copyFrom(new String(message.getValue().toByteArray())); |
| 73 | } else { |
Daniele Moro | 8eb7c0a | 2021-05-17 14:49:31 +0200 | [diff] [blame] | 74 | try { |
| 75 | value = copyAndFit(message.getValue().asReadOnlyByteBuffer(), |
| 76 | pktMeta.getBitwidth()); |
| 77 | } catch (ImmutableByteSequence.ByteSequenceTrimException e) { |
| 78 | throw new CodecException(e.getMessage()); |
| 79 | } |
Daniele Moro | 787a064 | 2021-02-23 15:28:07 +0100 | [diff] [blame] | 80 | } |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 81 | return PiPacketMetadata.builder() |
Daniele Moro | 8eb7c0a | 2021-05-17 14:49:31 +0200 | [diff] [blame] | 82 | .withId(PiPacketMetadataId.of(pktMeta.getName())) |
Daniele Moro | 787a064 | 2021-02-23 15:28:07 +0100 | [diff] [blame] | 83 | .withValue(value) |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 84 | .build(); |
| 85 | } |
| 86 | } |