summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/chains/DomChainBuilderBase.java
blob: a6c9cca29e72bcc3fa03355c9b6d22d2bad5d798 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.builder.xml.dom.chains;

import com.yahoo.component.chain.model.ChainSpecification;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.config.model.producer.AbstractConfigProducer;
import com.yahoo.vespa.model.builder.xml.dom.VespaDomBuilder;
import com.yahoo.vespa.model.container.component.chain.Chain;
import com.yahoo.vespa.model.container.component.chain.ChainedComponent;
import org.w3c.dom.Element;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Base functionality for all chain builders (docprocChain, searchChain, provider, source)
 * @author Tony Vaagenes
 */
public abstract class DomChainBuilderBase<COMPONENT extends ChainedComponent<?>, CHAIN extends Chain<COMPONENT>>
        extends VespaDomBuilder.DomConfigProducerBuilder<CHAIN> {

    private final Collection<ComponentsBuilder.ComponentType<COMPONENT>> allowedComponentTypes;
    protected final Map<String, ComponentsBuilder.ComponentType<?>> outerComponentTypeByComponentName;

    public DomChainBuilderBase(Collection<ComponentsBuilder.ComponentType<COMPONENT>> allowedComponentTypes,
                               Map<String, ComponentsBuilder.ComponentType<?>> outerComponentTypeByComponentName) {
        this.allowedComponentTypes = allowedComponentTypes;
        this.outerComponentTypeByComponentName = outerComponentTypeByComponentName;
    }

    public final CHAIN doBuild(DeployState deployState, AbstractConfigProducer<?> ancestor, Element producerSpec) {
        ComponentsBuilder<COMPONENT> componentsBuilder =
                new ComponentsBuilder<>(deployState, ancestor, allowedComponentTypes, List.of(producerSpec), outerComponentTypeByComponentName);
        ChainSpecification specWithoutInnerComponents =
                new ChainSpecificationBuilder(producerSpec).build(componentsBuilder.getOuterComponentReferences());

        CHAIN chain = buildChain(deployState, ancestor, producerSpec, specWithoutInnerComponents);
        addInnerComponents(chain, componentsBuilder.getComponentDefinitions());

        return chain;
    }

    private void addInnerComponents(CHAIN chain, Collection<COMPONENT> componentDefinitions) {
        for (COMPONENT innerComponent : componentDefinitions) {
            chain.addInnerComponent(innerComponent);
        }
    }

    protected abstract CHAIN buildChain(DeployState deployState, AbstractConfigProducer<?> ancestor, Element producerSpec,
                                        ChainSpecification specWithoutInnerComponents);
}