summaryrefslogtreecommitdiffstats
path: root/application-model/src/main/java/com/yahoo/vespa/applicationmodel/InfrastructureApplication.java
blob: 72a278b248a1618d68c7d3a88c53993ae9e78743 (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 Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.applicationmodel;

import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.NodeType;

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * Special infrastructure applications in hosted Vespa.
 *
 * @author hakonhall
 */
public enum InfrastructureApplication {

    CONTROLLER_HOST("controller-host", NodeType.controllerhost),
    CONTROLLER("controller", NodeType.controller),
    CONFIG_SERVER_HOST("configserver-host", NodeType.confighost),
    CONFIG_SERVER("zone-config-servers", NodeType.config),
    PROXY_HOST("proxy-host", NodeType.proxyhost),
    PROXY("routing", NodeType.proxy),
    TENANT_HOST("tenant-host", NodeType.host);

    private final ApplicationId id;
    private final NodeType nodeType;

    /** Returns all applications that MAY be encountered in hosted Vespa, e.g. not DEV_HOST. */
    public static List<InfrastructureApplication> toList() {
        return List.of(values());
    }

    public static InfrastructureApplication withNodeType(NodeType nodeType) {
        return Stream.of(values())
                     .filter(application -> nodeType == application.nodeType)
                     .findAny()
                     .orElseThrow(() -> new IllegalArgumentException("No application associated with " + nodeType));
    }

    public static Optional<InfrastructureApplication> ofOptional(ApplicationId applicationId) {
        for (var application : values()) {
            if (application.id.equals(applicationId)) {
                return Optional.of(application);
            }
        }

        return Optional.empty();
    }

    InfrastructureApplication(String name, NodeType nodeType) {
        this.id = ApplicationId.from(TenantId.HOSTED_VESPA.value(), name, "default");
        this.nodeType = nodeType;
    }

    public ApplicationId id() { return id; }
    /** Avoid using {@link #name()} which is the name of the enum constant. */
    public String applicationName() { return id.application().value(); }
    public NodeType nodeType() { return nodeType; }
    public boolean isConfigServerLike() { return this == CONFIG_SERVER || this == CONTROLLER; }
    public boolean isConfigServerHostLike() { return this == CONFIG_SERVER_HOST || this == CONTROLLER_HOST; }

    @Override
    public String toString() {
        return "InfrastructureApplication{" +
               "id=" + id +
               ", nodeType=" + nodeType +
               '}';
    }
}