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.openecomp.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.openecomp.sparky.config.oxm.OxmEntityDescriptor;
33 import org.openecomp.sparky.config.oxm.OxmModelLoader;
34 import org.openecomp.sparky.dal.NetworkTransaction;
35 import org.openecomp.sparky.dal.rest.OperationResult;
38 * The Class ActiveInventoryEntityStatistics.
40 public class ActiveInventoryEntityStatistics {
42 private static final String TOTAL = "Total";
44 private static final String FOUND = "Found";
46 private static final String NO_PAYLOAD = "NoPayload";
48 private static final String NOT_FOUND = "NotFound";
50 private static final String NUM_RETRIES = "NumRetries";
52 private static final String ERROR = "Error";
54 private OxmModelLoader loader;
57 private Map<String, HashMap<String, AtomicInteger>> activeInventoryEntityStatistics;
60 * Creates the entity op stats.
62 * @return the hash map
64 private HashMap<String, AtomicInteger> createEntityOpStats() {
66 HashMap<String, AtomicInteger> opStats = new HashMap<String, AtomicInteger>();
68 opStats.put(TOTAL, new AtomicInteger());
69 opStats.put(FOUND, new AtomicInteger());
70 opStats.put(NO_PAYLOAD, new AtomicInteger());
71 opStats.put(NOT_FOUND, new AtomicInteger());
72 opStats.put(NUM_RETRIES, new AtomicInteger());
73 opStats.put(ERROR, new AtomicInteger());
80 * private void createSearchableActiveInventoryEntityStatistics() {
82 * Map<String,OxmEntityDescriptor> descriptors = loader.getSearchableEntityDescriptors();
84 * if(descriptors == null) { return; }
86 * OxmEntityDescriptor d = null; for ( String key : descriptors.keySet() ) { d =
87 * descriptors.get(key); activeInventoryEntityStatistics.put(d.getEntityName(),
88 * createEntityOpStats()); }
94 * private void createCrossEntityReferenceActiveInventoryEntityStatistics() {
96 * Map<String,OxmEntityDescriptor> descriptors = loader.getCrossReferenceEntityDescriptors();
104 * Initializecreate active inventory entity statistics.
106 private void initializecreateActiveInventoryEntityStatistics() {
107 Set<String> keys = activeInventoryEntityStatistics.keySet();
109 Set<String> opStatKeySet = null;
110 Map<String, AtomicInteger> opStats = null;
112 for (String k : keys) {
114 opStats = activeInventoryEntityStatistics.get(k);
116 opStatKeySet = opStats.keySet();
118 for (String opStatKey : opStatKeySet) {
119 opStats.get(opStatKey).set(0);
125 * Instantiates a new active inventory entity statistics.
127 * @param loader the loader
129 public ActiveInventoryEntityStatistics(OxmModelLoader loader) {
130 this.loader = loader;
131 activeInventoryEntityStatistics = new HashMap<String, HashMap<String, AtomicInteger>>();
132 // createSearchableActiveInventoryEntityStatistics();
133 // createCrossEntityReferenceActiveInventoryEntityStatistics();
138 * Initialize counters from oxm entity descriptors.
140 * @param descriptors the descriptors
142 public void initializeCountersFromOxmEntityDescriptors(
143 Map<String, OxmEntityDescriptor> descriptors) {
145 if (descriptors == null) {
149 OxmEntityDescriptor descriptor = null;
150 for (String key : descriptors.keySet()) {
151 descriptor = descriptors.get(key);
152 activeInventoryEntityStatistics.put(descriptor.getEntityName(), createEntityOpStats());
160 public void reset() {
161 initializecreateActiveInventoryEntityStatistics();
165 * Gets the result code.
168 * @return the result code
170 private int getResultCode(NetworkTransaction txn) {
177 OperationResult or = txn.getOperationResult();
183 return or.getResultCode();
188 * Update active inventory entity counters.
192 private void updateActiveInventoryEntityCounters(NetworkTransaction txn) {
198 Map<String, AtomicInteger> opStats = activeInventoryEntityStatistics.get(txn.getEntityType());
200 int rc = getResultCode(txn);
202 switch (txn.getOperationType()) {
206 opStats.get(TOTAL).incrementAndGet();
208 if (200 <= rc && rc <= 299) {
209 opStats.get(FOUND).incrementAndGet();
210 } else if (rc == 404) {
211 opStats.get(NOT_FOUND).incrementAndGet();
213 opStats.get(ERROR).incrementAndGet();
220 // nothing else for now
225 OperationResult or = txn.getOperationResult();
227 if (or != null && or.wasSuccessful()) {
229 if (or.getResult() == null || or.getResult().length() == 0) {
230 opStats.get(NO_PAYLOAD).incrementAndGet();
233 if (or.getNumRequestRetries() > 0) {
234 opStats.get(NUM_RETRIES).addAndGet(or.getNumRequestRetries());
247 public void updateCounters(NetworkTransaction txn) {
249 updateActiveInventoryEntityCounters(txn);
253 public String getStatisticsReport() {
255 StringBuilder sb = new StringBuilder(128);
258 * sort entities, then sort nested op codes
261 TreeMap<String, HashMap<String, AtomicInteger>> activeInventoryEntitySortedTreeMap =
262 new TreeMap<String, HashMap<String, AtomicInteger>>(new Comparator<String>() {
265 public int compare(String o1, String o2) {
266 return o1.toLowerCase().compareTo(o2.toLowerCase());
270 activeInventoryEntitySortedTreeMap.putAll(activeInventoryEntityStatistics);
272 for (String counterEntityKey : activeInventoryEntitySortedTreeMap.keySet()) {
274 HashMap<String, AtomicInteger> entityCounters =
275 activeInventoryEntitySortedTreeMap.get(counterEntityKey);
277 AtomicInteger total = entityCounters.get(TOTAL);
278 AtomicInteger found = entityCounters.get(FOUND);
279 AtomicInteger noPayload = entityCounters.get(NO_PAYLOAD);
280 AtomicInteger notFound = entityCounters.get(NOT_FOUND);
281 AtomicInteger numRetries = entityCounters.get(NUM_RETRIES);
282 AtomicInteger error = entityCounters.get(ERROR);
284 int totalValue = (total == null) ? 0 : total.get();
285 int foundValue = (found == null) ? 0 : found.get();
286 int noPayloadValue = (noPayload == null) ? 0 : noPayload.get();
287 int notFoundValue = (notFound == null) ? 0 : notFound.get();
288 int numRetriesValue = (numRetries == null) ? 0 : numRetries.get();
289 int errorValue = (error == null) ? 0 : error.get();
292 .append(String.format(
293 "%-30s TOTAL: %-12d FOUND: %-12d NO_PAYLOAD:"
294 + " %-12d NOT_FOUND: %-12d NUM_RETRIES: %-12d ERROR: %-12d",
295 counterEntityKey, totalValue, foundValue, noPayloadValue, notFoundValue,
296 numRetriesValue, errorValue));
299 return sb.toString();