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.aai;
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;
37 * The Class ActiveInventoryEntityStatistics.
39 public class ActiveInventoryEntityStatistics {
41 private static final String TOTAL = "Total";
43 private static final String FOUND = "Found";
45 private static final String NO_PAYLOAD = "NoPayload";
47 private static final String NOT_FOUND = "NotFound";
49 private static final String NUM_RETRIES = "NumRetries";
51 private static final String ERROR = "Error";
53 private Map<String, HashMap<String, AtomicInteger>> activeInventoryEntityStatistics;
56 * Creates the entity op stats.
58 * @return the hash map
60 private HashMap<String, AtomicInteger> createEntityOpStats() {
62 HashMap<String, AtomicInteger> opStats = new HashMap<String, AtomicInteger>();
64 opStats.put(TOTAL, new AtomicInteger());
65 opStats.put(FOUND, new AtomicInteger());
66 opStats.put(NO_PAYLOAD, new AtomicInteger());
67 opStats.put(NOT_FOUND, new AtomicInteger());
68 opStats.put(NUM_RETRIES, new AtomicInteger());
69 opStats.put(ERROR, new AtomicInteger());
76 * Initializecreate active inventory entity statistics.
78 private void initializecreateActiveInventoryEntityStatistics() {
79 Set<String> keys = activeInventoryEntityStatistics.keySet();
81 Set<String> opStatKeySet = null;
82 Map<String, AtomicInteger> opStats = null;
84 for (String k : keys) {
86 opStats = activeInventoryEntityStatistics.get(k);
88 opStatKeySet = opStats.keySet();
90 for (String opStatKey : opStatKeySet) {
91 opStats.get(opStatKey).set(0);
97 * Instantiates a new active inventory entity statistics.
99 * @param loader the loader
101 public ActiveInventoryEntityStatistics() {
102 activeInventoryEntityStatistics = new HashMap<String, HashMap<String, AtomicInteger>>();
107 * Initialize counters from oxm entity descriptors.
109 * @param descriptors the descriptors
111 public void intializeEntityCounters(
112 String... entityTypes) {
114 if (entityTypes != null && entityTypes.length > 0) {
115 for (String entityType : entityTypes) {
116 activeInventoryEntityStatistics.put(entityType, createEntityOpStats());
123 public void intializeEntityCounters(
124 Set<String> entityTypes) {
126 if (entityTypes != null && entityTypes.size() > 0) {
127 for (String entityType : entityTypes) {
128 activeInventoryEntityStatistics.put(entityType, createEntityOpStats());
139 public void reset() {
140 initializecreateActiveInventoryEntityStatistics();
144 * Gets the result code.
147 * @return the result code
149 private int getResultCode(NetworkTransaction txn) {
156 OperationResult or = txn.getOperationResult();
162 return or.getResultCode();
167 * Update active inventory entity counters.
171 private void updateActiveInventoryEntityCounters(NetworkTransaction txn) {
177 Map<String, AtomicInteger> opStats = activeInventoryEntityStatistics.get(txn.getEntityType());
179 int rc = getResultCode(txn);
181 switch (txn.getOperationType()) {
185 opStats.get(TOTAL).incrementAndGet();
187 if (200 <= rc && rc <= 299) {
188 opStats.get(FOUND).incrementAndGet();
189 } else if (rc == 404) {
190 opStats.get(NOT_FOUND).incrementAndGet();
192 opStats.get(ERROR).incrementAndGet();
199 // nothing else for now
204 OperationResult or = txn.getOperationResult();
206 if (or != null && or.wasSuccessful()) {
208 if (or.getResult() == null || or.getResult().length() == 0) {
209 opStats.get(NO_PAYLOAD).incrementAndGet();
212 if (or.getNumRetries() > 0) {
213 opStats.get(NUM_RETRIES).addAndGet(or.getNumRetries());
226 public void updateCounters(NetworkTransaction txn) {
228 updateActiveInventoryEntityCounters(txn);
232 public String getStatisticsReport() {
234 StringBuilder sb = new StringBuilder(128);
237 * sort entities, then sort nested op codes
240 TreeMap<String, HashMap<String, AtomicInteger>> activeInventoryEntitySortedTreeMap =
241 new TreeMap<String, HashMap<String, AtomicInteger>>(new Comparator<String>() {
244 public int compare(String o1, String o2) {
245 return o1.toLowerCase().compareTo(o2.toLowerCase());
249 activeInventoryEntitySortedTreeMap.putAll(activeInventoryEntityStatistics);
251 for (String counterEntityKey : activeInventoryEntitySortedTreeMap.keySet()) {
253 HashMap<String, AtomicInteger> entityCounters =
254 activeInventoryEntitySortedTreeMap.get(counterEntityKey);
256 AtomicInteger total = entityCounters.get(TOTAL);
257 AtomicInteger found = entityCounters.get(FOUND);
258 AtomicInteger noPayload = entityCounters.get(NO_PAYLOAD);
259 AtomicInteger notFound = entityCounters.get(NOT_FOUND);
260 AtomicInteger numRetries = entityCounters.get(NUM_RETRIES);
261 AtomicInteger error = entityCounters.get(ERROR);
263 int totalValue = (total == null) ? 0 : total.get();
264 int foundValue = (found == null) ? 0 : found.get();
265 int noPayloadValue = (noPayload == null) ? 0 : noPayload.get();
266 int notFoundValue = (notFound == null) ? 0 : notFound.get();
267 int numRetriesValue = (numRetries == null) ? 0 : numRetries.get();
268 int errorValue = (error == null) ? 0 : error.get();
271 .append(String.format(
272 "%-30s TOTAL: %-12d FOUND: %-12d NO_PAYLOAD:"
273 + " %-12d NOT_FOUND: %-12d NUM_RETRIES: %-12d ERROR: %-12d",
274 counterEntityKey, totalValue, foundValue, noPayloadValue, notFoundValue,
275 numRetriesValue, errorValue));
278 return sb.toString();