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