ba5a8378470131f68c4fd839ea3e738af1f32d7e
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.common.database.responses;
23
24 import java.io.IOException;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import org.elasticsearch.client.Response;
29 import org.json.JSONException;
30 import org.json.JSONObject;
31
32 public class NodeStatsResponse extends BaseResponse {
33
34     private NodesInfo nodesInfo;
35     private Map<String, NodeStats> nodeStats;
36
37     public NodesInfo getNodesInfo() {
38         return this.nodesInfo;
39     }
40
41     public Map<String, NodeStats> getNodeStatistics() {
42         return this.nodeStats;
43     }
44
45     public NodeStatsResponse(Response response) throws UnsupportedOperationException, IOException, JSONException {
46         super(response);
47
48         JSONObject o = this.getJson(response);
49         String k;
50         if (o != null) {
51             this.nodesInfo = new NodesInfo(o.getJSONObject("_nodes"));
52             this.nodeStats = new HashMap<>();
53             if (this.nodesInfo.successful > 0) {
54                 JSONObject stats = o.getJSONObject("nodes");
55                 for (Object key : stats.keySet()) {
56                     k = String.valueOf(key);
57                     this.nodeStats.put(k, new NodeStats(k, stats.getJSONObject(k)));
58                 }
59             }
60         }
61     }
62
63
64
65     public static class NodesInfo {
66         @Override
67         public String toString() {
68             return "NodesInfo [total=" + total + ", successful=" + successful + ", failed=" + failed + "]";
69         }
70
71         public final int total;
72         public final int successful;
73         public final int failed;
74
75         public NodesInfo(JSONObject o) {
76             this.total = o.getInt("total");
77             this.successful = o.getInt("successful");
78             this.failed = o.getInt("failed");
79         }
80     }
81     public static class NodeStats {
82         public final String name;
83         public final NodeTotalDiskStats total;
84
85         @Override
86         public String toString() {
87             return "NodeStats [name=" + name + ", total=" + total + "]";
88         }
89
90         public NodeStats(String name, JSONObject o) {
91             this.name = name;
92             this.total = new NodeTotalDiskStats(o.getJSONObject("fs").getJSONObject("total"));
93         }
94     }
95     public static class NodeTotalDiskStats {
96         public final long total;
97         public final long available;
98         public final long free;
99
100         @Override
101         public String toString() {
102             return "NodeTotalDiskStats [total=" + total + ", available=" + available + ", free=" + free
103                     + ", getUseDiskPercentage()=" + getUseDiskPercentage() + "]";
104         }
105
106         public float getUseDiskPercentage() {
107             return (total - available) * 100.0f / (float) total;
108         }
109
110         public NodeTotalDiskStats(JSONObject o) {
111             this.total = o.getLong("total_in_bytes");
112             this.available = o.getLong("available_in_bytes");
113             this.free = o.getLong("free_in_bytes");
114         }
115     }
116 }