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

import com.yahoo.config.application.api.ApplicationPackage;
import com.yahoo.config.model.ConfigModelContext;
import com.yahoo.config.model.api.HostProvisioner;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.config.model.provision.InMemoryProvisioner;
import com.yahoo.config.model.provision.SingleNodeProvisioner;
import com.yahoo.config.model.test.MockApplicationPackage;
import com.yahoo.config.model.test.MockRoot;
import com.yahoo.text.XML;
import com.yahoo.vespa.model.admin.Admin;
import com.yahoo.vespa.model.admin.monitoring.DefaultMonitoring;
import com.yahoo.vespa.model.admin.monitoring.builder.Metrics;
import com.yahoo.vespa.model.content.cluster.ContentCluster;
import org.w3c.dom.Document;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * For testing purposes only.
 * 
 * @author geirst
 */
public class ContentClusterUtils {

    public static MockRoot createMockRoot(String[] hosts) {
        return createMockRoot(hosts, SchemaBuilder.createSchemas("test"));
    }

    private static MockRoot createMockRoot(HostProvisioner provisioner, List<String> schemas) {
        return createMockRoot(provisioner, schemas, new DeployState.Builder());
    }

    public static MockRoot createMockRoot(HostProvisioner provisioner, List<String> schemas, DeployState.Builder deployStateBuilder) {
        ApplicationPackage applicationPackage = new MockApplicationPackage.Builder().withSchemas(schemas).build();
        DeployState deployState = deployStateBuilder.applicationPackage(applicationPackage)
                          .modelHostProvisioner(provisioner)
                          .build();
        return new MockRoot("", deployState);
    }

    public static MockRoot createMockRoot(String[] hosts, List<String> schemas) {
        return createMockRoot(new InMemoryProvisioner(true, false, hosts), schemas);
    }

    public static MockRoot createMockRoot(List<String> schemas) {
        return createMockRoot(new SingleNodeProvisioner(), schemas);
    }

    public static MockRoot createMockRoot(List<String> schemas, DeployState.Builder deployStateBuilder) {
        return createMockRoot(new SingleNodeProvisioner(), schemas, deployStateBuilder);
    }

    public static ContentCluster createCluster(String clusterXml, MockRoot root) {
        ConfigModelContext.ApplicationType applicationType = ConfigModelContext.ApplicationType.DEFAULT;
        Admin admin = new Admin(root,
                                new DefaultMonitoring(),
                                new Metrics(),
                                root.getDeployState().getProperties().multitenant(),
                                root.getDeployState().isHosted(),
                                applicationType);
        Document doc = XML.getDocument(clusterXml);
        ConfigModelContext context = ConfigModelContext.create(applicationType, root.getDeployState(),
                                                               null,null, root, null);

        return new ContentCluster.Builder(admin).build(Collections.emptyList(), context, doc.getDocumentElement());
    }

    public static ContentCluster createCluster(String clusterXml, List<String> schemas, DeployState.Builder deployStateBuilder) throws Exception {
        MockRoot root = createMockRoot(schemas, deployStateBuilder);
        ContentCluster cluster = createCluster(clusterXml, root);
        root.freezeModelTopology();
        cluster.validate();
        return cluster;
    }

    public static ContentCluster createCluster(String clusterXml, List<String> schemas) throws Exception {
        return createCluster(clusterXml, schemas, new DeployState.Builder());
    }

    public static ContentCluster createCluster(String clusterXml) throws Exception {
        return createCluster(clusterXml, SchemaBuilder.createSchemas("test"), new DeployState.Builder());
    }

    public static ContentCluster createCluster(String clusterXml, DeployState.Builder deployStateBuilder) throws Exception {
        return createCluster(clusterXml, SchemaBuilder.createSchemas("test"), deployStateBuilder);
    }

    public static String createClusterXml(String groupXml, int redundancy, int searchableCopies) {
        return createClusterXml(groupXml, Optional.empty(), redundancy, searchableCopies);
    }

    public static String createClusterXml(String groupXml, Optional<String> dispatchXml, int redundancy, int searchableCopies) {
        return new ContentClusterBuilder().
                groupXml(groupXml).
                dispatchXml(dispatchXml).
                redundancy(redundancy).
                searchableCopies(searchableCopies).getXml();
    }

}