Search in sources :

Example 1 with PTRRecord

use of org.xbill.DNS.PTRRecord in project opennms by OpenNMS.

the class JUnitDNSServerExecutionListener method beforeTestMethod.

/**
 * {@inheritDoc}
 */
@Override
public void beforeTestMethod(final TestContext testContext) throws Exception {
    super.beforeTestMethod(testContext);
    final JUnitDNSServer config = findTestAnnotation(JUnitDNSServer.class, testContext);
    if (config == null) {
        return;
    }
    LOG.info("initializing DNS on port {}", config.port());
    m_server = new DNSServer();
    m_server.addPort(config.port());
    for (final DNSZone dnsZone : config.zones()) {
        String name = dnsZone.name();
        if (!name.endsWith(".")) {
            name = name + ".";
        }
        final Name zoneName = Name.fromString(name, Name.root);
        LOG.debug("zoneName = {}", zoneName);
        final Zone zone = new Zone(zoneName, new Record[] { new SOARecord(zoneName, DClass.IN, DEFAULT_TTL, zoneName, Name.fromString("admin." + name), 1, DEFAULT_TTL, DEFAULT_TTL, DEFAULT_TTL, DEFAULT_TTL), new NSRecord(zoneName, DClass.IN, DEFAULT_TTL, Name.fromString("resolver1.opendns.com.")), new NSRecord(zoneName, DClass.IN, DEFAULT_TTL, Name.fromString("resolver2.opendns.com.")), new ARecord(zoneName, DClass.IN, DEFAULT_TTL, InetAddressUtils.addr(dnsZone.v4address())), new AAAARecord(zoneName, DClass.IN, DEFAULT_TTL, InetAddressUtils.addr(dnsZone.v6address())) });
        LOG.debug("zone = {}", zone);
        Matcher m;
        for (final DNSEntry entry : dnsZone.entries()) {
            LOG.debug("adding entry: {}", entry);
            String hostname = entry.hostname();
            final Name recordName = Name.fromString(hostname, zoneName);
            LOG.debug("name = {}", recordName);
            switch(entry.type()) {
                case "A":
                    zone.addRecord(new ARecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, InetAddressUtils.addr(entry.data())));
                    break;
                case "AAAA":
                    zone.addRecord(new AAAARecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, InetAddressUtils.addr(entry.data())));
                    break;
                case "CNAME":
                    zone.addRecord(new CNAMERecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, Name.fromString(entry.data())));
                    break;
                case "NS":
                    zone.addRecord(new NSRecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, Name.fromString(entry.data())));
                    break;
                case "MX":
                    m = s_mxPattern.matcher(entry.data());
                    if (m.matches()) {
                        zone.addRecord(new MXRecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, Integer.valueOf(m.group(1)), Name.fromString(m.group(2))));
                    } else {
                        LOG.error("Entry data '{}' does not match MX pattern", entry.data());
                    }
                    break;
                case "PTR":
                    zone.addRecord(new PTRRecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, Name.fromString(entry.data())));
                    break;
                case "SOA":
                    m = s_soaPattern.matcher(entry.data());
                    if (m.matches()) {
                        zone.addRecord(new SOARecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, Name.fromString(m.group(1)), Name.fromString(m.group(2)), Long.valueOf(m.group(3)), Long.valueOf(m.group(4)), Long.valueOf(m.group(5)), Long.valueOf(m.group(6)), Long.valueOf(m.group(7))));
                    } else {
                        LOG.error("Entry data '{}' does not match SOA pattern", entry.data());
                    }
                    break;
                case "SRV":
                    m = s_srvPattern.matcher(entry.data());
                    if (m.matches()) {
                        zone.addRecord(new SRVRecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, Integer.valueOf(m.group(1)), Integer.valueOf(m.group(2)), Integer.valueOf(m.group(3)), Name.fromString(m.group(4))));
                    } else {
                        LOG.error("Entry data '{}' does not match MX pattern", entry.data());
                    }
                    break;
                case "TXT":
                    zone.addRecord(new TXTRecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, entry.data()));
                    break;
                default:
                    LOG.error("DNS entry type {} not supported.", entry.type());
                    break;
            }
        }
        m_server.addZone(zone);
    }
    LOG.debug("starting DNS server");
    m_server.start();
}
Also used : JUnitDNSServer(org.opennms.core.test.dns.annotations.JUnitDNSServer) AAAARecord(org.xbill.DNS.AAAARecord) Matcher(java.util.regex.Matcher) Zone(org.xbill.DNS.Zone) DNSZone(org.opennms.core.test.dns.annotations.DNSZone) DNSZone(org.opennms.core.test.dns.annotations.DNSZone) Name(org.xbill.DNS.Name) CNAMERecord(org.xbill.DNS.CNAMERecord) PTRRecord(org.xbill.DNS.PTRRecord) DNSEntry(org.opennms.core.test.dns.annotations.DNSEntry) SOARecord(org.xbill.DNS.SOARecord) ARecord(org.xbill.DNS.ARecord) AAAARecord(org.xbill.DNS.AAAARecord) TXTRecord(org.xbill.DNS.TXTRecord) MXRecord(org.xbill.DNS.MXRecord) JUnitDNSServer(org.opennms.core.test.dns.annotations.JUnitDNSServer) NSRecord(org.xbill.DNS.NSRecord) SOARecord(org.xbill.DNS.SOARecord) SRVRecord(org.xbill.DNS.SRVRecord)

Aggregations

Matcher (java.util.regex.Matcher)1 DNSEntry (org.opennms.core.test.dns.annotations.DNSEntry)1 DNSZone (org.opennms.core.test.dns.annotations.DNSZone)1 JUnitDNSServer (org.opennms.core.test.dns.annotations.JUnitDNSServer)1 AAAARecord (org.xbill.DNS.AAAARecord)1 ARecord (org.xbill.DNS.ARecord)1 CNAMERecord (org.xbill.DNS.CNAMERecord)1 MXRecord (org.xbill.DNS.MXRecord)1 NSRecord (org.xbill.DNS.NSRecord)1 Name (org.xbill.DNS.Name)1 PTRRecord (org.xbill.DNS.PTRRecord)1 SOARecord (org.xbill.DNS.SOARecord)1 SRVRecord (org.xbill.DNS.SRVRecord)1 TXTRecord (org.xbill.DNS.TXTRecord)1 Zone (org.xbill.DNS.Zone)1