c1ed906e28128c970fa33c99ee6cfd05be8dbf05
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / dal / aai / ActiveInventoryEntityStatistics.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.aai;
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.sparky.config.oxm.OxmEntityDescriptor;
33 import org.onap.aai.sparky.config.oxm.OxmModelLoader;
34 import org.onap.aai.sparky.dal.NetworkTransaction;
35 import org.onap.aai.sparky.dal.rest.OperationResult;
36
37 /**
38  * The Class ActiveInventoryEntityStatistics.
39  */
40 public class ActiveInventoryEntityStatistics {
41
42   private static final String TOTAL = "Total";
43
44   private static final String FOUND = "Found";
45
46   private static final String NO_PAYLOAD = "NoPayload";
47
48   private static final String NOT_FOUND = "NotFound";
49
50   private static final String NUM_RETRIES = "NumRetries";
51
52   private static final String ERROR = "Error";
53
54   private OxmModelLoader loader;
55
56
57   private Map<String, HashMap<String, AtomicInteger>> activeInventoryEntityStatistics;
58
59   /**
60    * Creates the entity op stats.
61    *
62    * @return the hash map
63    */
64   private HashMap<String, AtomicInteger> createEntityOpStats() {
65
66     HashMap<String, AtomicInteger> opStats = new HashMap<String, AtomicInteger>();
67
68     opStats.put(TOTAL, new AtomicInteger());
69     opStats.put(FOUND, new AtomicInteger());
70     opStats.put(NO_PAYLOAD, new AtomicInteger());
71     opStats.put(NOT_FOUND, new AtomicInteger());
72     opStats.put(NUM_RETRIES, new AtomicInteger());
73     opStats.put(ERROR, new AtomicInteger());
74
75     return opStats;
76
77   }
78
79   /*
80    * private void createSearchableActiveInventoryEntityStatistics() {
81    * 
82    * Map<String,OxmEntityDescriptor> descriptors = loader.getSearchableEntityDescriptors();
83    * 
84    * if(descriptors == null) { return; }
85    * 
86    * OxmEntityDescriptor d = null; for ( String key : descriptors.keySet() ) { d =
87    * descriptors.get(key); activeInventoryEntityStatistics.put(d.getEntityName(),
88    * createEntityOpStats()); }
89    * 
90    * }
91    */
92
93   /*
94    * private void createCrossEntityReferenceActiveInventoryEntityStatistics() {
95    * 
96    * Map<String,OxmEntityDescriptor> descriptors = loader.getCrossReferenceEntityDescriptors();
97    * 
98    * 
99    * }
100    */
101
102
103   /**
104    * Initializecreate active inventory entity statistics.
105    */
106   private void initializecreateActiveInventoryEntityStatistics() {
107     Set<String> keys = activeInventoryEntityStatistics.keySet();
108
109     Set<String> opStatKeySet = null;
110     Map<String, AtomicInteger> opStats = null;
111
112     for (String k : keys) {
113
114       opStats = activeInventoryEntityStatistics.get(k);
115
116       opStatKeySet = opStats.keySet();
117
118       for (String opStatKey : opStatKeySet) {
119         opStats.get(opStatKey).set(0);
120       }
121     }
122   }
123
124   /**
125    * Instantiates a new active inventory entity statistics.
126    *
127    * @param loader the loader
128    */
129   public ActiveInventoryEntityStatistics(OxmModelLoader loader) {
130     this.loader = loader;
131     activeInventoryEntityStatistics = new HashMap<String, HashMap<String, AtomicInteger>>();
132     // createSearchableActiveInventoryEntityStatistics();
133     // createCrossEntityReferenceActiveInventoryEntityStatistics();
134     reset();
135   }
136
137   /**
138    * Initialize counters from oxm entity descriptors.
139    *
140    * @param descriptors the descriptors
141    */
142   public void initializeCountersFromOxmEntityDescriptors(
143       Map<String, OxmEntityDescriptor> descriptors) {
144
145     if (descriptors == null) {
146       return;
147     }
148
149     OxmEntityDescriptor descriptor = null;
150     for (String key : descriptors.keySet()) {
151       descriptor = descriptors.get(key);
152       activeInventoryEntityStatistics.put(descriptor.getEntityName(), createEntityOpStats());
153     }
154   }
155
156
157   /**
158    * Reset.
159    */
160   public void reset() {
161     initializecreateActiveInventoryEntityStatistics();
162   }
163
164   /**
165    * Gets the result code.
166    *
167    * @param txn the txn
168    * @return the result code
169    */
170   private int getResultCode(NetworkTransaction txn) {
171
172
173     if (txn == null) {
174       return -1;
175     }
176
177     OperationResult or = txn.getOperationResult();
178
179     if (or == null) {
180       return -1;
181     }
182
183     return or.getResultCode();
184
185   }
186
187   /**
188    * Update active inventory entity counters.
189    *
190    * @param txn the txn
191    */
192   private void updateActiveInventoryEntityCounters(NetworkTransaction txn) {
193
194     if (txn == null) {
195       return;
196     }
197
198     Map<String, AtomicInteger> opStats = activeInventoryEntityStatistics.get(txn.getEntityType());
199
200     int rc = getResultCode(txn);
201
202     switch (txn.getOperationType()) {
203
204       case GET: {
205
206         opStats.get(TOTAL).incrementAndGet();
207
208         if (200 <= rc && rc <= 299) {
209           opStats.get(FOUND).incrementAndGet();
210         } else if (rc == 404) {
211           opStats.get(NOT_FOUND).incrementAndGet();
212         } else {
213           opStats.get(ERROR).incrementAndGet();
214         }
215
216         break;
217       }
218
219       default: {
220         // nothing else for now
221       }
222
223     }
224
225     OperationResult or = txn.getOperationResult();
226
227     if (or != null && or.wasSuccessful()) {
228
229       if (or.getResult() == null || or.getResult().length() == 0) {
230         opStats.get(NO_PAYLOAD).incrementAndGet();
231       }
232
233       if (or.getNumRequestRetries() > 0) {
234         opStats.get(NUM_RETRIES).addAndGet(or.getNumRequestRetries());
235       }
236
237     }
238
239
240   }
241
242   /**
243    * Update counters.
244    *
245    * @param txn the txn
246    */
247   public void updateCounters(NetworkTransaction txn) {
248
249     updateActiveInventoryEntityCounters(txn);
250
251   }
252
253   public String getStatisticsReport() {
254
255     StringBuilder sb = new StringBuilder(128);
256
257     /*
258      * sort entities, then sort nested op codes
259      */
260
261     TreeMap<String, HashMap<String, AtomicInteger>> activeInventoryEntitySortedTreeMap =
262         new TreeMap<String, HashMap<String, AtomicInteger>>(new Comparator<String>() {
263
264           @Override
265           public int compare(String o1, String o2) {
266             return o1.toLowerCase().compareTo(o2.toLowerCase());
267           }
268         });
269
270     activeInventoryEntitySortedTreeMap.putAll(activeInventoryEntityStatistics);
271
272     for (String counterEntityKey : activeInventoryEntitySortedTreeMap.keySet()) {
273
274       HashMap<String, AtomicInteger> entityCounters =
275           activeInventoryEntitySortedTreeMap.get(counterEntityKey);
276
277       AtomicInteger total = entityCounters.get(TOTAL);
278       AtomicInteger found = entityCounters.get(FOUND);
279       AtomicInteger noPayload = entityCounters.get(NO_PAYLOAD);
280       AtomicInteger notFound = entityCounters.get(NOT_FOUND);
281       AtomicInteger numRetries = entityCounters.get(NUM_RETRIES);
282       AtomicInteger error = entityCounters.get(ERROR);
283
284       int totalValue = (total == null) ? 0 : total.get();
285       int foundValue = (found == null) ? 0 : found.get();
286       int noPayloadValue = (noPayload == null) ? 0 : noPayload.get();
287       int notFoundValue = (notFound == null) ? 0 : notFound.get();
288       int numRetriesValue = (numRetries == null) ? 0 : numRetries.get();
289       int errorValue = (error == null) ? 0 : error.get();
290
291       sb.append("\n            ")
292           .append(String.format(
293               "%-30s TOTAL: %-12d FOUND: %-12d NO_PAYLOAD:"
294                   + " %-12d NOT_FOUND: %-12d NUM_RETRIES: %-12d ERROR: %-12d",
295               counterEntityKey, totalValue, foundValue, noPayloadValue, notFoundValue,
296               numRetriesValue, errorValue));
297     }
298
299     return sb.toString();
300   }
301
302
303
304 }