aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/chains/search/DomFederationSearcherBuilder.java
blob: 6454178094270aa47801615b3038b92e4df40fef (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
// 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.search;

import com.yahoo.component.ComponentId;
import com.yahoo.component.ComponentSpecification;
import com.yahoo.config.model.builder.xml.XmlHelper;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.search.searchchain.model.federation.FederationOptions;
import com.yahoo.search.searchchain.model.federation.FederationSearcherModel;
import com.yahoo.text.XML;
import com.yahoo.config.model.producer.AnyConfigProducer;
import com.yahoo.config.model.producer.TreeConfigProducer;
import com.yahoo.vespa.model.builder.xml.dom.DomComponentBuilder;
import com.yahoo.vespa.model.builder.xml.dom.VespaDomBuilder;
import com.yahoo.vespa.model.builder.xml.dom.chains.GenericChainedComponentModelBuilder;
import com.yahoo.vespa.model.container.component.Component;
import com.yahoo.vespa.model.container.search.searchchain.FederationSearcher;
import com.yahoo.vespa.model.container.search.searchchain.Searcher;
import org.w3c.dom.Element;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * Builds a federation searcher config producer from an element.
 *
 * @author Tony Vaagenes
 */
public class DomFederationSearcherBuilder extends VespaDomBuilder.DomConfigProducerBuilderBase<Searcher<?>> {

    static class FederationSearcherModelBuilder extends GenericChainedComponentModelBuilder {

        private final List<FederationSearcherModel.TargetSpec> sources;
        private final boolean inheritDefaultSources;

        FederationSearcherModelBuilder(Element searcherSpec) {
            super(searcherSpec);
            sources = readSources(searcherSpec);
            inheritDefaultSources = readSourceSet(searcherSpec);
        }

        private boolean readSourceSet(Element searcherSpec) {
            return XML.getChild(searcherSpec, "source-set") != null;
        }

        private List<FederationSearcherModel.TargetSpec> readSources(Element searcherSpec) {
            List<FederationSearcherModel.TargetSpec> sources = new ArrayList<>();
            for (Element source : XML.getChildren(searcherSpec, "source")) {
                sources.add(readSource(source));
            }
            return sources;
        }

        private FederationSearcherModel.TargetSpec readSource(Element source) {
            ComponentSpecification componentSpecification = XmlHelper.getIdRef(source);

            FederationOptions federationOptions =
                    readFederationOptions(XML.getChild(source,  FederationOptionsBuilder.federationOptionsElement));

            return new FederationSearcherModel.TargetSpec(componentSpecification, federationOptions);
        }

        private FederationOptions readFederationOptions(Element federationOptionsElement) {
            if (federationOptionsElement == null) {
                return new FederationOptions();
            } else {
                return new FederationOptionsBuilder(federationOptionsElement).build();
            }
        }

        protected FederationSearcherModel build() {
            return new FederationSearcherModel(componentId, dependencies, sources, inheritDefaultSources);
        }

    }

    @Override
    protected FederationSearcher doBuild(DeployState deployState, TreeConfigProducer<AnyConfigProducer> ancestor, Element searcherElement) {
        FederationSearcherModel model = new FederationSearcherModelBuilder(searcherElement).build();
        Optional<Component> targetSelector = buildTargetSelector(deployState, ancestor, searcherElement, model.getComponentId());

        return new FederationSearcher(model, targetSelector);
    }

    private Optional<Component> buildTargetSelector(DeployState deployState, TreeConfigProducer<AnyConfigProducer> ancestor, Element searcherElement, ComponentId namespace) {
        Element targetSelectorElement = XML.getChild(searcherElement, "target-selector");
        if (targetSelectorElement == null)
            return Optional.empty();

        return Optional.of(new DomComponentBuilder(namespace).build(deployState, ancestor, targetSelectorElement));
    }

}