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