blob: ab29e6269687e358837d5f1b4eaaa4ac75cc8b5d [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
Daniele Moro53a3cdf2021-05-17 14:49:31 +020028import static org.onlab.util.ImmutableByteSequence.copyAndFit;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080029import 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 */
35public 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 Moro53a3cdf2021-05-17 14:49:31 +020058 throws P4InfoBrowser.NotFoundException, CodecException {
59 final P4InfoOuterClass.ControllerPacketMetadata.Metadata pktMeta =
Daniele Moro7aa13e62021-02-23 15:28:07 +010060 browser.packetMetadatas(ctrlPktMetaPreamble.getId())
Daniele Moro53a3cdf2021-05-17 14:49:31 +020061 .getById(message.getMetadataId());
Daniele Moro7aa13e62021-02-23 15:28:07 +010062 final ImmutableByteSequence value;
Daniele Moro53a3cdf2021-05-17 14:49:31 +020063 if (browser.isTypeString(pktMeta.getTypeName())) {
Daniele Moro7aa13e62021-02-23 15:28:07 +010064 value = copyFrom(new String(message.getValue().toByteArray()));
65 } else {
Daniele Moro53a3cdf2021-05-17 14:49:31 +020066 try {
67 value = copyAndFit(message.getValue().asReadOnlyByteBuffer(),
68 pktMeta.getBitwidth());
69 } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
70 throw new CodecException(e.getMessage());
71 }
Daniele Moro7aa13e62021-02-23 15:28:07 +010072 }
Carmelo Cascone4c289b72019-01-22 15:30:45 -080073 return PiPacketMetadata.builder()
Daniele Moro53a3cdf2021-05-17 14:49:31 +020074 .withId(PiPacketMetadataId.of(pktMeta.getName()))
Daniele Moro7aa13e62021-02-23 15:28:07 +010075 .withValue(value)
Carmelo Cascone4c289b72019-01-22 15:30:45 -080076 .build();
77 }
78}