NeutronSubnet extends NeutronBaseAttributes<NeutronSubnet> { public boolean isValidIp(String ipAddress) { if (ipVersion == IPV4_VERSION) { try { SubnetUtils util = new SubnetUtils(cidr); SubnetInfo info = util.getInfo(); return info.isInRange(ipAddress); } catch (IllegalArgumentException e) { LOG.warn("Failure in isValidIp()", e); return false; } } if (ipVersion == IPV6_VERSION) { String[] parts = cidr.split("/"); int length = Integer.parseInt(parts[1]); byte[] cidrBytes = ((Inet6Address) InetAddresses.forString(parts[0])).getAddress(); byte[] ipBytes = ((Inet6Address) InetAddresses.forString(ipAddress)).getAddress(); for (int index = 0; index < length; index++) { if ((cidrBytes[index / IPV6_LENGTH_BYTES] & IPV6_LSB_MASK & 1 << IPV6_BYTE_OFFSET - index % IPV6_LENGTH_BYTES) != ( ipBytes[index / IPV6_LENGTH_BYTES] & IPV6_LSB_MASK & 1 << IPV6_BYTE_OFFSET - index % IPV6_LENGTH_BYTES)) { return false; } } return true; } return false; }  String getNetworkUUID(); void setNetworkUUID(String networkUUID); Integer getIpVersion(); void setIpVersion(Integer ipVersion); String getCidr(); void setCidr(String cidr); String getGatewayIp(); void setGatewayIp(String gatewayIp); List<String> getDnsNameservers(); void setDnsNameservers(List<String> dnsNameservers); List<NeutronSubnetIpAllocationPool> getAllocationPools(); void setAllocationPools(List<NeutronSubnetIpAllocationPool> allocationPools); List<NeutronRoute> getHostRoutes(); void setHostRoutes(List<NeutronRoute> hostRoutes); boolean isEnableDHCP(); Boolean getEnableDHCP(); void setEnableDHCP(Boolean newValue); String getIpV6AddressMode(); void setIpV6AddressMode(String ipV6AddressMode); String getIpV6RaMode(); void setIpV6RaMode(String ipV6RaMode); boolean isValidCIDR(); boolean gatewayIp_Pool_overlap(); @Override void initDefaults(); boolean isValidIp(String ipAddress); String getLowAddr(); @Override String toString();  }
@Test public void isValidIpTest() { NeutronSubnet objectUT = new NeutronSubnet(); objectUT.setIpVersion(4); objectUT.setCidr("10.18.0.0/24"); Assert.assertFalse("isValidIp Negative Test 1: test of IP address outside of CIDR block failed", objectUT.isValidIp("10.18.1.1")); Assert.assertTrue("isValidIp Test 1: test of IP address within CIDR block failed", objectUT.isValidIp("10.18.0.1")); objectUT.setIpVersion(6); objectUT.setCidr("2015::0/64"); Assert.assertFalse("isValidIp v6 Negative Test 1: test of IP address outside of CIDR block failed", objectUT.isValidIp("2015:0:0:1:0:0:0:1")); Assert.assertTrue("isValidIp v6 Test 1: test of IP address within CIDR block failed", objectUT.isValidIp("2015:0:0:0:1:0:0:1")); }