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.aai;
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;
39 * The Class ActiveInventoryEntityStatistics.
41 public class ActiveInventoryEntityStatistics {
43 private static final String TOTAL = "Total";
45 private static final String FOUND = "Found";
47 private static final String NO_PAYLOAD = "NoPayload";
49 private static final String NOT_FOUND = "NotFound";
51 private static final String NUM_RETRIES = "NumRetries";
53 private static final String ERROR = "Error";
55 private Map<String, HashMap<String, AtomicInteger>> activeInventoryEntityStatistics;
58 * Creates the entity op stats.
60 * @return the hash map
62 private HashMap<String, AtomicInteger> createEntityOpStats() {
64 HashMap<String, AtomicInteger> opStats = new HashMap<String, AtomicInteger>();
66 opStats.put(TOTAL, new AtomicInteger());
67 opStats.put(FOUND, new AtomicInteger());
68 opStats.put(NO_PAYLOAD, new AtomicInteger());
69 opStats.put(NOT_FOUND, new AtomicInteger());
70 opStats.put(NUM_RETRIES, new AtomicInteger());
71 opStats.put(ERROR, new AtomicInteger());
78 * Initializecreate active inventory entity statistics.
80 private void initializecreateActiveInventoryEntityStatistics() {
81 Set<String> keys = activeInventoryEntityStatistics.keySet();
83 Set<String> opStatKeySet = null;
84 Map<String, AtomicInteger> opStats = null;
86 for (String k : keys) {
88 opStats = activeInventoryEntityStatistics.get(k);
90 opStatKeySet = opStats.keySet();
92 for (String opStatKey : opStatKeySet) {
93 opStats.get(opStatKey).set(0);
99 * Instantiates a new active inventory entity statistics.
101 * @param loader the loader
103 public ActiveInventoryEntityStatistics() {
104 activeInventoryEntityStatistics = new HashMap<String, HashMap<String, AtomicInteger>>();
109 * Initialize counters from oxm entity descriptors.
111 * @param descriptors the descriptors
113 public void intializeEntityCounters(
114 String... entityTypes) {
116 if (entityTypes != null && entityTypes.length > 0) {
117 for (String entityType : entityTypes) {
118 activeInventoryEntityStatistics.put(entityType, createEntityOpStats());
125 public void intializeEntityCounters(
126 Set<String> entityTypes) {
128 if (entityTypes != null && entityTypes.size() > 0) {
129 for (String entityType : entityTypes) {
130 activeInventoryEntityStatistics.put(entityType, createEntityOpStats());
141 public void reset() {
142 initializecreateActiveInventoryEntityStatistics();
146 * Gets the result code.
149 * @return the result code
151 private int getResultCode(NetworkTransaction txn) {
158 OperationResult or = txn.getOperationResult();
164 return or.getResultCode();
169 * Update active inventory entity counters.
173 private void updateActiveInventoryEntityCounters(NetworkTransaction txn) {
179 Map<String, AtomicInteger> opStats = activeInventoryEntityStatistics.get(txn.getEntityType());
181 int rc = getResultCode(txn);
183 switch (txn.getOperationType()) {
187 opStats.get(TOTAL).incrementAndGet();
189 if (200 <= rc && rc <= 299) {
190 opStats.get(FOUND).incrementAndGet();
191 } else if (rc == 404) {
192 opStats.get(NOT_FOUND).incrementAndGet();
194 opStats.get(ERROR).incrementAndGet();
201 // nothing else for now
206 OperationResult or = txn.getOperationResult();
208 if (or != null && or.wasSuccessful()) {
210 if (or.getResult() == null || or.getResult().length() == 0) {
211 opStats.get(NO_PAYLOAD).incrementAndGet();
214 if (or.getNumRetries() > 0) {
215 opStats.get(NUM_RETRIES).addAndGet(or.getNumRetries());
228 public void updateCounters(NetworkTransaction txn) {
230 updateActiveInventoryEntityCounters(txn);
234 public String getStatisticsReport() {
236 StringBuilder sb = new StringBuilder(128);
239 * sort entities, then sort nested op codes
242 TreeMap<String, HashMap<String, AtomicInteger>> activeInventoryEntitySortedTreeMap =
243 new TreeMap<String, HashMap<String, AtomicInteger>>(new Comparator<String>() {
246 public int compare(String o1, String o2) {
247 return o1.toLowerCase().compareTo(o2.toLowerCase());
251 activeInventoryEntitySortedTreeMap.putAll(activeInventoryEntityStatistics);
253 for (String counterEntityKey : activeInventoryEntitySortedTreeMap.keySet()) {
255 HashMap<String, AtomicInteger> entityCounters =
256 activeInventoryEntitySortedTreeMap.get(counterEntityKey);
258 AtomicInteger total = entityCounters.get(TOTAL);
259 AtomicInteger found = entityCounters.get(FOUND);
260 AtomicInteger noPayload = entityCounters.get(NO_PAYLOAD);
261 AtomicInteger notFound = entityCounters.get(NOT_FOUND);
262 AtomicInteger numRetries = entityCounters.get(NUM_RETRIES);
263 AtomicInteger error = entityCounters.get(ERROR);
265 int totalValue = (total == null) ? 0 : total.get();
266 int foundValue = (found == null) ? 0 : found.get();
267 int noPayloadValue = (noPayload == null) ? 0 : noPayload.get();
268 int notFoundValue = (notFound == null) ? 0 : notFound.get();
269 int numRetriesValue = (numRetries == null) ? 0 : numRetries.get();
270 int errorValue = (error == null) ? 0 : error.get();
273 .append(String.format(
274 "%-30s TOTAL: %-12d FOUND: %-12d NO_PAYLOAD:"
275 + " %-12d NOT_FOUND: %-12d NUM_RETRIES: %-12d ERROR: %-12d",
276 counterEntityKey, totalValue, foundValue, noPayloadValue, notFoundValue,
277 numRetriesValue, errorValue));
280 return sb.toString();