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