aboutsummaryrefslogtreecommitdiffstats
path: root/config-provisioning/src/main/java/com/yahoo/config/provision/NodeFlavors.java
blob: c06db3199eceb6c50beebb52fdfbcc744c75880e (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.provision;

import com.yahoo.component.annotation.Inject;
import com.yahoo.config.provisioning.FlavorsConfig;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * All the flavors configured in this zone (i.e this should be called HostFlavors).
 *
 * @author bratseth
 */
public class NodeFlavors {

    /** Flavors which are configured in this zone */
    private final Map<String, Flavor> configuredFlavors;

    @Inject
    public NodeFlavors(FlavorsConfig config) {
        this(toFlavors(config));
    }

    public NodeFlavors(Collection<Flavor> flavors) {
        Map<String, Flavor> map = new LinkedHashMap<>();
        for (Flavor flavor : flavors)
            map.put(flavor.name(), flavor);
        configuredFlavors = Collections.unmodifiableMap(map);
    }

    public List<Flavor> getFlavors() {
        return new ArrayList<>(configuredFlavors.values());
    }

    /** Returns a flavor by name, or empty if there is no flavor with this name and it cannot be created on the fly. */
    public Optional<Flavor> getFlavor(String name) {
        if (configuredFlavors.containsKey(name))
            return Optional.of(configuredFlavors.get(name));

        NodeResources nodeResources = NodeResources.fromLegacyName(name);
        return Optional.of(new Flavor(nodeResources));
    }

    /**
     * Returns the flavor with the given name or throws an IllegalArgumentException if it does not exist
     * and cannot be created on the fly.
     */
    public Flavor getFlavorOrThrow(String flavorName) {
        return getFlavor(flavorName).orElseThrow(() -> new IllegalArgumentException("Unknown flavor '" + flavorName + "'"));
    }

    /** Returns true if this flavor is configured or can be created on the fly */
    public boolean exists(String flavorName) {
        return getFlavor(flavorName).isPresent();
    }

    private static Collection<Flavor> toFlavors(FlavorsConfig config) {
        return config.flavor().stream().map(Flavor::new).toList();
    }

    @Override
    public String toString() {
        return String.join(",", configuredFlavors.keySet());
    }

}