aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/config/model/producer/AbstractConfigProducerRoot.java
blob: 84a718d02edd33b25189b57fb166147cd0fc4e48 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.model.producer;

import com.yahoo.config.model.ConfigModelRepo;
import com.yahoo.vespa.model.ConfigProducer;
import com.yahoo.vespa.model.ConfigProducerRoot;
import com.yahoo.vespa.model.Service;
import com.yahoo.vespa.model.filedistribution.FileDistributionConfigProducer;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;

/**
 * The parent class of classes having the role as the root of a config producer tree.
 *
 * @author Tony Vaagenes
 */
public abstract class AbstractConfigProducerRoot extends TreeConfigProducer<AnyConfigProducer>
        implements ConfigProducerRoot {

    /** The ConfigProducers contained in this model indexed by config id */
    protected final Map<String, ConfigProducer> id2producer = new LinkedHashMap<>();

    public AbstractConfigProducerRoot(String rootConfigId) {
        super(rootConfigId);
    }

    public AbstractConfigProducerRoot getRoot() {
        return this;
    }

    public abstract FileDistributionConfigProducer getFileDistributionConfigProducer();

    /**
     * Freezes the parent - child connections of the model
     * and sets information derived from the topology.
     */
    public void freezeModelTopology() {
        freeze();
        setupConfigId("");
        aggregateDescendantServices();
    }

    public abstract ConfigModelRepo configModelRepo();

    /**
     * Returns the ConfigProducer with the given id if such configId exists.
     *
     * @param  configId The configId, e.g. "search.0/tld.0"
     * @return ConfigProducer with the given configId
     */
    public Optional<ConfigProducer> getConfigProducer(String configId) {
        return Optional.ofNullable(id2producer.get(configId));
    }

    /**
     * Returns the Service with the given id if such configId exists and it belongs to a Service ConfigProducer.
     *
     * @param  configId The configId, e.g. "search.0/tld.0"
     * @return Service with the given configId
     */
    public Optional<Service> getService(String configId) {
        return getConfigProducer(configId)
                .filter(Service.class::isInstance)
                .map(Service.class::cast);
    }
}