DnsmasqDriver implements DHCPDriver { @Override public Response createSubnet( String subnetId, String gateway, String cidr, String lowIp, String highIp) throws Exception { logger.info(String.format( "Creating subnet configuration files for [%s], cidr [%s], gateway [%s], lowIp [%s], highIp [%s]", subnetId, cidr, gateway, lowIp, highIp)); String netmask = IpHelper.calculateNetmaskStringFromCidr(cidr); String newOptionFilePath = dhcpOptionFileCopyDir + "/" + subnetId; File newOptionFile = new File(newOptionFilePath); if (!newOptionFile.exists()) { newOptionFile.createNewFile(); } PrintWriter newOptionFileWriter = new PrintWriter(newOptionFile, "UTF-8"); newOptionFileWriter.println("tag:" + subnetId + ",3," + gateway); newOptionFileWriter.println("tag:" + subnetId + ",1," + netmask); newOptionFileWriter.close(); String oldOptionFilePath = dhcpOptionFileDir + "/" + subnetId; File oldOptionFile = new File(oldOptionFilePath); Files.move(newOptionFile.toPath(), oldOptionFile.toPath(), StandardCopyOption.REPLACE_EXISTING); PrintWriter configFileWriter = new PrintWriter(new FileWriter(dhcpConfigFilePath, true)); configFileWriter.println("dhcp-range=tag:" + subnetId + "," + lowIp + "," + highIp); configFileWriter.close(); Response response = new Response(); 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 testCreateSubnet() { try { setUpDriver(successScript, DnsmasqDriverTest.class.getResource("/dnsmasq.leases").getPath()); String gateway = "192.168.1.1"; String cidr = "192.168.1.0/8"; dnsmasqDriver.createSubnet("subnet1", gateway, cidr, "192.168.1.0", "192.168.1.16"); FileReader optionFileReader = new FileReader( DnsmasqDriverTest.class.getResource("/options").getPath() + "/subnet1"); BufferedReader optionFileBufferedReader = new BufferedReader(optionFileReader); String optionFileLine; boolean hasGatewayLine = false; boolean hasNetmaskLine = false; while ((optionFileLine = optionFileBufferedReader.readLine()) != null) { if (optionFileLine.contains("tag:subnet1,3,192.168.1.1")) { hasGatewayLine = true; } else if (optionFileLine.contains("tag:subnet1,1,255.0.0.0")) { hasNetmaskLine = true; } } if (!hasGatewayLine) { fail("Missing gateway line in option file"); } if (!hasNetmaskLine) { fail("Missing netmask line in option file"); } optionFileBufferedReader.close(); FileReader configFileReader = new FileReader( DnsmasqDriverTest.class.getResource("/config/dnsmasq.conf").getPath()); BufferedReader configFileBufferedReader = new BufferedReader(configFileReader); String configFileLine; boolean hasConfigFileLine = false; while ((configFileLine = configFileBufferedReader.readLine()) != null) { if (!configFileLine.contains("subnet1")) { continue; } if (!configFileLine.equals("dhcp-range=tag:subnet1,192.168.1.0,192.168.1.16")) { fail("Wrong content of the config file: " + configFileLine); } hasConfigFileLine = true; break; } if (!hasConfigFileLine) { fail("Config file does not contain dhcp range for the subnet"); } configFileBufferedReader.close(); } catch (Throwable e) { fail("Failed with exception: " + e.getMessage()); } }