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