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

import com.google.common.collect.ImmutableMap;
import com.google.inject.Inject;
import com.yahoo.config.provisioning.FlavorsConfig;

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

/**
 * All the available node flavors.
 *
 * @author bratseth
 */
public class NodeFlavors {

    /** Flavors <b>which are configured</b> in this zone */
    private final ImmutableMap<String, Flavor> flavors;

    @Inject
    public NodeFlavors(FlavorsConfig config) {
        ImmutableMap.Builder<String, Flavor> b = new ImmutableMap.Builder<>();
        for (Flavor flavor : toFlavors(config))
            b.put(flavor.name(), flavor);
        this.flavors = b.build();
    }

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

    /** Returns a flavor by name, or empty if there is no flavor with this name. */
    public Optional<Flavor> getFlavor(String name) {
        return Optional.ofNullable(flavors.get(name));
    }

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

    private List<String> canonicalFlavorNames() {
        return flavors.values().stream().map(Flavor::canonicalName).distinct().sorted().collect(Collectors.toList());
    }

    private static Collection<Flavor> toFlavors(FlavorsConfig config) {
        Map<String, Flavor> flavors = new HashMap<>();
        // First pass, create all flavors, but do not include flavorReplacesConfig.
        for (FlavorsConfig.Flavor flavorConfig : config.flavor()) {
            flavors.put(flavorConfig.name(), new Flavor(flavorConfig));
        }
        // Second pass, set flavorReplacesConfig to point to correct flavor.
        for (FlavorsConfig.Flavor flavorConfig : config.flavor()) {
            Flavor flavor = flavors.get(flavorConfig.name());
            for (FlavorsConfig.Flavor.Replaces flavorReplacesConfig : flavorConfig.replaces()) {
                if (! flavors.containsKey(flavorReplacesConfig.name())) {
                    throw new IllegalStateException("Replaces for " + flavor.name() + 
                                                    " pointing to a non existing flavor: " + flavorReplacesConfig.name());
                }
                flavor.replaces().add(flavors.get(flavorReplacesConfig.name()));
            }
            flavor.freeze();
        }
        // Third pass, ensure that retired flavors have a replacement
        for (Flavor flavor : flavors.values()) {
            if (flavor.isRetired() && !hasReplacement(flavors.values(), flavor)) {
                throw new IllegalStateException(
                        String.format("Flavor '%s' is retired, but has no replacement", flavor.name())
                );
            }
        }
        return flavors.values();
    }

    private static boolean hasReplacement(Collection<Flavor> flavors, Flavor flavor) {
        return flavors.stream()
                .filter(f -> !f.equals(flavor))
                .anyMatch(f -> f.satisfies(flavor));
    }

}