Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / dal / elasticsearch / ElasticSearchEntityStatistics.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.elasticsearch;
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.HttpMethod;
39 import org.openecomp.sparky.dal.rest.OperationResult;
40
41 /**
42  * The Class ElasticSearchEntityStatistics.
43  */
44 public class ElasticSearchEntityStatistics {
45
46   private static final String TOTAL = "Total";
47   private static final String CREATED = "Created";
48   private static final String MODIFIED = "Modified";
49   private static final String OTHERSUCCESS = "OTHERSUCCESS";
50   private static final String DELETED = "DELETED";
51   private static final String ERROR = "ERROR";
52
53   private Map<String, HashMap<String, AtomicInteger>> entityStatistics;
54   private OxmModelLoader loader;
55
56   /**
57    * Creates the entity op stats.
58    *
59    * @return the hash map
60    */
61   private HashMap<String, AtomicInteger> createEntityOpStats() {
62
63     HashMap<String, AtomicInteger> opStats = new HashMap<String, AtomicInteger>();
64
65     opStats.put(TOTAL, new AtomicInteger());
66     opStats.put(CREATED, new AtomicInteger());
67     opStats.put(MODIFIED, new AtomicInteger());
68     opStats.put(OTHERSUCCESS, new AtomicInteger());
69     opStats.put(DELETED, new AtomicInteger());
70     opStats.put(ERROR, new AtomicInteger());
71
72     return opStats;
73
74   }
75
76   /*
77    * private void createActiveInventoryEntityStatistics() {
78    * 
79    * Map<String,OxmEntityDescriptor> descriptors = loader.getSearchableEntityDescriptors();
80    * 
81    * if(descriptors == null) { return; }
82    * 
83    * OxmEntityDescriptor d = null; for ( String key : descriptors.keySet() ) { d =
84    * descriptors.get(key); entityStatistics.put(d.getEntityName(), createEntityOpStats()); }
85    * 
86    * }
87    */
88
89   /**
90    * Initializecreate active inventory entity statistics.
91    */
92   private void initializecreateActiveInventoryEntityStatistics() {
93     Set<String> keys = entityStatistics.keySet();
94
95     Set<String> opStatKeySet = null;
96     Map<String, AtomicInteger> opStats = null;
97
98     for (String k : keys) {
99
100       opStats = entityStatistics.get(k);
101
102       opStatKeySet = opStats.keySet();
103
104       for (String opStatKey : opStatKeySet) {
105         opStats.get(opStatKey).set(0);
106       }
107     }
108   }
109
110   /**
111    * Instantiates a new elastic search entity statistics.
112    *
113    * @param loader the loader
114    */
115   public ElasticSearchEntityStatistics(OxmModelLoader loader) {
116     this.loader = loader;
117     entityStatistics = new HashMap<String, HashMap<String, AtomicInteger>>();
118     // createActiveInventoryEntityStatistics();
119     reset();
120   }
121
122   /**
123    * Initialize counters from oxm entity descriptors.
124    *
125    * @param descriptors the descriptors
126    */
127   public void initializeCountersFromOxmEntityDescriptors(
128       Map<String, OxmEntityDescriptor> descriptors) {
129
130     if (descriptors == null) {
131       return;
132     }
133
134     OxmEntityDescriptor descriptor = null;
135     for (String key : descriptors.keySet()) {
136       descriptor = descriptors.get(key);
137       entityStatistics.put(descriptor.getEntityName(), createEntityOpStats());
138     }
139   }
140
141   /**
142    * Reset.
143    */
144   public void reset() {
145     initializecreateActiveInventoryEntityStatistics();
146   }
147
148   /**
149    * Gets the result code.
150    *
151    * @param txn the txn
152    * @return the result code
153    */
154   private int getResultCode(NetworkTransaction txn) {
155
156
157     if (txn == null) {
158       return -1;
159     }
160
161     OperationResult or = txn.getOperationResult();
162
163     if (or == null) {
164       return -1;
165     }
166
167     return or.getResultCode();
168
169   }
170
171   /**
172    * Update elastic search entity counters.
173    *
174    * @param txn the txn
175    */
176   private void updateElasticSearchEntityCounters(NetworkTransaction txn) {
177
178     if (txn == null) {
179       return;
180     }
181
182     Map<String, AtomicInteger> entityOpStats = entityStatistics.get(txn.getEntityType());
183
184     int resultCode = getResultCode(txn);
185
186     if (txn.getOperationType() == HttpMethod.PUT) {
187
188       entityOpStats.get(TOTAL).incrementAndGet();
189
190       if (resultCode == 201) {
191         entityOpStats.get(CREATED).incrementAndGet();
192       } else if (resultCode == 200) {
193         entityOpStats.get(MODIFIED).incrementAndGet();
194       } else if (202 <= resultCode && resultCode <= 299) {
195         entityOpStats.get(OTHERSUCCESS).incrementAndGet();
196       } else {
197         entityOpStats.get(ERROR).incrementAndGet();
198       }
199
200     } else if (txn.getOperationType() == HttpMethod.DELETE) {
201
202       entityOpStats.get(TOTAL).incrementAndGet();
203
204       if (200 <= resultCode && resultCode <= 299) {
205         entityOpStats.get(DELETED).incrementAndGet();
206       } else {
207         entityOpStats.get(ERROR).incrementAndGet();
208       }
209     }
210
211   }
212
213   /**
214    * Update counters.
215    *
216    * @param txn the txn
217    */
218   public void updateCounters(NetworkTransaction txn) {
219
220     updateElasticSearchEntityCounters(txn);
221
222   }
223
224   public String getStatisticsReport() {
225
226     StringBuilder sb = new StringBuilder(128);
227
228     /*
229      * sort entities, then sort nested op codes
230      */
231
232     TreeMap<String, HashMap<String, AtomicInteger>> elasticEntitySortedTreeMap =
233         new TreeMap<String, HashMap<String, AtomicInteger>>(new Comparator<String>() {
234
235           @Override
236           public int compare(String o1, String o2) {
237             return o1.toLowerCase().compareTo(o2.toLowerCase());
238           }
239         });
240
241     elasticEntitySortedTreeMap.putAll(entityStatistics);
242
243     for (String counterEntityKey : elasticEntitySortedTreeMap.keySet()) {
244
245       HashMap<String, AtomicInteger> entityCounters =
246           elasticEntitySortedTreeMap.get(counterEntityKey);
247
248       AtomicInteger total = entityCounters.get(TOTAL);
249       AtomicInteger created = entityCounters.get(CREATED);
250       AtomicInteger modified = entityCounters.get(MODIFIED);
251       AtomicInteger otherSuccess = entityCounters.get(OTHERSUCCESS);
252       AtomicInteger deleted = entityCounters.get(DELETED);
253       AtomicInteger error = entityCounters.get(ERROR);
254
255       int totalValue = (total == null) ? 0 : total.get();
256       int createdValue = (created == null) ? 0 : created.get();
257       int modifiedValue = (modified == null) ? 0 : modified.get();
258       int otherSuccessValue = (otherSuccess == null) ? 0 : otherSuccess.get();
259       int deletedValue = (deleted == null) ? 0 : deleted.get();
260       int errorValue = (error == null) ? 0 : error.get();
261
262       sb.append("\n            ")
263           .append(String.format(
264               "%-30s TOTAL: %-12d CREATED: %-12d MODIFIED:"
265               + " %-12d OTHER_2XX: %-12d DELETED: %-12d ERROR: %-12d",
266               counterEntityKey, totalValue, createdValue, modifiedValue, otherSuccessValue,
267               deletedValue, errorValue));
268     }
269     return sb.toString();
270   }
271
272
273
274 }