blob: 3b8fad666c10b9d8fb1c6addd6c21e0cc15bbcb1 [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.
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070011 * This class is immutable.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080012 */
Ray Milkey269ffb92014-04-03 14:43:30 -070013@JsonDeserialize(using = IPv4NetDeserializer.class)
14@JsonSerialize(using = IPv4NetSerializer.class)
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070015public final class IPv4Net {
16 private final IPv4 address; // The IPv4 address
17 private final short prefixLen; // The prefix length
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080018
19 /**
20 * Default constructor.
21 */
22 public IPv4Net() {
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070023 this.address = null;
Ray Milkey269ffb92014-04-03 14:43:30 -070024 this.prefixLen = 0;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080025 }
26
27 /**
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070028 * Copy constructor.
29 *
30 * @param other the object to copy from.
31 */
32 public IPv4Net(IPv4Net other) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070033 if (other.address != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070034 this.address = new IPv4(other.address);
Ray Milkeyb29e6262014-04-09 16:02:14 -070035 } else {
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070036 this.address = null;
Ray Milkeyb29e6262014-04-09 16:02:14 -070037 }
Ray Milkey269ffb92014-04-03 14:43:30 -070038 this.prefixLen = other.prefixLen;
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070039 }
40
41 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080042 * Constructor for a given address and prefix length.
43 *
Ray Milkey269ffb92014-04-03 14:43:30 -070044 * @param address the address to use.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080045 * @param prefixLen the prefix length to use.
46 */
47 public IPv4Net(IPv4 address, short prefixLen) {
Ray Milkey269ffb92014-04-03 14:43:30 -070048 this.address = address;
49 this.prefixLen = prefixLen;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080050 }
51
52 /**
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080053 * Constructor from a string.
54 *
55 * @param value the value to use.
56 */
57 public IPv4Net(String value) {
Ray Milkey269ffb92014-04-03 14:43:30 -070058 String[] splits = value.split("/");
59 if (splits.length != 2) {
60 throw new IllegalArgumentException("Specified IPv4Net address must contain an IPv4 " +
61 "address and a prefix length separated by '/'");
62 }
63 this.address = new IPv4(splits[0]);
64 this.prefixLen = Short.decode(splits[1]);
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080065 }
66
67 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080068 * Get the address value of the IPv4Net address.
69 *
70 * @return the address value of the IPv4Net address.
71 */
Ray Milkey269ffb92014-04-03 14:43:30 -070072 public IPv4 address() {
73 return address;
74 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080075
76 /**
77 * Get the prefix length value of the IPv4Net address.
78 *
79 * @return the prefix length value of the IPv4Net address.
80 */
Ray Milkey269ffb92014-04-03 14:43:30 -070081 public short prefixLen() {
82 return prefixLen;
83 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080084
85 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080086 * Convert the IPv4Net value to an "address/prefixLen" string.
87 *
88 * @return the IPv4Net value as an "address/prefixLen" string.
89 */
90 @Override
91 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -070092 return this.address.toString() + "/" + this.prefixLen;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080093 }
94}