blob: 3aa5a8bd58974cf17d0c99dbea60edbeccd4586b [file] [log] [blame]
Jonathan Hart23701d12014-04-03 10:45:48 -07001package net.onrc.onos.core.util;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08002
Jonathan Hart23701d12014-04-03 10:45:48 -07003import net.onrc.onos.core.util.serializers.IPv4NetDeserializer;
4import net.onrc.onos.core.util.serializers.IPv4NetSerializer;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08005
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -08006import org.codehaus.jackson.map.annotate.JsonDeserialize;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08007import org.codehaus.jackson.map.annotate.JsonSerialize;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08008
9/**
10 * The class representing an IPv4 network address.
11 */
Ray Milkey269ffb92014-04-03 14:43:30 -070012@JsonDeserialize(using = IPv4NetDeserializer.class)
13@JsonSerialize(using = IPv4NetSerializer.class)
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080014public class IPv4Net {
Ray Milkey269ffb92014-04-03 14:43:30 -070015 private IPv4 address; // The IPv4 address
16 private short prefixLen; // The prefix length
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080017
18 /**
19 * Default constructor.
20 */
21 public IPv4Net() {
Ray Milkey269ffb92014-04-03 14:43:30 -070022 this.prefixLen = 0;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080023 }
24
25 /**
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070026 * Copy constructor.
27 *
28 * @param other the object to copy from.
29 */
30 public IPv4Net(IPv4Net other) {
Ray Milkey269ffb92014-04-03 14:43:30 -070031 if (other.address != null)
32 this.address = new IPv4(other.address);
33 this.prefixLen = other.prefixLen;
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070034 }
35
36 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080037 * Constructor for a given address and prefix length.
38 *
Ray Milkey269ffb92014-04-03 14:43:30 -070039 * @param address the address to use.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080040 * @param prefixLen the prefix length to use.
41 */
42 public IPv4Net(IPv4 address, short prefixLen) {
Ray Milkey269ffb92014-04-03 14:43:30 -070043 this.address = address;
44 this.prefixLen = prefixLen;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080045 }
46
47 /**
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080048 * Constructor from a string.
49 *
50 * @param value the value to use.
51 */
52 public IPv4Net(String value) {
Ray Milkey269ffb92014-04-03 14:43:30 -070053 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 Radoslavov2013cbb2013-02-26 10:15:18 -080060 }
61
62 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080063 * Get the address value of the IPv4Net address.
64 *
65 * @return the address value of the IPv4Net address.
66 */
Ray Milkey269ffb92014-04-03 14:43:30 -070067 public IPv4 address() {
68 return address;
69 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080070
71 /**
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080072 * Set the address value of the IPv4Net address.
73 *
74 * @param address the address to use.
75 */
76 public void setAddress(IPv4 address) {
Ray Milkey269ffb92014-04-03 14:43:30 -070077 this.address = address;
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080078 }
79
80 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080081 * Get the prefix length value of the IPv4Net address.
82 *
83 * @return the prefix length value of the IPv4Net address.
84 */
Ray Milkey269ffb92014-04-03 14:43:30 -070085 public short prefixLen() {
86 return prefixLen;
87 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080088
89 /**
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080090 * 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 Milkey269ffb92014-04-03 14:43:30 -070095 this.prefixLen = prefixLen;
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080096 }
97
98 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080099 * Set the value of the IPv4Net address.
100 *
Ray Milkey269ffb92014-04-03 14:43:30 -0700101 * @param address the address to use.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800102 * @param prefixLen the prefix length to use.
103 */
104 public void setValue(IPv4 address, short prefixLen) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700105 this.address = address;
106 this.prefixLen = prefixLen;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800107 }
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 Milkey269ffb92014-04-03 14:43:30 -0700116 return this.address.toString() + "/" + this.prefixLen;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800117 }
118}