// 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 states; private final Set hostnames; private final Set parentHostnames; private final Set applications; private final Set clusterIds; private final Set clusterTypes; private NodeFilter(boolean includeDeprovisioned, Set states, Set hostnames, Set parentHostnames, Set applications, Set clusterIds, Set 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 states() { return states; } public Set hostnames() { return hostnames; } public Set parentHostnames() { return parentHostnames; } public Set applications() { return applications; } public Set clusterIds() { return clusterIds; } public Set 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 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 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 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 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 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 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()); } }