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