Added openflow-codec and openflowj_netty from openflowplugin
[openflowjava.git] / third-party / openflowj_netty / src / main / java / org / openflow / protocol / statistics / OFFlowStatisticsReply.java
1 /**
2 *    Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior
3 *    University
4
5 *    Licensed under the Apache License, Version 2.0 (the "License"); you may
6 *    not use this file except in compliance with the License. You may obtain
7 *    a copy of the License at
8 *
9 *         http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *    Unless required by applicable law or agreed to in writing, software
12 *    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 *    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 *    License for the specific language governing permissions and limitations
15 *    under the License.
16 **/
17
18 package org.openflow.protocol.statistics;
19
20 import java.util.List;
21
22 import org.jboss.netty.buffer.ChannelBuffer;
23 import org.openflow.protocol.OFMatch;
24 import org.openflow.protocol.action.OFAction;
25 import org.openflow.protocol.factory.OFActionFactory;
26 import org.openflow.protocol.factory.OFActionFactoryAware;
27 import org.openflow.util.U16;
28
29 /**
30  * Represents an ofp_flow_stats structure
31  * @author David Erickson (daviderickson@cs.stanford.edu)
32  */
33 public class OFFlowStatisticsReply implements OFStatistics, OFActionFactoryAware {
34     public static int MINIMUM_LENGTH = 88;
35
36     protected OFActionFactory actionFactory;
37     protected short length = (short) MINIMUM_LENGTH;
38     protected byte tableId;
39     protected OFMatch match;
40     protected int durationSeconds;
41     protected int durationNanoseconds;
42     protected short priority;
43     protected short idleTimeout;
44     protected short hardTimeout;
45     protected long cookie;
46     protected long packetCount;
47     protected long byteCount;
48     protected List<OFAction> actions;
49
50     /**
51      * @return the tableId
52      */
53     public byte getTableId() {
54         return tableId;
55     }
56
57     /**
58      * @param tableId the tableId to set
59      */
60     public void setTableId(byte tableId) {
61         this.tableId = tableId;
62     }
63
64     /**
65      * @return the match
66      */
67     public OFMatch getMatch() {
68         return match;
69     }
70
71     /**
72      * @param match the match to set
73      */
74     public void setMatch(OFMatch match) {
75         this.match = match;
76     }
77
78     /**
79      * @return the durationSeconds
80      */
81     public int getDurationSeconds() {
82         return durationSeconds;
83     }
84
85     /**
86      * @param durationSeconds the durationSeconds to set
87      */
88     public void setDurationSeconds(int durationSeconds) {
89         this.durationSeconds = durationSeconds;
90     }
91
92     /**
93      * @return the durationNanoseconds
94      */
95     public int getDurationNanoseconds() {
96         return durationNanoseconds;
97     }
98
99     /**
100      * @param durationNanoseconds the durationNanoseconds to set
101      */
102     public void setDurationNanoseconds(int durationNanoseconds) {
103         this.durationNanoseconds = durationNanoseconds;
104     }
105
106     /**
107      * @return the priority
108      */
109     public short getPriority() {
110         return priority;
111     }
112
113     /**
114      * @param priority the priority to set
115      */
116     public void setPriority(short priority) {
117         this.priority = priority;
118     }
119
120     /**
121      * @return the idleTimeout
122      */
123     public short getIdleTimeout() {
124         return idleTimeout;
125     }
126
127     /**
128      * @param idleTimeout the idleTimeout to set
129      */
130     public void setIdleTimeout(short idleTimeout) {
131         this.idleTimeout = idleTimeout;
132     }
133
134     /**
135      * @return the hardTimeout
136      */
137     public short getHardTimeout() {
138         return hardTimeout;
139     }
140
141     /**
142      * @param hardTimeout the hardTimeout to set
143      */
144     public void setHardTimeout(short hardTimeout) {
145         this.hardTimeout = hardTimeout;
146     }
147
148     /**
149      * @return the cookie
150      */
151     public long getCookie() {
152         return cookie;
153     }
154
155     /**
156      * @param cookie the cookie to set
157      */
158     public void setCookie(long cookie) {
159         this.cookie = cookie;
160     }
161
162     /**
163      * @return the packetCount
164      */
165     public long getPacketCount() {
166         return packetCount;
167     }
168
169     /**
170      * @param packetCount the packetCount to set
171      */
172     public void setPacketCount(long packetCount) {
173         this.packetCount = packetCount;
174     }
175
176     /**
177      * @return the byteCount
178      */
179     public long getByteCount() {
180         return byteCount;
181     }
182
183     /**
184      * @param byteCount the byteCount to set
185      */
186     public void setByteCount(long byteCount) {
187         this.byteCount = byteCount;
188     }
189
190     /**
191      * @param length the length to set
192      */
193     public void setLength(short length) {
194         this.length = length;
195     }
196
197     @Override
198     public int getLength() {
199         return U16.f(length);
200     }
201
202     /**
203      * @param actionFactory the actionFactory to set
204      */
205     @Override
206     public void setActionFactory(OFActionFactory actionFactory) {
207         this.actionFactory = actionFactory;
208     }
209
210     /**
211      * @return the actions
212      */
213     public List<OFAction> getActions() {
214         return actions;
215     }
216
217     /**
218      * @param actions the actions to set
219      */
220     public void setActions(List<OFAction> actions) {
221         this.actions = actions;
222     }
223
224     @Override
225     public void readFrom(ChannelBuffer data) {
226         this.length = data.readShort();
227         this.tableId = data.readByte();
228         data.readByte(); // pad
229         if (this.match == null)
230             this.match = new OFMatch();
231         this.match.readFrom(data);
232         this.durationSeconds = data.readInt();
233         this.durationNanoseconds = data.readInt();
234         this.priority = data.readShort();
235         this.idleTimeout = data.readShort();
236         this.hardTimeout = data.readShort();
237         data.readInt(); // pad
238         data.readShort(); // pad
239         this.cookie = data.readLong();
240         this.packetCount = data.readLong();
241         this.byteCount = data.readLong();
242         if (this.actionFactory == null)
243             throw new RuntimeException("OFActionFactory not set");
244         this.actions = this.actionFactory.parseActions(data, getLength() -
245                 MINIMUM_LENGTH);
246     }
247
248     @Override
249     public void writeTo(ChannelBuffer data) {
250         data.writeShort(this.length);
251         data.writeByte(this.tableId);
252         data.writeByte((byte) 0);
253         this.match.writeTo(data);
254         data.writeInt(this.durationSeconds);
255         data.writeInt(this.durationNanoseconds);
256         data.writeShort(this.priority);
257         data.writeShort(this.idleTimeout);
258         data.writeShort(this.hardTimeout);
259         data.writeInt(0); // pad
260         data.writeShort((short)0); // pad
261         data.writeLong(this.cookie);
262         data.writeLong(this.packetCount);
263         data.writeLong(this.byteCount);
264         if (actions != null) {
265             for (OFAction action : actions) {
266                 action.writeTo(data);
267             }
268         }
269     }
270
271     @Override
272     public String toString() {
273         String str = "match=" + this.match;
274         str += " tableId=" + this.tableId;
275         str += " durationSeconds=" + this.durationSeconds;
276         str += " durationNanoseconds=" + this.durationNanoseconds;
277         str += " priority=" + this.priority;
278         str += " idleTimeout=" + this.idleTimeout;
279         str += " hardTimeout=" + this.hardTimeout;
280         str += " cookie=" + this.cookie;
281         str += " packetCount=" + this.packetCount;
282         str += " byteCount=" + this.byteCount;
283         str += " action=" + this.actions;
284         
285         return str;
286     }
287     
288     @Override
289     public int hashCode() {
290         final int prime = 419;
291         int result = 1;
292         result = prime * result + (int) (byteCount ^ (byteCount >>> 32));
293         result = prime * result + (int) (cookie ^ (cookie >>> 32));
294         result = prime * result + durationNanoseconds;
295         result = prime * result + durationSeconds;
296         result = prime * result + hardTimeout;
297         result = prime * result + idleTimeout;
298         result = prime * result + length;
299         result = prime * result + ((match == null) ? 0 : match.hashCode());
300         result = prime * result + (int) (packetCount ^ (packetCount >>> 32));
301         result = prime * result + priority;
302         result = prime * result + tableId;
303         return result;
304     }
305
306     @Override
307     public boolean equals(Object obj) {
308         if (this == obj) {
309             return true;
310         }
311         if (obj == null) {
312             return false;
313         }
314         if (!(obj instanceof OFFlowStatisticsReply)) {
315             return false;
316         }
317         OFFlowStatisticsReply other = (OFFlowStatisticsReply) obj;
318         if (byteCount != other.byteCount) {
319             return false;
320         }
321         if (cookie != other.cookie) {
322             return false;
323         }
324         if (durationNanoseconds != other.durationNanoseconds) {
325             return false;
326         }
327         if (durationSeconds != other.durationSeconds) {
328             return false;
329         }
330         if (hardTimeout != other.hardTimeout) {
331             return false;
332         }
333         if (idleTimeout != other.idleTimeout) {
334             return false;
335         }
336         if (length != other.length) {
337             return false;
338         }
339         if (match == null) {
340             if (other.match != null) {
341                 return false;
342             }
343         } else if (!match.equals(other.match)) {
344             return false;
345         }
346         if (packetCount != other.packetCount) {
347             return false;
348         }
349         if (priority != other.priority) {
350             return false;
351         }
352         if (tableId != other.tableId) {
353             return false;
354         }
355         return true;
356     }
357 }