aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/test/ApiConfigModel.java
blob: c170abc04ae16ece566f8441d306922c3422ae83 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.test;

import com.yahoo.config.model.ConfigModel;
import com.yahoo.config.model.ConfigModelContext;
import com.yahoo.config.model.ConfigModelRepo;
import com.yahoo.config.model.builder.xml.ConfigModelId;
import com.yahoo.vespa.model.builder.xml.dom.LegacyConfigModelBuilder;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * This is a plugin for testing the plugin API exchange mechanism in
 * the vespamodel. It uses the API of another plugin.
 *
 * @author  gjoranv
 */
public class ApiConfigModel extends ConfigModel {

    private List<ApiService> apiServices = new ArrayList<>();

    public ApiConfigModel(ConfigModelContext modelContext) {
        super(modelContext);
    }

    // Inherit doc from ConfigModel.
    public void prepare(ConfigModelRepo configModelRepo) {
        int numSimpleServices = 0;
        ConfigModel simplePlugin = configModelRepo.get("simple");

        if ((simplePlugin != null) && (simplePlugin instanceof TestApi)) {
            TestApi testApi = (TestApi) simplePlugin;
            numSimpleServices = testApi.getNumSimpleServices();
        }
        for (Object apiService : apiServices) {
            ApiService as = (ApiService) apiService;
            as.setNumSimpleServices(numSimpleServices);
        }
    }

    public static class Builder extends LegacyConfigModelBuilder<ApiConfigModel> {

        public Builder() {
            super(ApiConfigModel.class);
        }

        @Override
        public List<ConfigModelId> handlesElements() {
            return Arrays.asList(ConfigModelId.fromName("api"));
        }

        @Override
        public void doBuild(ApiConfigModel configModel, Element spec, ConfigModelContext context) {
            NodeList pl = spec.getElementsByTagName("apiservice");
            if (pl.getLength() > 0) {
                for (int i=0; i < pl.getLength(); i++) {
                    configModel.apiServices.add(new DomTestServiceBuilder.ApiServiceBuilder(i).build(context.getDeployState(), context.getParentProducer(), (Element) pl.item(i)));
                }
            }
        }

    }

}