blob: 876f84ce4d8ae3e1031eb6190c7262a870016a08 [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 Moro7aa13e62021-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
28import 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 */
34public 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 Moro7aa13e62021-02-23 15:28:07 +010058 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 Cascone4c289b72019-01-22 15:30:45 -080067 return PiPacketMetadata.builder()
Daniele Moro7aa13e62021-02-23 15:28:07 +010068 .withId(PiPacketMetadataId.of(packetMetadata.getName()))
69 .withValue(value)
Carmelo Cascone4c289b72019-01-22 15:30:45 -080070 .build();
71 }
72}