summaryrefslogtreecommitdiffstats
path: root/config-provisioning/src/main/java/com/yahoo/config/provision/HostSpec.java
blob: 2a5d27a0fe754b89bc341f104451cd9b163ca057 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.provision;

import com.yahoo.component.Version;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * A specification of a host and its role.
 * Equality and order is determined by the host name.
 *
 * @author hmusum
 */
public class HostSpec implements Comparable<HostSpec> {

    /** The name of this host */
    private final String hostname;

    /** Aliases of this host */
    private final List<String> aliases;

    /** The current membership role of this host in the cluster it belongs to */
    private final Optional<ClusterMembership> membership;

    private final Optional<Flavor> flavor;

    private final Optional<Version> version;

    private final Optional<DockerImage> dockerImageRepo;

    private final Optional<NetworkPorts> networkPorts;

    private final Optional<NodeResources> requestedResources;

    public HostSpec(String hostname, Optional<ClusterMembership> membership) {
        this(hostname, new ArrayList<>(), Optional.empty(), membership);
    }

    public HostSpec(String hostname, ClusterMembership membership, Flavor flavor, Optional<Version> version) {
        this(hostname, new ArrayList<>(), Optional.of(flavor), Optional.of(membership), version);
    }

    public HostSpec(String hostname, List<String> aliases) {
        this(hostname, aliases, Optional.empty(), Optional.empty());
    }

    public HostSpec(String hostname, List<String> aliases, Flavor flavor) {
        this(hostname, aliases, Optional.of(flavor), Optional.empty());
    }

    public HostSpec(String hostname, List<String> aliases, ClusterMembership membership) {
        this(hostname, aliases, Optional.empty(), Optional.of(membership));
    }

    public HostSpec(String hostname, List<String> aliases, Optional<Flavor> flavor, Optional<ClusterMembership> membership) {
        this(hostname, aliases, flavor, membership, Optional.empty());
    }

    public HostSpec(String hostname, List<String> aliases, Optional<Flavor> flavor,
                    Optional<ClusterMembership> membership, Optional<Version> version) {
        this(hostname, aliases, flavor, membership, version, Optional.empty());
    }

    public HostSpec(String hostname, List<String> aliases, Optional<Flavor> flavor,
                    Optional<ClusterMembership> membership, Optional<Version> version,
                    Optional<NetworkPorts> networkPorts) {
        this(hostname, aliases, flavor, membership, version, networkPorts, Optional.empty());
    }

    public HostSpec(String hostname, List<String> aliases, Optional<Flavor> flavor,
                    Optional<ClusterMembership> membership, Optional<Version> version,
                    Optional<NetworkPorts> networkPorts, Optional<NodeResources> requestedResources) {
        this(hostname, aliases, flavor, membership, version, networkPorts, requestedResources, Optional.empty());
    }

    public HostSpec(String hostname, List<String> aliases, Optional<Flavor> flavor,
                    Optional<ClusterMembership> membership, Optional<Version> version,
                    Optional<NetworkPorts> networkPorts, Optional<NodeResources> requestedResources,
                    Optional<DockerImage> dockerImageRepo) {
        if (hostname == null || hostname.isEmpty()) throw new IllegalArgumentException("Hostname must be specified");
        this.hostname = hostname;
        this.aliases = List.copyOf(aliases);
        this.flavor = flavor;
        this.membership = membership;
        this.version = Objects.requireNonNull(version, "Version cannot be null but can be empty");
        this.networkPorts = Objects.requireNonNull(networkPorts, "Network ports cannot be null but can be empty");
        this.requestedResources = Objects.requireNonNull(requestedResources, "RequestedResources cannot be null");
        this.dockerImageRepo = Objects.requireNonNull(dockerImageRepo, "Docker image repo cannot be null but can be empty");
    }

    /** Returns the name identifying this host */
    public String hostname() { return hostname; }

    /** Returns the aliases of this host as an immutable list. This may be empty but never null. */
    public List<String> aliases() { return aliases; }

    public Optional<Flavor> flavor() { return flavor; }

    /** Returns the current version of Vespa running on this node, or empty if not known */
    public Optional<com.yahoo.component.Version> version() { return version; }

    /** Returns the membership of this host, or an empty value if not present */
    public Optional<ClusterMembership> membership() { return membership; }

    /** Returns the network port allocations on this host, or empty if not present */
    public Optional<NetworkPorts> networkPorts() { return networkPorts; }

    /** Returns the requested resources leading to this host being provisioned, or empty if not known */
    public Optional<NodeResources> requestedResources() { return requestedResources; }

    public Optional<DockerImage> dockerImageRepo() { return dockerImageRepo; }

    public HostSpec withPorts(Optional<NetworkPorts> ports) {
        return new HostSpec(hostname, aliases, flavor, membership, version, ports, requestedResources, dockerImageRepo);
    }

    @Override
    public String toString() {
        return hostname +
               (! aliases.isEmpty() ? " (aliases: " + aliases + ")" : "") +
               (membership.isPresent() ? " (membership: " + membership.get() + ")" : " (no membership)");
    }

    @Override
    public boolean equals(Object other) {
        if (other == this) return true;
        if ( ! (other instanceof HostSpec)) return false;

        return ((HostSpec)other).hostname.equals(this.hostname);
    }

    @Override
    public int hashCode() {
        return hostname.hashCode();
    }

    @Override
    public int compareTo(HostSpec other) {
        return hostname.compareTo(other.hostname);
    }

}