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