Initial opendaylight infrastructure commit!!
[controller.git] / third-party / openflowj / src / main / java / org / openflow / protocol / OFMessage.java
1 package org.openflow.protocol;
2
3 import java.io.Serializable;
4 import java.nio.ByteBuffer;
5
6 import org.openflow.util.U16;
7 import org.openflow.util.U32;
8 import org.openflow.util.U8;
9
10 /**
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.
13  *
14  * @author David Erickson (daviderickson@cs.stanford.edu) - Feb 3, 2010
15  * @author Rob Sherwood (rob.sherwood@stanford.edu) - Feb 3, 2010
16  */
17 public class OFMessage implements Serializable{
18     public static byte OFP_VERSION = 0x01;
19     public static int MINIMUM_LENGTH = 8;
20
21     protected byte version;
22     protected OFType type;
23     protected short length;
24     protected int xid;
25
26     public OFMessage() {
27         this.version = OFP_VERSION;
28     }
29
30     /**
31      * Get the length of this message
32      *
33      * @return
34      */
35     public short getLength() {
36         return length;
37     }
38
39     /**
40      * Get the length of this message, unsigned
41      *
42      * @return
43      */
44     public int getLengthU() {
45         return U16.f(length);
46     }
47
48     /**
49      * Set the length of this message
50      *
51      * @param length
52      */
53     public OFMessage setLength(short length) {
54         this.length = length;
55         return this;
56     }
57
58     /**
59      * Set the length of this message, unsigned
60      *
61      * @param length
62      */
63     public OFMessage setLengthU(int length) {
64         this.length = U16.t(length);
65         return this;
66     }
67
68     /**
69      * Get the type of this message
70      *
71      * @return
72      */
73     public OFType getType() {
74         return type;
75     }
76
77     /**
78      * Set the type of this message
79      *
80      * @param type
81      */
82     public void setType(OFType type) {
83         this.type = type;
84     }
85
86     /**
87      * Get the OpenFlow version of this message
88      *
89      * @return
90      */
91     public byte getVersion() {
92         return version;
93     }
94
95     /**
96      * Set the OpenFlow version of this message
97      *
98      * @param version
99      */
100     public void setVersion(byte version) {
101         this.version = version;
102     }
103
104     /**
105      * Get the transaction id of this message
106      *
107      * @return
108      */
109     public int getXid() {
110         return xid;
111     }
112
113     /**
114      * Set the transaction id of this message
115      *
116      * @param xid
117      */
118     public void setXid(int xid) {
119         this.xid = xid;
120     }
121
122     /**
123      * Read this message off the wire from the specified ByteBuffer
124      * @param data
125      */
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();
131     }
132
133     /**
134      * Write this message's binary format to the specified ByteBuffer
135      * @param data
136      */
137     public void writeTo(ByteBuffer data) {
138         data.put(version);
139         data.put(type.getTypeValue());
140         data.putShort(length);
141         data.putInt(xid);
142     }
143
144     /**
145      * Returns a summary of the message
146      * @return "ofmsg=v=$version;t=$type:l=$len:xid=$xid"
147      */
148     public String toString() {
149         return "ofmsg" +
150             ":v=" + U8.f(this.getVersion()) +
151             ";t=" + this.getType() +
152             ";l=" + this.getLengthU() +
153             ";x=" + U32.f(this.getXid());
154     }
155
156     @Override
157     public int hashCode() {
158         final int prime = 97;
159         int result = 1;
160         result = prime * result + length;
161         result = prime * result + ((type == null) ? 0 : type.hashCode());
162         result = prime * result + version;
163         result = prime * result + xid;
164         return result;
165     }
166
167     @Override
168     public boolean equals(Object obj) {
169         if (this == obj) {
170             return true;
171         }
172         if (obj == null) {
173             return false;
174         }
175         if (!(obj instanceof OFMessage)) {
176             return false;
177         }
178         OFMessage other = (OFMessage) obj;
179         if (length != other.length) {
180             return false;
181         }
182         if (type == null) {
183             if (other.type != null) {
184                 return false;
185             }
186         } else if (!type.equals(other.type)) {
187             return false;
188         }
189         if (version != other.version) {
190             return false;
191         }
192         if (xid != other.xid) {
193             return false;
194         }
195         return true;
196     }
197 }