aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/application/RoutingPolicy.java
blob: 4934c29afe4565a25191ebcb5aaf2a19f214eefe (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.application;

import com.google.common.base.Strings;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.config.provision.HostName;
import com.yahoo.vespa.hosted.controller.api.integration.zone.ZoneId;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Represents the DNS routing policy for a load balancer.
 *
 * @author mortent
 */
public class RoutingPolicy {

    private static final String ignoredEndpointPart = "default";

    private final ApplicationId owner;
    private final String recordId;
    private final HostName alias;
    private final HostName canonicalName;

    public RoutingPolicy(ApplicationId owner, String recordId, HostName alias, HostName canonicalName) {
        this.owner = Objects.requireNonNull(owner, "owner must be non-null");
        this.recordId = Objects.requireNonNull(recordId, "recordId must be non-null");
        this.alias = Objects.requireNonNull(alias, "alias must be non-null");
        this.canonicalName = Objects.requireNonNull(canonicalName, "canonicalName must be non-null");
    }

    /** The application owning this */
    public ApplicationId owner() {
        return owner;
    }

    /** The ID of the DNS record identifying this */
    public String recordId() {
        return recordId;
    }

    /** This alias (lhs of a CNAME record) */
    public HostName alias() {
        return alias;
    }

    /** The canonical name for this (rhs of a CNAME record) */
    public HostName canonicalName() {
        return canonicalName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        RoutingPolicy that = (RoutingPolicy) o;
        return owner.equals(that.owner) &&
               recordId.equals(that.recordId) &&
               alias.equals(that.alias) &&
               canonicalName.equals(that.canonicalName);
    }

    @Override
    public int hashCode() {
        return Objects.hash(owner, recordId, alias, canonicalName);
    }

    @Override
    public String toString() {
        return String.format("%s: %s -> %s, owned by %s", recordId, alias, canonicalName, owner.toShortString());
    }

    public static String createAlias(ClusterSpec.Id clusterId, ApplicationId applicationId, ZoneId zoneId) {
        List<String> parts = Arrays.asList(ignorePartIfDefault(clusterId.value()),
                                           ignorePartIfDefault(applicationId.instance().value()),
                                           applicationId.application().value(),
                                           applicationId.tenant().value() +
                                           "." + zoneId.value() + "." + "vespa.oath.cloud"
        );
        return parts.stream()
                    .filter(s -> !Strings.isNullOrEmpty((s)))
                    .collect(Collectors.joining("--"));
    }

    private static String ignorePartIfDefault(String s) {
        return ignoredEndpointPart.equalsIgnoreCase(s) ? "" : s;
    }

}