aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/container/component/chain/ChainsConfigGenerator.java
blob: 542ea51f8297d939adaf1a2467b32e2f8d9f2cd9 (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
// Copyright Yahoo. 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.ComponentSpecification;
import com.yahoo.component.chain.Phase;
import com.yahoo.component.chain.model.ChainSpecification;
import com.yahoo.container.core.ChainsConfig;

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

import static com.yahoo.container.core.ChainsConfig.Chains.*;

/**
 * @author Tony Vaagenes
 * @author gjoranv
 *
 * Generates config for a all the chains.
 */
class ChainsConfigGenerator<T extends Chain> {

    public static <T extends Chain> void generate(ChainsConfig.Builder builder, Collection<T> chains) {
        for (T chain : chains) {
            builder.chains(getChain(chain));
        }
     }

    private static <T extends Chain> ChainsConfig.Chains.Builder getChain(T chain) {
        ChainSpecification specification = chain.getChainSpecification();

        return new ChainsConfig.Chains.Builder()
                .type(chain.getType())
                .id(specification.componentId.stringValue())
                .components(getComponents(specification.componentReferences))
                .inherits(getComponents(specification.inheritance.chainSpecifications))
                .excludes(getComponents(specification.inheritance.excludedComponents))
                .phases(getPhases(specification.phases()));
    }

    private static List<String> getComponents(Collection<ComponentSpecification> componentSpecs) {
        List<String> components = new ArrayList<>();
        for (ComponentSpecification spec : componentSpecs)
            components.add(spec.stringValue());
        return components;
    }

    private static List<Phases.Builder> getPhases(Collection<Phase> phases) {
        List<Phases.Builder> builders = new ArrayList<>();
        for (Phase phase : phases) {
            builders.add(
                    new Phases.Builder()
                            .id(phase.getName())
                            .before(phase.before())
                            .after(phase.after()));
        }
        return builders;
    }

}