Added class InterfaceIpAddress and the corresponding unit test.
That class can be used to represent the IP address information on an interface.
Showing
2 changed files
with
398 additions
and
0 deletions
1 | +package org.onlab.onos.net.host; | ||
2 | + | ||
3 | +import java.util.Objects; | ||
4 | +import org.onlab.packet.IpAddress; | ||
5 | +import org.onlab.packet.IpPrefix; | ||
6 | + | ||
7 | +import static com.google.common.base.MoreObjects.toStringHelper; | ||
8 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
9 | + | ||
10 | +/** | ||
11 | + * Represents a single IP address information on an interface. | ||
12 | + * | ||
13 | + * TODO: | ||
14 | + * - Add computation for the default broadcast address if it is not | ||
15 | + * specified | ||
16 | + * - Add explicit checks that each IP address or prefix belong to the | ||
17 | + * same IP version: IPv4/IPv6. | ||
18 | + * - Inside the copy constructor we should use copy constructors for each | ||
19 | + * field | ||
20 | + */ | ||
21 | +public class InterfaceIpAddress { | ||
22 | + private final IpAddress ipAddress; | ||
23 | + private final IpPrefix subnetAddress; | ||
24 | + private final IpAddress broadcastAddress; | ||
25 | + private final IpAddress peerAddress; | ||
26 | + | ||
27 | + /** | ||
28 | + * Copy constructor. | ||
29 | + * | ||
30 | + * @param other the object to copy from | ||
31 | + */ | ||
32 | + public InterfaceIpAddress(InterfaceIpAddress other) { | ||
33 | + // TODO: we should use copy constructors for each field | ||
34 | + this.ipAddress = other.ipAddress; | ||
35 | + this.subnetAddress = other.subnetAddress; | ||
36 | + this.broadcastAddress = other.broadcastAddress; | ||
37 | + this.peerAddress = other.peerAddress; | ||
38 | + } | ||
39 | + | ||
40 | + /** | ||
41 | + * Constructor for a given IP address and a subnet address. | ||
42 | + * | ||
43 | + * @param ipAddress the IP address | ||
44 | + * @param subnetAddress the IP subnet address | ||
45 | + */ | ||
46 | + public InterfaceIpAddress(IpAddress ipAddress, IpPrefix subnetAddress) { | ||
47 | + this.ipAddress = checkNotNull(ipAddress); | ||
48 | + this.subnetAddress = checkNotNull(subnetAddress); | ||
49 | + // TODO: Recompute the default broadcast address from the subnet | ||
50 | + // address | ||
51 | + this.broadcastAddress = null; | ||
52 | + this.peerAddress = null; | ||
53 | + } | ||
54 | + | ||
55 | + /** | ||
56 | + * Constructor for a given IP address and a subnet address. | ||
57 | + * | ||
58 | + * @param ipAddress the IP address | ||
59 | + * @param subnetAddress the IP subnet address | ||
60 | + * @param broadcastAddress the IP broadcast address. It can be used | ||
61 | + * to specify non-default broadcast address | ||
62 | + */ | ||
63 | + public InterfaceIpAddress(IpAddress ipAddress, IpPrefix subnetAddress, | ||
64 | + IpAddress broadcastAddress) { | ||
65 | + this.ipAddress = checkNotNull(ipAddress); | ||
66 | + this.subnetAddress = checkNotNull(subnetAddress); | ||
67 | + this.broadcastAddress = broadcastAddress; | ||
68 | + this.peerAddress = null; | ||
69 | + } | ||
70 | + | ||
71 | + /** | ||
72 | + * Constructor for a given IP address and a subnet address. | ||
73 | + * | ||
74 | + * @param ipAddress the IP address | ||
75 | + * @param subnetAddress the IP subnet address | ||
76 | + * @param broadcastAddress the IP broadcast address. It can be used | ||
77 | + * to specify non-default broadcast address. It should be null for | ||
78 | + * point-to-point interfaces with a peer address | ||
79 | + * @param peerAddress the peer IP address for point-to-point interfaces | ||
80 | + */ | ||
81 | + public InterfaceIpAddress(IpAddress ipAddress, IpPrefix subnetAddress, | ||
82 | + IpAddress broadcastAddress, | ||
83 | + IpAddress peerAddress) { | ||
84 | + this.ipAddress = checkNotNull(ipAddress); | ||
85 | + this.subnetAddress = checkNotNull(subnetAddress); | ||
86 | + this.broadcastAddress = broadcastAddress; | ||
87 | + this.peerAddress = peerAddress; | ||
88 | + } | ||
89 | + | ||
90 | + /** | ||
91 | + * Gets the IP address. | ||
92 | + * | ||
93 | + * @return the IP address | ||
94 | + */ | ||
95 | + public IpAddress ipAddress() { | ||
96 | + return ipAddress; | ||
97 | + } | ||
98 | + | ||
99 | + /** | ||
100 | + * Gets the IP subnet address. | ||
101 | + * | ||
102 | + * @return the IP subnet address | ||
103 | + */ | ||
104 | + public IpPrefix subnetAddress() { | ||
105 | + return subnetAddress; | ||
106 | + } | ||
107 | + | ||
108 | + /** | ||
109 | + * Gets the subnet IP broadcast address. | ||
110 | + * | ||
111 | + * @return the subnet IP broadcast address | ||
112 | + */ | ||
113 | + public IpAddress broadcastAddress() { | ||
114 | + return broadcastAddress; | ||
115 | + } | ||
116 | + | ||
117 | + /** | ||
118 | + * Gets the IP point-to-point interface peer address. | ||
119 | + * | ||
120 | + * @return the IP point-to-point interface peer address | ||
121 | + */ | ||
122 | + public IpAddress peerAddress() { | ||
123 | + return peerAddress; | ||
124 | + } | ||
125 | + | ||
126 | + @Override | ||
127 | + public boolean equals(Object other) { | ||
128 | + if (other == this) { | ||
129 | + return true; | ||
130 | + } | ||
131 | + if (!(other instanceof InterfaceIpAddress)) { | ||
132 | + return false; | ||
133 | + } | ||
134 | + InterfaceIpAddress otherAddr = (InterfaceIpAddress) other; | ||
135 | + | ||
136 | + return Objects.equals(this.ipAddress, otherAddr.ipAddress) | ||
137 | + && Objects.equals(this.subnetAddress, otherAddr.subnetAddress) | ||
138 | + && Objects.equals(this.broadcastAddress, | ||
139 | + otherAddr.broadcastAddress) | ||
140 | + && Objects.equals(this.peerAddress, otherAddr.peerAddress); | ||
141 | + } | ||
142 | + | ||
143 | + @Override | ||
144 | + public int hashCode() { | ||
145 | + return Objects.hash(ipAddress, subnetAddress, broadcastAddress, | ||
146 | + peerAddress); | ||
147 | + } | ||
148 | + | ||
149 | + @Override | ||
150 | + public String toString() { | ||
151 | + return toStringHelper(this).add("ipAddress", ipAddress) | ||
152 | + .add("subnetAddress", subnetAddress) | ||
153 | + .add("broadcastAddress", broadcastAddress) | ||
154 | + .add("peerAddress", peerAddress) | ||
155 | + .omitNullValues().toString(); | ||
156 | + } | ||
157 | +} |
1 | +package org.onlab.onos.net.host; | ||
2 | + | ||
3 | +import org.junit.Test; | ||
4 | +import org.onlab.packet.IpAddress; | ||
5 | +import org.onlab.packet.IpPrefix; | ||
6 | + | ||
7 | +import static org.hamcrest.Matchers.is; | ||
8 | +import static org.hamcrest.Matchers.not; | ||
9 | +import static org.hamcrest.Matchers.nullValue; | ||
10 | +import static org.junit.Assert.assertThat; | ||
11 | + | ||
12 | +/** | ||
13 | + * Tests for class {@link InterfaceIpAddress}. | ||
14 | + */ | ||
15 | +public class InterfaceIpAddressTest { | ||
16 | + private static final IpAddress IP_ADDRESS = IpAddress.valueOf("1.2.3.4"); | ||
17 | + private static final IpPrefix SUBNET_ADDRESS = | ||
18 | + IpPrefix.valueOf("1.2.0.0/16"); | ||
19 | + private static final IpAddress BROADCAST_ADDRESS = | ||
20 | + IpAddress.valueOf("1.2.0.255"); // NOTE: non-default broadcast | ||
21 | + private static final IpAddress PEER_ADDRESS = IpAddress.valueOf("5.6.7.8"); | ||
22 | + | ||
23 | + private static final IpAddress IP_ADDRESS2 = IpAddress.valueOf("10.2.3.4"); | ||
24 | + private static final IpPrefix SUBNET_ADDRESS2 = | ||
25 | + IpPrefix.valueOf("10.2.0.0/16"); | ||
26 | + private static final IpAddress BROADCAST_ADDRESS2 = | ||
27 | + IpAddress.valueOf("10.2.0.255"); // NOTE: non-default broadcast | ||
28 | + private static final IpAddress PEER_ADDRESS2 = | ||
29 | + IpAddress.valueOf("50.6.7.8"); | ||
30 | + | ||
31 | + /** | ||
32 | + * Tests valid class copy constructor. | ||
33 | + */ | ||
34 | + @Test | ||
35 | + public void testCopyConstructor() { | ||
36 | + InterfaceIpAddress fromAddr; | ||
37 | + InterfaceIpAddress toAddr; | ||
38 | + | ||
39 | + // Regular interface address with default broadcast address | ||
40 | + fromAddr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS); | ||
41 | + toAddr = new InterfaceIpAddress(fromAddr); | ||
42 | + assertThat(toAddr.toString(), | ||
43 | + is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16}")); | ||
44 | + | ||
45 | + // Interface address with non-default broadcast address | ||
46 | + fromAddr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, | ||
47 | + BROADCAST_ADDRESS); | ||
48 | + toAddr = new InterfaceIpAddress(fromAddr); | ||
49 | + assertThat(toAddr.toString(), | ||
50 | + is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16, broadcastAddress=1.2.0.255}")); | ||
51 | + | ||
52 | + // Point-to-point address with peer IP address | ||
53 | + fromAddr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null, | ||
54 | + PEER_ADDRESS); | ||
55 | + toAddr = new InterfaceIpAddress(fromAddr); | ||
56 | + assertThat(toAddr.toString(), | ||
57 | + is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16, peerAddress=5.6.7.8}")); | ||
58 | + } | ||
59 | + | ||
60 | + /** | ||
61 | + * Tests invalid class copy constructor for a null object to copy from. | ||
62 | + */ | ||
63 | + @Test(expected = NullPointerException.class) | ||
64 | + public void testInvalidConstructorNullObject() { | ||
65 | + InterfaceIpAddress fromAddr = null; | ||
66 | + InterfaceIpAddress toAddr = new InterfaceIpAddress(fromAddr); | ||
67 | + } | ||
68 | + | ||
69 | + /** | ||
70 | + * Tests valid class constructor for regular interface address with | ||
71 | + * default broadcast address. | ||
72 | + */ | ||
73 | + @Test | ||
74 | + public void testConstructorForDefaultBroadcastAddress() { | ||
75 | + InterfaceIpAddress addr = | ||
76 | + new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS); | ||
77 | + assertThat(addr.toString(), | ||
78 | + is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16}")); | ||
79 | + } | ||
80 | + | ||
81 | + /** | ||
82 | + * Tests valid class constructor for interface address with | ||
83 | + * non-default broadcast address. | ||
84 | + */ | ||
85 | + @Test | ||
86 | + public void testConstructorForNonDefaultBroadcastAddress() { | ||
87 | + InterfaceIpAddress addr = | ||
88 | + new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, | ||
89 | + BROADCAST_ADDRESS); | ||
90 | + assertThat(addr.toString(), | ||
91 | + is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16, broadcastAddress=1.2.0.255}")); | ||
92 | + } | ||
93 | + | ||
94 | + /** | ||
95 | + * Tests valid class constructor for point-to-point interface address with | ||
96 | + * peer address. | ||
97 | + */ | ||
98 | + @Test | ||
99 | + public void testConstructorForPointToPointAddress() { | ||
100 | + InterfaceIpAddress addr = | ||
101 | + new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null, | ||
102 | + PEER_ADDRESS); | ||
103 | + assertThat(addr.toString(), | ||
104 | + is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16, peerAddress=5.6.7.8}")); | ||
105 | + } | ||
106 | + | ||
107 | + /** | ||
108 | + * Tests getting the fields of an interface address. | ||
109 | + */ | ||
110 | + @Test | ||
111 | + public void testGetFields() { | ||
112 | + InterfaceIpAddress addr; | ||
113 | + | ||
114 | + // Regular interface address with default broadcast address | ||
115 | + addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS); | ||
116 | + assertThat(addr.ipAddress().toString(), is("1.2.3.4")); | ||
117 | + assertThat(addr.subnetAddress().toString(), is("1.2.0.0/16")); | ||
118 | + assertThat(addr.broadcastAddress(), is(nullValue())); // TODO: Fix | ||
119 | + assertThat(addr.peerAddress(), is(nullValue())); | ||
120 | + | ||
121 | + // Interface address with non-default broadcast address | ||
122 | + addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, | ||
123 | + BROADCAST_ADDRESS); | ||
124 | + assertThat(addr.ipAddress().toString(), is("1.2.3.4")); | ||
125 | + assertThat(addr.subnetAddress().toString(), is("1.2.0.0/16")); | ||
126 | + assertThat(addr.broadcastAddress().toString(), is("1.2.0.255")); | ||
127 | + assertThat(addr.peerAddress(), is(nullValue())); | ||
128 | + | ||
129 | + // Point-to-point address with peer IP address | ||
130 | + addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null, | ||
131 | + PEER_ADDRESS); | ||
132 | + assertThat(addr.ipAddress().toString(), is("1.2.3.4")); | ||
133 | + assertThat(addr.subnetAddress().toString(), is("1.2.0.0/16")); | ||
134 | + assertThat(addr.broadcastAddress(), is(nullValue())); | ||
135 | + assertThat(addr.peerAddress().toString(), is("5.6.7.8")); | ||
136 | + } | ||
137 | + | ||
138 | + /** | ||
139 | + * Tests equality of {@link InterfaceIpAddress}. | ||
140 | + */ | ||
141 | + @Test | ||
142 | + public void testEquality() { | ||
143 | + InterfaceIpAddress addr1, addr2; | ||
144 | + | ||
145 | + // Regular interface address with default broadcast address | ||
146 | + addr1 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS); | ||
147 | + addr2 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS); | ||
148 | + assertThat(addr1, is(addr2)); | ||
149 | + | ||
150 | + // Interface address with non-default broadcast address | ||
151 | + addr1 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, | ||
152 | + BROADCAST_ADDRESS); | ||
153 | + addr2 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, | ||
154 | + BROADCAST_ADDRESS); | ||
155 | + assertThat(addr1, is(addr2)); | ||
156 | + | ||
157 | + // Point-to-point address with peer IP address | ||
158 | + addr1 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null, | ||
159 | + PEER_ADDRESS); | ||
160 | + addr2 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null, | ||
161 | + PEER_ADDRESS); | ||
162 | + assertThat(addr1, is(addr2)); | ||
163 | + } | ||
164 | + | ||
165 | + /** | ||
166 | + * Tests non-equality of {@link InterfaceIpAddress}. | ||
167 | + */ | ||
168 | + @Test | ||
169 | + public void testNonEquality() { | ||
170 | + InterfaceIpAddress addr1, addr2, addr3, addr4; | ||
171 | + | ||
172 | + // Regular interface address with default broadcast address | ||
173 | + addr1 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS); | ||
174 | + // Interface address with non-default broadcast address | ||
175 | + addr2 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, | ||
176 | + BROADCAST_ADDRESS); | ||
177 | + // Point-to-point address with peer IP address | ||
178 | + addr3 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null, | ||
179 | + PEER_ADDRESS); | ||
180 | + | ||
181 | + // Test interface addresses with different properties: | ||
182 | + // - default-broadcast vs non-default broadcast | ||
183 | + // - regular vs point-to-point | ||
184 | + assertThat(addr1, is(not(addr2))); | ||
185 | + assertThat(addr1, is(not(addr3))); | ||
186 | + assertThat(addr2, is(not(addr3))); | ||
187 | + | ||
188 | + // Test regular interface address with default broadcast address | ||
189 | + addr4 = new InterfaceIpAddress(IP_ADDRESS2, SUBNET_ADDRESS); | ||
190 | + assertThat(addr1, is(not(addr4))); | ||
191 | + addr4 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS2); | ||
192 | + assertThat(addr1, is(not(addr4))); | ||
193 | + | ||
194 | + // Test interface address with non-default broadcast address | ||
195 | + addr4 = new InterfaceIpAddress(IP_ADDRESS2, SUBNET_ADDRESS, | ||
196 | + BROADCAST_ADDRESS); | ||
197 | + assertThat(addr2, is(not(addr4))); | ||
198 | + addr4 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS2, | ||
199 | + BROADCAST_ADDRESS); | ||
200 | + assertThat(addr2, is(not(addr4))); | ||
201 | + addr4 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, | ||
202 | + BROADCAST_ADDRESS2); | ||
203 | + assertThat(addr2, is(not(addr4))); | ||
204 | + | ||
205 | + // Test point-to-point address with peer IP address | ||
206 | + addr4 = new InterfaceIpAddress(IP_ADDRESS2, SUBNET_ADDRESS, null, | ||
207 | + PEER_ADDRESS); | ||
208 | + assertThat(addr3, is(not(addr4))); | ||
209 | + addr4 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS2, null, | ||
210 | + PEER_ADDRESS); | ||
211 | + assertThat(addr3, is(not(addr4))); | ||
212 | + addr4 = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null, | ||
213 | + PEER_ADDRESS2); | ||
214 | + assertThat(addr3, is(not(addr4))); | ||
215 | + } | ||
216 | + | ||
217 | + /** | ||
218 | + * Tests object string representation. | ||
219 | + */ | ||
220 | + @Test | ||
221 | + public void testToString() { | ||
222 | + InterfaceIpAddress addr; | ||
223 | + | ||
224 | + // Regular interface address with default broadcast address | ||
225 | + addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS); | ||
226 | + assertThat(addr.toString(), | ||
227 | + is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16}")); | ||
228 | + | ||
229 | + // Interface address with non-default broadcast address | ||
230 | + addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, | ||
231 | + BROADCAST_ADDRESS); | ||
232 | + assertThat(addr.toString(), | ||
233 | + is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16, broadcastAddress=1.2.0.255}")); | ||
234 | + | ||
235 | + // Point-to-point address with peer IP address | ||
236 | + addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS, null, | ||
237 | + PEER_ADDRESS); | ||
238 | + assertThat(addr.toString(), | ||
239 | + is("InterfaceIpAddress{ipAddress=1.2.3.4, subnetAddress=1.2.0.0/16, peerAddress=5.6.7.8}")); | ||
240 | + } | ||
241 | +} |
-
Please register or login to post a comment