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