summaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/main/java/com/yahoo/vespa/clustercontroller/core/restapiv2/requests/ClusterStateRequest.java
blob: 66ca9e2132e6efcfe4625cd51050829392f58493 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.core.restapiv2.requests;

import com.yahoo.vdslib.state.NodeType;
import com.yahoo.vespa.clustercontroller.core.RemoteClusterControllerTask;
import com.yahoo.vespa.clustercontroller.core.restapiv2.Id;
import com.yahoo.vespa.clustercontroller.core.restapiv2.Request;
import com.yahoo.vespa.clustercontroller.core.restapiv2.Response;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.StateRestApiException;

public class ClusterStateRequest extends Request<Response.ClusterResponse> {
    private final Id.Cluster id;
    private final int recursive;

    public ClusterStateRequest(Id.Cluster id, int recursive) {
        super(MasterState.MUST_BE_MASTER);
        this.id = id;
        this.recursive = recursive;
    }

    @Override
    public Response.ClusterResponse calculateResult(RemoteClusterControllerTask.Context context) throws StateRestApiException {
        Response.ClusterResponse result = new Response.ClusterResponse();
        result.addState("generated", new Response.UnitStateImpl(context.currentState.getClusterState()));
        for (NodeType type : NodeType.getTypes()) {
            Id.Service serviceId = new Id.Service(id, type);
            if (recursive > 0) {
                ServiceStateRequest ssr = new ServiceStateRequest(serviceId, recursive - 1);
                result.addEntry("service", type.toString(), ssr.calculateResult(context));
            } else {
                result.addLink("service", type.toString(), serviceId.toString());
            }
        }
        return result;
    }
}