aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/container/component/chain/Chains.java
blob: 0f17cfb3933f00fca3b164534253b4f2d2fd96d5 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.container.component.chain;

import com.yahoo.component.chain.model.ChainsModel;
import com.yahoo.component.provider.ComponentRegistry;
import com.yahoo.container.core.ChainsConfig;
import com.yahoo.config.model.producer.AnyConfigProducer;
import com.yahoo.config.model.producer.TreeConfigProducer;
import com.yahoo.vespa.model.container.component.ComponentGroup;
import com.yahoo.vespa.model.container.component.ConfigProducerGroup;

import java.util.LinkedHashSet;
import java.util.Set;

/**
 * Root config producer the whole chains model(contains chains and components).
 *
 * @author Tony Vaagenes
 * @author gjoranv
 */
public class Chains<CHAIN extends Chain<?>>
        extends TreeConfigProducer<AnyConfigProducer>
        implements ChainsConfig.Producer {

    private final ComponentGroup<ChainedComponent<?>> componentGroup;
    private final ConfigProducerGroup<CHAIN> chainGroup;

    public Chains(TreeConfigProducer<? super Chains> parent, String subId) {
        super(parent, subId);
        componentGroup = new ComponentGroup<>(this, "component");
        chainGroup = new ConfigProducerGroup<>(this, "chain");
    }

    public void initializeComponents() {
        for (ChainedComponent component : allComponents()) {
            component.initialize();
        }
    }

    public void validate() throws Exception {
        ChainsModel chainsModel = new ChainsModel();

        for (CHAIN chain : allChains().allComponents()) {
            chainsModel.register(chain.getChainSpecification());
        }
        for (ChainedComponent<?> component : allComponents()) {
            chainsModel.register(component.getGlobalComponentId(), component.model);
        }
        chainsModel.validate();

        super.validate();
    }

    public Set<ChainedComponent<?>> allComponents() {
        Set<ChainedComponent<?>> result = new LinkedHashSet<>();
        result.addAll(componentGroup.getComponents());

        for (CHAIN chain : allChains().allComponents()) {
            result.addAll(chain.getInnerComponents());
        }
        return result;
    }

    public ComponentRegistry<ChainedComponent<?>> componentsRegistry() {
        ComponentRegistry<ChainedComponent<?>> result = new ComponentRegistry<>();

        for (ChainedComponent<?> component: componentGroup.getComponents())
            result.register(component.getGlobalComponentId(), component);

        for (CHAIN chain : allChains().allComponents()) {
            for (ChainedComponent<?> component: chain.getInnerComponents()) {
                result.register(component.getGlobalComponentId(), component);
            }
        }
        return result;
    }

    public ComponentRegistry<CHAIN> allChains() {
        ComponentRegistry<CHAIN> allChains = new ComponentRegistry<>();
        for (CHAIN chain : chainGroup.getComponents()) {
            allChains.register(chain.getId(), chain);
        }
        allChains.freeze();
        return allChains;
    }

    public void add(CHAIN chain) {
        chainGroup.addComponent(chain.getId(), chain);
    }

    public void add(ChainedComponent outerComponent) {
        componentGroup.addComponent(outerComponent);
    }

    @Override
    public void getConfig(ChainsConfig.Builder builder) {
        ChainsConfigGenerator.generate(builder, allChains().allComponents());
        ChainedComponentConfigGenerator.generate(builder, allComponents());
    }

    public ConfigProducerGroup<ChainedComponent<?>> getComponentGroup() {
        return componentGroup;
    }

    protected ConfigProducerGroup<CHAIN> getChainGroup() {
        return chainGroup;
    }

}