summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/chains/DomChainsBuilder.java
blob: a5e09d206bf8223a1e57b03207685618b603ae06 (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
// 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.config.application.Xml;
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.builder.xml.dom.chains.ComponentsBuilder.ComponentType;
import com.yahoo.vespa.model.container.component.chain.Chain;
import com.yahoo.vespa.model.container.component.chain.ChainedComponent;
import com.yahoo.vespa.model.container.component.chain.Chains;
import org.w3c.dom.Element;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * NOTE: This class _must_ be abstract, due to calling subclass method in ctor.
 * @author Tony Vaagenes
 * @author gjoranv
 */
public abstract
class DomChainsBuilder<COMPONENT extends ChainedComponent<?>, CHAIN extends Chain<COMPONENT>, CHAINS extends Chains<CHAIN>>
        extends VespaDomBuilder.DomConfigProducerBuilder<CHAINS> {

    private final Collection<ComponentType<COMPONENT>> allowedComponentTypes;
    private final String appPkgChainsDir;
    private final Element outerChainsElem;

    protected DomChainsBuilder(Element outerChainsElem,
                               Collection<ComponentType<COMPONENT>> allowedComponentTypes,
                               String appPkgChainsDir) {

        this.outerChainsElem = outerChainsElem;
        this.allowedComponentTypes = new ArrayList<>(allowedComponentTypes);
        this.appPkgChainsDir = appPkgChainsDir;
    }

    protected abstract CHAINS newChainsInstance(AbstractConfigProducer parent);

    @Override
    protected final CHAINS doBuild(DeployState deployState, AbstractConfigProducer parent, Element chainsElement) {
        CHAINS chains = newChainsInstance(parent);

        List<Element> allChainElements = allChainElements(deployState, parent, chainsElement);
        if (! allChainElements.isEmpty()) {
            ComponentsBuilder<COMPONENT> outerComponentsBuilder = readOuterComponents(deployState, chains, allChainElements);
            ChainsBuilder<COMPONENT, CHAIN> chainsBuilder = readChains(deployState, chains, allChainElements,
                                                                       outerComponentsBuilder.getComponentTypeByComponentName());

            addOuterComponents(chains, outerComponentsBuilder);
            addChains(chains, chainsBuilder);
        }
        return chains;
    }

    private List<Element> allChainElements(DeployState deployState, AbstractConfigProducer ancestor, Element chainsElement) {
        List<Element> chainsElements = new ArrayList<>();
        if (outerChainsElem != null)
            chainsElements.add(outerChainsElem);
        chainsElements.add(chainsElement);

        if (appPkgChainsDir != null)
            chainsElements.addAll(Xml.allElemsFromPath(deployState.getApplicationPackage(), appPkgChainsDir));

        return chainsElements;
    }

    private ComponentsBuilder<COMPONENT> readOuterComponents(DeployState deployState, AbstractConfigProducer ancestor, List<Element> chainsElems) {
        return new ComponentsBuilder<>(deployState, ancestor, allowedComponentTypes, chainsElems, null);
    }

    protected abstract
    ChainsBuilder<COMPONENT, CHAIN> readChains(DeployState deployState, AbstractConfigProducer ancestor, List<Element> allChainsElems,
                                               Map<String, ComponentsBuilder.ComponentType> outerComponentTypeByComponentName);

    private void addOuterComponents(CHAINS chains, ComponentsBuilder<COMPONENT> outerComponentsBuilder) {
        assert (outerComponentsBuilder.getOuterComponentReferences().isEmpty());

        for (ChainedComponent outerComponent : outerComponentsBuilder.getComponentDefinitions()) {
            chains.add(outerComponent);
        }
    }

    private void addChains(CHAINS chains, ChainsBuilder<COMPONENT, CHAIN> chainsBuilder) {
        for (CHAIN chain : chainsBuilder.getChains()) {
            chains.add(chain);
        }
    }
}