a3803b2e0c40e85c7829ef74b84d0e9e0c5f90c6
[openflowjava.git] / third-party / openflow-codec / src / main / java / org / openflow / codec / protocol / statistics / OFPTableFeatures.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.factory.OFPTableFeaturePropFactory;
8 import org.openflow.codec.protocol.factory.OFPTableFeaturePropFactoryAware;
9 import org.openflow.codec.protocol.statistics.table.OFPTableFeaturePropHeader;
10 import org.openflow.codec.util.StringByteSerializer;
11
12 /**
13  * Represents an ofp_table_features structure
14  *
15  * @author AnilGujele
16  */
17 public class OFPTableFeatures implements OFPStatistics,
18                 OFPTableFeaturePropFactoryAware, Serializable
19 {
20         private static final int MINIMUM_LENGTH = 64;
21         private static final int MAX_TABLE_NAME_LEN = 32;
22
23         private short length = MINIMUM_LENGTH;
24         private byte tableId;
25         private String name;
26         private long metadataMatch;
27         private long metadataWrite;
28         private int config;
29         private int maxEntries;
30         private List<OFPTableFeaturePropHeader> properties;
31
32         private OFPTableFeaturePropFactory tableFeaturePropFactory;
33
34         /**
35          * @return the tableId
36          */
37         public byte getTableId()
38         {
39                 return tableId;
40         }
41
42         /**
43          * @param tableId
44          *            the tableId to set
45          */
46         public void setTableId(byte tableId)
47         {
48                 this.tableId = tableId;
49         }
50
51         /**
52          *
53          * @return
54          */
55         public String getName()
56         {
57                 return name;
58         }
59
60         /**
61          *
62          * @param name
63          */
64         public void setName(String name)
65         {
66                 this.name = name;
67         }
68
69         /**
70          *
71          * @return
72          */
73         public long getMetadataMatch()
74         {
75                 return metadataMatch;
76         }
77
78         /**
79          *
80          * @param metadataMatch
81          */
82         public void setMetadataMatch(long metadataMatch)
83         {
84                 this.metadataMatch = metadataMatch;
85         }
86
87         /**
88          *
89          * @return
90          */
91         public long getMetadataWrite()
92         {
93                 return metadataWrite;
94         }
95
96         /**
97          *
98          * @param metadataWrite
99          */
100         public void setMetadataWrite(long metadataWrite)
101         {
102                 this.metadataWrite = metadataWrite;
103         }
104
105         /**
106          *
107          * @return
108          */
109         public int getConfig()
110         {
111                 return config;
112         }
113
114         /**
115          *
116          * @param config
117          */
118         public void setConfig(int config)
119         {
120                 this.config = config;
121         }
122
123         /**
124          *
125          * @return
126          */
127         public int getMaxEntries()
128         {
129                 return maxEntries;
130         }
131
132         /**
133          *
134          * @param maxEntries
135          */
136         public void setMaxEntries(int maxEntries)
137         {
138                 this.maxEntries = maxEntries;
139         }
140
141         /**
142          *
143          * @return
144          */
145         public List<OFPTableFeaturePropHeader> getProperties()
146         {
147                 return properties;
148         }
149
150         /**
151          *
152          * @param properties
153          */
154         public void setProperties(List<OFPTableFeaturePropHeader> properties)
155         {
156                 this.properties = properties;
157                 updateLength();
158         }
159
160         /**
161          * update length TODO: All the associated properties are 8 byte (64 bits)
162          * aligned. Length of each property excludes the padding, if we sum up each
163          * associated property length it will give us length excluding the total
164          * bytes of padding added to make it align. We assume that table feature
165          * response message switch will send in response to OFPMP_TABLE_FEATURES
166          * request, will also consider padding in the message lengh. Something to
167          * cross check with switch implementation.
168          */
169         private void updateLength()
170         {
171                 length = MINIMUM_LENGTH;
172                 if (null != properties)
173                 {
174                         for (OFPTableFeaturePropHeader prop : properties)
175                         {
176                                 int len = prop.getLengthU();
177                                 /* Add the aligned length, including padding */
178                                 length += len + (8 - (len % 8));
179
180                         }
181                 }
182         }
183
184         @Override
185         public int getLength()
186         {
187                 return length;
188         }
189
190         @Override
191         public void readFrom(IDataBuffer data)
192         {
193
194                 this.length = data.getShort();
195                 this.tableId = data.get();
196                 data.get(); // pad
197                 data.getInt(); // pad
198                 this.name = StringByteSerializer.readFrom(data, MAX_TABLE_NAME_LEN);
199                 this.metadataMatch = data.getLong();
200                 this.metadataWrite = data.getLong();
201                 this.config = data.getInt();
202                 this.maxEntries = data.getInt();
203                 int propLength = this.length - OFPTableFeatures.MINIMUM_LENGTH;
204                 if (null == this.tableFeaturePropFactory)
205                         throw new RuntimeException("OFPTableFeaturePropFactory is not set");
206                 this.properties = tableFeaturePropFactory.parseTableFeatureProp(data,
207                                 propLength);
208                 if (this.properties.isEmpty())
209                 {
210                         properties = null;
211                 }
212
213         }
214
215         @Override
216         public void writeTo(IDataBuffer data)
217         {
218
219                 data.putShort(length);
220                 data.put(this.tableId);
221                 data.put((byte) 0); // pad
222                 data.putInt(0); // pad
223                 StringByteSerializer.writeTo(data, MAX_TABLE_NAME_LEN, this.name);
224                 data.putLong(this.metadataMatch);
225                 data.putLong(this.metadataWrite);
226                 data.putInt(this.config);
227                 data.putInt(this.maxEntries);
228                 if (null != properties)
229                 {
230                         for (OFPTableFeaturePropHeader prop : properties)
231                         {
232                                 prop.writeTo(data);
233                         }
234                 }
235         }
236
237         @Override
238         public int hashCode()
239         {
240                 final int prime = 4491;
241                 int result = 1;
242                 result = prime * result + maxEntries;
243                 result = prime * result + config;
244                 result = prime * result
245                                 + (int) (metadataMatch ^ (metadataMatch >>> 32));
246                 result = prime * result
247                                 + (int) (metadataWrite ^ (metadataWrite >>> 32));
248                 result = prime * result + ((name == null) ? 0 : name.hashCode());
249                 result = prime * result + tableId;
250                 result = prime * result + length;
251                 result = prime * result
252                                 + ((properties == null) ? 0 : properties.hashCode());
253                 return result;
254         }
255
256         @Override
257         public boolean equals(Object obj)
258         {
259                 if (this == obj)
260                 {
261                         return true;
262                 }
263                 if (obj == null)
264                 {
265                         return false;
266                 }
267                 if (!(obj instanceof OFPTableFeatures))
268                 {
269                         return false;
270                 }
271                 OFPTableFeatures other = (OFPTableFeatures) obj;
272                 if (maxEntries != other.maxEntries)
273                 {
274                         return false;
275                 }
276                 if (config != other.config)
277                 {
278                         return false;
279                 }
280                 if (metadataMatch != other.metadataMatch)
281                 {
282                         return false;
283                 }
284                 if (metadataWrite != other.metadataWrite)
285                 {
286                         return false;
287                 }
288                 if (length != other.length)
289                 {
290                         return false;
291                 }
292                 if (tableId != other.tableId)
293                 {
294                         return false;
295                 }
296                 if (name == null)
297                 {
298                         if (other.name != null)
299                         {
300                                 return false;
301                         }
302                 } else if (!name.equals(other.name))
303                 {
304                         return false;
305                 }
306                 if (properties == null)
307                 {
308                         if (other.properties != null)
309                         {
310                                 return false;
311                         }
312                 } else if (!properties.equals(other.properties))
313                 {
314                         return false;
315                 }
316                 return true;
317         }
318
319         @Override
320         public void setTableFeaturePropFactory(
321                         OFPTableFeaturePropFactory tableFeaturePropFactory)
322         {
323
324                 this.tableFeaturePropFactory = tableFeaturePropFactory;
325         }
326 }