Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / dal / aai / ActiveInventoryEntityStatistics.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25
26 package org.openecomp.sparky.dal.aai;
27
28 import java.util.Comparator;
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.Set;
32 import java.util.TreeMap;
33 import java.util.concurrent.atomic.AtomicInteger;
34
35 import org.openecomp.sparky.config.oxm.OxmEntityDescriptor;
36 import org.openecomp.sparky.config.oxm.OxmModelLoader;
37 import org.openecomp.sparky.dal.NetworkTransaction;
38 import org.openecomp.sparky.dal.rest.OperationResult;
39
40 /**
41  * The Class ActiveInventoryEntityStatistics.
42  */
43 public class ActiveInventoryEntityStatistics {
44
45   private static final String TOTAL = "Total";
46
47   private static final String FOUND = "Found";
48
49   private static final String NO_PAYLOAD = "NoPayload";
50
51   private static final String NOT_FOUND = "NotFound";
52
53   private static final String NUM_RETRIES = "NumRetries";
54
55   private static final String ERROR = "Error";
56
57   private OxmModelLoader loader;
58
59
60   private Map<String, HashMap<String, AtomicInteger>> activeInventoryEntityStatistics;
61
62   /**
63    * Creates the entity op stats.
64    *
65    * @return the hash map
66    */
67   private HashMap<String, AtomicInteger> createEntityOpStats() {
68
69     HashMap<String, AtomicInteger> opStats = new HashMap<String, AtomicInteger>();
70
71     opStats.put(TOTAL, new AtomicInteger());
72     opStats.put(FOUND, new AtomicInteger());
73     opStats.put(NO_PAYLOAD, new AtomicInteger());
74     opStats.put(NOT_FOUND, new AtomicInteger());
75     opStats.put(NUM_RETRIES, new AtomicInteger());
76     opStats.put(ERROR, new AtomicInteger());
77
78     return opStats;
79
80   }
81
82   /*
83    * private void createSearchableActiveInventoryEntityStatistics() {
84    * 
85    * Map<String,OxmEntityDescriptor> descriptors = loader.getSearchableEntityDescriptors();
86    * 
87    * if(descriptors == null) { return; }
88    * 
89    * OxmEntityDescriptor d = null; for ( String key : descriptors.keySet() ) { d =
90    * descriptors.get(key); activeInventoryEntityStatistics.put(d.getEntityName(),
91    * createEntityOpStats()); }
92    * 
93    * }
94    */
95
96   /*
97    * private void createCrossEntityReferenceActiveInventoryEntityStatistics() {
98    * 
99    * Map<String,OxmEntityDescriptor> descriptors = loader.getCrossReferenceEntityDescriptors();
100    * 
101    * 
102    * }
103    */
104
105
106   /**
107    * Initializecreate active inventory entity statistics.
108    */
109   private void initializecreateActiveInventoryEntityStatistics() {
110     Set<String> keys = activeInventoryEntityStatistics.keySet();
111
112     Set<String> opStatKeySet = null;
113     Map<String, AtomicInteger> opStats = null;
114
115     for (String k : keys) {
116
117       opStats = activeInventoryEntityStatistics.get(k);
118
119       opStatKeySet = opStats.keySet();
120
121       for (String opStatKey : opStatKeySet) {
122         opStats.get(opStatKey).set(0);
123       }
124     }
125   }
126
127   /**
128    * Instantiates a new active inventory entity statistics.
129    *
130    * @param loader the loader
131    */
132   public ActiveInventoryEntityStatistics(OxmModelLoader loader) {
133     this.loader = loader;
134     activeInventoryEntityStatistics = new HashMap<String, HashMap<String, AtomicInteger>>();
135     // createSearchableActiveInventoryEntityStatistics();
136     // createCrossEntityReferenceActiveInventoryEntityStatistics();
137     reset();
138   }
139
140   /**
141    * Initialize counters from oxm entity descriptors.
142    *
143    * @param descriptors the descriptors
144    */
145   public void initializeCountersFromOxmEntityDescriptors(
146       Map<String, OxmEntityDescriptor> descriptors) {
147
148     if (descriptors == null) {
149       return;
150     }
151
152     OxmEntityDescriptor descriptor = null;
153     for (String key : descriptors.keySet()) {
154       descriptor = descriptors.get(key);
155       activeInventoryEntityStatistics.put(descriptor.getEntityName(), createEntityOpStats());
156     }
157   }
158
159
160   /**
161    * Reset.
162    */
163   public void reset() {
164     initializecreateActiveInventoryEntityStatistics();
165   }
166
167   /**
168    * Gets the result code.
169    *
170    * @param txn the txn
171    * @return the result code
172    */
173   private int getResultCode(NetworkTransaction txn) {
174
175
176     if (txn == null) {
177       return -1;
178     }
179
180     OperationResult or = txn.getOperationResult();
181
182     if (or == null) {
183       return -1;
184     }
185
186     return or.getResultCode();
187
188   }
189
190   /**
191    * Update active inventory entity counters.
192    *
193    * @param txn the txn
194    */
195   private void updateActiveInventoryEntityCounters(NetworkTransaction txn) {
196
197     if (txn == null) {
198       return;
199     }
200
201     Map<String, AtomicInteger> opStats = activeInventoryEntityStatistics.get(txn.getEntityType());
202
203     int rc = getResultCode(txn);
204
205     switch (txn.getOperationType()) {
206
207       case GET: {
208
209         opStats.get(TOTAL).incrementAndGet();
210
211         if (200 <= rc && rc <= 299) {
212           opStats.get(FOUND).incrementAndGet();
213         } else if (rc == 404) {
214           opStats.get(NOT_FOUND).incrementAndGet();
215         } else {
216           opStats.get(ERROR).incrementAndGet();
217         }
218
219         break;
220       }
221
222       default: {
223         // nothing else for now
224       }
225
226     }
227
228     OperationResult or = txn.getOperationResult();
229
230     if (or != null && or.wasSuccessful()) {
231
232       if (or.getResult() == null || or.getResult().length() == 0) {
233         opStats.get(NO_PAYLOAD).incrementAndGet();
234       }
235
236       if (or.getNumRequestRetries() > 0) {
237         opStats.get(NUM_RETRIES).addAndGet(or.getNumRequestRetries());
238       }
239
240     }
241
242
243   }
244
245   /**
246    * Update counters.
247    *
248    * @param txn the txn
249    */
250   public void updateCounters(NetworkTransaction txn) {
251
252     updateActiveInventoryEntityCounters(txn);
253
254   }
255
256   public String getStatisticsReport() {
257
258     StringBuilder sb = new StringBuilder(128);
259
260     /*
261      * sort entities, then sort nested op codes
262      */
263
264     TreeMap<String, HashMap<String, AtomicInteger>> activeInventoryEntitySortedTreeMap =
265         new TreeMap<String, HashMap<String, AtomicInteger>>(new Comparator<String>() {
266
267           @Override
268           public int compare(String o1, String o2) {
269             return o1.toLowerCase().compareTo(o2.toLowerCase());
270           }
271         });
272
273     activeInventoryEntitySortedTreeMap.putAll(activeInventoryEntityStatistics);
274
275     for (String counterEntityKey : activeInventoryEntitySortedTreeMap.keySet()) {
276
277       HashMap<String, AtomicInteger> entityCounters =
278           activeInventoryEntitySortedTreeMap.get(counterEntityKey);
279
280       AtomicInteger total = entityCounters.get(TOTAL);
281       AtomicInteger found = entityCounters.get(FOUND);
282       AtomicInteger noPayload = entityCounters.get(NO_PAYLOAD);
283       AtomicInteger notFound = entityCounters.get(NOT_FOUND);
284       AtomicInteger numRetries = entityCounters.get(NUM_RETRIES);
285       AtomicInteger error = entityCounters.get(ERROR);
286
287       int totalValue = (total == null) ? 0 : total.get();
288       int foundValue = (found == null) ? 0 : found.get();
289       int noPayloadValue = (noPayload == null) ? 0 : noPayload.get();
290       int notFoundValue = (notFound == null) ? 0 : notFound.get();
291       int numRetriesValue = (numRetries == null) ? 0 : numRetries.get();
292       int errorValue = (error == null) ? 0 : error.get();
293
294       sb.append("\n            ")
295           .append(String.format(
296               "%-30s TOTAL: %-12d FOUND: %-12d NO_PAYLOAD:"
297               + " %-12d NOT_FOUND: %-12d NUM_RETRIES: %-12d ERROR: %-12d",
298               counterEntityKey, totalValue, foundValue, noPayloadValue, notFoundValue,
299               numRetriesValue, errorValue));
300     }
301
302     return sb.toString();
303   }
304
305
306
307 }