blob: 5eace27582e572eb28cf6ec036c256454f941ab2 [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.FlowIdDeserializer;
6import net.onrc.onos.core.util.serializers.FlowIdSerializer;
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 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 = FlowIdDeserializer.class)
17@JsonSerialize(using = FlowIdSerializer.class)
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070018public final class FlowId implements Comparable<FlowId> {
19 private final static long INVALID = -1;
20 private final long value;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080021
22 /**
23 * Default constructor.
24 */
25 public FlowId() {
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070026 this.value = FlowId.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 FlowId(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 FlowId(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 ID.
59 *
60 * @return the value of the Flow 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 ID is valid.
68 *
69 * @return true if the Flow ID is valid, otherwise false.
70 */
71 @JsonIgnore
72 public boolean isValid() {
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070073 return (this.value() != FlowId.INVALID);
Pavlin Radoslavovf74271f2013-11-25 18:22:47 -080074 }
75
76 /**
Pavlin Radoslavovad008e02013-02-21 18:42:42 -080077 * Convert the Flow ID value to a hexadecimal string.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080078 *
Pavlin Radoslavovad008e02013-02-21 18:42:42 -080079 * @return the Flow ID value to a hexadecimal string.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080080 */
81 @Override
82 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -070083 return "0x" + Long.toHexString(this.value);
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080084 }
Toshio Koidefff66422013-06-26 13:57:24 -070085
Toshio Koided8651e72013-06-27 11:28:11 -070086 /**
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 Milkey269ffb92014-04-03 14:43:30 -070090 * 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 Koided8651e72013-06-27 11:28:11 -070092 */
Ray Milkey269ffb92014-04-03 14:43:30 -070093 @Override
94 public int compareTo(FlowId o) {
95 return Long.valueOf(this.value).compareTo(o.value());
96 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080097}