Search in sources :

Example 1 with DNSZone

use of org.opennms.core.test.dns.annotations.DNSZone 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);
        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);
            if (entry.ipv6()) {
                zone.addRecord(new AAAARecord(recordName, DClass.IN, DEFAULT_TTL, InetAddressUtils.addr(entry.address())));
            } else {
                zone.addRecord(new ARecord(recordName, DClass.IN, DEFAULT_TTL, InetAddressUtils.addr(entry.address())));
            }
        }
        m_server.addZone(zone);
    }
    LOG.debug("starting DNS server");
    m_server.start();
}
Also used : DNSEntry(org.opennms.core.test.dns.annotations.DNSEntry) JUnitDNSServer(org.opennms.core.test.dns.annotations.JUnitDNSServer) SOARecord(org.xbill.DNS.SOARecord) ARecord(org.xbill.DNS.ARecord) AAAARecord(org.xbill.DNS.AAAARecord) AAAARecord(org.xbill.DNS.AAAARecord) Zone(org.xbill.DNS.Zone) DNSZone(org.opennms.core.test.dns.annotations.DNSZone) DNSZone(org.opennms.core.test.dns.annotations.DNSZone) JUnitDNSServer(org.opennms.core.test.dns.annotations.JUnitDNSServer) NSRecord(org.xbill.DNS.NSRecord) SOARecord(org.xbill.DNS.SOARecord) Name(org.xbill.DNS.Name)

Aggregations

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 NSRecord (org.xbill.DNS.NSRecord)1 Name (org.xbill.DNS.Name)1 SOARecord (org.xbill.DNS.SOARecord)1 Zone (org.xbill.DNS.Zone)1