HIGUCHI Yuta | 356086e | 2013-06-12 15:21:19 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.util; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 2 | |
Pavlin Radoslavov | 8e5bab2 | 2013-04-02 04:19:41 +0000 | [diff] [blame] | 3 | import java.math.BigInteger; |
| 4 | |
HIGUCHI Yuta | f086d8a | 2013-06-12 15:26:35 -0700 | [diff] [blame] | 5 | import net.onrc.onos.ofcontroller.util.serializers.FlowIdDeserializer; |
| 6 | import net.onrc.onos.ofcontroller.util.serializers.FlowIdSerializer; |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 7 | |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 8 | import org.codehaus.jackson.map.annotate.JsonDeserialize; |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 9 | import org.codehaus.jackson.map.annotate.JsonSerialize; |
| 10 | |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 11 | /** |
| 12 | * The class representing a Flow ID. |
| 13 | */ |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 14 | @JsonDeserialize(using=FlowIdDeserializer.class) |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 15 | @JsonSerialize(using=FlowIdSerializer.class) |
Toshio Koide | fff6642 | 2013-06-26 13:57:24 -0700 | [diff] [blame] | 16 | public class FlowId implements Comparable<FlowId> { |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 17 | private long value; |
| 18 | |
| 19 | /** |
| 20 | * Default constructor. |
| 21 | */ |
| 22 | public FlowId() { |
| 23 | this.value = 0; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Constructor from an integer value. |
| 28 | * |
| 29 | * @param value the value to use. |
| 30 | */ |
| 31 | public FlowId(long value) { |
| 32 | this.value = value; |
| 33 | } |
| 34 | |
| 35 | /** |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 36 | * Constructor from a string. |
| 37 | * |
| 38 | * @param value the value to use. |
| 39 | */ |
| 40 | public FlowId(String value) { |
Pavlin Radoslavov | 8e5bab2 | 2013-04-02 04:19:41 +0000 | [diff] [blame] | 41 | // |
| 42 | // Use the help of BigInteger to parse strings representing |
| 43 | // large unsigned hex long values. |
| 44 | // |
| 45 | char c = 0; |
| 46 | if (value.length() > 2) |
| 47 | c = value.charAt(1); |
| 48 | if ((c == 'x') || (c == 'X')) |
| 49 | this.value = new BigInteger(value.substring(2), 16).longValue(); |
| 50 | else |
| 51 | this.value = Long.decode(value); |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 55 | * Get the value of the Flow ID. |
| 56 | * |
| 57 | * @return the value of the Flow ID. |
| 58 | */ |
| 59 | public long value() { return value; } |
| 60 | |
| 61 | /** |
| 62 | * Set the value of the Flow ID. |
| 63 | * |
| 64 | * @param value the value to set. |
| 65 | */ |
| 66 | public void setValue(long value) { |
| 67 | this.value = value; |
| 68 | } |
| 69 | |
| 70 | /** |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 71 | * Convert the Flow ID value to a hexadecimal string. |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 72 | * |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 73 | * @return the Flow ID value to a hexadecimal string. |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 74 | */ |
| 75 | @Override |
| 76 | public String toString() { |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 77 | return "0x" + Long.toHexString(this.value); |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 78 | } |
Toshio Koide | fff6642 | 2013-06-26 13:57:24 -0700 | [diff] [blame] | 79 | |
Toshio Koide | d8651e7 | 2013-06-27 11:28:11 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Compare two FlowId objects numerically using their Flow IDs. |
| 82 | * |
| 83 | * @return the value 0 if the Flow ID is equal to the argument's Flow ID; |
| 84 | * a value less than 0 if the Flow ID is numerically less than the argument's Flow ID; |
| 85 | * and a value greater than 0 if the Flow ID is numerically greater than the argument's Flow ID. |
| 86 | */ |
| 87 | @Override |
Toshio Koide | fff6642 | 2013-06-26 13:57:24 -0700 | [diff] [blame] | 88 | public int compareTo(FlowId o) { |
| 89 | return Long.valueOf(this.value).compareTo(o.value()); |
| 90 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 91 | } |