1 package org.openflow.codec.protocol;
3 import java.util.Arrays;
6 import org.openflow.codec.io.IDataBuffer;
7 import org.openflow.codec.protocol.action.OFPAction;
8 import org.openflow.codec.protocol.factory.OFPActionFactory;
9 import org.openflow.codec.protocol.factory.OFPActionFactoryAware;
10 import org.openflow.codec.util.U16;
13 * Represents an ofp_packet_out message
15 * @author David Erickson (daviderickson@cs.stanford.edu) - Mar 12, 2010
17 public class OFPPacketOut extends OFPMessage implements OFPActionFactoryAware {
18 public static int MINIMUM_LENGTH = 24;
19 public static int BUFFER_ID_NONE = 0xffffffff;
21 protected OFPActionFactory actionFactory;
22 protected int bufferId;
24 protected short actionsLength;
25 protected List<OFPAction> actions;
26 protected byte[] packetData;
28 public OFPPacketOut() {
30 this.type = OFPType.PACKET_OUT;
31 this.length = U16.t(MINIMUM_LENGTH);
39 public int getBufferId() {
48 public OFPPacketOut setBufferId(int bufferId) {
49 this.bufferId = bufferId;
54 * Returns the packet data
58 public byte[] getPacketData() {
59 return this.packetData;
63 * Sets the packet data
67 public OFPPacketOut setPacketData(byte[] packetData) {
68 this.packetData = packetData;
73 private void updateLength() {
74 short newLength = (short) ((packetData == null) ? 0 : packetData.length);
75 this.length = newLength;
83 public int getInPort() {
92 public OFPPacketOut setInPort(int inPort) {
98 * Set in_port. Convenience method using OFPPort enum.
102 public OFPPacketOut setInPort(OFPPortNo inPort) {
103 this.inPort = inPort.getValue();
112 public short getActionsLength() {
113 return this.actionsLength;
117 * Get actions_len, unsigned
121 public int getActionsLengthU() {
122 return U16.f(this.actionsLength);
128 * @param actionsLength
130 public OFPPacketOut setActionsLength(short actionsLength) {
131 this.actionsLength = actionsLength;
136 * Returns the actions contained in this message
138 * @return a list of ordered OFPAction objects
140 public List<OFPAction> getActions() {
145 * Sets the list of actions on this message
148 * a list of ordered OFPAction objects
150 public OFPPacketOut setActions(List<OFPAction> actions) {
151 this.actions = actions;
156 public void setActionFactory(OFPActionFactory actionFactory) {
157 this.actionFactory = actionFactory;
161 public void readFrom(IDataBuffer data) {
162 super.readFrom(data);
163 this.bufferId = data.getInt();
164 this.inPort = data.getInt();
165 this.actionsLength = data.getShort();
166 data.getInt(); // pad
167 data.getShort(); // pad
168 if (this.actionFactory == null)
169 throw new RuntimeException("ActionFactory not set");
170 this.actions = this.actionFactory.parseActions(data, getActionsLengthU());
171 this.packetData = new byte[getLengthU() - MINIMUM_LENGTH - getActionsLengthU()];
172 data.get(this.packetData);
176 public void writeTo(IDataBuffer data) {
178 data.putInt(bufferId);
180 data.putShort(actionsLength);
181 data.putInt(0); // pad
182 data.putShort((short) 0); // pad
183 for (OFPAction action : actions) {
184 action.writeTo(data);
186 if (this.packetData != null)
187 data.put(this.packetData);
191 public int hashCode() {
192 final int prime = 293;
193 int result = super.hashCode();
194 result = prime * result + ((actions == null) ? 0 : actions.hashCode());
195 result = prime * result + actionsLength;
196 result = prime * result + bufferId;
197 result = prime * result + inPort;
198 result = prime * result + Arrays.hashCode(packetData);
203 public boolean equals(Object obj) {
207 if (!super.equals(obj)) {
210 if (!(obj instanceof OFPPacketOut)) {
213 OFPPacketOut other = (OFPPacketOut) obj;
214 if (actions == null) {
215 if (other.actions != null) {
218 } else if (!actions.equals(other.actions)) {
221 if (actionsLength != other.actionsLength) {
224 if (bufferId != other.bufferId) {
227 if (inPort != other.inPort) {
230 if (!Arrays.equals(packetData, other.packetData)) {
239 * @see java.lang.Object#toString()
242 public String toString() {
243 return "OFPPacketOut [actionFactory=" + actionFactory + ", actions=" + actions + ", actionsLength="
244 + actionsLength + ", bufferId=0x" + Integer.toHexString(bufferId) + ", inPort=" + inPort
245 + ", packetData=" + Arrays.toString(packetData) + "]";