aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/container/search/searchchain/defaultsearchchains/LocalClustersCreator.java
blob: 54e1ec63549c2ab5dade64dcd00ecf65b15d1872 (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 Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.container.search.searchchain.defaultsearchchains;

import com.yahoo.component.ComponentId;
import com.yahoo.component.chain.model.ChainSpecification;
import com.yahoo.search.searchchain.model.federation.FederationOptions;
import com.yahoo.search.searchchain.model.federation.LocalProviderSpec;
import com.yahoo.vespa.model.container.search.searchchain.LocalProvider;
import com.yahoo.vespa.model.container.search.searchchain.SearchChains;

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

/**
 * Adds default search chains for all local clusters not mentioned explicitly
 * @author Tony Vaagenes
 */
public class LocalClustersCreator {

    private static ChainSpecification emptySearchChainSpecification(String componentName) {
        return new ChainSpecification(new ComponentId(componentName),
                                      VespaSearchChainsCreator.inheritsVespaPhases(), //TODO: refactor
                                      List.of(),
                                      Set.of());
    }

    private static LocalProvider createDefaultLocalProvider(String clusterName) {
        return new LocalProvider(emptySearchChainSpecification(clusterName),
                                 new FederationOptions(),
                                 new LocalProviderSpec(clusterName));
    }

    private static Set<String> presentClusters(SearchChains searchChains) {
        Set<String> presentClusters = new LinkedHashSet<>();
        for (LocalProvider provider : searchChains.localProviders()) {
            presentClusters.add(provider.getClusterName());
        }
        return presentClusters;
    }

    public static void addDefaultLocalProviders(SearchChains searchChains, Set<String> clusterNames) {
        Set<String> missingClusters = new LinkedHashSet<>(clusterNames);
        missingClusters.removeAll(presentClusters(searchChains));

        for (String clusterName : missingClusters) {
            searchChains.add(createDefaultLocalProvider(clusterName));
        }
    }

}