blob: 6d020c3fb91c697e7e212de3d7bcd0a930ee4b1f [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.PiPipeconf;
21import org.onosproject.net.pi.runtime.PiPacketOperation;
22import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser;
23import p4.config.v1.P4InfoOuterClass;
24import p4.v1.P4RuntimeOuterClass;
25
26import static org.onosproject.p4runtime.ctl.codec.Codecs.CODECS;
27
28/**
29 * Codec for P4Runtime PacketOut. Only encoding is supported, as decoding is not
30 * meaningful in this case (packet-outs are always generated by the client).
31 */
32public final class PacketOutCodec
33 extends AbstractCodec<PiPacketOperation,
34 P4RuntimeOuterClass.PacketOut, Object> {
35
36 private static final String PACKET_OUT = "packet_out";
37
38 @Override
39 protected P4RuntimeOuterClass.PacketOut encode(
40 PiPacketOperation piPacket, Object ignored, PiPipeconf pipeconf,
41 P4InfoBrowser browser)
42 throws CodecException, P4InfoBrowser.NotFoundException {
43 final P4InfoOuterClass.Preamble ctrlPktMetaPreamble = browser
44 .controllerPacketMetadatas()
45 .getByName(PACKET_OUT)
46 .getPreamble();
47 return P4RuntimeOuterClass.PacketOut.newBuilder()
48 .addAllMetadata(CODECS.packetMetadata().encodeAll(
49 piPacket.metadatas(), ctrlPktMetaPreamble, pipeconf))
50 .setPayload(ByteString.copyFrom(piPacket.data().asReadOnlyBuffer()))
51 .build();
52
53 }
54
55 @Override
56 protected PiPacketOperation decode(
57 P4RuntimeOuterClass.PacketOut message, Object ignored,
58 PiPipeconf pipeconf, P4InfoBrowser browser)
59 throws CodecException {
60 throw new CodecException("Decoding of packet-out is not supported");
61 }
62}