1 package org.openflow.protocol;
3 import java.nio.ByteBuffer;
4 import java.util.Arrays;
7 import org.openflow.protocol.action.OFAction;
8 import org.openflow.protocol.factory.OFActionFactory;
9 import org.openflow.protocol.factory.OFActionFactoryAware;
10 import org.openflow.util.U16;
13 * Represents an ofp_packet_out message
15 * @author David Erickson (daviderickson@cs.stanford.edu) - Mar 12, 2010
17 public class OFPacketOut extends OFMessage implements OFActionFactoryAware {
18 public static int MINIMUM_LENGTH = 16;
19 public static int BUFFER_ID_NONE = 0xffffffff;
21 protected OFActionFactory actionFactory;
22 protected int bufferId;
23 protected short inPort;
24 protected short actionsLength;
25 protected List<OFAction> actions;
26 protected byte[] packetData;
28 public OFPacketOut() {
30 this.type = OFType.PACKET_OUT;
31 this.length = U16.t(MINIMUM_LENGTH);
38 public int getBufferId() {
46 public OFPacketOut setBufferId(int bufferId) {
47 this.bufferId = bufferId;
52 * Returns the packet data
55 public byte[] getPacketData() {
56 return this.packetData;
60 * Sets the packet data
63 public OFPacketOut setPacketData(byte[] packetData) {
64 this.packetData = packetData;
72 public short getInPort() {
80 public OFPacketOut setInPort(short inPort) {
86 * Set in_port. Convenience method using OFPort enum.
89 public OFPacketOut setInPort(OFPort inPort) {
90 this.inPort = inPort.getValue();
98 public short getActionsLength() {
99 return this.actionsLength;
103 * Get actions_len, unsigned
106 public int getActionsLengthU() {
107 return U16.f(this.actionsLength);
112 * @param actionsLength
114 public OFPacketOut setActionsLength(short actionsLength) {
115 this.actionsLength = actionsLength;
120 * Returns the actions contained in this message
121 * @return a list of ordered OFAction objects
123 public List<OFAction> getActions() {
128 * Sets the list of actions on this message
129 * @param actions a list of ordered OFAction objects
131 public OFPacketOut setActions(List<OFAction> actions) {
132 this.actions = actions;
137 public void setActionFactory(OFActionFactory actionFactory) {
138 this.actionFactory = actionFactory;
142 public void readFrom(ByteBuffer data) {
143 super.readFrom(data);
144 this.bufferId = data.getInt();
145 this.inPort = data.getShort();
146 this.actionsLength = data.getShort();
147 if ( this.actionFactory == null)
148 throw new RuntimeException("ActionFactory not set");
149 this.actions = this.actionFactory.parseActions(data, getActionsLengthU());
150 this.packetData = new byte[getLengthU() - MINIMUM_LENGTH - getActionsLengthU()];
151 data.get(this.packetData);
155 public void writeTo(ByteBuffer data) {
157 data.putInt(bufferId);
158 data.putShort(inPort);
159 data.putShort(actionsLength);
160 for (OFAction action : actions) {
161 action.writeTo(data);
163 if (this.packetData != null)
164 data.put(this.packetData);
168 public int hashCode() {
169 final int prime = 293;
170 int result = super.hashCode();
171 result = prime * result + ((actions == null) ? 0 : actions.hashCode());
172 result = prime * result + actionsLength;
173 result = prime * result + bufferId;
174 result = prime * result + inPort;
175 result = prime * result + Arrays.hashCode(packetData);
180 public boolean equals(Object obj) {
184 if (!super.equals(obj)) {
187 if (!(obj instanceof OFPacketOut)) {
190 OFPacketOut other = (OFPacketOut) obj;
191 if (actions == null) {
192 if (other.actions != null) {
195 } else if (!actions.equals(other.actions)) {
198 if (actionsLength != other.actionsLength) {
201 if (bufferId != other.bufferId) {
204 if (inPort != other.inPort) {
207 if (!Arrays.equals(packetData, other.packetData)) {
214 * @see java.lang.Object#toString()
217 public String toString() {
218 return "OFPacketOut [actionFactory=" + actionFactory + ", actions="
219 + actions + ", actionsLength=" + actionsLength + ", bufferId=0x"
220 + Integer.toHexString(bufferId) + ", inPort=" + inPort + ", packetData="
221 + Arrays.toString(packetData) + "]";