blob: 373962e49be07c640f8b7fb6bb7c8d3cbcdeca7f [file] [log] [blame]
Jonathan Hart23701d12014-04-03 10:45:48 -07001package net.onrc.onos.core.util;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08002
Pavlin Radoslavov8e5bab22013-04-02 04:19:41 +00003import java.math.BigInteger;
4
Jonathan Hart23701d12014-04-03 10:45:48 -07005import net.onrc.onos.core.util.serializers.FlowEntryIdDeserializer;
6import net.onrc.onos.core.util.serializers.FlowEntryIdSerializer;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08007
Pavlin Radoslavovf74271f2013-11-25 18:22:47 -08008import org.codehaus.jackson.annotate.JsonIgnore;
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -08009import org.codehaus.jackson.map.annotate.JsonDeserialize;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -080010import org.codehaus.jackson.map.annotate.JsonSerialize;
11
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080012/**
13 * The class representing a Flow Entry ID.
14 */
Ray Milkey269ffb92014-04-03 14:43:30 -070015@JsonDeserialize(using = FlowEntryIdDeserializer.class)
16@JsonSerialize(using = FlowEntryIdSerializer.class)
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080017public class FlowEntryId {
18 private long value;
19
20 /**
21 * Default constructor.
22 */
23 public FlowEntryId() {
Ray Milkey269ffb92014-04-03 14:43:30 -070024 this.value = -1;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080025 }
26
27 /**
28 * Constructor from an integer value.
29 *
30 * @param value the value to use.
31 */
32 public FlowEntryId(long value) {
Ray Milkey269ffb92014-04-03 14:43:30 -070033 this.value = value;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080034 }
35
36 /**
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080037 * Constructor from a string.
38 *
39 * @param value the value to use.
40 */
41 public FlowEntryId(String value) {
Ray Milkey269ffb92014-04-03 14:43:30 -070042 //
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 Radoslavov2013cbb2013-02-26 10:15:18 -080053 }
54
55 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080056 * Get the value of the Flow Entry ID.
57 *
58 * @return the value of the Flow Entry ID.
59 */
Ray Milkey269ffb92014-04-03 14:43:30 -070060 public long value() {
61 return value;
62 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080063
64 /**
65 * Set the value of the Flow Entry ID.
66 *
67 * @param value the value to set.
68 */
69 public void setValue(long value) {
Ray Milkey269ffb92014-04-03 14:43:30 -070070 this.value = value;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080071 }
Pavlin Radoslavovf74271f2013-11-25 18:22:47 -080072
73 /**
74 * Test whether the Flow Entry ID is valid.
75 *
76 * @return true if the Flow Entry ID is valid, otherwise false.
77 */
78 @JsonIgnore
79 public boolean isValid() {
Ray Milkey269ffb92014-04-03 14:43:30 -070080 return (this.value() != -1);
Pavlin Radoslavovf74271f2013-11-25 18:22:47 -080081 }
82
Brian O'Connora8e49802013-10-30 20:49:59 -070083 /**
Ray Milkey269ffb92014-04-03 14:43:30 -070084 * Returns true of the object is another Flow Entry ID with
Brian O'Connora8e49802013-10-30 20:49:59 -070085 * the same value; otherwise, returns false.
Ray Milkey269ffb92014-04-03 14:43:30 -070086 *
Brian O'Connora8e49802013-10-30 20:49:59 -070087 * @param Object to compare
88 */
89 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -070090 public boolean equals(Object obj) {
91 if (obj != null && obj.getClass() == this.getClass()) {
92 FlowEntryId entry = (FlowEntryId) obj;
93 return this.value() == entry.value();
94 }
95 return false;
Brian O'Connora8e49802013-10-30 20:49:59 -070096 }
Ray Milkey269ffb92014-04-03 14:43:30 -070097
Brian O'Connora8e49802013-10-30 20:49:59 -070098 /**
99 * Return the hash code of the Flow Entry ID
100 */
101 @Override
102 public int hashCode() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700103 return Long.valueOf(value).hashCode();
Brian O'Connora8e49802013-10-30 20:49:59 -0700104 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800105
106 /**
Pavlin Radoslavovad008e02013-02-21 18:42:42 -0800107 * Convert the Flow Entry ID value to a hexadecimal string.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800108 *
Pavlin Radoslavovad008e02013-02-21 18:42:42 -0800109 * @return the Flow Entry ID value to a hexadecimal string.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800110 */
111 @Override
112 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700113 return "0x" + Long.toHexString(this.value);
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800114 }
115}