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; |
| 14 | import org.onlab.onos.of.controller.OpenFlowPacketContext; |
| 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 | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 35 | sendBufferedPacket(); |
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); |
| 40 | ofPktCtx.build(eth, OFPort.FLOOD); |
| 41 | } |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 42 | |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 46 | private void sendBufferedPacket() { |
| 47 | List<Instruction> ins = treatmentBuilder().build().instructions(); |
| 48 | OFPort p = null; |
| 49 | //TODO: support arbitrary list of treatments |
| 50 | for (Instruction i : ins) { |
| 51 | if (i.type() == Type.OUTPUT) { |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 52 | p = buildPort(((OutputInstruction) i).port()); |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 53 | break; //for now... |
| 54 | } |
| 55 | } |
| 56 | ofPktCtx.build(p); |
| 57 | ofPktCtx.send(); |
| 58 | } |
| 59 | |
| 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 | } |