UI Exensibility config cleanup
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / dal / elasticsearch / ElasticSearchEntityStatistics.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.dal.elasticsearch;
24
25 import java.util.Comparator;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.Set;
29 import java.util.TreeMap;
30 import java.util.concurrent.atomic.AtomicInteger;
31
32 import org.onap.aai.restclient.client.OperationResult;
33 import org.onap.aai.sparky.dal.NetworkTransaction;
34 import org.onap.aai.sparky.dal.rest.HttpMethod;
35
36
37 /**
38  * The Class ElasticSearchEntityStatistics.
39  */
40 public class ElasticSearchEntityStatistics {
41
42   private static final String TOTAL = "Total";
43   private static final String CREATED = "Created";
44   private static final String MODIFIED = "Modified";
45   private static final String OTHERSUCCESS = "OTHERSUCCESS";
46   private static final String DELETED = "DELETED";
47   private static final String ERROR = "ERROR";
48
49   private Map<String, HashMap<String, AtomicInteger>> entityStatistics;
50   
51   /**
52    * Creates the entity op stats.
53    *
54    * @return the hash map
55    */
56   private HashMap<String, AtomicInteger> createEntityOpStats() {
57
58     HashMap<String, AtomicInteger> opStats = new HashMap<String, AtomicInteger>();
59
60     opStats.put(TOTAL, new AtomicInteger());
61     opStats.put(CREATED, new AtomicInteger());
62     opStats.put(MODIFIED, new AtomicInteger());
63     opStats.put(OTHERSUCCESS, new AtomicInteger());
64     opStats.put(DELETED, new AtomicInteger());
65     opStats.put(ERROR, new AtomicInteger());
66
67     return opStats;
68
69   }
70
71   /**
72    * Initializecreate active inventory entity statistics.
73    */
74   private void initializecreateActiveInventoryEntityStatistics() {
75     Set<String> keys = entityStatistics.keySet();
76
77     Set<String> opStatKeySet = null;
78     Map<String, AtomicInteger> opStats = null;
79
80     for (String k : keys) {
81
82       opStats = entityStatistics.get(k);
83
84       opStatKeySet = opStats.keySet();
85
86       for (String opStatKey : opStatKeySet) {
87         opStats.get(opStatKey).set(0);
88       }
89     }
90   }
91
92   /**
93    * Instantiates a new elastic search entity statistics.
94    *
95    * @param loader the loader
96    */
97   public ElasticSearchEntityStatistics() {
98     entityStatistics = new HashMap<String, HashMap<String, AtomicInteger>>();
99     reset();
100   }
101
102   /**
103    * Initialize counters from oxm entity descriptors.
104    *
105    * @param descriptors the descriptors
106    */
107   public void intializeEntityCounters(
108       String... entityTypes) {
109
110     if (entityTypes != null && entityTypes.length > 0) {
111       for (String entityType : entityTypes) {
112         entityStatistics.put(entityType, createEntityOpStats());
113       }
114
115     }
116     
117   }
118   
119   public void intializeEntityCounters(
120       Set<String> entityTypes) {
121
122     if (entityTypes != null && entityTypes.size() > 0) {
123       for (String entityType : entityTypes) {
124         entityStatistics.put(entityType, createEntityOpStats());
125       }
126     }
127     
128   }
129
130   /**
131    * Reset.
132    */
133   public void reset() {
134     initializecreateActiveInventoryEntityStatistics();
135   }
136
137   /**
138    * Gets the result code.
139    *
140    * @param txn the txn
141    * @return the result code
142    */
143   private int getResultCode(NetworkTransaction txn) {
144
145
146     if (txn == null) {
147       return -1;
148     }
149
150     OperationResult or = txn.getOperationResult();
151
152     if (or == null) {
153       return -1;
154     }
155
156     return or.getResultCode();
157
158   }
159
160   /**
161    * Update elastic search entity counters.
162    *
163    * @param txn the txn
164    */
165   private void updateElasticSearchEntityCounters(NetworkTransaction txn) {
166
167     if (txn == null) {
168       return;
169     }
170
171     Map<String, AtomicInteger> entityOpStats = entityStatistics.get(txn.getEntityType());
172
173     int resultCode = getResultCode(txn);
174
175     if (txn.getOperationType() == HttpMethod.PUT) {
176
177       entityOpStats.get(TOTAL).incrementAndGet();
178
179       if (resultCode == 201) {
180         entityOpStats.get(CREATED).incrementAndGet();
181       } else if (resultCode == 200) {
182         entityOpStats.get(MODIFIED).incrementAndGet();
183       } else if (202 <= resultCode && resultCode <= 299) {
184         entityOpStats.get(OTHERSUCCESS).incrementAndGet();
185       } else {
186         entityOpStats.get(ERROR).incrementAndGet();
187       }
188
189     } else if (txn.getOperationType() == HttpMethod.DELETE) {
190
191       entityOpStats.get(TOTAL).incrementAndGet();
192
193       if (200 <= resultCode && resultCode <= 299) {
194         entityOpStats.get(DELETED).incrementAndGet();
195       } else {
196         entityOpStats.get(ERROR).incrementAndGet();
197       }
198     }
199
200   }
201
202   /**
203    * Update counters.
204    *
205    * @param txn the txn
206    */
207   public void updateCounters(NetworkTransaction txn) {
208
209     updateElasticSearchEntityCounters(txn);
210
211   }
212
213   public String getStatisticsReport() {
214
215     StringBuilder sb = new StringBuilder(128);
216
217     /*
218      * sort entities, then sort nested op codes
219      */
220
221     TreeMap<String, HashMap<String, AtomicInteger>> elasticEntitySortedTreeMap =
222         new TreeMap<String, HashMap<String, AtomicInteger>>(new Comparator<String>() {
223
224           @Override
225           public int compare(String o1, String o2) {
226             return o1.toLowerCase().compareTo(o2.toLowerCase());
227           }
228         });
229
230     elasticEntitySortedTreeMap.putAll(entityStatistics);
231
232     for (String counterEntityKey : elasticEntitySortedTreeMap.keySet()) {
233
234       HashMap<String, AtomicInteger> entityCounters =
235           elasticEntitySortedTreeMap.get(counterEntityKey);
236
237       AtomicInteger total = entityCounters.get(TOTAL);
238       AtomicInteger created = entityCounters.get(CREATED);
239       AtomicInteger modified = entityCounters.get(MODIFIED);
240       AtomicInteger otherSuccess = entityCounters.get(OTHERSUCCESS);
241       AtomicInteger deleted = entityCounters.get(DELETED);
242       AtomicInteger error = entityCounters.get(ERROR);
243
244       int totalValue = (total == null) ? 0 : total.get();
245       int createdValue = (created == null) ? 0 : created.get();
246       int modifiedValue = (modified == null) ? 0 : modified.get();
247       int otherSuccessValue = (otherSuccess == null) ? 0 : otherSuccess.get();
248       int deletedValue = (deleted == null) ? 0 : deleted.get();
249       int errorValue = (error == null) ? 0 : error.get();
250
251       sb.append("\n            ")
252           .append(String.format(
253               "%-30s TOTAL: %-12d CREATED: %-12d MODIFIED:"
254               + " %-12d OTHER_2XX: %-12d DELETED: %-12d ERROR: %-12d",
255               counterEntityKey, totalValue, createdValue, modifiedValue, otherSuccessValue,
256               deletedValue, errorValue));
257     }
258     return sb.toString();
259   }
260
261
262
263 }