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

import ai.vespa.http.DomainName;
import com.google.common.net.InetAddresses;

import java.util.Objects;

/**
 * Represents a server behind a load balancer.
 *
 * @author mpolden
 */
public class Real implements Comparable<Real> {

    public static final int defaultPort = 4443;

    private final DomainName hostname;
    private final String ipAddress;
    private final int port;

    public Real(DomainName hostname, String ipAddress) {
        this(hostname, ipAddress, defaultPort);
    }

    public Real(DomainName hostname, String ipAddress, int port) {
        this.hostname = hostname;
        this.ipAddress = requireIpAddress(ipAddress);
        if (port < 1 || port > 65535) {
            throw new IllegalArgumentException("port number must be >= 1 and <= 65535");
        }
        this.port = port;
    }

    /** The hostname of this real */
    public DomainName hostname() {
        return hostname;
    }

    /** Target IP address for this real */
    public String ipAddress() {
        return ipAddress;
    }

    /** Target port for this real */
    public int port() {
        return port;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Real real = (Real) o;
        return port == real.port &&
               Objects.equals(hostname, real.hostname) &&
               Objects.equals(ipAddress, real.ipAddress);
    }

    @Override
    public int hashCode() {
        return Objects.hash(hostname, ipAddress, port);
    }

    @Override
    public String toString() {
        return "real server " + hostname + " (" + ipAddress + ":" + port + ")";
    }

    private static String requireIpAddress(String ipAddress) {
        Objects.requireNonNull(ipAddress, "ipAddress must be non-null");
        try {
            InetAddresses.forString(ipAddress);
            return ipAddress;
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("ipAddress must be a valid IP address", e);
        }
    }

    @Override
    public int compareTo(Real that) {
        return hostname.compareTo(that.hostname());
    }

}