aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/modelfactory/ModelFactoryRegistry.java
blob: b595cf0d7eaa8e24ced73e31bc9cab1ab051a5c3 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.modelfactory;

import com.yahoo.component.annotation.Inject;
import com.yahoo.component.provider.ComponentRegistry;
import com.yahoo.config.model.api.ModelFactory;
import com.yahoo.component.Version;
import com.yahoo.vespa.config.server.http.UnknownVespaVersionException;

import java.util.*;

/**
 * A registry of model factories. Allows querying for a specific version of a {@link ModelFactory} or
 * simply returning all of them. Keeps track of the latest {@link Version} supported.
 *
 * @author Ulf Lilleengen
 */
public class ModelFactoryRegistry {

    private final Map<Version, ModelFactory> factories = new HashMap<>();

    @Inject
    public ModelFactoryRegistry(ComponentRegistry<ModelFactory> factories) {
        this(factories.allComponents());
    }

    public ModelFactoryRegistry(List<ModelFactory> modelFactories) {
        if (modelFactories.isEmpty()) {
            throw new IllegalArgumentException("No ModelFactory instances registered, cannot build config models");
        }
        for (ModelFactory factory : modelFactories) {
            factories.put(factory.version(), factory);
        }
    }

    public Set<Version> allVersions() { return factories.keySet(); }

    /**
     * Returns the factory for the given version
     *
     * @throws UnknownVespaVersionException if there is no factory for this version
     */
    public ModelFactory getFactory(Version version) {
        if ( ! factories.containsKey(version))
            throw new UnknownVespaVersionException("Unknown Vespa version '" + version +
                                                   "', cannot build config model for this version, known versions: " + allVersions());
        return factories.get(version);
    }

    /**
     * Return all factories that can build a model.
     *
     * @return An immutable collection of {@link com.yahoo.config.model.api.ModelFactory} instances.
     */
    public Collection<ModelFactory> getFactories() {
        return Collections.unmodifiableCollection(factories.values());
    }

}