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