alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 1 | package org.onlab.onos.provider.of.packet.impl; |
| 2 | |
| 3 | import static org.slf4j.LoggerFactory.getLogger; |
| 4 | |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 5 | import java.util.List; |
| 6 | |
| 7 | import org.onlab.onos.net.PortNumber; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 8 | import org.onlab.onos.net.flow.instructions.Instruction; |
| 9 | import org.onlab.onos.net.flow.instructions.Instruction.Type; |
| 10 | import org.onlab.onos.net.flow.instructions.Instructions.OutputInstruction; |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 11 | import org.onlab.onos.net.packet.DefaultPacketContext; |
| 12 | import org.onlab.onos.net.packet.InboundPacket; |
| 13 | import org.onlab.onos.net.packet.OutboundPacket; |
tom | 9c94c5b | 2014-09-17 13:14:42 -0700 | [diff] [blame] | 14 | import org.onlab.onos.openflow.controller.OpenFlowPacketContext; |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 15 | import org.onlab.packet.Ethernet; |
| 16 | import org.projectfloodlight.openflow.types.OFPort; |
| 17 | import org.slf4j.Logger; |
| 18 | |
| 19 | public class OpenFlowCorePacketContext extends DefaultPacketContext { |
| 20 | |
| 21 | private final Logger log = getLogger(getClass()); |
| 22 | |
| 23 | private final OpenFlowPacketContext ofPktCtx; |
| 24 | |
| 25 | protected OpenFlowCorePacketContext(long time, InboundPacket inPkt, |
| 26 | OutboundPacket outPkt, boolean block, OpenFlowPacketContext ofPktCtx) { |
| 27 | super(time, inPkt, outPkt, block); |
| 28 | this.ofPktCtx = ofPktCtx; |
| 29 | } |
| 30 | |
| 31 | @Override |
| 32 | public void send() { |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 33 | if (!this.block()) { |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 34 | if (outPacket() == null) { |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame^] | 35 | sendPacket(null); |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 36 | } else { |
| 37 | Ethernet eth = new Ethernet(); |
| 38 | eth.deserialize(outPacket().data().array(), 0, |
| 39 | outPacket().data().array().length); |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame^] | 40 | sendPacket(eth); |
| 41 | |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 42 | } |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 43 | |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame^] | 47 | private void sendPacket(Ethernet eth) { |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 48 | List<Instruction> ins = treatmentBuilder().build().instructions(); |
| 49 | OFPort p = null; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 50 | //TODO: support arbitrary list of treatments must be supported in ofPacketContext |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 51 | for (Instruction i : ins) { |
| 52 | if (i.type() == Type.OUTPUT) { |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 53 | p = buildPort(((OutputInstruction) i).port()); |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 54 | break; //for now... |
| 55 | } |
| 56 | } |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame^] | 57 | if (eth == null) { |
| 58 | ofPktCtx.build(p); |
| 59 | } else { |
| 60 | ofPktCtx.build(eth, p); |
| 61 | } |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 62 | ofPktCtx.send(); |
| 63 | } |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 64 | private OFPort buildPort(PortNumber port) { |
| 65 | return OFPort.of((int) port.toLong()); |
| 66 | } |
| 67 | |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 68 | } |