aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/test/ModelAmendingTestCase.java
blob: 998e19794fc34017d1583a0ac948c940c652680e (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// 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.component.ComponentId;
import com.yahoo.config.model.ConfigModel;
import com.yahoo.config.model.ConfigModelContext;
import com.yahoo.config.model.ConfigModelRegistry;
import com.yahoo.config.model.MapConfigModelRegistry;
import com.yahoo.config.model.admin.AdminModel;
import com.yahoo.config.model.builder.xml.ConfigModelBuilder;
import com.yahoo.config.model.builder.xml.ConfigModelId;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.config.model.producer.TreeConfigProducer;
import com.yahoo.vespa.defaults.Defaults;
import com.yahoo.vespa.model.AbstractService;
import com.yahoo.vespa.model.HostResource;
import com.yahoo.vespa.model.PortAllocBridge;
import com.yahoo.vespa.model.VespaModel;
import com.yahoo.vespa.model.container.ContainerCluster;
import com.yahoo.vespa.model.container.ContainerModel;
import com.yahoo.vespa.model.container.xml.ContainerModelBuilder;
import com.yahoo.vespa.model.content.Content;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Element;

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

import static org.junit.jupiter.api.Assertions.*;

/**
 * Demonstrates how a model can be added at build time to amend another model.
 * This is useful is situations where the core Vespa config models needs to be
 * modified by third party code which follows the installed environment rather than
 * the application.
 *
 * @author bratseth
 */
public class ModelAmendingTestCase {

    @Test
    void testModelAmending() {
        ConfigModelRegistry amendingModelRepo = MapConfigModelRegistry.createFromList(new AdminModelAmenderBuilder(),
                new ContainerModelAmenderBuilder(),
                new ContentModelAmenderBuilder());
        String services =
                "<services version='1.0'>" +
                        "    <admin version='4.0'/>" +
                        "    <container id='test1' version='1.0'>" +
                        "        <search/>" +
                        "        <nodes count='2'/>" +
                        "    </container>" +
                        "    <container id='test2' version='1.0'>" +
                        "        <http><server id='server1' port='" + Defaults.getDefaults().vespaWebServicePort() + "'/></http>" +
                        "        <document-api/>" +
                        "        <nodes count='2'/>" +
                        "    </container>" +
                        "    <content id='test3' version='1.0'>" +
                        "        <redundancy>1</redundancy>" +
                        "        <documents>" +
                        "            <document mode='index' type='type1'/>" +
                        "        </documents>" +
                        "        <nodes count='2'/>" +
                        "    </content>" +
                        "    <content id='test4' version='1.0'>" +
                        "        <redundancy>1</redundancy>" +
                        "        <documents>" +
                        "            <document mode='index' type='type1'/>" +
                        "        </documents>" +
                        "        <nodes count='3'/>" +
                        "    </content>" +
                        "</services>";
        VespaModelTester tester = new VespaModelTester(amendingModelRepo);
        tester.addHosts(12);
        VespaModel model = tester.createModel(services);

        // Check that all hosts are amended
        for (HostResource host : model.getAdmin().hostSystem().getHosts()) {
            assertFalse(host.getHost().getChildrenByTypeRecursive(AmendedService.class).isEmpty(), host + " is amended");
        }

        // Check that container clusters are amended
        assertEquals(2, model.getContainerClusters().size());
        assertNotNull(model.getContainerClusters().get("test1").getComponentsMap().get(new ComponentId("com.yahoo.MyAmendedComponent")));
        assertNotNull(model.getContainerClusters().get("test2").getComponentsMap().get(new ComponentId("com.yahoo.MyAmendedComponent")));
    }

    @Test
    void testModelAmendingWithDedicatedCC() {
        ConfigModelRegistry amendingModelRepo = MapConfigModelRegistry.createFromList(new AdminModelAmenderBuilder(),
                new ContainerModelAmenderBuilder(),
                new ContentModelAmenderBuilder());
        String services =
                "<services version='1.0'>" +
                        "    <admin version='4.0'/>" +
                        "    <container id='test1' version='1.0'>" +
                        "        <search/>" +
                        "        <nodes count='2'/>" +
                        "    </container>" +
                        "    <container id='test2' version='1.0'>" +
                        "        <http><server id='server1' port='" + Defaults.getDefaults().vespaWebServicePort() + "'/></http>" +
                        "        <document-api/>" +
                        "        <nodes count='2'/>" +
                        "    </container>" +
                        "    <content id='test3' version='1.0'>" +
                        "        <redundancy>1</redundancy>" +
                        "        <documents>" +
                        "            <document mode='index' type='type1'/>" +
                        "        </documents>" +
                        "        <nodes count='2'/>" +
                        "    </content>" +
                        "    <content id='test4' version='1.0'>" +
                        "        <redundancy>1</redundancy>" +
                        "        <documents>" +
                        "            <document mode='index' type='type1'/>" +
                        "        </documents>" +
                        "        <nodes count='3'/>" +
                        "    </content>" +
                        "</services>";
        VespaModelTester tester = new VespaModelTester(amendingModelRepo);
        tester.addHosts(12);
        VespaModel model = tester.createModel(services);

        // Check that all hosts are amended
        for (HostResource host : model.getAdmin().hostSystem().getHosts()) {
            assertFalse(host.getHost().getChildrenByTypeRecursive(AmendedService.class).isEmpty(), host + " is amended");
        }

        // Check that container clusters are amended
        assertEquals(2, model.getContainerClusters().size());
        assertNotNull(model.getContainerClusters().get("test1").getComponentsMap().get(new ComponentId("com.yahoo.MyAmendedComponent")));
        assertNotNull(model.getContainerClusters().get("test2").getComponentsMap().get(new ComponentId("com.yahoo.MyAmendedComponent")));
    }

    public static class AdminModelAmenderBuilder extends ConfigModelBuilder<AdminModelAmender> {

        public AdminModelAmenderBuilder() {
            super(AdminModelAmender.class);
        }

        @Override
        public List<ConfigModelId> handlesElements() {
            List<ConfigModelId> adminElements = new ArrayList<>();
            adminElements.addAll(AdminModel.BuilderV2.configModelIds);
            adminElements.addAll(AdminModel.BuilderV4.configModelIds);
            return adminElements;
        }

        @Override
        public void doBuild(AdminModelAmender model, Element spec, ConfigModelContext modelContext) {
            for (AdminModel adminModel : model.adminModels)
                amend(modelContext.getDeployState(), adminModel);
        }

        private void amend(DeployState deployState, AdminModel adminModel) {
            for (HostResource host : adminModel.getAdmin().hostSystem().getHosts()) {
                if ( ! host.getHost().getChildrenByTypeRecursive(AmendedService.class).isEmpty()) continue; // already amended
                adminModel.getAdmin().addAndInitializeService(deployState, host, new AmendedService(host.getHost()));
            }
        }

    }

    /** To test that we can amend hosts with an additional service */
    private static class AmendedService extends AbstractService {

        public AmendedService(TreeConfigProducer parent) {
            super(parent, "testservice");
        }

        @Override
        public int getPortCount() {
            return 0;
        }

        @Override public void allocatePorts(int start, PortAllocBridge from) { }
    }

    public static class AdminModelAmender extends ConfigModel {

        /** The admin models this builder amends */
        private final Collection<AdminModel> adminModels;

        /** Depend on all models adding hosts to the system as this this should amend services on all hosts */
        public AdminModelAmender(ConfigModelContext modelContext, Collection<AdminModel> adminModels, 
                                 Collection<ContainerModel> containerModels, Collection<Content> contentModels) {
            super(modelContext);
            this.adminModels = adminModels;
        }

        @Override
        public boolean isServing() { return false; }

    }

    public static class ContainerModelAmenderBuilder extends ConfigModelBuilder<ContainerModelAmender> {

        private boolean built = false;

        public ContainerModelAmenderBuilder() {
            super(ContainerModelAmender.class);
        }

        @Override
        public List<ConfigModelId> handlesElements() {
            return ContainerModelBuilder.configModelIds;
        }

        @Override
        public void doBuild(ContainerModelAmender model, Element spec, ConfigModelContext modelContext) {
            if (built) return; // the same instance will be called once per container cluster
            for (ContainerModel containerModel : model.containerModels)
                amend(containerModel.getCluster());
            built = true;
        }

        static void amend(ContainerCluster cluster) {
            cluster.addSimpleComponent("com.yahoo.MyAmendedComponent", null, "my-amendment-bundle");
        }

    }

    public static class ContainerModelAmender extends ConfigModel {

        /** The container models this builder amends */
        private final Collection<ContainerModel> containerModels;

        public ContainerModelAmender(ConfigModelContext modelContext, Collection<ContainerModel> containerModels) {
            super(modelContext);
            this.containerModels = containerModels;
        }

        @Override
        public boolean isServing() { return false; }

    }

    public static class ContentModelAmenderBuilder extends ConfigModelBuilder<ContentModelAmender> {

        private boolean built = false;

        public ContentModelAmenderBuilder() {
            super(ContentModelAmender.class);
        }

        @Override
        public List<ConfigModelId> handlesElements() {
            return Content.Builder.configModelIds;
        }

        @Override
        public void doBuild(ContentModelAmender model, Element spec, ConfigModelContext modelContext) {
            if (built) return; // the same instance will be called once per content cluster
            for (Content contentModel : model.contentModels)
                contentModel.ownedIndexingCluster().ifPresent(ContainerModelAmenderBuilder::amend);
            built = true;
        }
    }

    public static class ContentModelAmender extends ConfigModel {

        private final Collection<Content> contentModels;

        public ContentModelAmender(ConfigModelContext modelContext, Collection<Content> contentModels) {
            super(modelContext);
            this.contentModels = contentModels;
        }

        @Override
        public boolean isServing() { return false; }

    }

}