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