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

import com.yahoo.config.model.api.ApplicationInfo;
import com.yahoo.config.model.api.HostInfo;
import com.yahoo.config.model.api.Model;
import com.yahoo.config.model.api.PortInfo;
import com.yahoo.config.model.api.ServiceInfo;
import com.yahoo.config.model.api.SuperModel;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.InstanceName;
import com.yahoo.vespa.service.slobrok.SlobrokMonitor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ExampleModel {

    static final String CLUSTER_ID = "cluster-id";
    static final String SERVICE_NAME = "service-name";
    static final String SERVICE_TYPE = SlobrokMonitor.SLOBROK_SERVICE_TYPE;
    static final String CONFIG_ID = "configid/1";
    static final String TENANT = "tenant";
    static final String APPLICATION_NAME = "application";
    public static final String INSTANCE_NAME = "default";
    public static final ApplicationId APPLICATION_ID = ApplicationId.from(TENANT, APPLICATION_NAME, INSTANCE_NAME);

    public static SuperModel createExampleSuperModelWithOneRpcPort(String hostname, int rpcPort) {
        List<String> hosts = Stream.of(hostname).toList();

        ApplicationInfo applicationInfo = ExampleModel
                .createApplication(TENANT, APPLICATION_NAME)
                .addServiceCluster(CLUSTER_ID, SERVICE_NAME, SERVICE_TYPE, hosts)
                .addPort(rpcPort, "footag", SlobrokMonitor.SLOBROK_RPC_PORT_TAG)
                .addPort(rpcPort + 1, "bartag")
                .then()
                .build();

        Map<ApplicationId, ApplicationInfo> applicationInfos = new HashMap<>();
        applicationInfos.put(applicationInfo.getApplicationId(), applicationInfo);
        return new SuperModel(applicationInfos, true);
    }

    public static ApplicationBuilder createApplication(String tenant,
                                                       String applicationName) {
        return new ApplicationBuilder(tenant, applicationName);
    }


    public static class ApplicationBuilder {
        private final String tenant;
        private final String applicationName;
        private final List<ClusterBuilder> clusters = new ArrayList<>();

        ApplicationBuilder(String tenant, String applicationName) {
            this.tenant = tenant;
            this.applicationName = applicationName;
        }

        ClusterBuilder addServiceCluster(
                String clusterId,
                String serviceName,
                String serviceType,
                List<String> hosts) {
            return new ClusterBuilder(
                    this,
                    clusterId,
                    serviceName,
                    serviceType,
                    hosts);
        }

        public ApplicationInfo build() {
            List<String> allHosts = clusters.stream()
                    .flatMap(clusterBuilder -> clusterBuilder.hosts.stream())
                    .distinct()
                    .toList();

            List<HostInfo> hostInfos = new ArrayList<>();
            for (String hostname : allHosts) {
                List<ServiceInfo> serviceInfos = new ArrayList<>();
                for (ClusterBuilder cluster : clusters) {
                    buildServiceInfo(hostname, cluster).ifPresent(serviceInfos::add);
                }

                HostInfo hostInfo = new HostInfo(hostname, serviceInfos);
                hostInfos.add(hostInfo);
            }

            ApplicationId id = ApplicationId.from(
                    tenant,
                    applicationName,
                    InstanceName.defaultName().toString());

            Model model = mock(Model.class);
            when(model.getHosts()).thenReturn(hostInfos);

            return new ApplicationInfo(id, 1, model);
        }

        private Optional<ServiceInfo> buildServiceInfo(
                String hostname,
                ClusterBuilder cluster) {
            int hostIndex = cluster.hosts.indexOf(hostname);
            if (hostIndex < 0) {
                return Optional.empty();
            }

            Map<String, String> properties = new HashMap<>();
            properties.put(ModelGenerator.CLUSTER_ID_PROPERTY_NAME, cluster.clusterId);
            return Optional.of(new ServiceInfo(
                    cluster.serviceName,
                    cluster.serviceType,
                    cluster.portInfos,
                    properties,
                    "configid/" + (hostIndex + 1),
                    hostname));
        }
    }

    static class ClusterBuilder {
        private final ApplicationBuilder applicationBuilder;
        private final String clusterId;
        private final String serviceName;
        private final String serviceType;
        private final List<String> hosts;
        private final List<PortInfo> portInfos = new ArrayList<>();

        ClusterBuilder(ApplicationBuilder applicationBuilder,
                       String clusterId,
                       String serviceName,
                       String serviceType,
                       List<String> hosts) {
            this.applicationBuilder = applicationBuilder;
            this.clusterId = clusterId;
            this.serviceName = serviceName;
            this.serviceType = serviceType;
            this.hosts = hosts;
        }

        /**
         * A bit unrealistic, but the port is the same on all hosts.
         */
        ClusterBuilder addPort(int port, String... tags) {
            portInfos.add(new PortInfo(port, Arrays.asList(tags)));
            return this;
        }

        ApplicationBuilder then() {
            applicationBuilder.clusters.add(this);
            return applicationBuilder;
        }
    }
}