aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-apps/src/main/java/com/yahoo/vespa/clustercontroller/apps/clustercontroller/StateRestApiV2Handler.java
blob: 7ca36f7c9fbd444a8d0391db774771495fd53f59 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.apps.clustercontroller;

import com.yahoo.cloud.config.ClusterInfoConfig;
import com.yahoo.component.annotation.Inject;
import com.yahoo.container.jdisc.RequestView;
import com.yahoo.container.jdisc.utils.CapabilityRequiringRequestHandler;
import com.yahoo.security.tls.Capability;
import com.yahoo.vespa.clustercontroller.apputil.communication.http.JDiscHttpRequestHandler;
import com.yahoo.vespa.clustercontroller.core.restapiv2.ClusterControllerStateRestAPI;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.server.RestApiHandler;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;

public class StateRestApiV2Handler extends JDiscHttpRequestHandler implements CapabilityRequiringRequestHandler {

    private static final Logger log = Logger.getLogger(StateRestApiV2Handler.class.getName());

    @Inject
    public StateRestApiV2Handler(ClusterController cc, ClusterInfoConfig config,
                                 JDiscHttpRequestHandler.Context ctx)
    {
        this(new ClusterControllerStateRestAPI(cc, getClusterControllerSockets(config)), "/cluster/v2", ctx);
    }

    @Override public Capability requiredCapability(RequestView __) { return Capability.CLUSTER_CONTROLLER__STATE; }

    private StateRestApiV2Handler(ClusterControllerStateRestAPI restApi, String pathPrefix,
                                  JDiscHttpRequestHandler.Context ctx)
    {
        super(new RestApiHandler(restApi).setDefaultPathPrefix(pathPrefix), ctx);
    }

    // This method is package-private instead of private to be accessible to unit-tests.
    static Map<Integer, ClusterControllerStateRestAPI.Socket> getClusterControllerSockets(ClusterInfoConfig config) {
        Map<Integer, ClusterControllerStateRestAPI.Socket> result = new TreeMap<>();
        for (ClusterInfoConfig.Services service : config.services()) {
            for (ClusterInfoConfig.Services.Ports port : service.ports()) {
                Set<String> tags = parseTags(port.tags());
                if (tags.contains("http") && tags.contains("state")) {
                    result.put(service.index(), new ClusterControllerStateRestAPI.Socket(service.hostname(), port.number()));
                    break;
                }
            }
        }
        if (result.isEmpty()) {
            log.warning("Found no cluster controller in model config");
        } else if (log.isLoggable(Level.FINE)) {
            StringBuilder sb = new StringBuilder();
            sb.append("Found ").append(result.size()).append(" cluster controllers in model config:");
            for (Map.Entry<Integer, ClusterControllerStateRestAPI.Socket> e : result.entrySet()) {
                sb.append("\n  ").append(e.getKey()).append(" -> ").append(e.getValue());
            }
            log.fine(sb.toString());
        }
        return result;
    }

    private static Set<String> parseTags(String tags) {
        Set<String> set = new HashSet<>();
        for(String s : tags.toLowerCase().split(" ")) {
            set.add(s.trim());
        }
        return set;
    }

}