68baeedca0a57da65225639f849e735c17e4f278
[openflowjava.git] / third-party / openflow-codec / src / main / java / org / openflow / codec / protocol / OFPFlowRemoved.java
1 package org.openflow.codec.protocol;
2
3 import org.openflow.codec.io.IDataBuffer;
4 import org.openflow.codec.util.U16;
5
6 /**
7  * Represents an ofp_flow_removed message
8  *
9  * @author David Erickson (daviderickson@cs.stanford.edu)
10  * @author AnilGujele
11  *
12  */
13 public class OFPFlowRemoved extends OFPMessage {
14     public static final int MINIMUM_LENGTH = 56;
15     public static final int MATCH_DEFAULT_LENGTH = 8;
16     public static final int MINIMUM_LENGTH_WITHOUT_MATCH = MINIMUM_LENGTH - MATCH_DEFAULT_LENGTH;
17
18     /**
19      * correspond to enum ofp_flow_removed_reason
20      *
21      * @author AnilGujele
22      *
23      */
24     public enum OFFlowRemovedReason {
25         OFPRR_IDLE_TIMEOUT, OFPRR_HARD_TIMEOUT, OFPRR_DELETE, OFPRR_GROUP_DELETE
26     }
27
28     protected long cookie;
29     protected short priority;
30     protected OFFlowRemovedReason reason;
31     protected byte tableId;
32     protected int durationSeconds;
33     protected int durationNanoseconds;
34     protected short idleTimeout;
35     protected short hardTimeout;
36     protected long packetCount;
37     protected long byteCount;
38     protected OFPMatch match;
39
40     public OFPFlowRemoved() {
41         super();
42         this.type = OFPType.FLOW_REMOVED;
43         this.length = U16.t(MINIMUM_LENGTH);
44     }
45
46     /**
47      * Get cookie
48      *
49      * @return
50      */
51     public long getCookie() {
52         return this.cookie;
53     }
54
55     /**
56      * Set cookie
57      *
58      * @param cookie
59      */
60     public void setCookie(long cookie) {
61         this.cookie = cookie;
62     }
63
64     /**
65      * Get idle_timeout
66      *
67      * @return
68      */
69     public short getIdleTimeout() {
70         return this.idleTimeout;
71     }
72
73     /**
74      * Set idle_timeout
75      *
76      * @param idleTimeout
77      */
78     public void setIdleTimeout(short idleTimeout) {
79         this.idleTimeout = idleTimeout;
80     }
81
82     /**
83      * get hard timeout
84      *
85      * @return
86      */
87     public short getHardTimeout() {
88         return hardTimeout;
89     }
90
91     /**
92      * set hard timeout
93      *
94      * @param hardTimeout
95      */
96     public void setHardTimeout(short hardTimeout) {
97         this.hardTimeout = hardTimeout;
98     }
99
100     /**
101      * Gets a copy of the OFPMatch object for this FlowMod, changes to this
102      * object do not modify the FlowMod
103      *
104      * @return
105      */
106     public OFPMatch getMatch() {
107         return this.match;
108     }
109
110     /**
111      * Set match
112      *
113      * @param match
114      */
115     public void setMatch(OFPMatch match) {
116         this.match = match;
117         updateLength();
118     }
119
120     private void updateLength() {
121         int matchLength = (null == this.match) ? MATCH_DEFAULT_LENGTH : this.match.getLengthWithPadding();
122         this.length = (short) (OFPFlowRemoved.MINIMUM_LENGTH_WITHOUT_MATCH + matchLength);
123     }
124
125     /**
126      * Get priority
127      *
128      * @return
129      */
130     public short getPriority() {
131         return this.priority;
132     }
133
134     /**
135      * Set priority
136      *
137      * @param priority
138      */
139     public void setPriority(short priority) {
140         this.priority = priority;
141     }
142
143     /**
144      * @return the reason
145      */
146     public OFFlowRemovedReason getReason() {
147         return reason;
148     }
149
150     /**
151      * @param reason
152      *            the reason to set
153      */
154     public void setReason(OFFlowRemovedReason reason) {
155         this.reason = reason;
156     }
157
158     /**
159      * get table id
160      *
161      * @return
162      */
163     public byte getTableId() {
164         return tableId;
165     }
166
167     /**
168      * set table id
169      *
170      * @param tableId
171      */
172     public void setTableId(byte tableId) {
173         this.tableId = tableId;
174     }
175
176     /**
177      * @return the durationSeconds
178      */
179     public int getDurationSeconds() {
180         return durationSeconds;
181     }
182
183     /**
184      * @param durationSeconds
185      *            the durationSeconds to set
186      */
187     public void setDurationSeconds(int durationSeconds) {
188         this.durationSeconds = durationSeconds;
189     }
190
191     /**
192      * @return the durationNanoseconds
193      */
194     public int getDurationNanoseconds() {
195         return durationNanoseconds;
196     }
197
198     /**
199      * @param durationNanoseconds
200      *            the durationNanoseconds to set
201      */
202     public void setDurationNanoseconds(int durationNanoseconds) {
203         this.durationNanoseconds = durationNanoseconds;
204     }
205
206     /**
207      * @return the packetCount
208      */
209     public long getPacketCount() {
210         return packetCount;
211     }
212
213     /**
214      * @param packetCount
215      *            the packetCount to set
216      */
217     public void setPacketCount(long packetCount) {
218         this.packetCount = packetCount;
219     }
220
221     /**
222      * @return the byteCount
223      */
224     public long getByteCount() {
225         return byteCount;
226     }
227
228     /**
229      * @param byteCount
230      *            the byteCount to set
231      */
232     public void setByteCount(long byteCount) {
233         this.byteCount = byteCount;
234     }
235
236     @Override
237     public void readFrom(IDataBuffer data) {
238         super.readFrom(data);
239         this.cookie = data.getLong();
240         this.priority = data.getShort();
241         this.reason = OFFlowRemovedReason.values()[(0xff & data.get())];
242         this.tableId = data.get();
243         this.durationSeconds = data.getInt();
244         this.durationNanoseconds = data.getInt();
245         this.idleTimeout = data.getShort();
246         this.hardTimeout = data.getShort();
247         this.packetCount = data.getLong();
248         this.byteCount = data.getLong();
249         if (this.match == null)
250             this.match = new OFPMatch();
251         this.match.readFrom(data);
252     }
253
254     @Override
255     public void writeTo(IDataBuffer data) {
256         super.writeTo(data);
257         data.putLong(cookie);
258         data.putShort(priority);
259         data.put((byte) this.reason.ordinal());
260         data.put((byte) tableId);
261         data.putInt(this.durationSeconds);
262         data.putInt(this.durationNanoseconds);
263         data.putShort(idleTimeout);
264         data.putShort(hardTimeout);
265         data.putLong(this.packetCount);
266         data.putLong(this.byteCount);
267         this.match.writeTo(data);
268     }
269
270     @Override
271     public int hashCode() {
272         final int prime = 271;
273         int result = super.hashCode();
274         result = prime * result + (int) (byteCount ^ (byteCount >>> 32));
275         result = prime * result + (int) (cookie ^ (cookie >>> 32));
276         result = prime * result + durationNanoseconds;
277         result = prime * result + durationSeconds;
278         result = prime * result + idleTimeout;
279         result = prime * result + hardTimeout;
280         result = prime * result + ((match == null) ? 0 : match.hashCode());
281         result = prime * result + (int) (packetCount ^ (packetCount >>> 32));
282         result = prime * result + priority;
283         result = prime * result + tableId;
284         result = prime * result + ((reason == null) ? 0 : reason.hashCode());
285         return result;
286     }
287
288     @Override
289     public boolean equals(Object obj) {
290         if (this == obj) {
291             return true;
292         }
293         if (!super.equals(obj)) {
294             return false;
295         }
296         if (!(obj instanceof OFPFlowRemoved)) {
297             return false;
298         }
299         OFPFlowRemoved other = (OFPFlowRemoved) obj;
300         if (byteCount != other.byteCount) {
301             return false;
302         }
303         if (cookie != other.cookie) {
304             return false;
305         }
306         if (durationNanoseconds != other.durationNanoseconds) {
307             return false;
308         }
309         if (durationSeconds != other.durationSeconds) {
310             return false;
311         }
312         if (idleTimeout != other.idleTimeout) {
313             return false;
314         }
315         if (hardTimeout != other.hardTimeout) {
316             return false;
317         }
318
319         if (match == null) {
320             if (other.match != null) {
321                 return false;
322             }
323         } else if (!match.equals(other.match)) {
324             return false;
325         }
326         if (packetCount != other.packetCount) {
327             return false;
328         }
329         if (priority != other.priority) {
330             return false;
331         }
332         if (tableId != other.tableId) {
333             return false;
334         }
335         if (reason == null) {
336             if (other.reason != null) {
337                 return false;
338             }
339         } else if (!reason.equals(other.reason)) {
340             return false;
341         }
342         return true;
343     }
344 }