Srinivas Bandi
Committed by Gerrit Code Review

Fixing ONOS-4875

Change-Id: I2b4fec44f623cc6df90c45c1cabc8c40601bf4b6
...@@ -60,11 +60,11 @@ public class InterfaceIpAddress { ...@@ -60,11 +60,11 @@ public class InterfaceIpAddress {
60 * @param subnetAddress the IP subnet address 60 * @param subnetAddress the IP subnet address
61 */ 61 */
62 public InterfaceIpAddress(IpAddress ipAddress, IpPrefix subnetAddress) { 62 public InterfaceIpAddress(IpAddress ipAddress, IpPrefix subnetAddress) {
63 - this.ipAddress = checkNotNull(ipAddress); 63 + checkArgument(checkNotNull(ipAddress).version() == checkNotNull(subnetAddress).version(),
64 - this.subnetAddress = checkNotNull(subnetAddress); 64 + "IP and subnet version mismatch");
65 - // TODO: Recompute the default broadcast address from the subnet 65 + this.ipAddress = ipAddress;
66 - // address 66 + this.subnetAddress = subnetAddress;
67 - this.broadcastAddress = null; 67 + this.broadcastAddress = computeBroadcastAddress(ipAddress, subnetAddress);
68 this.peerAddress = null; 68 this.peerAddress = null;
69 } 69 }
70 70
...@@ -78,8 +78,10 @@ public class InterfaceIpAddress { ...@@ -78,8 +78,10 @@ public class InterfaceIpAddress {
78 */ 78 */
79 public InterfaceIpAddress(IpAddress ipAddress, IpPrefix subnetAddress, 79 public InterfaceIpAddress(IpAddress ipAddress, IpPrefix subnetAddress,
80 IpAddress broadcastAddress) { 80 IpAddress broadcastAddress) {
81 - this.ipAddress = checkNotNull(ipAddress); 81 + checkArgument(checkNotNull(ipAddress).version() == checkNotNull(subnetAddress).version(),
82 - this.subnetAddress = checkNotNull(subnetAddress); 82 + "IP and subnet version mismatch");
83 + this.ipAddress = ipAddress;
84 + this.subnetAddress = subnetAddress;
83 this.broadcastAddress = broadcastAddress; 85 this.broadcastAddress = broadcastAddress;
84 this.peerAddress = null; 86 this.peerAddress = null;
85 } 87 }
...@@ -97,8 +99,10 @@ public class InterfaceIpAddress { ...@@ -97,8 +99,10 @@ public class InterfaceIpAddress {
97 public InterfaceIpAddress(IpAddress ipAddress, IpPrefix subnetAddress, 99 public InterfaceIpAddress(IpAddress ipAddress, IpPrefix subnetAddress,
98 IpAddress broadcastAddress, 100 IpAddress broadcastAddress,
99 IpAddress peerAddress) { 101 IpAddress peerAddress) {
100 - this.ipAddress = checkNotNull(ipAddress); 102 + checkArgument(checkNotNull(ipAddress).version() == checkNotNull(subnetAddress).version(),
101 - this.subnetAddress = checkNotNull(subnetAddress); 103 + "IP and subnet version mismatch");
104 + this.ipAddress = ipAddress;
105 + this.subnetAddress = subnetAddress;
102 this.broadcastAddress = broadcastAddress; 106 this.broadcastAddress = broadcastAddress;
103 this.peerAddress = peerAddress; 107 this.peerAddress = peerAddress;
104 } 108 }
...@@ -157,6 +161,21 @@ public class InterfaceIpAddress { ...@@ -157,6 +161,21 @@ public class InterfaceIpAddress {
157 return new InterfaceIpAddress(addr, subnet); 161 return new InterfaceIpAddress(addr, subnet);
158 } 162 }
159 163
164 + /**
165 + * Compute the IP broadcast address.
166 + *
167 + * @return the IP broadcast address
168 + */
169 + public static IpAddress computeBroadcastAddress(IpAddress ipAddress, IpPrefix subnetAddress) {
170 + if (ipAddress.isIp6()) {
171 + return null;
172 + } else {
173 + IpAddress maskedIP = IpAddress.makeMaskedAddress(ipAddress, subnetAddress.prefixLength());
174 + int ipB = maskedIP.getIp4Address().toInt() | ((1 << (32 - subnetAddress.prefixLength())) - 1);
175 + return IpAddress.valueOf(ipB);
176 + }
177 + }
178 +
160 @Override 179 @Override
161 public boolean equals(Object other) { 180 public boolean equals(Object other) {
162 if (other == this) { 181 if (other == this) {
......
...@@ -34,6 +34,10 @@ public class InterfaceIpAddressTest { ...@@ -34,6 +34,10 @@ public class InterfaceIpAddressTest {
34 private static final IpAddress BROADCAST_ADDRESS = 34 private static final IpAddress BROADCAST_ADDRESS =
35 IpAddress.valueOf("1.2.0.255"); // NOTE: non-default broadcast 35 IpAddress.valueOf("1.2.0.255"); // NOTE: non-default broadcast
36 private static final IpAddress PEER_ADDRESS = IpAddress.valueOf("5.6.7.8"); 36 private static final IpAddress PEER_ADDRESS = IpAddress.valueOf("5.6.7.8");
37 + private static final IpAddress DEF_BROADCAST_ADDRESS =
38 + IpAddress.valueOf("1.2.255.255"); // NOTE: default broadcast
39 + private static final IpPrefix V6_SUBNET_ADDRESS =
40 + IpPrefix.valueOf("::/64");
37 41
38 private static final IpAddress IP_ADDRESS2 = IpAddress.valueOf("10.2.3.4"); 42 private static final IpAddress IP_ADDRESS2 = IpAddress.valueOf("10.2.3.4");
39 private static final IpPrefix SUBNET_ADDRESS2 = 43 private static final IpPrefix SUBNET_ADDRESS2 =
...@@ -97,8 +101,16 @@ public class InterfaceIpAddressTest { ...@@ -97,8 +101,16 @@ public class InterfaceIpAddressTest {
97 new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS); 101 new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS);
98 assertThat(addr.ipAddress(), is(IP_ADDRESS)); 102 assertThat(addr.ipAddress(), is(IP_ADDRESS));
99 assertThat(addr.subnetAddress(), is(SUBNET_ADDRESS)); 103 assertThat(addr.subnetAddress(), is(SUBNET_ADDRESS));
100 - assertThat(addr.broadcastAddress(), nullValue()); 104 + assertThat(addr.broadcastAddress(), is(DEF_BROADCAST_ADDRESS));
101 assertThat(addr.peerAddress(), nullValue()); 105 assertThat(addr.peerAddress(), nullValue());
106 +
107 + IpPrefix subnetAddr = IpPrefix.valueOf("10.2.3.0/24");
108 + InterfaceIpAddress addr1 = new InterfaceIpAddress(IP_ADDRESS2, subnetAddr);
109 + assertThat(addr1.broadcastAddress().toString(), is("10.2.3.255"));
110 +
111 + IpAddress ipAddress = IpAddress.valueOf("2001::4");
112 + InterfaceIpAddress addr2 = new InterfaceIpAddress(ipAddress, V6_SUBNET_ADDRESS);
113 + assertThat(addr2.broadcastAddress(), is(nullValue()));
102 } 114 }
103 115
104 /** 116 /**
...@@ -144,7 +156,7 @@ public class InterfaceIpAddressTest { ...@@ -144,7 +156,7 @@ public class InterfaceIpAddressTest {
144 addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS); 156 addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS);
145 assertThat(addr.ipAddress().toString(), is("1.2.3.4")); 157 assertThat(addr.ipAddress().toString(), is("1.2.3.4"));
146 assertThat(addr.subnetAddress().toString(), is("1.2.0.0/16")); 158 assertThat(addr.subnetAddress().toString(), is("1.2.0.0/16"));
147 - assertThat(addr.broadcastAddress(), is(nullValue())); // TODO: Fix 159 + assertThat(addr.broadcastAddress(), is(DEF_BROADCAST_ADDRESS));
148 assertThat(addr.peerAddress(), is(nullValue())); 160 assertThat(addr.peerAddress(), is(nullValue()));
149 161
150 // Interface address with non-default broadcast address 162 // Interface address with non-default broadcast address
...@@ -243,4 +255,11 @@ public class InterfaceIpAddressTest { ...@@ -243,4 +255,11 @@ public class InterfaceIpAddressTest {
243 assertThat(addr3, is(not(addr4))); 255 assertThat(addr3, is(not(addr4)));
244 } 256 }
245 257
258 + /**
259 + * Tests invalid class copy constructor for a null object to copy from.
260 + */
261 + @Test(expected = IllegalArgumentException.class)
262 + public void testIllegalConstructorArgument() {
263 + InterfaceIpAddress toAddr = new InterfaceIpAddress(IP_ADDRESS, V6_SUBNET_ADDRESS);
264 + }
246 } 265 }
......