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.aai;
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.OperationResult;
41 * The Class ActiveInventoryEntityStatistics.
43 public class ActiveInventoryEntityStatistics {
45 private static final String TOTAL = "Total";
47 private static final String FOUND = "Found";
49 private static final String NO_PAYLOAD = "NoPayload";
51 private static final String NOT_FOUND = "NotFound";
53 private static final String NUM_RETRIES = "NumRetries";
55 private static final String ERROR = "Error";
57 private OxmModelLoader loader;
60 private Map<String, HashMap<String, AtomicInteger>> activeInventoryEntityStatistics;
63 * Creates the entity op stats.
65 * @return the hash map
67 private HashMap<String, AtomicInteger> createEntityOpStats() {
69 HashMap<String, AtomicInteger> opStats = new HashMap<String, AtomicInteger>();
71 opStats.put(TOTAL, new AtomicInteger());
72 opStats.put(FOUND, new AtomicInteger());
73 opStats.put(NO_PAYLOAD, new AtomicInteger());
74 opStats.put(NOT_FOUND, new AtomicInteger());
75 opStats.put(NUM_RETRIES, new AtomicInteger());
76 opStats.put(ERROR, new AtomicInteger());
83 * private void createSearchableActiveInventoryEntityStatistics() {
85 * Map<String,OxmEntityDescriptor> descriptors = loader.getSearchableEntityDescriptors();
87 * if(descriptors == null) { return; }
89 * OxmEntityDescriptor d = null; for ( String key : descriptors.keySet() ) { d =
90 * descriptors.get(key); activeInventoryEntityStatistics.put(d.getEntityName(),
91 * createEntityOpStats()); }
97 * private void createCrossEntityReferenceActiveInventoryEntityStatistics() {
99 * Map<String,OxmEntityDescriptor> descriptors = loader.getCrossReferenceEntityDescriptors();
107 * Initializecreate active inventory entity statistics.
109 private void initializecreateActiveInventoryEntityStatistics() {
110 Set<String> keys = activeInventoryEntityStatistics.keySet();
112 Set<String> opStatKeySet = null;
113 Map<String, AtomicInteger> opStats = null;
115 for (String k : keys) {
117 opStats = activeInventoryEntityStatistics.get(k);
119 opStatKeySet = opStats.keySet();
121 for (String opStatKey : opStatKeySet) {
122 opStats.get(opStatKey).set(0);
128 * Instantiates a new active inventory entity statistics.
130 * @param loader the loader
132 public ActiveInventoryEntityStatistics(OxmModelLoader loader) {
133 this.loader = loader;
134 activeInventoryEntityStatistics = new HashMap<String, HashMap<String, AtomicInteger>>();
135 // createSearchableActiveInventoryEntityStatistics();
136 // createCrossEntityReferenceActiveInventoryEntityStatistics();
141 * Initialize counters from oxm entity descriptors.
143 * @param descriptors the descriptors
145 public void initializeCountersFromOxmEntityDescriptors(
146 Map<String, OxmEntityDescriptor> descriptors) {
148 if (descriptors == null) {
152 OxmEntityDescriptor descriptor = null;
153 for (String key : descriptors.keySet()) {
154 descriptor = descriptors.get(key);
155 activeInventoryEntityStatistics.put(descriptor.getEntityName(), createEntityOpStats());
163 public void reset() {
164 initializecreateActiveInventoryEntityStatistics();
168 * Gets the result code.
171 * @return the result code
173 private int getResultCode(NetworkTransaction txn) {
180 OperationResult or = txn.getOperationResult();
186 return or.getResultCode();
191 * Update active inventory entity counters.
195 private void updateActiveInventoryEntityCounters(NetworkTransaction txn) {
201 Map<String, AtomicInteger> opStats = activeInventoryEntityStatistics.get(txn.getEntityType());
203 int rc = getResultCode(txn);
205 switch (txn.getOperationType()) {
209 opStats.get(TOTAL).incrementAndGet();
211 if (200 <= rc && rc <= 299) {
212 opStats.get(FOUND).incrementAndGet();
213 } else if (rc == 404) {
214 opStats.get(NOT_FOUND).incrementAndGet();
216 opStats.get(ERROR).incrementAndGet();
223 // nothing else for now
228 OperationResult or = txn.getOperationResult();
230 if (or != null && or.wasSuccessful()) {
232 if (or.getResult() == null || or.getResult().length() == 0) {
233 opStats.get(NO_PAYLOAD).incrementAndGet();
236 if (or.getNumRequestRetries() > 0) {
237 opStats.get(NUM_RETRIES).addAndGet(or.getNumRequestRetries());
250 public void updateCounters(NetworkTransaction txn) {
252 updateActiveInventoryEntityCounters(txn);
256 public String getStatisticsReport() {
258 StringBuilder sb = new StringBuilder(128);
261 * sort entities, then sort nested op codes
264 TreeMap<String, HashMap<String, AtomicInteger>> activeInventoryEntitySortedTreeMap =
265 new TreeMap<String, HashMap<String, AtomicInteger>>(new Comparator<String>() {
268 public int compare(String o1, String o2) {
269 return o1.toLowerCase().compareTo(o2.toLowerCase());
273 activeInventoryEntitySortedTreeMap.putAll(activeInventoryEntityStatistics);
275 for (String counterEntityKey : activeInventoryEntitySortedTreeMap.keySet()) {
277 HashMap<String, AtomicInteger> entityCounters =
278 activeInventoryEntitySortedTreeMap.get(counterEntityKey);
280 AtomicInteger total = entityCounters.get(TOTAL);
281 AtomicInteger found = entityCounters.get(FOUND);
282 AtomicInteger noPayload = entityCounters.get(NO_PAYLOAD);
283 AtomicInteger notFound = entityCounters.get(NOT_FOUND);
284 AtomicInteger numRetries = entityCounters.get(NUM_RETRIES);
285 AtomicInteger error = entityCounters.get(ERROR);
287 int totalValue = (total == null) ? 0 : total.get();
288 int foundValue = (found == null) ? 0 : found.get();
289 int noPayloadValue = (noPayload == null) ? 0 : noPayload.get();
290 int notFoundValue = (notFound == null) ? 0 : notFound.get();
291 int numRetriesValue = (numRetries == null) ? 0 : numRetries.get();
292 int errorValue = (error == null) ? 0 : error.get();
295 .append(String.format(
296 "%-30s TOTAL: %-12d FOUND: %-12d NO_PAYLOAD:"
297 + " %-12d NOT_FOUND: %-12d NUM_RETRIES: %-12d ERROR: %-12d",
298 counterEntityKey, totalValue, foundValue, noPayloadValue, notFoundValue,
299 numRetriesValue, errorValue));
302 return sb.toString();