1 package org.openflow.protocol;
3 import java.io.Serializable;
4 import java.nio.ByteBuffer;
6 import org.openflow.util.U16;
7 import org.openflow.util.U32;
8 import org.openflow.util.U8;
11 * The base class for all OpenFlow protocol messages. This class contains the
12 * equivalent of the ofp_header which is present in all OpenFlow messages.
14 * @author David Erickson (daviderickson@cs.stanford.edu) - Feb 3, 2010
15 * @author Rob Sherwood (rob.sherwood@stanford.edu) - Feb 3, 2010
17 public class OFMessage implements Serializable{
18 public static byte OFP_VERSION = 0x01;
19 public static int MINIMUM_LENGTH = 8;
21 protected byte version;
22 protected OFType type;
23 protected short length;
27 this.version = OFP_VERSION;
31 * Get the length of this message
35 public short getLength() {
40 * Get the length of this message, unsigned
44 public int getLengthU() {
49 * Set the length of this message
53 public OFMessage setLength(short length) {
59 * Set the length of this message, unsigned
63 public OFMessage setLengthU(int length) {
64 this.length = U16.t(length);
69 * Get the type of this message
73 public OFType getType() {
78 * Set the type of this message
82 public void setType(OFType type) {
87 * Get the OpenFlow version of this message
91 public byte getVersion() {
96 * Set the OpenFlow version of this message
100 public void setVersion(byte version) {
101 this.version = version;
105 * Get the transaction id of this message
109 public int getXid() {
114 * Set the transaction id of this message
118 public void setXid(int xid) {
123 * Read this message off the wire from the specified ByteBuffer
126 public void readFrom(ByteBuffer data) {
127 this.version = data.get();
128 this.type = OFType.valueOf(data.get());
129 this.length = data.getShort();
130 this.xid = data.getInt();
134 * Write this message's binary format to the specified ByteBuffer
137 public void writeTo(ByteBuffer data) {
139 data.put(type.getTypeValue());
140 data.putShort(length);
145 * Returns a summary of the message
146 * @return "ofmsg=v=$version;t=$type:l=$len:xid=$xid"
148 public String toString() {
150 ":v=" + U8.f(this.getVersion()) +
151 ";t=" + this.getType() +
152 ";l=" + this.getLengthU() +
153 ";x=" + U32.f(this.getXid());
157 public int hashCode() {
158 final int prime = 97;
160 result = prime * result + length;
161 result = prime * result + ((type == null) ? 0 : type.hashCode());
162 result = prime * result + version;
163 result = prime * result + xid;
168 public boolean equals(Object obj) {
175 if (!(obj instanceof OFMessage)) {
178 OFMessage other = (OFMessage) obj;
179 if (length != other.length) {
183 if (other.type != null) {
186 } else if (!type.equals(other.type)) {
189 if (version != other.version) {
192 if (xid != other.xid) {