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