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

import com.yahoo.config.ConfigInstance;
import com.yahoo.config.FileReference;
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.provision.AllocatedHosts;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.vespa.config.ConfigKey;
import com.yahoo.vespa.config.buildergen.ConfigDefinition;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Model with two services, one that does not have a state port
 *
 * @author hakonhall
 */
public class MockModel implements Model {

    private final Collection<HostInfo> hosts;

    static MockModel createContainer(String hostname, int statePort) {
        return new MockModel(Collections.singleton(createContainerHost(hostname, statePort)));
    }

    static HostInfo createContainerHost(String hostname, int statePort) {
        ServiceInfo container = createServiceInfo(hostname, "container", "container",
                                                  ClusterSpec.Type.container, statePort, "state");
        ServiceInfo serviceNoStatePort = createServiceInfo(hostname, "logserver", "logserver",
                                                           ClusterSpec.Type.admin, 1234, "logtp");
        return new HostInfo(hostname, Arrays.asList(container, serviceNoStatePort));
    }

    static MockModel createConfigProxies(List<String> hostnames, int rpcPort) {
        Set<HostInfo> hostInfos = new HashSet<>();
        hostnames.forEach(hostname -> {
            ServiceInfo configProxy = createServiceInfo(hostname, "configproxy", "configproxy",
                                            ClusterSpec.Type.admin, rpcPort, "rpc");
            hostInfos.add(new HostInfo(hostname, Collections.singletonList(configProxy)));
        });
        return new MockModel(hostInfos);
    }

    static ServiceInfo createServiceInfo(
            String hostname,
            String name,
            String type,
            ClusterSpec.Type clusterType,
            int port,
            String portTags) {
        PortInfo portInfo = new PortInfo(port, Arrays.stream(portTags.split(" ")).collect(Collectors.toSet()));
        Map<String, String> properties = new HashMap<>();
        properties.put("clustername", "default");
        properties.put("clustertype", clusterType.name());
        return new ServiceInfo(name, type, Collections.singleton(portInfo), properties, "", hostname);
    }

    MockModel(Collection<HostInfo> hosts) {
        this.hosts = hosts;
    }

    @Override
    public ConfigInstance.Builder getConfigInstance(ConfigKey<?> configKey, ConfigDefinition targetDef) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Set<ConfigKey<?>> allConfigsProduced() {
        throw new UnsupportedOperationException();
    }

    @Override
    public Collection<HostInfo> getHosts() {
        return hosts;
    }

    @Override
    public Set<String> allConfigIds() {
        throw new UnsupportedOperationException();
    }

    @Override
    public Set<FileReference> fileReferences() { return new HashSet<>(); }

    @Override
    public AllocatedHosts allocatedHosts() {
        throw new UnsupportedOperationException();
    }

}