blob: 24aa00e522dbfafb277b661965ebf2dcef2a3a43 [file] [log] [blame]
Carmelo Cascone4c289b72019-01-22 15:30:45 -08001/*
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
17package org.onosproject.p4runtime.ctl.codec;
18
19import com.google.protobuf.ByteString;
Daniele Moro787a0642021-02-23 15:28:07 +010020import org.onlab.util.ImmutableByteSequence;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080021import org.onosproject.net.pi.model.PiPacketMetadataId;
22import org.onosproject.net.pi.model.PiPipeconf;
23import org.onosproject.net.pi.runtime.PiPacketMetadata;
24import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser;
25import p4.config.v1.P4InfoOuterClass;
26import p4.v1.P4RuntimeOuterClass;
27
Yi Tseng38213992022-03-17 16:04:06 -070028import java.nio.ByteBuffer;
29
Daniele Moro8eb7c0a2021-05-17 14:49:31 +020030import static org.onlab.util.ImmutableByteSequence.copyAndFit;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080031import 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 */
37public 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 Tseng38213992022-03-17 16:04:06 -070046 P4InfoOuterClass.ControllerPacketMetadata.Metadata packetMetadata = browser
Carmelo Cascone4c289b72019-01-22 15:30:45 -080047 .packetMetadatas(ctrlPktMetaPreamble.getId())
Yi Tseng38213992022-03-17 16:04:06 -070048 .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 Cascone4c289b72019-01-22 15:30:45 -080055 return P4RuntimeOuterClass.PacketMetadata.newBuilder()
Yi Tseng38213992022-03-17 16:04:06 -070056 .setMetadataId(packetMetadata.getId())
57 .setValue(ByteString.copyFrom(value))
Carmelo Cascone4c289b72019-01-22 15:30:45 -080058 .build();
59 }
60
61 @Override
62 protected PiPacketMetadata decode(
63 P4RuntimeOuterClass.PacketMetadata message,
64 P4InfoOuterClass.Preamble ctrlPktMetaPreamble,
65 PiPipeconf pipeconf, P4InfoBrowser browser)
Daniele Moro8eb7c0a2021-05-17 14:49:31 +020066 throws P4InfoBrowser.NotFoundException, CodecException {
67 final P4InfoOuterClass.ControllerPacketMetadata.Metadata pktMeta =
Daniele Moro787a0642021-02-23 15:28:07 +010068 browser.packetMetadatas(ctrlPktMetaPreamble.getId())
Daniele Moro8eb7c0a2021-05-17 14:49:31 +020069 .getById(message.getMetadataId());
Daniele Moro787a0642021-02-23 15:28:07 +010070 final ImmutableByteSequence value;
Daniele Moro8eb7c0a2021-05-17 14:49:31 +020071 if (browser.isTypeString(pktMeta.getTypeName())) {
Daniele Moro787a0642021-02-23 15:28:07 +010072 value = copyFrom(new String(message.getValue().toByteArray()));
73 } else {
Daniele Moro8eb7c0a2021-05-17 14:49:31 +020074 try {
75 value = copyAndFit(message.getValue().asReadOnlyByteBuffer(),
76 pktMeta.getBitwidth());
77 } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
78 throw new CodecException(e.getMessage());
79 }
Daniele Moro787a0642021-02-23 15:28:07 +010080 }
Carmelo Cascone4c289b72019-01-22 15:30:45 -080081 return PiPacketMetadata.builder()
Daniele Moro8eb7c0a2021-05-17 14:49:31 +020082 .withId(PiPacketMetadataId.of(pktMeta.getName()))
Daniele Moro787a0642021-02-23 15:28:07 +010083 .withValue(value)
Carmelo Cascone4c289b72019-01-22 15:30:45 -080084 .build();
85 }
86}