blob: 3f78085fdb7f1725beecc7124a1e92fa919b5e6a [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;
20import org.onosproject.net.pi.model.PiPacketMetadataId;
21import org.onosproject.net.pi.model.PiPipeconf;
22import org.onosproject.net.pi.runtime.PiPacketMetadata;
23import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser;
24import p4.config.v1.P4InfoOuterClass;
25import p4.v1.P4RuntimeOuterClass;
26
27import static org.onlab.util.ImmutableByteSequence.copyFrom;
28
29/**
30 * Coded for P4Runtime PacketMetadata. The metadata is expected to be a Preamble
31 * of a P4Info.ControllerPacketMetadata message.
32 */
33public final class PacketMetadataCodec
34 extends AbstractCodec<PiPacketMetadata,
35 P4RuntimeOuterClass.PacketMetadata, P4InfoOuterClass.Preamble> {
36
37 @Override
38 protected P4RuntimeOuterClass.PacketMetadata encode(
39 PiPacketMetadata piEntity, P4InfoOuterClass.Preamble ctrlPktMetaPreamble,
40 PiPipeconf pipeconf, P4InfoBrowser browser)
41 throws P4InfoBrowser.NotFoundException {
42 final int metadataId = browser
43 .packetMetadatas(ctrlPktMetaPreamble.getId())
44 .getByName(piEntity.id().id()).getId();
45 return P4RuntimeOuterClass.PacketMetadata.newBuilder()
46 .setMetadataId(metadataId)
47 .setValue(ByteString.copyFrom(piEntity.value().asReadOnlyBuffer()))
48 .build();
49 }
50
51 @Override
52 protected PiPacketMetadata decode(
53 P4RuntimeOuterClass.PacketMetadata message,
54 P4InfoOuterClass.Preamble ctrlPktMetaPreamble,
55 PiPipeconf pipeconf, P4InfoBrowser browser)
56 throws P4InfoBrowser.NotFoundException {
57 final String packetMetadataName = browser
58 .packetMetadatas(ctrlPktMetaPreamble.getId())
59 .getById(message.getMetadataId()).getName();
60 final PiPacketMetadataId metadataId = PiPacketMetadataId
61 .of(packetMetadataName);
62 return PiPacketMetadata.builder()
63 .withId(metadataId)
64 .withValue(copyFrom(message.getValue().asReadOnlyByteBuffer()))
65 .build();
66 }
67}