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 | |
HIGUCHI Yuta | f086d8a | 2013-06-12 15:26:35 -0700 | [diff] [blame] | 3 | import net.onrc.onos.ofcontroller.util.serializers.IPv4Deserializer; |
| 4 | import net.onrc.onos.ofcontroller.util.serializers.IPv4Serializer; |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 5 | |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 6 | import org.codehaus.jackson.map.annotate.JsonDeserialize; |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 7 | import org.codehaus.jackson.map.annotate.JsonSerialize; |
| 8 | |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 9 | /** |
| 10 | * The class representing an IPv4 address. |
| 11 | */ |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 12 | @JsonDeserialize(using=IPv4Deserializer.class) |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 13 | @JsonSerialize(using=IPv4Serializer.class) |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 14 | public class IPv4 { |
| 15 | private int value; |
| 16 | |
| 17 | /** |
| 18 | * Default constructor. |
| 19 | */ |
| 20 | public IPv4() { |
| 21 | this.value = 0; |
| 22 | } |
| 23 | |
| 24 | /** |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 25 | * Copy constructor. |
| 26 | * |
| 27 | * @param other the object to copy from. |
| 28 | */ |
| 29 | public IPv4(IPv4 other) { |
| 30 | this.value = other.value; |
| 31 | } |
| 32 | |
| 33 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 34 | * Constructor from an integer value. |
| 35 | * |
| 36 | * @param value the value to use. |
| 37 | */ |
| 38 | public IPv4(int value) { |
| 39 | this.value = value; |
| 40 | } |
| 41 | |
| 42 | /** |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 43 | * Constructor from a string. |
| 44 | * |
| 45 | * @param value the value to use. |
| 46 | */ |
| 47 | public IPv4(String value) { |
| 48 | String[] splits = value.split("\\."); |
| 49 | if (splits.length != 4) |
| 50 | throw new IllegalArgumentException("Specified IPv4 address must contain four " + |
| 51 | "numerical digits separated by '.'"); |
| 52 | |
| 53 | int result = 0; |
| 54 | for (int i = 0; i < 4; ++i) { |
| 55 | result |= Integer.valueOf(splits[i]) << ((3-i)*8); |
| 56 | } |
| 57 | this.value = result; |
| 58 | } |
| 59 | |
| 60 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 61 | * Get the value of the IPv4 address. |
| 62 | * |
| 63 | * @return the value of the IPv4 address. |
| 64 | */ |
| 65 | public int value() { return value; } |
| 66 | |
| 67 | /** |
| 68 | * Set the value of the IPv4 address. |
| 69 | * |
| 70 | * @param value the value to set. |
| 71 | */ |
| 72 | public void setValue(int value) { |
| 73 | this.value = value; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Convert the IPv4 value to a '.' separated string. |
| 78 | * |
| 79 | * @return the IPv4 value as a '.' separated string. |
| 80 | */ |
| 81 | @Override |
| 82 | public String toString() { |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 83 | return ((this.value >> 24) & 0xFF) + "." + |
| 84 | ((this.value >> 16) & 0xFF) + "." + |
| 85 | ((this.value >> 8) & 0xFF) + "." + |
| 86 | (this.value & 0xFF); |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 87 | } |
| 88 | } |