Fixing ONOS-4875

Change-Id: I2b4fec44f623cc6df90c45c1cabc8c40601bf4b6
(cherry picked from commit 1a5491ecaf6e648a547ea1265796835f7a24d300)
This commit is contained in:
Srinivas Bandi 2016-08-05 13:46:10 +05:30
parent ef835c9d46
commit bc38cd417c
2 changed files with 49 additions and 11 deletions

View File

@ -60,11 +60,11 @@ public class InterfaceIpAddress {
* @param subnetAddress the IP subnet address * @param subnetAddress the IP subnet address
*/ */
public InterfaceIpAddress(IpAddress ipAddress, IpPrefix subnetAddress) { public InterfaceIpAddress(IpAddress ipAddress, IpPrefix subnetAddress) {
this.ipAddress = checkNotNull(ipAddress); checkArgument(checkNotNull(ipAddress).version() == checkNotNull(subnetAddress).version(),
this.subnetAddress = checkNotNull(subnetAddress); "IP and subnet version mismatch");
// TODO: Recompute the default broadcast address from the subnet this.ipAddress = ipAddress;
// address this.subnetAddress = subnetAddress;
this.broadcastAddress = null; this.broadcastAddress = computeBroadcastAddress(ipAddress, subnetAddress);
this.peerAddress = null; this.peerAddress = null;
} }
@ -78,8 +78,10 @@ public class InterfaceIpAddress {
*/ */
public InterfaceIpAddress(IpAddress ipAddress, IpPrefix subnetAddress, public InterfaceIpAddress(IpAddress ipAddress, IpPrefix subnetAddress,
IpAddress broadcastAddress) { IpAddress broadcastAddress) {
this.ipAddress = checkNotNull(ipAddress); checkArgument(checkNotNull(ipAddress).version() == checkNotNull(subnetAddress).version(),
this.subnetAddress = checkNotNull(subnetAddress); "IP and subnet version mismatch");
this.ipAddress = ipAddress;
this.subnetAddress = subnetAddress;
this.broadcastAddress = broadcastAddress; this.broadcastAddress = broadcastAddress;
this.peerAddress = null; this.peerAddress = null;
} }
@ -97,8 +99,10 @@ public class InterfaceIpAddress {
public InterfaceIpAddress(IpAddress ipAddress, IpPrefix subnetAddress, public InterfaceIpAddress(IpAddress ipAddress, IpPrefix subnetAddress,
IpAddress broadcastAddress, IpAddress broadcastAddress,
IpAddress peerAddress) { IpAddress peerAddress) {
this.ipAddress = checkNotNull(ipAddress); checkArgument(checkNotNull(ipAddress).version() == checkNotNull(subnetAddress).version(),
this.subnetAddress = checkNotNull(subnetAddress); "IP and subnet version mismatch");
this.ipAddress = ipAddress;
this.subnetAddress = subnetAddress;
this.broadcastAddress = broadcastAddress; this.broadcastAddress = broadcastAddress;
this.peerAddress = peerAddress; this.peerAddress = peerAddress;
} }
@ -157,6 +161,21 @@ public class InterfaceIpAddress {
return new InterfaceIpAddress(addr, subnet); return new InterfaceIpAddress(addr, subnet);
} }
/**
* Compute the IP broadcast address.
*
* @return the IP broadcast address
*/
public static IpAddress computeBroadcastAddress(IpAddress ipAddress, IpPrefix subnetAddress) {
if (ipAddress.isIp6()) {
return null;
} else {
IpAddress maskedIP = IpAddress.makeMaskedAddress(ipAddress, subnetAddress.prefixLength());
int ipB = maskedIP.getIp4Address().toInt() | ((1 << (32 - subnetAddress.prefixLength())) - 1);
return IpAddress.valueOf(ipB);
}
}
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
if (other == this) { if (other == this) {

View File

@ -34,6 +34,10 @@ public class InterfaceIpAddressTest {
private static final IpAddress BROADCAST_ADDRESS = private static final IpAddress BROADCAST_ADDRESS =
IpAddress.valueOf("1.2.0.255"); // NOTE: non-default broadcast IpAddress.valueOf("1.2.0.255"); // NOTE: non-default broadcast
private static final IpAddress PEER_ADDRESS = IpAddress.valueOf("5.6.7.8"); private static final IpAddress PEER_ADDRESS = IpAddress.valueOf("5.6.7.8");
private static final IpAddress DEF_BROADCAST_ADDRESS =
IpAddress.valueOf("1.2.255.255"); // NOTE: default broadcast
private static final IpPrefix V6_SUBNET_ADDRESS =
IpPrefix.valueOf("::/64");
private static final IpAddress IP_ADDRESS2 = IpAddress.valueOf("10.2.3.4"); private static final IpAddress IP_ADDRESS2 = IpAddress.valueOf("10.2.3.4");
private static final IpPrefix SUBNET_ADDRESS2 = private static final IpPrefix SUBNET_ADDRESS2 =
@ -97,8 +101,16 @@ public class InterfaceIpAddressTest {
new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS); new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS);
assertThat(addr.ipAddress(), is(IP_ADDRESS)); assertThat(addr.ipAddress(), is(IP_ADDRESS));
assertThat(addr.subnetAddress(), is(SUBNET_ADDRESS)); assertThat(addr.subnetAddress(), is(SUBNET_ADDRESS));
assertThat(addr.broadcastAddress(), nullValue()); assertThat(addr.broadcastAddress(), is(DEF_BROADCAST_ADDRESS));
assertThat(addr.peerAddress(), nullValue()); assertThat(addr.peerAddress(), nullValue());
IpPrefix subnetAddr = IpPrefix.valueOf("10.2.3.0/24");
InterfaceIpAddress addr1 = new InterfaceIpAddress(IP_ADDRESS2, subnetAddr);
assertThat(addr1.broadcastAddress().toString(), is("10.2.3.255"));
IpAddress ipAddress = IpAddress.valueOf("2001::4");
InterfaceIpAddress addr2 = new InterfaceIpAddress(ipAddress, V6_SUBNET_ADDRESS);
assertThat(addr2.broadcastAddress(), is(nullValue()));
} }
/** /**
@ -144,7 +156,7 @@ public class InterfaceIpAddressTest {
addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS); addr = new InterfaceIpAddress(IP_ADDRESS, SUBNET_ADDRESS);
assertThat(addr.ipAddress().toString(), is("1.2.3.4")); assertThat(addr.ipAddress().toString(), is("1.2.3.4"));
assertThat(addr.subnetAddress().toString(), is("1.2.0.0/16")); assertThat(addr.subnetAddress().toString(), is("1.2.0.0/16"));
assertThat(addr.broadcastAddress(), is(nullValue())); // TODO: Fix assertThat(addr.broadcastAddress(), is(DEF_BROADCAST_ADDRESS));
assertThat(addr.peerAddress(), is(nullValue())); assertThat(addr.peerAddress(), is(nullValue()));
// Interface address with non-default broadcast address // Interface address with non-default broadcast address
@ -243,4 +255,11 @@ public class InterfaceIpAddressTest {
assertThat(addr3, is(not(addr4))); assertThat(addr3, is(not(addr4)));
} }
/**
* Tests invalid class copy constructor for a null object to copy from.
*/
@Test(expected = IllegalArgumentException.class)
public void testIllegalConstructorArgument() {
InterfaceIpAddress toAddr = new InterfaceIpAddress(IP_ADDRESS, V6_SUBNET_ADDRESS);
}
} }