aboutsummaryrefslogtreecommitdiffstats
path: root/flags/src/main/java/com/yahoo/vespa/flags/custom/ClusterCapacity.java
blob: 8ac83f2b2aa9153d14f404bb2e6813b2a3b637ad (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.flags.custom;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Objects;
import java.util.OptionalDouble;

import static com.yahoo.vespa.flags.custom.Validation.requireNonNegative;
import static com.yahoo.vespa.flags.custom.Validation.validArchitectures;
import static com.yahoo.vespa.flags.custom.Validation.validClusterTypes;
import static com.yahoo.vespa.flags.custom.Validation.validDiskSpeeds;
import static com.yahoo.vespa.flags.custom.Validation.validStorageTypes;
import static com.yahoo.vespa.flags.custom.Validation.validateEnum;

/**
 * @author freva
 */
// @Immutable
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class ClusterCapacity {
    private final int count;
    private final OptionalDouble vcpu;
    private final OptionalDouble memoryGb;
    private final OptionalDouble diskGb;
    private final OptionalDouble bandwidthGbps;
    private final String diskSpeed;
    private final String storageType;
    private final String architecture;
    private final String clusterType;

    @JsonCreator
    public ClusterCapacity(@JsonProperty("count") int count,
                           @JsonProperty("vcpu") Double vcpu,
                           @JsonProperty("memoryGb") Double memoryGb,
                           @JsonProperty("diskGb") Double diskGb,
                           @JsonProperty("bandwidthGbps") Double bandwidthGbps,
                           @JsonProperty("diskSpeed") String diskSpeed,
                           @JsonProperty("storageType") String storageType,
                           @JsonProperty("architecture") String architecture,
                           @JsonProperty("clusterType") String clusterType) {
        this.count = (int) requireNonNegative("count", count);
        this.vcpu = vcpu == null ? OptionalDouble.empty() : OptionalDouble.of(requireNonNegative("vcpu", vcpu));
        this.memoryGb = memoryGb == null ? OptionalDouble.empty() : OptionalDouble.of(requireNonNegative("memoryGb", memoryGb));
        this.diskGb = diskGb == null ? OptionalDouble.empty() : OptionalDouble.of(requireNonNegative("diskGb", diskGb));
        this.bandwidthGbps = bandwidthGbps == null ? OptionalDouble.empty() : OptionalDouble.of(bandwidthGbps);
        this.diskSpeed = validateEnum("diskSpeed", validDiskSpeeds, diskSpeed == null ? "fast" : diskSpeed);
        this.storageType = validateEnum("storageType", validStorageTypes, storageType == null ? "any" : storageType);
        this.architecture = validateEnum("architecture", validArchitectures, architecture == null ? "x86_64" : architecture);
        this.clusterType = clusterType == null ? null : validateEnum("clusterType", validClusterTypes, clusterType);
    }

    /** Returns a new ClusterCapacity equal to {@code this}, but with the given count. */
    public ClusterCapacity withCount(int count) {
        return new ClusterCapacity(count, vcpuOrNull(), memoryGbOrNull(), diskGbOrNull(), bandwidthGbpsOrNull(),
                                   diskSpeed, storageType, architecture, clusterType);
    }

    @JsonGetter("count") public int count() { return count; }
    @JsonGetter("vcpu") public Double vcpuOrNull() {
        return vcpu.isPresent() ? vcpu.getAsDouble() : null;
    }
    @JsonGetter("memoryGb") public Double memoryGbOrNull() {
        return memoryGb.isPresent() ? memoryGb.getAsDouble() : null;
    }
    @JsonGetter("diskGb") public Double diskGbOrNull() {
        return diskGb.isPresent() ? diskGb.getAsDouble() : null;
    }
    @JsonGetter("bandwidthGbps") public Double bandwidthGbpsOrNull() {
        return bandwidthGbps.isPresent() ? bandwidthGbps.getAsDouble() : null;
    }
    @JsonGetter("diskSpeed") public String diskSpeed() { return diskSpeed; }
    @JsonGetter("storageType") public String storageType() { return storageType; }
    @JsonGetter("architecture") public String architecture() { return architecture; }
    @JsonGetter("clusterType") public String clusterType() { return clusterType; }

    @JsonIgnore public Double vcpu() { return vcpu.orElse(0.0); }
    @JsonIgnore public Double memoryGb() { return memoryGb.orElse(0.0); }
    @JsonIgnore public Double diskGb() { return diskGb.orElse(0.0); }
    @JsonIgnore public double bandwidthGbps() { return bandwidthGbps.orElse(1.0); }

    @Override
    public String toString() {
        return "ClusterCapacity{" +
                "count=" + count +
                ", vcpu=" + vcpu +
                ", memoryGb=" + memoryGb +
                ", diskGb=" + diskGb +
                ", bandwidthGbps=" + bandwidthGbps +
                ", diskSpeed=" + diskSpeed +
                ", storageType=" + storageType +
                ", architecture=" + architecture +
                ", clusterType=" + clusterType +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ClusterCapacity that = (ClusterCapacity) o;
        return count == that.count &&
                vcpu.equals(that.vcpu) &&
                memoryGb.equals(that.memoryGb) &&
                diskGb.equals(that.diskGb) &&
                bandwidthGbps.equals(that.bandwidthGbps) &&
                diskSpeed.equals(that.diskSpeed) &&
                storageType.equals(that.storageType) &&
                architecture.equals(that.architecture) &&
                clusterType.equals(that.clusterType);
    }

    @Override
    public int hashCode() {
        return Objects.hash(count, vcpu, memoryGb, diskGb, bandwidthGbps, diskSpeed, storageType, architecture, clusterType);
    }

}