NetUtil implements NetworkAddressService { public static String getIpv4NetAddress(String ipAddressString, short netMask) { String errorString = "IP '" + ipAddressString + "' is not a valid IPv4 address"; if (!isValidIPConfig(ipAddressString)) { throw new IllegalArgumentException(errorString); } if (netMask < 1 || netMask > 32) { throw new IllegalArgumentException("Netmask '" + netMask + "' is out of bounds (1-32)"); } String subnetMaskString = networkPrefixLengthToNetmask(netMask); String[] netMaskOctets = subnetMaskString.split("\\."); String[] ipv4AddressOctets = ipAddressString.split("\\."); String netAddress = ""; try { for (int i = 0; i < 4; i++) { netAddress += Integer.parseInt(ipv4AddressOctets[i]) & Integer.parseInt(netMaskOctets[i]); if (i < 3) { netAddress += "."; } } } catch (NumberFormatException e) { throw new IllegalArgumentException(errorString); } return netAddress; } @Activate  NetUtil(final @Reference SafeCaller safeCaller); @Modified synchronized void modified(Map<String, Object> config); @Override @Nullable String getPrimaryIpv4HostAddress(); @Override boolean isUseOnlyOneAddress(); @Override boolean isUseIPv6(); @Override void addNetworkAddressChangeListener(NetworkAddressChangeListener listener); @Override void removeNetworkAddressChangeListener(NetworkAddressChangeListener listener); static List<String> getAllBroadcastAddresses(); @Override @Nullable String getConfiguredBroadcastAddress(); static Collection<CidrAddress> getAllInterfaceAddresses(); static String networkPrefixLengthToNetmask(int prefixLength); static String getIpv4NetAddress(String ipAddressString, short netMask); static String getIpv4NetBroadcastAddress(String ipAddressString, short prefix); static boolean isValidIPConfig(String ipAddress); static final int POLL_INTERVAL_SECONDS; }
@Test public void testNetwork() { String network = NetUtil.getIpv4NetAddress("192.168.0.1", (short) 24); assertThat(network, is("192.168.0.0")); network = NetUtil.getIpv4NetAddress("192.168.23.5", (short) 24); assertThat(network, is("192.168.23.0")); network = NetUtil.getIpv4NetAddress("172.16.42.23", (short) 16); assertThat(network, is("172.16.0.0")); network = NetUtil.getIpv4NetAddress("10.8.13.5", (short) 8); assertThat(network, is("10.0.0.0")); network = NetUtil.getIpv4NetAddress("192.168.5.8", (short) 23); assertThat(network, is("192.168.4.0")); network = NetUtil.getIpv4NetAddress("192.168.5.8", (short) 27); assertThat(network, is("192.168.5.0")); network = NetUtil.getIpv4NetAddress("192.168.5.8", (short) 29); assertThat(network, is("192.168.5.8")); try { network = NetUtil.getIpv4NetAddress("192.168.5.8", (short) 33); } catch (IllegalArgumentException iae) { assertThat(iae.getMessage(), is("Netmask '33' is out of bounds (1-32)")); } try { network = NetUtil.getIpv4NetAddress("192.168.58", (short) 24); } catch (IllegalArgumentException iae) { assertThat(iae.getMessage(), is("IP '192.168.58' is not a valid IPv4 address")); } try { network = NetUtil.getIpv4NetAddress("SOME_TEXT", (short) 24); } catch (IllegalArgumentException iae) { assertThat(iae.getMessage(), is("IP 'SOME_TEXT' is not a valid IPv4 address")); } try { network = NetUtil.getIpv4NetAddress("SOME_TEXT", (short) 42); } catch (IllegalArgumentException iae) { assertThat(iae.getMessage(), is("IP 'SOME_TEXT' is not a valid IPv4 address")); } }