Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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 | |
| 17 | package org.onosproject.p4runtime.ctl; |
| 18 | |
| 19 | import com.google.protobuf.ByteString; |
| 20 | import org.onosproject.net.pi.model.PiPipeconf; |
| 21 | import org.onosproject.net.pi.runtime.PiPacketOperation; |
| 22 | import org.slf4j.Logger; |
| 23 | import p4.P4RuntimeOuterClass; |
| 24 | import p4.config.P4InfoOuterClass; |
| 25 | |
| 26 | import java.util.List; |
| 27 | import java.util.Objects; |
| 28 | import java.util.stream.Collectors; |
| 29 | |
| 30 | import static org.onosproject.p4runtime.ctl.P4InfoBrowser.*; |
| 31 | import static org.slf4j.LoggerFactory.getLogger; |
| 32 | import static p4.P4RuntimeOuterClass.PacketMetadata; |
| 33 | |
| 34 | /** |
| 35 | * Encoder of packet metadata, from ONOS Pi* format, to P4Runtime protobuf messages, and vice versa. |
| 36 | */ |
| 37 | final class PacketIOCodec { |
| 38 | |
| 39 | private static final Logger log = getLogger(PacketIOCodec.class); |
| 40 | |
| 41 | private static final String PACKET_OUT = "packet_out"; |
| 42 | |
| 43 | // TODO: implement cache of encoded entities. |
| 44 | |
| 45 | private PacketIOCodec() { |
| 46 | // hide. |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns a P4Runtime packet out protobuf message, encoded from the given PiPacketOperation |
| 51 | * for the given pipeconf. If a PI packet metadata inside the PacketOperation cannot be encoded, |
| 52 | * it is skipped, hence the returned PacketOut collection of metadatas might have different |
| 53 | * size than the input one. |
| 54 | * <p> |
| 55 | * Please check the log for an explanation of any error that might have occurred. |
| 56 | * |
| 57 | * @param packet PI pakcet operation |
| 58 | * @param pipeconf the pipeconf for the program on the switch |
| 59 | * @return a P4Runtime packet out protobuf message |
| 60 | * @throws NotFoundException if the browser can't find the packet_out in the given p4Info |
| 61 | */ |
| 62 | static P4RuntimeOuterClass.PacketOut encodePacketOut(PiPacketOperation packet, PiPipeconf pipeconf) |
| 63 | throws NotFoundException { |
| 64 | |
| 65 | //Get the P4browser |
| 66 | P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
| 67 | |
| 68 | //Get the packet out packet metadata |
| 69 | P4InfoOuterClass.ControllerPacketMetadata controllerPacketMetadata = |
| 70 | browser.controllerPacketMetadatas().getByName(PACKET_OUT); |
| 71 | P4RuntimeOuterClass.PacketOut.Builder packetOutBuilder = P4RuntimeOuterClass.PacketOut.newBuilder(); |
| 72 | |
| 73 | //outer controller packet metadata id |
| 74 | int controllerPacketMetadataId = controllerPacketMetadata.getPreamble().getId(); |
| 75 | |
| 76 | //Add all its metadata to the packet out |
| 77 | packetOutBuilder.addAllMetadata(encodePacketMetadata(packet, browser, controllerPacketMetadataId)); |
| 78 | |
| 79 | //Set the packet out payload |
| 80 | packetOutBuilder.setPayload(ByteString.copyFrom(packet.data().asReadOnlyBuffer())); |
| 81 | return packetOutBuilder.build(); |
| 82 | |
| 83 | } |
| 84 | |
| 85 | private static List<PacketMetadata> encodePacketMetadata(PiPacketOperation packet, |
| 86 | P4InfoBrowser browser, int controllerPacketMetadataId) { |
| 87 | return packet.metadatas().stream().map(metadata -> { |
| 88 | try { |
| 89 | //get each metadata id |
| 90 | int metadataId = browser.packetMetadatas(controllerPacketMetadataId) |
| 91 | .getByName(metadata.id().name()).getId(); |
| 92 | |
| 93 | //Add the metadata id and it's data the packet out |
| 94 | return PacketMetadata.newBuilder() |
| 95 | .setMetadataId(metadataId) |
| 96 | .setValue(ByteString.copyFrom(metadata.value().asReadOnlyBuffer())) |
| 97 | .build(); |
| 98 | } catch (NotFoundException e) { |
| 99 | log.error("Cant find metadata with name {} in p4Info file.", metadata.id().name()); |
| 100 | return null; |
| 101 | } |
| 102 | }).filter(Objects::nonNull).collect(Collectors.toList()); |
| 103 | } |
| 104 | |
| 105 | //TODO: add decode packets |
| 106 | |
| 107 | } |