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.
26 package org.openecomp.sparky.dal.elasticsearch;
28 import java.util.Comparator;
29 import java.util.HashMap;
32 import java.util.TreeMap;
33 import java.util.concurrent.atomic.AtomicInteger;
35 import org.openecomp.sparky.config.oxm.OxmEntityDescriptor;
36 import org.openecomp.sparky.config.oxm.OxmModelLoader;
37 import org.openecomp.sparky.dal.NetworkTransaction;
38 import org.openecomp.sparky.dal.rest.HttpMethod;
39 import org.openecomp.sparky.dal.rest.OperationResult;
42 * The Class ElasticSearchEntityStatistics.
44 public class ElasticSearchEntityStatistics {
46 private static final String TOTAL = "Total";
47 private static final String CREATED = "Created";
48 private static final String MODIFIED = "Modified";
49 private static final String OTHERSUCCESS = "OTHERSUCCESS";
50 private static final String DELETED = "DELETED";
51 private static final String ERROR = "ERROR";
53 private Map<String, HashMap<String, AtomicInteger>> entityStatistics;
54 private OxmModelLoader loader;
57 * Creates the entity op stats.
59 * @return the hash map
61 private HashMap<String, AtomicInteger> createEntityOpStats() {
63 HashMap<String, AtomicInteger> opStats = new HashMap<String, AtomicInteger>();
65 opStats.put(TOTAL, new AtomicInteger());
66 opStats.put(CREATED, new AtomicInteger());
67 opStats.put(MODIFIED, new AtomicInteger());
68 opStats.put(OTHERSUCCESS, new AtomicInteger());
69 opStats.put(DELETED, new AtomicInteger());
70 opStats.put(ERROR, new AtomicInteger());
77 * private void createActiveInventoryEntityStatistics() {
79 * Map<String,OxmEntityDescriptor> descriptors = loader.getSearchableEntityDescriptors();
81 * if(descriptors == null) { return; }
83 * OxmEntityDescriptor d = null; for ( String key : descriptors.keySet() ) { d =
84 * descriptors.get(key); entityStatistics.put(d.getEntityName(), createEntityOpStats()); }
90 * Initializecreate active inventory entity statistics.
92 private void initializecreateActiveInventoryEntityStatistics() {
93 Set<String> keys = entityStatistics.keySet();
95 Set<String> opStatKeySet = null;
96 Map<String, AtomicInteger> opStats = null;
98 for (String k : keys) {
100 opStats = entityStatistics.get(k);
102 opStatKeySet = opStats.keySet();
104 for (String opStatKey : opStatKeySet) {
105 opStats.get(opStatKey).set(0);
111 * Instantiates a new elastic search entity statistics.
113 * @param loader the loader
115 public ElasticSearchEntityStatistics(OxmModelLoader loader) {
116 this.loader = loader;
117 entityStatistics = new HashMap<String, HashMap<String, AtomicInteger>>();
118 // createActiveInventoryEntityStatistics();
123 * Initialize counters from oxm entity descriptors.
125 * @param descriptors the descriptors
127 public void initializeCountersFromOxmEntityDescriptors(
128 Map<String, OxmEntityDescriptor> descriptors) {
130 if (descriptors == null) {
134 OxmEntityDescriptor descriptor = null;
135 for (String key : descriptors.keySet()) {
136 descriptor = descriptors.get(key);
137 entityStatistics.put(descriptor.getEntityName(), createEntityOpStats());
144 public void reset() {
145 initializecreateActiveInventoryEntityStatistics();
149 * Gets the result code.
152 * @return the result code
154 private int getResultCode(NetworkTransaction txn) {
161 OperationResult or = txn.getOperationResult();
167 return or.getResultCode();
172 * Update elastic search entity counters.
176 private void updateElasticSearchEntityCounters(NetworkTransaction txn) {
182 Map<String, AtomicInteger> entityOpStats = entityStatistics.get(txn.getEntityType());
184 int resultCode = getResultCode(txn);
186 if (txn.getOperationType() == HttpMethod.PUT) {
188 entityOpStats.get(TOTAL).incrementAndGet();
190 if (resultCode == 201) {
191 entityOpStats.get(CREATED).incrementAndGet();
192 } else if (resultCode == 200) {
193 entityOpStats.get(MODIFIED).incrementAndGet();
194 } else if (202 <= resultCode && resultCode <= 299) {
195 entityOpStats.get(OTHERSUCCESS).incrementAndGet();
197 entityOpStats.get(ERROR).incrementAndGet();
200 } else if (txn.getOperationType() == HttpMethod.DELETE) {
202 entityOpStats.get(TOTAL).incrementAndGet();
204 if (200 <= resultCode && resultCode <= 299) {
205 entityOpStats.get(DELETED).incrementAndGet();
207 entityOpStats.get(ERROR).incrementAndGet();
218 public void updateCounters(NetworkTransaction txn) {
220 updateElasticSearchEntityCounters(txn);
224 public String getStatisticsReport() {
226 StringBuilder sb = new StringBuilder(128);
229 * sort entities, then sort nested op codes
232 TreeMap<String, HashMap<String, AtomicInteger>> elasticEntitySortedTreeMap =
233 new TreeMap<String, HashMap<String, AtomicInteger>>(new Comparator<String>() {
236 public int compare(String o1, String o2) {
237 return o1.toLowerCase().compareTo(o2.toLowerCase());
241 elasticEntitySortedTreeMap.putAll(entityStatistics);
243 for (String counterEntityKey : elasticEntitySortedTreeMap.keySet()) {
245 HashMap<String, AtomicInteger> entityCounters =
246 elasticEntitySortedTreeMap.get(counterEntityKey);
248 AtomicInteger total = entityCounters.get(TOTAL);
249 AtomicInteger created = entityCounters.get(CREATED);
250 AtomicInteger modified = entityCounters.get(MODIFIED);
251 AtomicInteger otherSuccess = entityCounters.get(OTHERSUCCESS);
252 AtomicInteger deleted = entityCounters.get(DELETED);
253 AtomicInteger error = entityCounters.get(ERROR);
255 int totalValue = (total == null) ? 0 : total.get();
256 int createdValue = (created == null) ? 0 : created.get();
257 int modifiedValue = (modified == null) ? 0 : modified.get();
258 int otherSuccessValue = (otherSuccess == null) ? 0 : otherSuccess.get();
259 int deletedValue = (deleted == null) ? 0 : deleted.get();
260 int errorValue = (error == null) ? 0 : error.get();
263 .append(String.format(
264 "%-30s TOTAL: %-12d CREATED: %-12d MODIFIED:"
265 + " %-12d OTHER_2XX: %-12d DELETED: %-12d ERROR: %-12d",
266 counterEntityKey, totalValue, createdValue, modifiedValue, otherSuccessValue,
267 deletedValue, errorValue));
269 return sb.toString();