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