Performacne improvements via adding a netty-based openflowj and openflow plugin;...
[controller.git] / third-party / openflowj_netty / src / main / java / org / openflow / protocol / OFPhysicalPort.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;
19
20 import java.io.UnsupportedEncodingException;
21 import java.nio.charset.Charset;
22 import java.util.Arrays;
23
24
25 import org.jboss.netty.buffer.ChannelBuffer;
26
27 /**
28  * Represents ofp_phy_port
29  * @author David Erickson (daviderickson@cs.stanford.edu) - Mar 25, 2010
30  */
31 public class OFPhysicalPort {
32     public static int MINIMUM_LENGTH = 48;
33     public static int OFP_ETH_ALEN = 6;
34
35     public enum OFPortConfig {
36         OFPPC_PORT_DOWN    (1 << 0) {
37             public String toString() {
38                 return "port-down (0x1)";
39             }
40         },
41         OFPPC_NO_STP       (1 << 1) {
42             public String toString() {
43                 return "no-stp (0x2)";
44             }
45         },
46         OFPPC_NO_RECV      (1 << 2) {
47             public String toString() {
48                 return "no-recv (0x4)";
49             }
50         },
51         OFPPC_NO_RECV_STP  (1 << 3) {
52             public String toString() {
53                 return "no-recv-stp (0x8)";
54             }
55         },
56         OFPPC_NO_FLOOD     (1 << 4) {
57             public String toString() {
58                 return "no-flood (0x10)";
59             }
60         },
61         OFPPC_NO_FWD       (1 << 5) {
62             public String toString() {
63                 return "no-fwd (0x20)";
64             }
65         },
66         OFPPC_NO_PACKET_IN (1 << 6) {
67             public String toString() {
68                 return "no-pkt-in (0x40)";
69             }
70         };
71
72         protected int value;
73
74         private OFPortConfig(int value) {
75             this.value = value;
76         }
77
78         /**
79          * @return the value
80          */
81         public int getValue() {
82             return value;
83         }
84     }
85
86     public enum OFPortState {
87         OFPPS_LINK_DOWN   (1 << 0) {
88             public String toString() {
89                 return "link-down (0x1)";
90             }
91         },
92         OFPPS_STP_LISTEN  (0 << 8) {
93             public String toString() {
94                 return "listen (0x0)";
95             }
96         },
97         OFPPS_STP_LEARN   (1 << 8) {
98             public String toString() {
99                 return "learn-no-relay (0x100)";
100             }
101         },
102         OFPPS_STP_FORWARD (2 << 8) {
103             public String toString() {
104                 return "forward (0x200)";
105             }
106         },
107         OFPPS_STP_BLOCK   (3 << 8) {
108             public String toString() {
109                 return "block-broadcast (0x300)";
110             }
111         },
112         OFPPS_STP_MASK    (3 << 8) {
113             public String toString() {
114                 return "block-broadcast (0x300)";
115             }
116         };
117
118         protected int value;
119
120         private OFPortState(int value) {
121             this.value = value;
122         }
123
124         /**
125          * @return the value
126          */
127         public int getValue() {
128             return value;
129         }
130     }
131
132     public enum OFPortFeatures {
133         OFPPF_10MB_HD    (1 << 0) {
134             public String toString() {
135                 return "10mb-hd (0x1)";
136             }
137         },
138         OFPPF_10MB_FD    (1 << 1) {
139             public String toString() {
140                 return "10mb-fd (0x2)";
141             }
142         },
143         OFPPF_100MB_HD   (1 << 2) {
144             public String toString() {
145                 return "100mb-hd (0x4)";
146             }
147         },
148         OFPPF_100MB_FD   (1 << 3) {
149             public String toString() {
150                 return "100mb-fd (0x8)";
151             }
152         },
153         OFPPF_1GB_HD     (1 << 4) {
154             public String toString() {
155                 return "1gb-hd (0x10)";
156             }
157         },
158         OFPPF_1GB_FD     (1 << 5) {
159             public String toString() {
160                 return "1gb-fd (0x20)";
161             }
162         },
163         OFPPF_10GB_FD    (1 << 6) {
164             public String toString() {
165                 return "10gb-fd (0x40)";
166             }
167         },
168         OFPPF_COPPER     (1 << 7) {
169             public String toString() {
170                 return "copper (0x80)";
171             }
172         },
173         OFPPF_FIBER      (1 << 8) {
174             public String toString() {
175                 return "fiber (0x100)";
176             }
177         },
178         OFPPF_AUTONEG    (1 << 9) {
179             public String toString() {
180                 return "autoneg (0x200)";
181             }
182         },
183         OFPPF_PAUSE      (1 << 10) {
184             public String toString() {
185                 return "pause (0x400)";
186             }
187         },
188         OFPPF_PAUSE_ASYM (1 << 11) {
189             public String toString() {
190                 return "pause-asym (0x800)";
191             }
192         };
193
194         protected int value;
195
196         private OFPortFeatures(int value) {
197             this.value = value;
198         }
199
200         /**
201          * @return the value
202          */
203         public int getValue() {
204             return value;
205         }
206     }
207
208     protected short portNumber;
209     protected byte[] hardwareAddress;
210     protected String name;
211     protected int config;
212     protected int state;
213     protected int currentFeatures;
214     protected int advertisedFeatures;
215     protected int supportedFeatures;
216     protected int peerFeatures;
217
218     /**
219      * @return the portNumber
220      */
221     public short getPortNumber() {
222         return portNumber;
223     }
224
225     /**
226      * @param portNumber the portNumber to set
227      */
228     public void setPortNumber(short portNumber) {
229         this.portNumber = portNumber;
230     }
231
232     /**
233      * @return the hardwareAddress
234      */
235     public byte[] getHardwareAddress() {
236         return hardwareAddress;
237     }
238
239     /**
240      * @param hardwareAddress the hardwareAddress to set
241      */
242     public void setHardwareAddress(byte[] hardwareAddress) {
243         if (hardwareAddress.length != OFP_ETH_ALEN)
244             throw new RuntimeException("Hardware address must have length "
245                     + OFP_ETH_ALEN);
246         this.hardwareAddress = hardwareAddress;
247     }
248
249     /**
250      * @return the name
251      */
252     public String getName() {
253         return name;
254     }
255
256     /**
257      * @param name the name to set
258      */
259     public void setName(String name) {
260         this.name = name;
261     }
262
263     /**
264      * @return the config
265      */
266     public int getConfig() {
267         return config;
268     }
269
270     /**
271      * @param config the config to set
272      */
273     public void setConfig(int config) {
274         this.config = config;
275     }
276
277     /**
278      * @return the state
279      */
280     public int getState() {
281         return state;
282     }
283
284     /**
285      * @param state the state to set
286      */
287     public void setState(int state) {
288         this.state = state;
289     }
290
291     /**
292      * @return the currentFeatures
293      */
294     public int getCurrentFeatures() {
295         return currentFeatures;
296     }
297
298     /**
299      * @param currentFeatures the currentFeatures to set
300      */
301     public void setCurrentFeatures(int currentFeatures) {
302         this.currentFeatures = currentFeatures;
303     }
304
305     /**
306      * @return the advertisedFeatures
307      */
308     public int getAdvertisedFeatures() {
309         return advertisedFeatures;
310     }
311
312     /**
313      * @param advertisedFeatures the advertisedFeatures to set
314      */
315     public void setAdvertisedFeatures(int advertisedFeatures) {
316         this.advertisedFeatures = advertisedFeatures;
317     }
318
319     /**
320      * @return the supportedFeatures
321      */
322     public int getSupportedFeatures() {
323         return supportedFeatures;
324     }
325
326     /**
327      * @param supportedFeatures the supportedFeatures to set
328      */
329     public void setSupportedFeatures(int supportedFeatures) {
330         this.supportedFeatures = supportedFeatures;
331     }
332
333     /**
334      * @return the peerFeatures
335      */
336     public int getPeerFeatures() {
337         return peerFeatures;
338     }
339
340     /**
341      * @param peerFeatures the peerFeatures to set
342      */
343     public void setPeerFeatures(int peerFeatures) {
344         this.peerFeatures = peerFeatures;
345     }
346
347     /**
348      * Read this message off the wire from the specified ByteBuffer
349      * @param data
350      */
351     public void readFrom(ChannelBuffer data) {
352         this.portNumber = data.readShort();
353         if (this.hardwareAddress == null)
354             this.hardwareAddress = new byte[OFP_ETH_ALEN];
355         data.readBytes(this.hardwareAddress);
356         byte[] name = new byte[16];
357         data.readBytes(name);
358         // find the first index of 0
359         int index = 0;
360         for (byte b : name) {
361             if (0 == b)
362                 break;
363             ++index;
364         }
365         this.name = new String(Arrays.copyOf(name, index),
366                 Charset.forName("ascii"));
367         this.config = data.readInt();
368         this.state = data.readInt();
369         this.currentFeatures = data.readInt();
370         this.advertisedFeatures = data.readInt();
371         this.supportedFeatures = data.readInt();
372         this.peerFeatures = data.readInt();
373     }
374
375     /**
376      * Write this message's binary format to the specified ByteBuffer
377      * @param data
378      */
379     public void writeTo(ChannelBuffer data) {
380         data.writeShort(this.portNumber);
381         data.writeBytes(hardwareAddress);
382         try {
383             byte[] name = this.name.getBytes("ASCII");
384             if (name.length < 16) {
385                 data.writeBytes(name);
386                 for (int i = name.length; i < 16; ++i) {
387                     data.writeByte((byte) 0);
388                 }
389             } else {
390                 data.writeBytes(name, 0, 15);
391                 data.writeByte((byte) 0);
392             }
393         } catch (UnsupportedEncodingException e) {
394             throw new RuntimeException(e);
395         }
396         data.writeInt(this.config);
397         data.writeInt(this.state);
398         data.writeInt(this.currentFeatures);
399         data.writeInt(this.advertisedFeatures);
400         data.writeInt(this.supportedFeatures);
401         data.writeInt(this.peerFeatures);
402     }
403
404     @Override
405     public int hashCode() {
406         final int prime = 307;
407         int result = 1;
408         result = prime * result + advertisedFeatures;
409         result = prime * result + config;
410         result = prime * result + currentFeatures;
411         result = prime * result + Arrays.hashCode(hardwareAddress);
412         result = prime * result + ((name == null) ? 0 : name.hashCode());
413         result = prime * result + peerFeatures;
414         result = prime * result + portNumber;
415         result = prime * result + state;
416         result = prime * result + supportedFeatures;
417         return result;
418     }
419
420     @Override
421     public boolean equals(Object obj) {
422         if (this == obj) {
423             return true;
424         }
425         if (obj == null) {
426             return false;
427         }
428         if (!(obj instanceof OFPhysicalPort)) {
429             return false;
430         }
431         OFPhysicalPort other = (OFPhysicalPort) obj;
432         if (advertisedFeatures != other.advertisedFeatures) {
433             return false;
434         }
435         if (config != other.config) {
436             return false;
437         }
438         if (currentFeatures != other.currentFeatures) {
439             return false;
440         }
441         if (!Arrays.equals(hardwareAddress, other.hardwareAddress)) {
442             return false;
443         }
444         if (name == null) {
445             if (other.name != null) {
446                 return false;
447             }
448         } else if (!name.equals(other.name)) {
449             return false;
450         }
451         if (peerFeatures != other.peerFeatures) {
452             return false;
453         }
454         if (portNumber != other.portNumber) {
455             return false;
456         }
457         if (state != other.state) {
458             return false;
459         }
460         if (supportedFeatures != other.supportedFeatures) {
461             return false;
462         }
463         return true;
464     }
465 }