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