aboutsummaryrefslogtreecommitdiffstats
path: root/flags/src/main/java/com/yahoo/vespa/flags/custom/SharedHost.java
blob: 66356d979a4837ab080867cfd3b5d85b60bae92a (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
// 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 com.yahoo.vespa.flags.PermanentFlags;

import java.util.List;
import java.util.Objects;

/**
 * Defines properties related to shared hosts, see {@link PermanentFlags#SHARED_HOST}.
 *
 * @author hakon
 */
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class SharedHost {

    private final List<HostResources> resources;

    public static SharedHost createDisabled() {
        return new SharedHost(null);
    }

    /**
     * @param resourcesOrNull the resources of the shared host (or several to support e.g. tiers or
     *                        fast/slow disks separately)
     */
    @JsonCreator
    public SharedHost(@JsonProperty("resources") List<HostResources> resourcesOrNull) {
        this.resources = resourcesOrNull == null ? List.of() : List.copyOf(resourcesOrNull);
    }

    @JsonGetter("resources")
    public List<HostResources> getResourcesOrNull() {
        return resources.isEmpty() ? null : resources;
    }

    /** Whether there are any shared hosts specifically for the given cluster type, or without a cluster type restriction. */
    @JsonIgnore
    public boolean supportsClusterType(String clusterType) {
        return resources.stream().anyMatch(resource -> resource.clusterType().map(clusterType::equalsIgnoreCase).orElse(true));
    }

    /** Whether there are any shared hosts specifically for the given cluster type. */
    @JsonIgnore
    public boolean hasClusterType(String clusterType) {
        return resources.stream().anyMatch(resource -> resource.clusterType().map(clusterType::equalsIgnoreCase).orElse(false));
    }

    @JsonIgnore
    public List<HostResources> getHostResources() {
        return resources;
    }

    @Override
    public String toString() {
        return resources.toString();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SharedHost that = (SharedHost) o;
        return resources.equals(that.resources);
    }

    @Override
    public int hashCode() {
        return Objects.hash(resources);
    }

}