DnsmasqDriver implements DHCPDriver { @Override public Response updateSubnet( String subnetId, Map<String, String> ipAddressToMACAddressMap, Long version) throws Exception { logger.info(String.format( "Updating subnet leases for [%s]: new mapping is [%s]", subnetId, ipAddressToMACAddressMap.toString())); Response response = new Response(); String oldSubnetFilename = dhcpHostFileDir + "/" + subnetId; File oldSubnetHostFile = new File(oldSubnetFilename); Map<String, String> ipToMacToRemove = new HashMap<>(); if (oldSubnetHostFile.exists()) { BufferedReader reader = new BufferedReader(new FileReader(oldSubnetHostFile)); try { String line = null; while ((line = reader.readLine()) != null) { Pattern versionPattern = Pattern.compile("^# Version=(?<version>[0-9]+)$"); Matcher versionMatcher = versionPattern.matcher(line); if (versionMatcher.matches()) { Long oldVersion = Long.parseLong(versionMatcher.group("version")); if (oldVersion > version) { response.exitCode = 1; return response; } if (oldVersion.equals(version)) { response.exitCode = 0; return response; } } else { String[] ipToMacParts = line.split(","); if (ipToMacParts.length != 3) { continue; } String ip = ipToMacParts[0]; String mac = ipToMacParts[1]; if (!ipAddressToMACAddressMap.containsKey(ip) || !ipAddressToMACAddressMap.get(ip).equalsIgnoreCase(mac)) { ipToMacToRemove.put(ip, mac); } } } } finally { reader.close(); } } String newSubnetFilename = dhcpHostFileCopyDir + "/" + subnetId; PrintWriter writer = new PrintWriter(newSubnetFilename, "UTF-8"); writer.println("# Version=" + version); for (Map.Entry<String, String> pair : ipAddressToMACAddressMap.entrySet()) { String line = pair.getKey() + "," + pair.getValue() + ",net:" + subnetId; writer.println(line); } writer.close(); File newSubnetHostFile = new File(newSubnetFilename); Files.move(newSubnetHostFile.toPath(), oldSubnetHostFile.toPath(), StandardCopyOption.REPLACE_EXISTING); logger.info(String.format( "Updating subnet leases for [%s]: mapping to be removed is [%s]", subnetId, ipToMacToRemove.toString())); if (!ipToMacToRemove.isEmpty()) { removeLinesFromFile(dhcpLeaseFilePath, ipToMacToRemove.entrySet().stream() .map(entry -> entry.getValue() + " " + entry.getKey()).collect(Collectors.toList())); } response.exitCode = 0; return response; } DnsmasqDriver(
      String dhcpLeaseFilePath,
      String dhcpHostFileDir,
      String dhcpOptionFileDir,
      String dhcpConfigFilePath); @Override boolean reload(); @Override boolean isRunning(); @Override Response createSubnet(
      String subnetId,
      String gateway,
      String cidr,
      String lowIp,
      String highIp); @Override Response deleteSubnet(
      String subnetId); @Override Response updateSubnet(
      String subnetId,
      Map<String, String> ipAddressToMACAddressMap,
      Long version);  }
@Test public void testUpdateSubnetIPAllocation() { try { setUpDriver(successScript, DnsmasqDriverTest.class.getResource("/dnsmasq.leases").getPath()); String ipAddress = "192.168.0.2"; String macAddress = "08:00:27:d8:7d:8e"; Map<String, String> macAddressIPMap = new HashMap<>(); macAddressIPMap.put(ipAddress, macAddress); dnsmasqDriver.updateSubnet("subnet1", macAddressIPMap, 1L); FileReader subnetHostFile = new FileReader( DnsmasqDriverTest.class.getResource("/hosts").getPath() + "/subnet1"); BufferedReader bufferedReader = new BufferedReader(subnetHostFile); String line; while ((line = bufferedReader.readLine()) != null) { if (line.contains("Version")) { assertEquals(line, "# Version=1"); continue; } if (!line.contains(ipAddress)) { fail(String.format("IP address not found in host file:", ipAddress)); } if (!line.contains(macAddress)) { fail(String.format("MAC address not found in host file:", macAddress)); } } bufferedReader.close(); } catch (Throwable e) { fail(String.format("Failed with exception: %s", e.toString())); } }