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

import com.yahoo.vdslib.state.NodeState;
import com.yahoo.vdslib.state.State;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.InternalFailure;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.StateRestApiException;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.response.CurrentUnitState;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.response.DistributionState;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.response.DistributionStates;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.response.SubUnitList;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.response.UnitAttributes;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.response.UnitMetrics;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.response.UnitResponse;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.response.UnitState;

import java.util.LinkedHashMap;
import java.util.Map;

public class Response {

    public static class UnitStateImpl implements UnitState {
        private final String id;
        private final String reason;

        public UnitStateImpl(State s) throws StateRestApiException {
            this.id = parseId(s);
            this.reason = "";
        }
        public UnitStateImpl(NodeState ns) throws StateRestApiException {
            this.id = parseId(ns.getState());
            this.reason = ns.getDescription();
        }

        public String parseId(State id) throws StateRestApiException {
            switch (id) {
                case UP: return "up";
                case DOWN: return "down";
                case INITIALIZING: return "initializing";
                case MAINTENANCE: return "maintenance";
                case RETIRED: return "retired";
                case STOPPING: return "stopping";
            }
            throw new InternalFailure("Unknown state '" + id + "' found.");
        }

        @Override
        public String id() { return id; }
        @Override
        public String reason() { return reason; }
        @Override
        public String toString() { return id() +": " + reason(); }
    }
    public static class Link implements SubUnitList {
        private final Map<String, String> links = new LinkedHashMap<>();
        private final Map<String, UnitResponse> units = new LinkedHashMap<>();

        public Link addLink(String unit, String link) {
            links.put(unit, link);
            return this;
        }

        public Link addUnit(String unit, UnitResponse r) {
            units.put(unit, r);
            return this;
        }

        @Override
        public Map<String, String> getSubUnitLinks() { return links; }
        @Override
        public Map<String, UnitResponse> getSubUnits() { return units; }
    }

    public static abstract class EmptyResponse<T extends UnitResponse>
            implements UnitResponse, UnitMetrics, UnitAttributes, CurrentUnitState, DistributionStates
    {
        protected final Map<String, String> attributes = new LinkedHashMap<>();
        protected final Map<String, SubUnitList> subUnits = new LinkedHashMap<>();
        protected final Map<String, Long> metrics = new LinkedHashMap<>();
        protected final Map<String, UnitState> stateMap = new LinkedHashMap<>();
        protected DistributionState publishedState = null;

        @Override
        public UnitAttributes getAttributes() { return attributes.isEmpty() ? null : this; }
        @Override
        public CurrentUnitState getCurrentState() { return stateMap.isEmpty() ? null : this; }
        @Override
        public Map<String, SubUnitList> getSubUnits() { return subUnits.isEmpty() ? null : subUnits; }
        @Override
        public UnitMetrics getMetrics() { return metrics.isEmpty() ? null : this; }
        @Override
        public DistributionStates getDistributionStates() {
            return (publishedState == null) ? null : this;
        }

        @Override
        public Map<String, Long> getMetricMap() { return metrics; }
        @Override
        public Map<String, UnitState> getStatePerType() { return stateMap; }
        @Override
        public Map<String, String> getAttributeValues() { return attributes; }
        @Override
        public DistributionState getPublishedState() {
            return publishedState;
        }

        public EmptyResponse<T> addLink(String type, String unit, String link) {
            Link list = (Link) subUnits.get(type);
            if (list == null) {
                list = new Link();
                subUnits.put(type, list);
            }
            list.addLink(unit, link);
            return this;
        }
        public EmptyResponse<T> addEntry(String type, String unit, T response) {
            Link list = (Link) subUnits.get(type);
            if (list == null) {
                list = new Link();
                subUnits.put(type, list);
            }
            list.addUnit(unit, response);
            return this;
        }
        public EmptyResponse<T> addMetric(String name, Long value) {
            metrics.put(name, value);
            return this;
        }
        public EmptyResponse<T> addState(String type, UnitStateImpl state) {
            stateMap.put(type, state);
            return this;
        }
        public EmptyResponse<T> addAttribute(String name, String value) {
            attributes.put(name, value);
            return this;
        }
        public EmptyResponse<T> setPublishedState(DistributionState publishedState) {
            this.publishedState = publishedState;
            return this;
        }
    }

    public static class ClusterListResponse extends EmptyResponse<ClusterResponse> {}
    public static class ClusterResponse extends EmptyResponse<ServiceResponse> {}
    public static class ServiceResponse extends EmptyResponse<NodeResponse> {}
    public static class NodeResponse extends EmptyResponse<PartitionResponse> {}
    public static class PartitionResponse extends EmptyResponse<UnitResponse> {}

}