private static Zone fillDynamicZone()

in traffic_router/core/src/main/java/org/apache/traffic_control/traffic_router/core/dns/ZoneManager.java [988:1077]


	private static Zone fillDynamicZone(final LoadingCache<ZoneKey, Zone> dzc, final Zone staticZone, final DNSRequest request, final DNSRouteResult result) {
		if (result == null || result.getAddresses() == null) {
			return null;
		}

		try {
			boolean nsSeen = false;
			final List<Record> records = new ArrayList<>();
			String dsRoutingName = null;
			boolean routingNameNSECSeen = false;
			for (final InetRecord address : result.getAddresses()) {
				final DeliveryService ds = result.getDeliveryService();
				Name name = request.getName();

				if (ds != null && ds.getRoutingName() != null) {
					dsRoutingName = ds.getRoutingName();
				}
				if (address.getType() == Type.NS) {
					name = staticZone.getOrigin();
				} else if (ds != null && (address.getType() == Type.A || address.getType() == Type.AAAA)) {
					final String routingName = ds.getRoutingName();
					name = new Name(routingName, staticZone.getOrigin()); // routingname.ds.cdn.tld
				} else if (ds != null && address.getType() == Type.NSEC && dsRoutingName != null &&
						address.getTarget().equals(new Name(dsRoutingName, staticZone.getOrigin()).toString())) {
					routingNameNSECSeen = true;
				}

				final Record record = createRecord(name, address);

				if (record != null) {
					records.add(record);
				}
				if (record instanceof NSRecord) {
					nsSeen = true;
				}
			}

			// populate the dynamic zone with any static entries that aren't NS records or routing names
			final Iterator<RRset> it = staticZone.iterator();

			while (it.hasNext()) {
				final RRset rrset = it.next();
				final Iterator<Record> rit = rrset.rrs();

				while (rit.hasNext()) {
					final Record r = rit.next();

					if (r instanceof NSRecord) { // NSRecords are handled below
						continue;
					}

					if (r.getType() == Type.NSEC && r.getName().toString().equals(staticZone.getOrigin().toString()) && dsRoutingName != null) {
						final Name dsFQDN = new Name(dsRoutingName, staticZone.getOrigin());
						if (r instanceof NSECRecord) {
							final NSECRecord removeRec = new NSECRecord(dsFQDN, r.getDClass(), r.getTTL(), ((NSECRecord) r).getNext(), ((NSECRecord) r).getTypes());
							if (dsFQDN.compareTo(removeRec.getNext()) < 0 && !routingNameNSECSeen) {
								records.add(removeRec);
								routingNameNSECSeen = true;
							} else {
								records.add(r);
							}
						}
					} else {
						records.add(r);
					}
				}

			}

			if (!records.isEmpty()) {
				if (!nsSeen) {
					records.addAll(createZoneNSRecords(staticZone));
				}

				try {
					final ZoneKey zoneKey = getSignatureManager().generateDynamicZoneKey(staticZone.getOrigin(), records, request.isDnssec());
					final Zone zone = dzc.get(zoneKey);
					return zone;
				} catch (ExecutionException e) {
					LOGGER.error(e, e);
				}

				return new Zone(staticZone.getOrigin(), records.toArray(new Record[0]));
			}
		} catch (final IOException e) {
			LOGGER.error(e.getMessage(), e);
		}

		return null;
	}