blob: 66b98a3f3d32be8f0f0dee48c7748dbb316c2bc3 [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 Hart46cc07f2014-08-21 18:59:48 -07003import java.util.Objects;
4
Jonathan Hart23701d12014-04-03 10:45:48 -07005import net.onrc.onos.core.util.serializers.IPv4NetDeserializer;
6import net.onrc.onos.core.util.serializers.IPv4NetSerializer;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08007
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -08008import org.codehaus.jackson.map.annotate.JsonDeserialize;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08009import org.codehaus.jackson.map.annotate.JsonSerialize;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080010
11/**
12 * The class representing an IPv4 network address.
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070013 * This class is immutable.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080014 */
Ray Milkey269ffb92014-04-03 14:43:30 -070015@JsonDeserialize(using = IPv4NetDeserializer.class)
16@JsonSerialize(using = IPv4NetSerializer.class)
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070017public final class IPv4Net {
18 private final IPv4 address; // The IPv4 address
19 private final short prefixLen; // The prefix length
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080020
21 /**
22 * Default constructor.
23 */
24 public IPv4Net() {
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070025 this.address = null;
Ray Milkey269ffb92014-04-03 14:43:30 -070026 this.prefixLen = 0;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080027 }
28
29 /**
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070030 * Copy constructor.
31 *
32 * @param other the object to copy from.
33 */
34 public IPv4Net(IPv4Net other) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070035 if (other.address != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070036 this.address = new IPv4(other.address);
Ray Milkeyb29e6262014-04-09 16:02:14 -070037 } else {
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070038 this.address = null;
Ray Milkeyb29e6262014-04-09 16:02:14 -070039 }
Ray Milkey269ffb92014-04-03 14:43:30 -070040 this.prefixLen = other.prefixLen;
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070041 }
42
43 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080044 * Constructor for a given address and prefix length.
45 *
Ray Milkey269ffb92014-04-03 14:43:30 -070046 * @param address the address to use.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080047 * @param prefixLen the prefix length to use.
48 */
49 public IPv4Net(IPv4 address, short prefixLen) {
Ray Milkey269ffb92014-04-03 14:43:30 -070050 this.address = address;
51 this.prefixLen = prefixLen;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080052 }
53
54 /**
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080055 * Constructor from a string.
56 *
57 * @param value the value to use.
58 */
59 public IPv4Net(String value) {
Ray Milkey269ffb92014-04-03 14:43:30 -070060 String[] splits = value.split("/");
61 if (splits.length != 2) {
62 throw new IllegalArgumentException("Specified IPv4Net address must contain an IPv4 " +
63 "address and a prefix length separated by '/'");
64 }
65 this.address = new IPv4(splits[0]);
66 this.prefixLen = Short.decode(splits[1]);
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080067 }
68
69 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080070 * Get the address value of the IPv4Net address.
71 *
72 * @return the address value of the IPv4Net address.
73 */
Ray Milkey269ffb92014-04-03 14:43:30 -070074 public IPv4 address() {
75 return address;
76 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080077
78 /**
79 * Get the prefix length value of the IPv4Net address.
80 *
81 * @return the prefix length value of the IPv4Net address.
82 */
Ray Milkey269ffb92014-04-03 14:43:30 -070083 public short prefixLen() {
84 return prefixLen;
85 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080086
87 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080088 * Convert the IPv4Net value to an "address/prefixLen" string.
89 *
90 * @return the IPv4Net value as an "address/prefixLen" string.
91 */
92 @Override
93 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -070094 return this.address.toString() + "/" + this.prefixLen;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080095 }
Jonathan Hart46cc07f2014-08-21 18:59:48 -070096
97 /**
98 * Compares the value of two IPv4Net objects.
99 * <p/>
100 * Note the value of the IPv4 address is compared directly between the
101 * objects, and must match exactly for the objects to be considered equal.
102 * This may result in objects which represent the same IP prefix being
103 * classified as unequal, because the unsignificant bits of the address
104 * field don't match (the bits to the right of the prefix length).
105 * <p/>
106 * TODO Change this behavior so that objects that represent the same prefix
107 * are classified as equal according to this equals method.
108 *
109 * @see Object#equals(Object)
110 */
111 @Override
112 public boolean equals(Object other) {
113 if (other == this) {
114 return true;
115 }
116
117 if (!(other instanceof IPv4Net)) {
118 return false;
119 }
120
121 IPv4Net otherIpv4Net = (IPv4Net) other;
122
123 return Objects.equals(this.address, otherIpv4Net.address)
124 && this.prefixLen == otherIpv4Net.prefixLen;
125 }
126
127 @Override
128 public int hashCode() {
129 return Objects.hash(address, prefixLen);
130 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800131}