blob: de955ba80ffe5039b101306e2ad66798ac465cb4 [file] [log] [blame]
HIGUCHI Yuta356086e2013-06-12 15:21:19 -07001package net.onrc.onos.ofcontroller.util;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08002
Pavlin Radoslavov8e5bab22013-04-02 04:19:41 +00003import java.math.BigInteger;
4
HIGUCHI Yutaf086d8a2013-06-12 15:26:35 -07005import net.onrc.onos.ofcontroller.util.serializers.FlowIdDeserializer;
6import net.onrc.onos.ofcontroller.util.serializers.FlowIdSerializer;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08007
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -08008import org.codehaus.jackson.map.annotate.JsonDeserialize;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08009import org.codehaus.jackson.map.annotate.JsonSerialize;
10
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080011/**
12 * The class representing a Flow ID.
13 */
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080014@JsonDeserialize(using=FlowIdDeserializer.class)
Pavlin Radoslavovad008e02013-02-21 18:42:42 -080015@JsonSerialize(using=FlowIdSerializer.class)
Toshio Koidefff66422013-06-26 13:57:24 -070016public class FlowId implements Comparable<FlowId> {
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080017 private long value;
18
19 /**
20 * Default constructor.
21 */
22 public FlowId() {
23 this.value = 0;
24 }
25
26 /**
27 * Constructor from an integer value.
28 *
29 * @param value the value to use.
30 */
31 public FlowId(long value) {
32 this.value = value;
33 }
34
35 /**
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080036 * Constructor from a string.
37 *
38 * @param value the value to use.
39 */
40 public FlowId(String value) {
Pavlin Radoslavov8e5bab22013-04-02 04:19:41 +000041 //
42 // Use the help of BigInteger to parse strings representing
43 // large unsigned hex long values.
44 //
45 char c = 0;
46 if (value.length() > 2)
47 c = value.charAt(1);
48 if ((c == 'x') || (c == 'X'))
49 this.value = new BigInteger(value.substring(2), 16).longValue();
50 else
51 this.value = Long.decode(value);
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080052 }
53
54 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080055 * Get the value of the Flow ID.
56 *
57 * @return the value of the Flow ID.
58 */
59 public long value() { return value; }
60
61 /**
62 * Set the value of the Flow ID.
63 *
64 * @param value the value to set.
65 */
66 public void setValue(long value) {
67 this.value = value;
68 }
69
70 /**
Pavlin Radoslavovad008e02013-02-21 18:42:42 -080071 * Convert the Flow ID value to a hexadecimal string.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080072 *
Pavlin Radoslavovad008e02013-02-21 18:42:42 -080073 * @return the Flow ID value to a hexadecimal string.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080074 */
75 @Override
76 public String toString() {
Pavlin Radoslavovad008e02013-02-21 18:42:42 -080077 return "0x" + Long.toHexString(this.value);
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080078 }
Toshio Koidefff66422013-06-26 13:57:24 -070079
Toshio Koided8651e72013-06-27 11:28:11 -070080 /**
81 * Compare two FlowId objects numerically using their Flow IDs.
82 *
83 * @return the value 0 if the Flow ID is equal to the argument's Flow ID;
84 * a value less than 0 if the Flow ID is numerically less than the argument's Flow ID;
85 * and a value greater than 0 if the Flow ID is numerically greater than the argument's Flow ID.
86 */
87 @Override
Toshio Koidefff66422013-06-26 13:57:24 -070088 public int compareTo(FlowId o) {
89 return Long.valueOf(this.value).compareTo(o.value());
90 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080091}