alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 1 | package org.onlab.onos.of.controller; |
| 2 | |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 3 | import java.util.Collections; |
| 4 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 5 | import org.onlab.packet.Ethernet; |
| 6 | import org.projectfloodlight.openflow.protocol.OFPacketIn; |
| 7 | import org.projectfloodlight.openflow.protocol.OFPacketOut; |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 8 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
alshabib | ddf02db | 2014-09-09 14:57:15 -0700 | [diff] [blame] | 9 | import org.projectfloodlight.openflow.protocol.action.OFActionOutput; |
alshabib | 51622f7 | 2014-09-11 11:22:33 -0700 | [diff] [blame] | 10 | import org.projectfloodlight.openflow.protocol.match.MatchField; |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 11 | import org.projectfloodlight.openflow.types.OFBufferId; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 12 | import org.projectfloodlight.openflow.types.OFPort; |
| 13 | |
alshabib | e703156 | 2014-09-12 18:17:37 -0700 | [diff] [blame] | 14 | public final class DefaultOpenFlowPacketContext implements OpenFlowPacketContext { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 15 | |
| 16 | private boolean free = true; |
| 17 | private boolean isBuilt = false; |
| 18 | private final OpenFlowSwitch sw; |
| 19 | private final OFPacketIn pktin; |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 20 | private OFPacketOut pktout = null; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 21 | |
alshabib | e703156 | 2014-09-12 18:17:37 -0700 | [diff] [blame] | 22 | private DefaultOpenFlowPacketContext(OpenFlowSwitch s, OFPacketIn pkt) { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 23 | this.sw = s; |
| 24 | this.pktin = pkt; |
| 25 | } |
| 26 | |
| 27 | @Override |
| 28 | public void block() { |
| 29 | free = false; |
| 30 | } |
| 31 | |
| 32 | @Override |
| 33 | public void send() { |
| 34 | if (free && isBuilt) { |
| 35 | sw.sendMsg(pktout); |
| 36 | } |
| 37 | |
| 38 | } |
| 39 | |
| 40 | @Override |
alshabib | ddf02db | 2014-09-09 14:57:15 -0700 | [diff] [blame] | 41 | public synchronized void build(OFPort outPort) { |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 42 | if (isBuilt) { |
| 43 | return; |
| 44 | } |
| 45 | OFPacketOut.Builder builder = sw.factory().buildPacketOut(); |
alshabib | ddf02db | 2014-09-09 14:57:15 -0700 | [diff] [blame] | 46 | OFAction act = buildOutput(outPort.getPortNumber()); |
| 47 | pktout = builder.setXid(pktin.getXid()) |
| 48 | .setBufferId(pktin.getBufferId()) |
| 49 | .setActions(Collections.singletonList(act)) |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 50 | .build(); |
alshabib | ddf02db | 2014-09-09 14:57:15 -0700 | [diff] [blame] | 51 | isBuilt = true; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public synchronized void build(Ethernet ethFrame, OFPort outPort) { |
| 56 | if (isBuilt) { |
| 57 | return; |
| 58 | } |
| 59 | OFPacketOut.Builder builder = sw.factory().buildPacketOut(); |
| 60 | OFAction act = buildOutput(outPort.getPortNumber()); |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 61 | pktout = builder.setXid(pktin.getXid()) |
| 62 | .setBufferId(OFBufferId.NO_BUFFER) |
| 63 | .setActions(Collections.singletonList(act)) |
| 64 | .setData(ethFrame.serialize()) |
| 65 | .build(); |
| 66 | isBuilt = true; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public Ethernet parsed() { |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 71 | Ethernet eth = new Ethernet(); |
| 72 | eth.deserialize(pktin.getData(), 0, pktin.getTotalLen()); |
| 73 | return eth; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public Dpid dpid() { |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 78 | return new Dpid(sw.getId()); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 79 | } |
| 80 | |
alshabib | e703156 | 2014-09-12 18:17:37 -0700 | [diff] [blame] | 81 | public static OpenFlowPacketContext packetContextFromPacketIn(OpenFlowSwitch s, OFPacketIn pkt) { |
| 82 | return new DefaultOpenFlowPacketContext(s, pkt); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 83 | } |
| 84 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 85 | @Override |
| 86 | public Integer inPort() { |
alshabib | 51622f7 | 2014-09-11 11:22:33 -0700 | [diff] [blame] | 87 | try { |
| 88 | return pktin.getInPort().getPortNumber(); |
| 89 | } catch (UnsupportedOperationException e) { |
| 90 | return pktin.getMatch().get(MatchField.IN_PORT).getPortNumber(); |
| 91 | } |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 92 | } |
| 93 | |
alshabib | 9ee6817 | 2014-09-09 14:45:14 -0700 | [diff] [blame] | 94 | @Override |
| 95 | public byte[] unparsed() { |
alshabib | 51622f7 | 2014-09-11 11:22:33 -0700 | [diff] [blame] | 96 | |
alshabib | 9ee6817 | 2014-09-09 14:45:14 -0700 | [diff] [blame] | 97 | return pktin.getData().clone(); |
alshabib | 51622f7 | 2014-09-11 11:22:33 -0700 | [diff] [blame] | 98 | |
alshabib | 9ee6817 | 2014-09-09 14:45:14 -0700 | [diff] [blame] | 99 | } |
| 100 | |
alshabib | ddf02db | 2014-09-09 14:57:15 -0700 | [diff] [blame] | 101 | private OFActionOutput buildOutput(Integer port) { |
| 102 | OFActionOutput act = sw.factory().actions() |
| 103 | .buildOutput() |
| 104 | .setPort(OFPort.of(port)) |
| 105 | .build(); |
| 106 | return act; |
| 107 | } |
| 108 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 109 | } |