aboutsummaryrefslogtreecommitdiffstats
path: root/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/node/OsVersion.java
blob: c914fb4ace86a7defa5d22086d81e7deaa57de49 (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
// 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.node;

import com.yahoo.component.Version;

import java.util.Objects;
import java.util.Optional;

/**
 * The OS version of a node. This contains the current and wanted OS version and is immutable.
 *
 * @author mpolden
 */
public class OsVersion {

    public static final OsVersion EMPTY = new OsVersion(Optional.empty(), Optional.empty());

    private final Optional<Version> current;
    private final Optional<Version> wanted;

    public OsVersion(Optional<Version> current, Optional<Version> wanted) {
        this.current = current;
        this.wanted = requireNonZero(wanted);
    }

    /** The version this node is currently running, if any */
    public Optional<Version> current() {
        return current;
    }

    /** The version this node should upgrade to, if any */
    public Optional<Version> wanted() {
        return wanted;
    }

    /** Returns whether this node is currently changing its version to the given version */
    public boolean changingTo(Version version) {
        return changing() && wanted.get().equals(version);
    }

    /** Returns whether this node is currently changing its version */
    public boolean changing() {
        return wanted.isPresent() && !current.equals(wanted);
    }

    /** Returns whether this node is downgrading its version */
    public boolean downgrading() {
        return (wanted.isPresent() && current.isPresent()) && wanted.get().isBefore(current.get());
    }

    /** Returns whether this is before the given version */
    public boolean isBefore(Version version) {
        return current.isEmpty() || current.get().isBefore(version);
    }

    /** Returns whether current version matches given version */
    public boolean matches(Version version) {
        return current.isPresent() && current.get().equals(version);
    }

    /** Returns a copy of this with current version set to given version */
    public OsVersion withCurrent(Optional<Version> version) {
        return new OsVersion(version, wanted);
    }

    /** Returns a copy of this with wanted version set to given version */
    public OsVersion withWanted(Optional<Version> version) {
        return new OsVersion(current, version);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        OsVersion osVersion = (OsVersion) o;
        return current.equals(osVersion.current) &&
               wanted.equals(osVersion.wanted);
    }

    @Override
    public int hashCode() {
        return Objects.hash(current, wanted);
    }

    @Override
    public String toString() {
        return "OS version " + current.map(Version::toFullString).orElse("<unset>") + " [wanted: " +
               wanted.map(Version::toFullString).orElse("<unset>") + "]";
    }

    private static Optional<Version> requireNonZero(Optional<Version> version) {
        Objects.requireNonNull(version, "version must be non-null");
        if (version.isEmpty()) return version;
        if (version.get().isEmpty()) throw new IllegalArgumentException("version must be non-zero");
        return version;
    }

}