aboutsummaryrefslogtreecommitdiffstats
path: root/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/provisioning/ProvisionedHost.java
blob: 8a84cfef09a97bf7f76df618c2e08f7c5082506b (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.provisioning;

import com.yahoo.component.Version;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.CloudAccount;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.config.provision.Flavor;
import com.yahoo.config.provision.HostName;
import com.yahoo.config.provision.NodeResources;
import com.yahoo.config.provision.NodeType;
import com.yahoo.vespa.hosted.provision.Node;
import com.yahoo.vespa.hosted.provision.node.IP;
import com.yahoo.vespa.hosted.provision.node.OsVersion;
import com.yahoo.vespa.hosted.provision.node.Status;

import java.time.Duration;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * Describes a single newly provisioned host by {@link HostProvisioner}.
 *
 * @author freva
 */
public class ProvisionedHost {

    private final String id;
    private final String hostHostname;
    private final Flavor hostFlavor;
    private final NodeType hostType;
    private final Optional<ApplicationId> provisionedForApplicationId;
    private final Optional<ApplicationId> exclusiveToApplicationId;
    private final Optional<ClusterSpec.Type> exclusiveToClusterType;
    private final List<HostName> nodeHostnames;
    private final NodeResources nodeResources;
    private final Version osVersion;
    private final CloudAccount cloudAccount;

    public ProvisionedHost(String id, String hostHostname, Flavor hostFlavor, NodeType hostType,
                           Optional<ApplicationId> provisionedForApplicationId,
                           Optional<ApplicationId> exclusiveToApplicationId,
                           Optional<ClusterSpec.Type> exclusiveToClusterType,
                           List<HostName> nodeHostnames, NodeResources nodeResources,
                           Version osVersion, CloudAccount cloudAccount) {
        if (!hostType.isHost()) throw new IllegalArgumentException(hostType + " is not a host");
        this.id = Objects.requireNonNull(id, "Host id must be set");
        this.hostHostname = Objects.requireNonNull(hostHostname, "Host hostname must be set");
        this.hostFlavor = Objects.requireNonNull(hostFlavor, "Host flavor must be set");
        this.hostType = Objects.requireNonNull(hostType, "Host type must be set");
        this.provisionedForApplicationId = Objects.requireNonNull(provisionedForApplicationId, "provisionedForApplicationId must be set");
        this.exclusiveToApplicationId = Objects.requireNonNull(exclusiveToApplicationId, "exclusiveToApplicationId must be set");
        this.exclusiveToClusterType = Objects.requireNonNull(exclusiveToClusterType, "exclusiveToClusterType must be set");
        this.nodeHostnames = validateNodeAddresses(nodeHostnames);
        this.nodeResources = Objects.requireNonNull(nodeResources, "Node resources must be set");
        this.osVersion = Objects.requireNonNull(osVersion, "OS version must be set");
        this.cloudAccount = Objects.requireNonNull(cloudAccount, "Cloud account must be set");
    }

    private static List<HostName> validateNodeAddresses(List<HostName> nodeHostnames) {
        Objects.requireNonNull(nodeHostnames, "Node hostnames must be set");
        if (nodeHostnames.isEmpty()) {
            throw new IllegalArgumentException("There must be at least one node hostname");
        }
        return nodeHostnames;
    }

    /** Generate {@link Node} instance representing the provisioned physical host */
    public Node generateHost(Duration hostTTL) {
        Node.Builder builder = Node.create(id, IP.Config.of(List.of(), List.of(), nodeHostnames), hostHostname, hostFlavor, hostType)
                                   .status(Status.initial().withOsVersion(OsVersion.EMPTY.withCurrent(Optional.of(osVersion))))
                                   .cloudAccount(cloudAccount);
        provisionedForApplicationId.ifPresent(builder::provisionedForApplicationId);
        exclusiveToApplicationId.ifPresent(builder::exclusiveToApplicationId);
        exclusiveToClusterType.ifPresent(builder::exclusiveToClusterType);
        if ( ! hostTTL.isZero()) builder.hostTTL(hostTTL);
        return builder.build();
    }

    /** Generate {@link Node} instance representing the node running on this physical host */
    public Node generateNode() {
        return Node.reserve(List.of(), nodeHostname(), hostHostname, nodeResources, hostType.childNodeType())
                   .cloudAccount(cloudAccount)
                   .build();
    }

    public String getId() { return id; }
    public String hostHostname() { return hostHostname; }
    public Flavor hostFlavor() { return hostFlavor; }
    public NodeType hostType() { return hostType; }
    public Optional<ApplicationId> provisionedForApplicationId() { return provisionedForApplicationId; }
    public Optional<ApplicationId> exclusiveToApplicationId() { return exclusiveToApplicationId; }
    public Optional<ClusterSpec.Type> exclusiveToClusterType() { return exclusiveToClusterType; }
    public List<HostName> nodeHostnames() { return nodeHostnames; }
    public NodeResources nodeResources() { return nodeResources; }
    public Version osVersion() { return osVersion; }
    public CloudAccount cloudAccount() { return cloudAccount; }

    public String nodeHostname() { return nodeHostnames.get(0).value(); }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ProvisionedHost that = (ProvisionedHost) o;
        return id.equals(that.id) &&
               hostHostname.equals(that.hostHostname) &&
               hostFlavor.equals(that.hostFlavor) &&
               hostType == that.hostType &&
               provisionedForApplicationId.equals(that.provisionedForApplicationId) &&
               exclusiveToApplicationId.equals(that.exclusiveToApplicationId) &&
               exclusiveToClusterType.equals(that.exclusiveToClusterType) &&
               nodeHostnames.equals(that.nodeHostnames) &&
               nodeResources.equals(that.nodeResources) &&
               osVersion.equals(that.osVersion) &&
               cloudAccount.equals(that.cloudAccount);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, hostHostname, hostFlavor, hostType, provisionedForApplicationId, exclusiveToApplicationId, exclusiveToClusterType, nodeHostnames, nodeResources, osVersion, cloudAccount);
    }

    @Override
    public String toString() {
        return "ProvisionedHost{" +
               "id='" + id + '\'' +
               ", hostHostname='" + hostHostname + '\'' +
               ", hostFlavor=" + hostFlavor +
               ", hostType=" + hostType +
               ", provisionedForApplicationId=" + provisionedForApplicationId +
               ", exclusiveToApplicationId=" + exclusiveToApplicationId +
               ", exclusiveToClusterType=" + exclusiveToClusterType +
               ", nodeHostnames=" + nodeHostnames +
               ", nodeResources=" + nodeResources +
               ", osVersion=" + osVersion +
               ", cloudAccount=" + cloudAccount +
               '}';
    }

}