aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/application/EndpointList.java
blob: 310a78e45f0e079c4ebd1332ecf6221ddec3c081 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright Yahoo. 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.yahoo.collections.AbstractFilteringList;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.config.provision.InstanceName;
import com.yahoo.config.provision.zone.AuthMethod;
import com.yahoo.vespa.hosted.controller.api.identifiers.DeploymentId;

import java.util.Collection;
import java.util.List;
import java.util.Optional;


/**
 * A list of endpoints for an application.
 *
 * @author mpolden
 */
public class EndpointList extends AbstractFilteringList<Endpoint, EndpointList> {

    public static final EndpointList EMPTY = EndpointList.copyOf(List.of());

    private EndpointList(Collection<? extends Endpoint> endpoints, boolean negate) {
        super(endpoints, negate, EndpointList::new);
        if (endpoints.stream().distinct().count() != endpoints.size()) {
            throw new IllegalArgumentException("Expected all endpoints to be distinct, got " + endpoints);
        }
    }

    /** Returns the primary (non-legacy) endpoint, if any */
    public Optional<Endpoint> primary() {
        return not().legacy().asList().stream().findFirst();
    }

    /** Returns the subset of endpoints named according to given ID and scope */
    public EndpointList named(EndpointId id, Endpoint.Scope scope) {
        return matching(endpoint -> endpoint.scope() == scope && // ID is only unique within a scope
                                    endpoint.name().equals(id.id()));
    }

    /** Returns the endpoint which has given DNS name, if any */
    public Optional<Endpoint> dnsName(String dnsName) {
        return matching(endpoint -> endpoint.dnsName().equals(dnsName)).first();
    }

    /** Returns the subset of endpoints pointing to given cluster */
    public EndpointList cluster(ClusterSpec.Id cluster) {
        return matching(endpoint -> endpoint.cluster().equals(cluster));
    }

    /** Returns the subset of endpoints pointing to given instance */
    public EndpointList instance(InstanceName instance) {
        return matching(endpoint -> endpoint.instance().isPresent() &&
                                    endpoint.instance().get().equals(instance));
    }

    /** Returns the subset of endpoints which target all the given deployments */
    public EndpointList targets(List<DeploymentId> deployments) {
        return matching(endpoint -> endpoint.deployments().containsAll(deployments));
    }

    /** Returns the subset of endpoints which target the given deployment */
    public EndpointList targets(DeploymentId deployment) {
        return targets(List.of(deployment));
    }

    /** Returns the subset of endpoints that are considered legacy */
    public EndpointList legacy() {
        return matching(Endpoint::legacy);
    }

    /** Returns the subset of endpoints generated by the system */
    public EndpointList generated() {
        return matching(endpoint -> endpoint.generated().isPresent());
    }

    /** Returns the subset of endpoints that require a rotation */
    public EndpointList requiresRotation() {
        return matching(Endpoint::requiresRotation);
    }

    /** Returns the subset of endpoints with given scope */
    public EndpointList scope(Endpoint.Scope scope) {
        return matching(endpoint -> endpoint.scope() == scope);
    }

    /** Returns the subset of endpoints that use direct routing */
    public EndpointList direct() {
        return matching(endpoint -> endpoint.routingMethod().isDirect());
    }

    /** Returns the subset of endpoints that use shared routing */
    public EndpointList shared() {
        return matching(endpoint -> endpoint.routingMethod().isShared());
    }

    /** Returns the subset of endpoints supporting given authentication method */
    public EndpointList authMethod(AuthMethod authMethod) {
        return matching(endpoint -> endpoint.authMethod() == authMethod);
    }

    public static EndpointList copyOf(Collection<Endpoint> endpoints) {
        return new EndpointList(endpoints, false);
    }

    public static EndpointList of(Endpoint ...endpoint) {
        return copyOf(List.of(endpoint));
    }

    @Override
    public String toString() {
        return asList().toString();
    }

}