2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 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 package org.onap.aai.sparky.dal.aai;
23 import java.util.Comparator;
24 import java.util.HashMap;
27 import java.util.TreeMap;
28 import java.util.concurrent.atomic.AtomicInteger;
30 import org.onap.aai.restclient.client.OperationResult;
31 import org.onap.aai.sparky.dal.NetworkTransaction;
35 * The Class ActiveInventoryEntityStatistics.
37 public class ActiveInventoryEntityStatistics {
39 private static final String TOTAL = "Total";
41 private static final String FOUND = "Found";
43 private static final String NO_PAYLOAD = "NoPayload";
45 private static final String NOT_FOUND = "NotFound";
47 private static final String NUM_RETRIES = "NumRetries";
49 private static final String ERROR = "Error";
51 private Map<String, HashMap<String, AtomicInteger>> activeInventoryEntityStatistics;
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(FOUND, new AtomicInteger());
64 opStats.put(NO_PAYLOAD, new AtomicInteger());
65 opStats.put(NOT_FOUND, new AtomicInteger());
66 opStats.put(NUM_RETRIES, new AtomicInteger());
67 opStats.put(ERROR, new AtomicInteger());
74 * Initializecreate active inventory entity statistics.
76 private void initializecreateActiveInventoryEntityStatistics() {
77 Set<String> keys = activeInventoryEntityStatistics.keySet();
79 Set<String> opStatKeySet = null;
80 Map<String, AtomicInteger> opStats = null;
82 for (String k : keys) {
84 opStats = activeInventoryEntityStatistics.get(k);
86 opStatKeySet = opStats.keySet();
88 for (String opStatKey : opStatKeySet) {
89 opStats.get(opStatKey).set(0);
95 * Instantiates a new active inventory entity statistics.
97 * @param loader the loader
99 public ActiveInventoryEntityStatistics() {
100 activeInventoryEntityStatistics = 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 activeInventoryEntityStatistics.put(entityType, createEntityOpStats());
121 public void intializeEntityCounters(
122 Set<String> entityTypes) {
124 if (entityTypes != null && entityTypes.size() > 0) {
125 for (String entityType : entityTypes) {
126 activeInventoryEntityStatistics.put(entityType, createEntityOpStats());
137 public void reset() {
138 initializecreateActiveInventoryEntityStatistics();
142 * Gets the result code.
145 * @return the result code
147 private int getResultCode(NetworkTransaction txn) {
154 OperationResult or = txn.getOperationResult();
160 return or.getResultCode();
165 * Update active inventory entity counters.
169 private void updateActiveInventoryEntityCounters(NetworkTransaction txn) {
175 Map<String, AtomicInteger> opStats = activeInventoryEntityStatistics.get(txn.getEntityType());
177 int rc = getResultCode(txn);
179 switch (txn.getOperationType()) {
183 opStats.get(TOTAL).incrementAndGet();
185 if (200 <= rc && rc <= 299) {
186 opStats.get(FOUND).incrementAndGet();
187 } else if (rc == 404) {
188 opStats.get(NOT_FOUND).incrementAndGet();
190 opStats.get(ERROR).incrementAndGet();
197 // nothing else for now
202 OperationResult or = txn.getOperationResult();
204 if (or != null && or.wasSuccessful()) {
206 if (or.getResult() == null || or.getResult().length() == 0) {
207 opStats.get(NO_PAYLOAD).incrementAndGet();
210 if (or.getNumRetries() > 0) {
211 opStats.get(NUM_RETRIES).addAndGet(or.getNumRetries());
224 public void updateCounters(NetworkTransaction txn) {
226 updateActiveInventoryEntityCounters(txn);
230 public String getStatisticsReport() {
232 StringBuilder sb = new StringBuilder(128);
235 * sort entities, then sort nested op codes
238 TreeMap<String, HashMap<String, AtomicInteger>> activeInventoryEntitySortedTreeMap =
239 new TreeMap<String, HashMap<String, AtomicInteger>>(new Comparator<String>() {
242 public int compare(String o1, String o2) {
243 return o1.toLowerCase().compareTo(o2.toLowerCase());
247 activeInventoryEntitySortedTreeMap.putAll(activeInventoryEntityStatistics);
249 for (String counterEntityKey : activeInventoryEntitySortedTreeMap.keySet()) {
251 HashMap<String, AtomicInteger> entityCounters =
252 activeInventoryEntitySortedTreeMap.get(counterEntityKey);
254 AtomicInteger total = entityCounters.get(TOTAL);
255 AtomicInteger found = entityCounters.get(FOUND);
256 AtomicInteger noPayload = entityCounters.get(NO_PAYLOAD);
257 AtomicInteger notFound = entityCounters.get(NOT_FOUND);
258 AtomicInteger numRetries = entityCounters.get(NUM_RETRIES);
259 AtomicInteger error = entityCounters.get(ERROR);
261 int totalValue = (total == null) ? 0 : total.get();
262 int foundValue = (found == null) ? 0 : found.get();
263 int noPayloadValue = (noPayload == null) ? 0 : noPayload.get();
264 int notFoundValue = (notFound == null) ? 0 : notFound.get();
265 int numRetriesValue = (numRetries == null) ? 0 : numRetries.get();
266 int errorValue = (error == null) ? 0 : error.get();
269 .append(String.format(
270 "%-30s TOTAL: %-12d FOUND: %-12d NO_PAYLOAD:"
271 + " %-12d NOT_FOUND: %-12d NUM_RETRIES: %-12d ERROR: %-12d",
272 counterEntityKey, totalValue, foundValue, noPayloadValue, notFoundValue,
273 numRetriesValue, errorValue));
276 return sb.toString();