Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.util; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 2 | |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 3 | import net.onrc.onos.core.util.serializers.IPv4NetDeserializer; |
| 4 | import net.onrc.onos.core.util.serializers.IPv4NetSerializer; |
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; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 8 | |
| 9 | /** |
| 10 | * The class representing an IPv4 network address. |
| 11 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 12 | @JsonDeserialize(using = IPv4NetDeserializer.class) |
| 13 | @JsonSerialize(using = IPv4NetSerializer.class) |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 14 | public class IPv4Net { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 15 | private IPv4 address; // The IPv4 address |
| 16 | private short prefixLen; // The prefix length |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * Default constructor. |
| 20 | */ |
| 21 | public IPv4Net() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 22 | this.prefixLen = 0; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | /** |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 26 | * Copy constructor. |
| 27 | * |
| 28 | * @param other the object to copy from. |
| 29 | */ |
| 30 | public IPv4Net(IPv4Net other) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 31 | if (other.address != null) |
| 32 | this.address = new IPv4(other.address); |
| 33 | this.prefixLen = other.prefixLen; |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 37 | * Constructor for a given address and prefix length. |
| 38 | * |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 39 | * @param address the address to use. |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 40 | * @param prefixLen the prefix length to use. |
| 41 | */ |
| 42 | public IPv4Net(IPv4 address, short prefixLen) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 43 | this.address = address; |
| 44 | this.prefixLen = prefixLen; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /** |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 48 | * Constructor from a string. |
| 49 | * |
| 50 | * @param value the value to use. |
| 51 | */ |
| 52 | public IPv4Net(String value) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 53 | String[] splits = value.split("/"); |
| 54 | if (splits.length != 2) { |
| 55 | throw new IllegalArgumentException("Specified IPv4Net address must contain an IPv4 " + |
| 56 | "address and a prefix length separated by '/'"); |
| 57 | } |
| 58 | this.address = new IPv4(splits[0]); |
| 59 | this.prefixLen = Short.decode(splits[1]); |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 63 | * Get the address value of the IPv4Net address. |
| 64 | * |
| 65 | * @return the address value of the IPv4Net address. |
| 66 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 67 | public IPv4 address() { |
| 68 | return address; |
| 69 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 70 | |
| 71 | /** |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 72 | * Set the address value of the IPv4Net address. |
| 73 | * |
| 74 | * @param address the address to use. |
| 75 | */ |
| 76 | public void setAddress(IPv4 address) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 77 | this.address = address; |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 81 | * Get the prefix length value of the IPv4Net address. |
| 82 | * |
| 83 | * @return the prefix length value of the IPv4Net address. |
| 84 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 85 | public short prefixLen() { |
| 86 | return prefixLen; |
| 87 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 88 | |
| 89 | /** |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 90 | * Set the prefix length value of the IPv4Net address. |
| 91 | * |
| 92 | * @param prefixLen the prefix length to use. |
| 93 | */ |
| 94 | public void setPrefixLen(short prefixLen) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 95 | this.prefixLen = prefixLen; |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 99 | * Set the value of the IPv4Net address. |
| 100 | * |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 101 | * @param address the address to use. |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 102 | * @param prefixLen the prefix length to use. |
| 103 | */ |
| 104 | public void setValue(IPv4 address, short prefixLen) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 105 | this.address = address; |
| 106 | this.prefixLen = prefixLen; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Convert the IPv4Net value to an "address/prefixLen" string. |
| 111 | * |
| 112 | * @return the IPv4Net value as an "address/prefixLen" string. |
| 113 | */ |
| 114 | @Override |
| 115 | public String toString() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 116 | return this.address.toString() + "/" + this.prefixLen; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 117 | } |
| 118 | } |