blob: e21dd1924b5dbb3893bf8214d1beed18ca717277 [file] [log] [blame]
Andrea Campanella378e21a2017-06-07 12:09:59 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Andrea Campanella378e21a2017-06-07 12:09:59 +02003 *
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
Andrea Campanella0288c872017-08-07 18:32:51 +020017package org.onosproject.drivers.p4runtime;
Andrea Campanella378e21a2017-06-07 12:09:59 +020018
Andrea Campanella378e21a2017-06-07 12:09:59 +020019import org.onosproject.net.packet.OutboundPacket;
20import org.onosproject.net.packet.PacketProgrammable;
Frank Wangc672c982017-07-18 16:09:28 +080021import org.onosproject.net.pi.model.PiPipelineInterpreter;
22import org.onosproject.net.pi.runtime.PiPacketOperation;
Frank Wangc672c982017-07-18 16:09:28 +080023
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020024import java.util.Collection;
Andrea Campanella378e21a2017-06-07 12:09:59 +020025
Andrea Campanella378e21a2017-06-07 12:09:59 +020026/**
Carmelo Casconee3a7c742017-09-01 01:25:52 +020027 * Implementation of PacketProgrammable behaviour for P4Runtime.
Andrea Campanella378e21a2017-06-07 12:09:59 +020028 */
Carmelo Casconee3a7c742017-09-01 01:25:52 +020029public class P4RuntimePacketProgrammable extends AbstractP4RuntimeHandlerBehaviour implements PacketProgrammable {
Carmelo Cascone59f57de2017-07-11 19:55:09 -040030
Andrea Campanella378e21a2017-06-07 12:09:59 +020031 @Override
32 public void emit(OutboundPacket packet) {
Frank Wangc672c982017-07-18 16:09:28 +080033
Carmelo Casconee3a7c742017-09-01 01:25:52 +020034 if (!this.setupBehaviour()) {
Frank Wangc672c982017-07-18 16:09:28 +080035 return;
36 }
37
Frank Wangc672c982017-07-18 16:09:28 +080038 final PiPipelineInterpreter interpreter = device.is(PiPipelineInterpreter.class)
39 ? device.as(PiPipelineInterpreter.class) : null;
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +020040 if (!device.is(PiPipelineInterpreter.class)) {
Carmelo Casconee3a7c742017-09-01 01:25:52 +020041 log.warn("Device {} with pipeconf {} has no interpreter, aborting emit operation", deviceId, pipeconf.id());
Frank Wangc672c982017-07-18 16:09:28 +080042 return;
43 }
44
45 try {
Carmelo Casconef3a1a382017-07-27 12:04:39 -040046 Collection<PiPacketOperation> operations = interpreter.mapOutboundPacket(packet);
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020047 operations.forEach(piPacketOperation -> {
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +020048 log.debug("Doing PiPacketOperation {}", piPacketOperation);
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020049 client.packetOut(piPacketOperation, pipeconf);
50 });
Frank Wangc672c982017-07-18 16:09:28 +080051 } catch (PiPipelineInterpreter.PiInterpreterException e) {
52 log.error("Interpreter of pipeconf {} was unable to translate outbound packet: {}",
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020053 pipeconf.id(), e.getMessage());
Frank Wangc672c982017-07-18 16:09:28 +080054 }
Andrea Campanella378e21a2017-06-07 12:09:59 +020055 }
56}