summaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/main/java/com/yahoo/vespa/clustercontroller/core/restapiv2/Response.java
blob: 26c66fea16514687bc3f8fe59f3b3f0dd8a963dd (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
// Copyright 2017 Yahoo Holdings. 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.DiskState;
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.*;

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 UnitStateImpl(DiskState ds) throws StateRestApiException {
            this.id = parseId(ds.getState());
            this.reason = ds.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 getId() { return id; }
        @Override
        public String getReason() { return 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
    {
        protected final Map<String, String> attributes = new LinkedHashMap<>();
        protected final Map<String, SubUnitList> subUnits = new LinkedHashMap<>();
        protected final Map<String, Number> metrics = new LinkedHashMap<>();
        protected final Map<String, UnitState> stateMap = new LinkedHashMap<>();

        @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 Map<String, Number> getMetricMap() { return metrics; }
        @Override
        public Map<String, UnitState> getStatePerType() { return stateMap; }
        @Override
        public Map<String, String> getAttributeValues() { return attributes; }

        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, Number 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 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> {}

}