9d235b1859d3dec2e420ab5535f18d62bb744c7a
[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 @Deprecated
33 public class NodeStatsResponse extends BaseResponse {
34
35     private NodesInfo nodesInfo;
36     private Map<String, NodeStats> nodeStats;
37
38     public NodesInfo getNodesInfo() {
39         return this.nodesInfo;
40     }
41
42     public Map<String, NodeStats> getNodeStatistics() {
43         return this.nodeStats;
44     }
45
46     public NodeStatsResponse(Response response) throws UnsupportedOperationException, IOException, JSONException {
47         super(response);
48
49         JSONObject o = this.getJson(response);
50         String k;
51         if (o != null) {
52             this.nodesInfo = new NodesInfo(o.getJSONObject("_nodes"));
53             this.nodeStats = new HashMap<>();
54             if (this.nodesInfo.successful > 0) {
55                 JSONObject stats = o.getJSONObject("nodes");
56                 for (Object key : stats.keySet()) {
57                     k = String.valueOf(key);
58                     this.nodeStats.put(k, new NodeStats(k, stats.getJSONObject(k)));
59                 }
60             }
61         }
62     }
63
64
65
66     public static class NodesInfo {
67         @Override
68         public String toString() {
69             return "NodesInfo [total=" + total + ", successful=" + successful + ", failed=" + failed + "]";
70         }
71
72         public final int total;
73         public final int successful;
74         public final int failed;
75
76         public NodesInfo(JSONObject o) {
77             this.total = o.getInt("total");
78             this.successful = o.getInt("successful");
79             this.failed = o.getInt("failed");
80         }
81     }
82     public static class NodeStats {
83         public final String name;
84         public final NodeTotalDiskStats total;
85
86         @Override
87         public String toString() {
88             return "NodeStats [name=" + name + ", total=" + total + "]";
89         }
90
91         public NodeStats(String name, JSONObject o) {
92             this.name = name;
93             this.total = new NodeTotalDiskStats(o.getJSONObject("fs").getJSONObject("total"));
94         }
95     }
96     public static class NodeTotalDiskStats {
97         public final long total;
98         public final long available;
99         public final long free;
100
101         @Override
102         public String toString() {
103             return "NodeTotalDiskStats [total=" + total + ", available=" + available + ", free=" + free
104                     + ", getUseDiskPercentage()=" + getUseDiskPercentage() + "]";
105         }
106
107         public float getUseDiskPercentage() {
108             return (total - available) * 100.0f / (float) total;
109         }
110
111         public NodeTotalDiskStats(JSONObject o) {
112             this.total = o.getLong("total_in_bytes");
113             this.available = o.getLong("available_in_bytes");
114             this.free = o.getLong("free_in_bytes");
115         }
116     }
117 }