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

import com.yahoo.text.StringUtilities;
import com.yahoo.vespa.hosted.provision.Node;

import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * Filter based on the parent host value (for virtualized nodes).
 * 
 * @author dybis
 */
public class ParentHostFilter {

    private ParentHostFilter() {}

    /** Creates a node filter which filters using the given parent host name */
    private static Predicate<Node> makePredicate(Set<String> parentHostNames) {
        Objects.requireNonNull(parentHostNames, "parentHostNames cannot be null");
        if (parentHostNames.isEmpty()) return node -> true;
        return node -> node.parentHostname().isPresent() && parentHostNames.contains(node.parentHostname().get());
    }

    /** Returns a copy of the given filter which only matches for the given parent */
    public static Predicate<Node> from(String parentNames) {
        return makePredicate(StringUtilities.split(parentNames).stream().collect(Collectors.toUnmodifiableSet()));
    }
}