Merge "Bug 2347: DOMConcurrentDataCommitCoordinator uses wrong phase name"
[controller.git] / third-party / openflowj / src / main / java / org / openflow / protocol / OFPhysicalPort.java
1 package org.openflow.protocol;
2
3 import java.io.Serializable;
4 import java.io.UnsupportedEncodingException;
5 import java.nio.ByteBuffer;
6 import java.nio.charset.Charset;
7 import java.util.Arrays;
8 import java.util.HashMap;
9
10
11
12 /**
13  * Represents ofp_phy_port
14  * @author David Erickson (daviderickson@cs.stanford.edu) - Mar 25, 2010
15  */
16 public class OFPhysicalPort implements Cloneable, Serializable {
17     public static int MINIMUM_LENGTH = 48;
18     public static int OFP_ETH_ALEN = 6;
19
20     public enum OFPortConfig {
21         OFPPC_PORT_DOWN    (1 << 0),
22         OFPPC_NO_STP       (1 << 1),
23         OFPPC_NO_RECV      (1 << 2),
24         OFPPC_NO_RECV_STP  (1 << 3),
25         OFPPC_NO_FLOOD     (1 << 4),
26         OFPPC_NO_FWD       (1 << 5),
27         OFPPC_NO_PACKET_IN (1 << 6);
28
29         protected int value;
30
31         private OFPortConfig(int value) {
32             this.value = value;
33         }
34
35         /**
36          * @return the value
37          */
38         public int getValue() {
39             return value;
40         }
41     }
42
43     public enum OFPortState {
44         OFPPS_LINK_DOWN   (1 << 0),
45         OFPPS_STP_LISTEN  (0 << 8),
46         OFPPS_STP_LEARN   (1 << 8),
47         OFPPS_STP_FORWARD (2 << 8),
48         OFPPS_STP_BLOCK   (3 << 8),
49         OFPPS_STP_MASK    (3 << 8);
50
51         protected int value;
52
53         private OFPortState(int value) {
54             this.value = value;
55         }
56
57         /**
58          * @return the value
59          */
60         public int getValue() {
61             return value;
62         }
63     }
64
65     public enum OFPortFeatures {
66         OFPPF_10MB_HD    (1 << 0),
67         OFPPF_10MB_FD    (1 << 1),
68         OFPPF_100MB_HD   (1 << 2),
69         OFPPF_100MB_FD   (1 << 3),
70         OFPPF_1GB_HD     (1 << 4),
71         OFPPF_1GB_FD     (1 << 5),
72         OFPPF_10GB_FD    (1 << 6),
73         OFPPF_COPPER     (1 << 7),
74         OFPPF_FIBER      (1 << 8),
75         OFPPF_AUTONEG    (1 << 9),
76         OFPPF_PAUSE      (1 << 10),
77         OFPPF_PAUSE_ASYM (1 << 11);
78
79         protected int value;
80
81         private OFPortFeatures(int value) {
82             this.value = value;
83         }
84
85         /**
86          * @return the value
87          */
88         public int getValue() {
89             return value;
90         }
91     }
92
93     protected short portNumber;
94     protected byte[] hardwareAddress;
95     protected String name;
96     protected int config;
97     protected int state;
98     protected int currentFeatures;
99     protected int advertisedFeatures;
100     protected int supportedFeatures;
101     protected int peerFeatures;
102
103     /**
104      * @return the portNumber
105      */
106     public short getPortNumber() {
107         return portNumber;
108     }
109
110     /**
111      * @param portNumber the portNumber to set
112      */
113     public void setPortNumber(short portNumber) {
114         this.portNumber = portNumber;
115     }
116
117     /**
118      * @return the hardwareAddress
119      */
120     public byte[] getHardwareAddress() {
121         return hardwareAddress;
122     }
123
124     /**
125      * @param hardwareAddress the hardwareAddress to set
126      */
127     public void setHardwareAddress(byte[] hardwareAddress) {
128         if (hardwareAddress.length != OFP_ETH_ALEN)
129             throw new RuntimeException("Hardware address must have length "
130                     + OFP_ETH_ALEN);
131         this.hardwareAddress = hardwareAddress;
132     }
133
134     /**
135      * @return the name
136      */
137     public String getName() {
138         return name;
139     }
140
141     /**
142      * @param name the name to set
143      */
144     public void setName(String name) {
145         this.name = name;
146     }
147
148     /**
149      * @return the config
150      */
151     public int getConfig() {
152         return config;
153     }
154
155     /**
156      * @param config the config to set
157      */
158     public void setConfig(int config) {
159         this.config = config;
160     }
161
162     /**
163      * @return the state
164      */
165     public int getState() {
166         return state;
167     }
168
169     /**
170      * @param state the state to set
171      */
172     public void setState(int state) {
173         this.state = state;
174     }
175
176     /**
177      * @return the currentFeatures
178      */
179     public int getCurrentFeatures() {
180         return currentFeatures;
181     }
182
183     /**
184      * @param currentFeatures the currentFeatures to set
185      */
186     public void setCurrentFeatures(int currentFeatures) {
187         this.currentFeatures = currentFeatures;
188     }
189
190     /**
191      * @return the advertisedFeatures
192      */
193     public int getAdvertisedFeatures() {
194         return advertisedFeatures;
195     }
196
197     /**
198      * @param advertisedFeatures the advertisedFeatures to set
199      */
200     public void setAdvertisedFeatures(int advertisedFeatures) {
201         this.advertisedFeatures = advertisedFeatures;
202     }
203
204     /**
205      * @return the supportedFeatures
206      */
207     public int getSupportedFeatures() {
208         return supportedFeatures;
209     }
210
211     /**
212      * @param supportedFeatures the supportedFeatures to set
213      */
214     public void setSupportedFeatures(int supportedFeatures) {
215         this.supportedFeatures = supportedFeatures;
216     }
217
218     /**
219      * @return the peerFeatures
220      */
221     public int getPeerFeatures() {
222         return peerFeatures;
223     }
224
225     /**
226      * @param peerFeatures the peerFeatures to set
227      */
228     public void setPeerFeatures(int peerFeatures) {
229         this.peerFeatures = peerFeatures;
230     }
231
232     /**
233      * Read this message off the wire from the specified ByteBuffer
234      * @param data
235      */
236     public void readFrom(ByteBuffer data) {
237         this.portNumber = data.getShort();
238         if (this.hardwareAddress == null)
239             this.hardwareAddress = new byte[OFP_ETH_ALEN];
240         data.get(this.hardwareAddress);
241         byte[] name = new byte[16];
242         data.get(name);
243         // find the first index of 0
244         int index = 0;
245         for (byte b : name) {
246             if (0 == b)
247                 break;
248             ++index;
249         }
250         this.name = new String(Arrays.copyOf(name, index),
251                 Charset.forName("ascii"));
252         this.config = data.getInt();
253         this.state = data.getInt();
254         this.currentFeatures = data.getInt();
255         this.advertisedFeatures = data.getInt();
256         this.supportedFeatures = data.getInt();
257         this.peerFeatures = data.getInt();
258     }
259
260     /**
261      * Write this message's binary format to the specified ByteBuffer
262      * @param data
263      */
264     public void writeTo(ByteBuffer data) {
265         data.putShort(this.portNumber);
266         data.put(hardwareAddress);
267         try {
268             byte[] name = this.name.getBytes("ASCII");
269             if (name.length < 16) {
270                 data.put(name);
271                 for (int i = name.length; i < 16; ++i) {
272                     data.put((byte) 0);
273                 }
274             } else {
275                 data.put(name, 0, 15);
276                 data.put((byte) 0);
277             }
278         } catch (UnsupportedEncodingException e) {
279             throw new RuntimeException(e);
280         }
281         data.putInt(this.config);
282         data.putInt(this.state);
283         data.putInt(this.currentFeatures);
284         data.putInt(this.advertisedFeatures);
285         data.putInt(this.supportedFeatures);
286         data.putInt(this.peerFeatures);
287     }
288
289     @Override
290     public int hashCode() {
291         final int prime = 307;
292         int result = 1;
293         result = prime * result + advertisedFeatures;
294         result = prime * result + config;
295         result = prime * result + currentFeatures;
296         result = prime * result + Arrays.hashCode(hardwareAddress);
297         result = prime * result + ((name == null) ? 0 : name.hashCode());
298         result = prime * result + peerFeatures;
299         result = prime * result + portNumber;
300         result = prime * result + state;
301         result = prime * result + supportedFeatures;
302         return result;
303     }
304
305     @Override
306     public boolean equals(Object obj) {
307         if (this == obj) {
308             return true;
309         }
310         if (obj == null) {
311             return false;
312         }
313         if (!(obj instanceof OFPhysicalPort)) {
314             return false;
315         }
316         OFPhysicalPort other = (OFPhysicalPort) obj;
317         if (advertisedFeatures != other.advertisedFeatures) {
318             return false;
319         }
320         if (config != other.config) {
321             return false;
322         }
323         if (currentFeatures != other.currentFeatures) {
324             return false;
325         }
326         if (!Arrays.equals(hardwareAddress, other.hardwareAddress)) {
327             return false;
328         }
329         if (name == null) {
330             if (other.name != null) {
331                 return false;
332             }
333         } else if (!name.equals(other.name)) {
334             return false;
335         }
336         if (peerFeatures != other.peerFeatures) {
337             return false;
338         }
339         if (portNumber != other.portNumber) {
340             return false;
341         }
342         if (state != other.state) {
343             return false;
344         }
345         if (supportedFeatures != other.supportedFeatures) {
346             return false;
347         }
348         return true;
349     }
350     
351     public OFPhysicalPort cloneOFPhysicalPort() {
352         OFPhysicalPort p;
353         try
354         {
355             p = (OFPhysicalPort) this.clone();
356             
357         }
358         catch (CloneNotSupportedException e)
359         {
360             throw new AssertionError();
361         }
362         return p;
363     }
364     
365 }