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