aboutsummaryrefslogtreecommitdiffstats
path: root/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/configserver/NodeFilter.java
blob: 65c8e8390c836e72e7fde1d2dc67a30628c60a92 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.integration.configserver;

import com.google.common.collect.ImmutableSet;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.config.provision.HostName;

import java.util.Objects;
import java.util.Set;

/**
 * A filter for listing nodes.
 *
 * This is immutable.
 *
 * @author mpolden
 */
public class NodeFilter {

    private final boolean includeDeprovisioned;
    private final Set<Node.State> states;
    private final Set<HostName> hostnames;
    private final Set<HostName> parentHostnames;
    private final Set<ApplicationId> applications;
    private final Set<ClusterSpec.Id> clusterIds;
    private final Set<Node.ClusterType> clusterTypes;

    private NodeFilter(boolean includeDeprovisioned, Set<Node.State> states, Set<HostName> hostnames,
                       Set<HostName> parentHostnames, Set<ApplicationId> applications,
                       Set<ClusterSpec.Id> clusterIds, Set<Node.ClusterType> clusterTypes) {
        this.includeDeprovisioned = includeDeprovisioned;
        // Uses Guava Set to preserve insertion order
        this.states = ImmutableSet.copyOf(Objects.requireNonNull(states));
        this.hostnames = ImmutableSet.copyOf(Objects.requireNonNull(hostnames));
        this.parentHostnames = ImmutableSet.copyOf(Objects.requireNonNull(parentHostnames));
        this.applications = ImmutableSet.copyOf(Objects.requireNonNull(applications));
        this.clusterIds = ImmutableSet.copyOf(Objects.requireNonNull(clusterIds));
        this.clusterTypes = ImmutableSet.copyOf(Objects.requireNonNull(clusterTypes));
        if (!includeDeprovisioned && states.contains(Node.State.deprovisioned)) {
            throw new IllegalArgumentException("Must include deprovisioned nodes when matching deprovisioned state");
        }
    }

    public boolean includeDeprovisioned() {
        return includeDeprovisioned;
    }

    public Set<Node.State> states() {
        return states;
    }

    public Set<HostName> hostnames() {
        return hostnames;
    }

    public Set<HostName> parentHostnames() {
        return parentHostnames;
    }

    public Set<ApplicationId> applications() {
        return applications;
    }

    public Set<ClusterSpec.Id> clusterIds() {
        return clusterIds;
    }

    public Set<Node.ClusterType> clusterTypes() {
        return clusterTypes;
    }

    public NodeFilter includeDeprovisioned(boolean includeDeprovisioned) {
        return new NodeFilter(includeDeprovisioned, states, hostnames, parentHostnames, applications, clusterIds, clusterTypes);
    }

    public NodeFilter states(Node.State... states) {
        return states(ImmutableSet.copyOf(states));
    }

    public NodeFilter states(Set<Node.State> states) {
        return new NodeFilter(includeDeprovisioned, states, hostnames, parentHostnames, applications, clusterIds, clusterTypes);
    }

    public NodeFilter hostnames(HostName... hostnames) {
        return hostnames(ImmutableSet.copyOf(hostnames));
    }

    public NodeFilter hostnames(Set<HostName> hostnames) {
        return new NodeFilter(includeDeprovisioned, states, hostnames, parentHostnames, applications, clusterIds, clusterTypes);
    }

    public NodeFilter parentHostnames(HostName... parentHostnames) {
        return parentHostnames(ImmutableSet.copyOf(parentHostnames));
    }

    public NodeFilter parentHostnames(Set<HostName> parentHostnames) {
        return new NodeFilter(includeDeprovisioned, states, hostnames, parentHostnames, applications, clusterIds, clusterTypes);
    }

    public NodeFilter applications(ApplicationId... applications) {
        return applications(ImmutableSet.copyOf(applications));
    }

    public NodeFilter applications(Set<ApplicationId> applications) {
        return new NodeFilter(includeDeprovisioned, states, hostnames, parentHostnames, applications, clusterIds, clusterTypes);
    }

    public NodeFilter clusterIds(ClusterSpec.Id... clusterIds) {
        return clusterIds(ImmutableSet.copyOf(clusterIds));
    }

    public NodeFilter clusterIds(Set<ClusterSpec.Id> clusterIds) {
        return new NodeFilter(includeDeprovisioned, states, hostnames, parentHostnames, applications, clusterIds, clusterTypes);
    }

    public NodeFilter clusterTypes(Node.ClusterType... clusterTypes) {
        return clusterTypes(ImmutableSet.copyOf(clusterTypes));
    }

    public NodeFilter clusterTypes(Set<Node.ClusterType> clusterTypes) {
        return new NodeFilter(includeDeprovisioned, states, hostnames, parentHostnames, applications, clusterIds, clusterTypes);
    }

    /** A filter which matches all nodes, except deprovisioned ones */
    public static NodeFilter all() {
        return new NodeFilter(false, Set.of(), Set.of(), Set.of(), Set.of(), Set.of(), Set.of());
    }

}