summaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/application/EndpointList.java
blob: e12bb5cda7f51906b5ef822983d71bad03c6af8b (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
// 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.yahoo.collections.AbstractFilteringList;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.SystemName;
import com.yahoo.vespa.hosted.controller.application.Endpoint.Port;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;


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

    public static final EndpointList EMPTY = new EndpointList(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);
        }
    }

    private EndpointList(Collection<? extends Endpoint> endpoints) {
        this(endpoints, false);
    }

    /** Returns the main endpoint, if any */
    public Optional<Endpoint> main() {
        return asList().stream().filter(Predicate.not(Endpoint::legacy)).findFirst();
    }

    /** Returns the subset of endpoints are either legacy or not */
    public EndpointList legacy(boolean legacy) {
        return matching(endpoint -> endpoint.legacy() == legacy);
    }

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

    public static EndpointList of(Stream<Endpoint> endpoints) {
        return new EndpointList(endpoints.collect(Collectors.toUnmodifiableList()));
    }

    /** Returns the default global endpoints in given system. Default endpoints are served by a pre-provisioned routing layer */
    public static EndpointList create(ApplicationId application, EndpointId endpointId, SystemName system) {
        switch (system) {
            case cd:
            case main:
                return new EndpointList(List.of(
                        Endpoint.of(application).named(endpointId).on(Port.plain(4080)).legacy().in(system),
                        Endpoint.of(application).named(endpointId).on(Port.tls(4443)).legacy().in(system),
                        Endpoint.of(application).named(endpointId).on(Port.tls(4443)).in(system)
                ));
        }
        return EMPTY;
    }

}