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