2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.dal.elasticsearch;
25 import java.util.Comparator;
26 import java.util.HashMap;
29 import java.util.TreeMap;
30 import java.util.concurrent.atomic.AtomicInteger;
32 import org.onap.aai.restclient.client.OperationResult;
33 import org.onap.aai.sparky.dal.NetworkTransaction;
34 import org.onap.aai.sparky.dal.rest.HttpMethod;
38 * The Class ElasticSearchEntityStatistics.
40 public class ElasticSearchEntityStatistics {
42 private static final String TOTAL = "Total";
43 private static final String CREATED = "Created";
44 private static final String MODIFIED = "Modified";
45 private static final String OTHERSUCCESS = "OTHERSUCCESS";
46 private static final String DELETED = "DELETED";
47 private static final String ERROR = "ERROR";
49 private Map<String, HashMap<String, AtomicInteger>> entityStatistics;
52 * Creates the entity op stats.
54 * @return the hash map
56 private HashMap<String, AtomicInteger> createEntityOpStats() {
58 HashMap<String, AtomicInteger> opStats = new HashMap<String, AtomicInteger>();
60 opStats.put(TOTAL, new AtomicInteger());
61 opStats.put(CREATED, new AtomicInteger());
62 opStats.put(MODIFIED, new AtomicInteger());
63 opStats.put(OTHERSUCCESS, new AtomicInteger());
64 opStats.put(DELETED, new AtomicInteger());
65 opStats.put(ERROR, new AtomicInteger());
72 * Initializecreate active inventory entity statistics.
74 private void initializecreateActiveInventoryEntityStatistics() {
75 Set<String> keys = entityStatistics.keySet();
77 Set<String> opStatKeySet = null;
78 Map<String, AtomicInteger> opStats = null;
80 for (String k : keys) {
82 opStats = entityStatistics.get(k);
84 opStatKeySet = opStats.keySet();
86 for (String opStatKey : opStatKeySet) {
87 opStats.get(opStatKey).set(0);
93 * Instantiates a new elastic search entity statistics.
95 * @param loader the loader
97 public ElasticSearchEntityStatistics() {
98 entityStatistics = new HashMap<String, HashMap<String, AtomicInteger>>();
103 * Initialize counters from oxm entity descriptors.
105 * @param descriptors the descriptors
107 public void intializeEntityCounters(String... entityTypes) {
109 if (entityTypes != null && entityTypes.length > 0) {
110 for (String entityType : entityTypes) {
111 entityStatistics.put(entityType, createEntityOpStats());
118 public void intializeEntityCounters(Set<String> entityTypes) {
120 if (entityTypes != null && entityTypes.size() > 0) {
121 for (String entityType : entityTypes) {
122 entityStatistics.put(entityType, createEntityOpStats());
131 public void reset() {
132 initializecreateActiveInventoryEntityStatistics();
136 * Gets the result code.
139 * @return the result code
141 private int getResultCode(NetworkTransaction txn) {
148 OperationResult or = txn.getOperationResult();
154 return or.getResultCode();
159 * Update elastic search entity counters.
163 private void updateElasticSearchEntityCounters(NetworkTransaction txn) {
169 Map<String, AtomicInteger> entityOpStats = entityStatistics.get(txn.getEntityType());
171 int resultCode = getResultCode(txn);
173 if (txn.getOperationType() == HttpMethod.PUT) {
175 entityOpStats.get(TOTAL).incrementAndGet();
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();
184 entityOpStats.get(ERROR).incrementAndGet();
187 } else if (txn.getOperationType() == HttpMethod.DELETE) {
189 entityOpStats.get(TOTAL).incrementAndGet();
191 if (200 <= resultCode && resultCode <= 299) {
192 entityOpStats.get(DELETED).incrementAndGet();
194 entityOpStats.get(ERROR).incrementAndGet();
205 public void updateCounters(NetworkTransaction txn) {
207 updateElasticSearchEntityCounters(txn);
211 public String getStatisticsReport() {
213 StringBuilder sb = new StringBuilder(128);
216 * sort entities, then sort nested op codes
219 TreeMap<String, HashMap<String, AtomicInteger>> elasticEntitySortedTreeMap =
220 new TreeMap<String, HashMap<String, AtomicInteger>>(new Comparator<String>() {
223 public int compare(String o1, String o2) {
224 return o1.toLowerCase().compareTo(o2.toLowerCase());
228 elasticEntitySortedTreeMap.putAll(entityStatistics);
230 for (String counterEntityKey : elasticEntitySortedTreeMap.keySet()) {
232 HashMap<String, AtomicInteger> entityCounters =
233 elasticEntitySortedTreeMap.get(counterEntityKey);
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);
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();
249 sb.append("\n ").append(String.format(
250 "%-30s TOTAL: %-12d CREATED: %-12d MODIFIED:"
251 + " %-12d OTHER_2XX: %-12d DELETED: %-12d ERROR: %-12d",
252 counterEntityKey, totalValue, createdValue, modifiedValue, otherSuccessValue,
253 deletedValue, errorValue));
255 return sb.toString();