blob: 5b19e91a675ccbc66c444de47eb4d52e50b85312 [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.
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070014 * This class is immutable.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080015 */
Ray Milkey269ffb92014-04-03 14:43:30 -070016@JsonDeserialize(using = FlowEntryIdDeserializer.class)
17@JsonSerialize(using = FlowEntryIdSerializer.class)
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070018public final class FlowEntryId {
Ray Milkeyec838942014-04-09 11:28:43 -070019 private static final long INVALID = -1;
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070020 private final long value;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080021
22 /**
23 * Default constructor.
24 */
25 public FlowEntryId() {
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070026 this.value = FlowEntryId.INVALID;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080027 }
28
29 /**
30 * Constructor from an integer value.
31 *
32 * @param value the value to use.
33 */
34 public FlowEntryId(long value) {
Ray Milkey269ffb92014-04-03 14:43:30 -070035 this.value = value;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080036 }
37
38 /**
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080039 * Constructor from a string.
40 *
41 * @param value the value to use.
42 */
43 public FlowEntryId(String value) {
Ray Milkey269ffb92014-04-03 14:43:30 -070044 //
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 Radoslavov2013cbb2013-02-26 10:15:18 -080055 }
56
57 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080058 * Get the value of the Flow Entry ID.
59 *
60 * @return the value of the Flow Entry ID.
61 */
Ray Milkey269ffb92014-04-03 14:43:30 -070062 public long value() {
63 return value;
64 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080065
66 /**
Pavlin Radoslavovf74271f2013-11-25 18:22:47 -080067 * Test whether the Flow Entry ID is valid.
68 *
69 * @return true if the Flow Entry ID is valid, otherwise false.
70 */
71 @JsonIgnore
72 public boolean isValid() {
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070073 return (this.value() != FlowEntryId.INVALID);
Pavlin Radoslavovf74271f2013-11-25 18:22:47 -080074 }
75
Brian O'Connora8e49802013-10-30 20:49:59 -070076 /**
Ray Milkey269ffb92014-04-03 14:43:30 -070077 * Returns true of the object is another Flow Entry ID with
Brian O'Connora8e49802013-10-30 20:49:59 -070078 * the same value; otherwise, returns false.
Ray Milkey269ffb92014-04-03 14:43:30 -070079 *
Brian O'Connora8e49802013-10-30 20:49:59 -070080 * @param Object to compare
81 */
82 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -070083 public boolean equals(Object obj) {
84 if (obj != null && obj.getClass() == this.getClass()) {
85 FlowEntryId entry = (FlowEntryId) obj;
86 return this.value() == entry.value();
87 }
88 return false;
Brian O'Connora8e49802013-10-30 20:49:59 -070089 }
Ray Milkey269ffb92014-04-03 14:43:30 -070090
Brian O'Connora8e49802013-10-30 20:49:59 -070091 /**
92 * Return the hash code of the Flow Entry ID
93 */
94 @Override
95 public int hashCode() {
Ray Milkey269ffb92014-04-03 14:43:30 -070096 return Long.valueOf(value).hashCode();
Brian O'Connora8e49802013-10-30 20:49:59 -070097 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080098
99 /**
Pavlin Radoslavovad008e02013-02-21 18:42:42 -0800100 * Convert the Flow Entry ID value to a hexadecimal string.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800101 *
Pavlin Radoslavovad008e02013-02-21 18:42:42 -0800102 * @return the Flow Entry ID value to a hexadecimal string.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800103 */
104 @Override
105 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700106 return "0x" + Long.toHexString(this.value);
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800107 }
108}