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