aboutsummaryrefslogtreecommitdiffstats
path: root/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/dns/LatencyDirectTarget.java
blob: 09795ae08a7b02ec255e470a7861cda0b4a7ec76 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.integration.dns;

import com.yahoo.config.provision.zone.ZoneId;

import java.util.Objects;

/**
 * An implementation of {@link DirectTarget} that uses latency-based routing.
 *
 * @author freva
 */
public final class LatencyDirectTarget extends DirectTarget {

    static final String TARGET_TYPE = "latency";

    private final ZoneId zone;

    public LatencyDirectTarget(RecordData recordData, ZoneId zone) {
        super(recordData, zone.value());
        this.zone = Objects.requireNonNull(zone);
    }

    /** The zone this record points to */
    public ZoneId zone() {
        return zone;
    }

    @Override
    public RecordData pack() {
        return RecordData.from(String.join("/", TARGET_TYPE, recordData().asString(), id()));
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;
        LatencyDirectTarget that = (LatencyDirectTarget) o;
        return zone.equals(that.zone);
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), zone);
    }

    @Override
    public String toString() {
        return "latency target for " + recordData() + " [id=" + id() + "]";
    }

    /** Unpack latency alias from given record data */
    public static LatencyDirectTarget unpack(RecordData data) {
        var parts = data.asString().split("/");
        if (parts.length != 3) {
            throw new IllegalArgumentException("Expected data to be on format target-type/record-data/zone-id, but got " +
                                               data.asString());
        }
        if (!TARGET_TYPE.equals(parts[0])) {
            throw new IllegalArgumentException("Unexpected type '" + parts[0] + "'");
        }
        return new LatencyDirectTarget(RecordData.from(parts[1]), ZoneId.from(parts[2]));
    }

}