aboutsummaryrefslogtreecommitdiffstats
path: root/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/dns/AliasTarget.java
blob: 1fece6518c6d0b1924148cfe5191583c8d125cb4 (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
67
68
// Copyright Vespa.ai. 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 ai.vespa.http.DomainName;

import java.util.Objects;

/**
 * The target of a {@link Record.Type#ALIAS} record. Contains record fields unique to aliases.
 *
 * @author mpolden
 */
public sealed abstract class AliasTarget permits LatencyAliasTarget, WeightedAliasTarget {

    private final DomainName name;
    private final String dnsZone;
    private final String id;

    public AliasTarget(DomainName name, String dnsZone, String id) {
        this.name = Objects.requireNonNull(name, "name must be non-null");
        this.dnsZone = Objects.requireNonNull(dnsZone, "dnsZone must be non-null");
        this.id = Objects.requireNonNull(id, "id must be non-null");
    }

    /** A unique identifier of this record within the ALIAS record group */
    public String id() {
        return id;
    }

    /** DNS name this points to */
    public final DomainName name() {
        return name;
    }

    /** The DNS zone this belongs to */
    public final String dnsZone() {
        return dnsZone;
    }

    /** Returns the fields in this encoded as record data */
    public abstract RecordData pack();

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        AliasTarget alias = (AliasTarget) o;
        return name.equals(alias.name) &&
               dnsZone.equals(alias.dnsZone) &&
               id.equals(alias.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, dnsZone, id);
    }

    /** Unpack target from given record data */
    public static AliasTarget unpack(RecordData data) {
        String[] parts = data.asString().split("/");
        return switch (parts[0]) {
            case LatencyAliasTarget.TARGET_TYPE -> LatencyAliasTarget.unpack(data);
            case WeightedAliasTarget.TARGET_TYPE -> WeightedAliasTarget.unpack(data);
            default -> throw new IllegalArgumentException("Unknown alias type '" + parts[0] + "'");
        };
    }

}