1 package org.openflow.protocol;
3 import java.nio.ByteBuffer;
4 import java.util.Arrays;
6 import org.openflow.util.U16;
7 import org.openflow.util.U8;
10 * Represents an ofp_packet_in
12 * @author David Erickson (daviderickson@cs.stanford.edu) - Feb 8, 2010
14 public class OFPacketIn extends OFMessage {
15 public static int MINIMUM_LENGTH = 18;
17 public enum OFPacketInReason {
21 protected int bufferId;
22 protected short totalLength;
23 protected short inPort;
24 protected OFPacketInReason reason;
25 protected byte[] packetData;
29 this.type = OFType.PACKET_IN;
30 this.length = U16.t(MINIMUM_LENGTH);
37 public int getBufferId() {
45 public OFPacketIn setBufferId(int bufferId) {
46 this.bufferId = bufferId;
51 * Returns the packet data
54 public byte[] getPacketData() {
55 return this.packetData;
59 * Sets the packet data, and updates the length of this message
62 public OFPacketIn setPacketData(byte[] packetData) {
63 this.packetData = packetData;
64 this.length = U16.t(OFPacketIn.MINIMUM_LENGTH + packetData.length);
72 public short getInPort() {
80 public OFPacketIn setInPort(short inPort) {
89 public OFPacketInReason getReason() {
97 public OFPacketIn setReason(OFPacketInReason reason) {
106 public short getTotalLength() {
107 return this.totalLength;
114 public OFPacketIn setTotalLength(short totalLength) {
115 this.totalLength = totalLength;
120 public void readFrom(ByteBuffer data) {
121 super.readFrom(data);
122 this.bufferId = data.getInt();
123 this.totalLength = data.getShort();
124 this.inPort = data.getShort();
125 this.reason = OFPacketInReason.values()[U8.f(data.get())];
127 this.packetData = new byte[getLengthU() - MINIMUM_LENGTH];
128 data.get(this.packetData);
132 public void writeTo(ByteBuffer data) {
134 data.putInt(bufferId);
135 data.putShort(totalLength);
136 data.putShort(inPort);
137 data.put((byte) reason.ordinal());
138 data.put((byte) 0x0); // pad
139 data.put(this.packetData);
143 public int hashCode() {
144 final int prime = 283;
145 int result = super.hashCode();
146 result = prime * result + bufferId;
147 result = prime * result + inPort;
148 result = prime * result + Arrays.hashCode(packetData);
149 result = prime * result + ((reason == null) ? 0 : reason.hashCode());
150 result = prime * result + totalLength;
155 public boolean equals(Object obj) {
159 if (!super.equals(obj)) {
162 if (!(obj instanceof OFPacketIn)) {
165 OFPacketIn other = (OFPacketIn) obj;
166 if (bufferId != other.bufferId) {
169 if (inPort != other.inPort) {
172 if (!Arrays.equals(packetData, other.packetData)) {
175 if (reason == null) {
176 if (other.reason != null) {
179 } else if (!reason.equals(other.reason)) {
182 if (totalLength != other.totalLength) {