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.FlowEntryIdDeserializer; |
| 6 | import net.onrc.onos.ofcontroller.util.serializers.FlowEntryIdSerializer; |
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 Entry ID. |
| 14 | */ |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 15 | @JsonDeserialize(using=FlowEntryIdDeserializer.class) |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 16 | @JsonSerialize(using=FlowEntryIdSerializer.class) |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 17 | public class FlowEntryId { |
| 18 | private long value; |
| 19 | |
| 20 | /** |
| 21 | * Default constructor. |
| 22 | */ |
| 23 | public FlowEntryId() { |
Pavlin Radoslavov | f74271f | 2013-11-25 18:22:47 -0800 | [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 FlowEntryId(long value) { |
| 33 | this.value = value; |
| 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 FlowEntryId(String value) { |
Pavlin Radoslavov | 8e5bab2 | 2013-04-02 04:19:41 +0000 | [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 Entry ID. |
| 57 | * |
| 58 | * @return the value of the Flow Entry ID. |
| 59 | */ |
| 60 | public long value() { return value; } |
| 61 | |
| 62 | /** |
| 63 | * Set the value of the Flow Entry ID. |
| 64 | * |
| 65 | * @param value the value to set. |
| 66 | */ |
| 67 | public void setValue(long value) { |
| 68 | this.value = value; |
| 69 | } |
Pavlin Radoslavov | f74271f | 2013-11-25 18:22:47 -0800 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Test whether the Flow Entry ID is valid. |
| 73 | * |
| 74 | * @return true if the Flow Entry ID is valid, otherwise false. |
| 75 | */ |
| 76 | @JsonIgnore |
| 77 | public boolean isValid() { |
| 78 | return (this.value() != -1); |
| 79 | } |
| 80 | |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 81 | /** |
| 82 | * Returns true of the object is another Flow Entry ID with |
| 83 | * the same value; otherwise, returns false. |
| 84 | * |
| 85 | * @param Object to compare |
| 86 | */ |
| 87 | @Override |
| 88 | public boolean equals(Object obj){ |
Naoki Shiota | 1a5ca91 | 2014-01-03 17:02:31 -0800 | [diff] [blame] | 89 | if(obj != null && obj.getClass() == this.getClass()) { |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 90 | FlowEntryId entry = (FlowEntryId) obj; |
| 91 | return this.value() == entry.value(); |
| 92 | } |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Return the hash code of the Flow Entry ID |
| 98 | */ |
| 99 | @Override |
| 100 | public int hashCode() { |
| 101 | return Long.valueOf(value).hashCode(); |
| 102 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 103 | |
| 104 | /** |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 105 | * Convert the Flow Entry ID value to a hexadecimal string. |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 106 | * |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 107 | * @return the Flow Entry ID value to a hexadecimal string. |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 108 | */ |
| 109 | @Override |
| 110 | public String toString() { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 111 | return "0x" + Long.toHexString(this.value); |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 112 | } |
| 113 | } |