2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.onap.aai.sparky.dal.elasticsearch;
27 import java.util.Comparator;
28 import java.util.HashMap;
31 import java.util.TreeMap;
32 import java.util.concurrent.atomic.AtomicInteger;
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;
40 * The Class ElasticSearchEntityStatistics.
42 public class ElasticSearchEntityStatistics {
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";
51 private Map<String, HashMap<String, AtomicInteger>> entityStatistics;
54 * Creates the entity op stats.
56 * @return the hash map
58 private HashMap<String, AtomicInteger> createEntityOpStats() {
60 HashMap<String, AtomicInteger> opStats = new HashMap<String, AtomicInteger>();
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());
74 * Initializecreate active inventory entity statistics.
76 private void initializecreateActiveInventoryEntityStatistics() {
77 Set<String> keys = entityStatistics.keySet();
79 Set<String> opStatKeySet = null;
80 Map<String, AtomicInteger> opStats = null;
82 for (String k : keys) {
84 opStats = entityStatistics.get(k);
86 opStatKeySet = opStats.keySet();
88 for (String opStatKey : opStatKeySet) {
89 opStats.get(opStatKey).set(0);
95 * Instantiates a new elastic search entity statistics.
97 * @param loader the loader
99 public ElasticSearchEntityStatistics() {
100 entityStatistics = new HashMap<String, HashMap<String, AtomicInteger>>();
105 * Initialize counters from oxm entity descriptors.
107 * @param descriptors the descriptors
109 public void intializeEntityCounters(
110 String... entityTypes) {
112 if (entityTypes != null && entityTypes.length > 0) {
113 for (String entityType : entityTypes) {
114 entityStatistics.put(entityType, createEntityOpStats());
121 public void intializeEntityCounters(
122 Set<String> entityTypes) {
124 if (entityTypes != null && entityTypes.size() > 0) {
125 for (String entityType : entityTypes) {
126 entityStatistics.put(entityType, createEntityOpStats());
135 public void reset() {
136 initializecreateActiveInventoryEntityStatistics();
140 * Gets the result code.
143 * @return the result code
145 private int getResultCode(NetworkTransaction txn) {
152 OperationResult or = txn.getOperationResult();
158 return or.getResultCode();
163 * Update elastic search entity counters.
167 private void updateElasticSearchEntityCounters(NetworkTransaction txn) {
173 Map<String, AtomicInteger> entityOpStats = entityStatistics.get(txn.getEntityType());
175 int resultCode = getResultCode(txn);
177 if (txn.getOperationType() == HttpMethod.PUT) {
179 entityOpStats.get(TOTAL).incrementAndGet();
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();
188 entityOpStats.get(ERROR).incrementAndGet();
191 } else if (txn.getOperationType() == HttpMethod.DELETE) {
193 entityOpStats.get(TOTAL).incrementAndGet();
195 if (200 <= resultCode && resultCode <= 299) {
196 entityOpStats.get(DELETED).incrementAndGet();
198 entityOpStats.get(ERROR).incrementAndGet();
209 public void updateCounters(NetworkTransaction txn) {
211 updateElasticSearchEntityCounters(txn);
215 public String getStatisticsReport() {
217 StringBuilder sb = new StringBuilder(128);
220 * sort entities, then sort nested op codes
223 TreeMap<String, HashMap<String, AtomicInteger>> elasticEntitySortedTreeMap =
224 new TreeMap<String, HashMap<String, AtomicInteger>>(new Comparator<String>() {
227 public int compare(String o1, String o2) {
228 return o1.toLowerCase().compareTo(o2.toLowerCase());
232 elasticEntitySortedTreeMap.putAll(entityStatistics);
234 for (String counterEntityKey : elasticEntitySortedTreeMap.keySet()) {
236 HashMap<String, AtomicInteger> entityCounters =
237 elasticEntitySortedTreeMap.get(counterEntityKey);
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);
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();
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));
260 return sb.toString();