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

/**
 * A node filter which matches a set of applications.
 *
 * @author bratseth
 */
public class ApplicationFilter {

    private ApplicationFilter() {}

    /** Creates a node filter which filters using the given host filter */
    private static Predicate<Node> makePredicate(Set<ApplicationId> applications) {
        Objects.requireNonNull(applications, "Applications set cannot be null, use an empty set");
        if (applications.isEmpty()) return node -> true;
        return node -> node.allocation().isPresent() && applications.contains(node.allocation().get().owner());
    }

    public static Predicate<Node> from(ApplicationId applicationId) {
        return makePredicate(Set.of(applicationId));
    }

    public static Predicate<Node> from(Set<ApplicationId> applicationIds) {
        return makePredicate(Set.copyOf(applicationIds));
    }

    public static Predicate<Node> from(String applicationIds) {
        return makePredicate(StringUtilities.split(applicationIds).stream()
                .map(ApplicationId::fromFullString)
                .collect(Collectors.toUnmodifiableSet()));
    }

}