329d0f00d0d2d6f695d0288de54aa50654b28346
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / dal / aai / ActiveInventoryProcessingExceptionStatistics.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.dal.aai;
24
25 import org.onap.aai.cl.api.Logger;
26 import org.onap.aai.cl.eelf.LoggerFactory;
27 import org.onap.aai.restclient.client.OperationResult;
28 import org.onap.aai.sparky.analytics.AbstractStatistics;
29 import org.onap.aai.sparky.dal.ActiveInventoryAdapter;
30 import org.onap.aai.sparky.dal.NetworkTransaction;
31 import org.onap.aai.sparky.logging.AaiUiMsgs;
32
33 /**
34  * The Class ActiveInventoryProcessingExceptionStatistics.
35  */
36 public class ActiveInventoryProcessingExceptionStatistics extends AbstractStatistics {
37
38   private static final Logger LOG =
39       LoggerFactory.getInstance().getLogger(ActiveInventoryAdapter.class);
40
41   private static final String NATIVE_SOCKET_CONNECT_EXCEPTION = "NativeSocketConnectException";
42   private static final String NATIVE_SOCKET_CONNECTION_RESET = "NativeSocketConnectionReset";
43   private static final String NATIVE_SOCKET_CONNECTION_REFUSED = "NativeSocketConnectionRefused";
44   private static final String CLIENT_TIMEOUT_EXCEPTION = "JerseyClientTimoutException";
45   private static final String UNKNOWN_EXCEPTION = "UnknownException";
46
47   /**
48    * Creates the counters.
49    */
50   private void createCounters() {
51     addCounter(NATIVE_SOCKET_CONNECT_EXCEPTION);
52     addCounter(NATIVE_SOCKET_CONNECTION_RESET);
53     addCounter(NATIVE_SOCKET_CONNECTION_REFUSED);
54     addCounter(CLIENT_TIMEOUT_EXCEPTION);
55     addCounter(UNKNOWN_EXCEPTION);
56   }
57
58   /**
59    * Instantiates a new active inventory processing exception statistics.
60    */
61   public ActiveInventoryProcessingExceptionStatistics() {
62     createCounters();
63     reset();
64   }
65
66   /**
67    * Update counters.
68    *
69    * @param txn the txn
70    */
71   public void updateCounters(NetworkTransaction txn) {
72
73     if (txn == null) {
74       return;
75     }
76
77     OperationResult or = txn.getOperationResult();
78
79     if (or != null && !or.wasSuccessful()) {
80
81       if (or.getResultCode() != 404) {
82
83         String result = or.getResult();
84
85         if (result != null) {
86
87           /*
88            * Try to classify exceptions and peg counters
89            */
90
91           if (result.contains("java.net.SocketTimeoutException: connect timed out")) {
92             pegCounter(CLIENT_TIMEOUT_EXCEPTION);
93           } else if (result.contains("java.net.ConnectException: Connection timed out: connect")) {
94             pegCounter(NATIVE_SOCKET_CONNECT_EXCEPTION);
95           } else if (result.contains("java.net.ConnectException: Connection refused: connect")) {
96             pegCounter(NATIVE_SOCKET_CONNECTION_REFUSED);
97           } else if (result.contains("java.net.SocketException: Connection reset")) {
98             pegCounter(NATIVE_SOCKET_CONNECTION_RESET);
99           } else {
100             pegCounter(UNKNOWN_EXCEPTION);
101             LOG.error(AaiUiMsgs.PEGGING_ERROR, result.toString());
102           }
103
104         }
105       }
106
107     }
108
109   }
110
111   public String getStatisticsReport() {
112
113     StringBuilder sb = new StringBuilder(128);
114
115     int nativeConnect = getCounterValue(NATIVE_SOCKET_CONNECT_EXCEPTION);
116     int nativeCxnReset = getCounterValue(NATIVE_SOCKET_CONNECTION_RESET);
117     int nativeCxnRefused = getCounterValue(NATIVE_SOCKET_CONNECTION_REFUSED);
118     int clientTimeout = getCounterValue(CLIENT_TIMEOUT_EXCEPTION);
119     int unknown = getCounterValue(UNKNOWN_EXCEPTION);
120
121     sb.append("\n            ")
122         .append(String.format("%-40s: %-12d", NATIVE_SOCKET_CONNECT_EXCEPTION, nativeConnect));
123     sb.append("\n            ")
124         .append(String.format("%-40s: %-12d", NATIVE_SOCKET_CONNECTION_RESET, nativeCxnReset));
125     sb.append("\n            ")
126         .append(String.format("%-40s: %-12d", NATIVE_SOCKET_CONNECTION_REFUSED, nativeCxnRefused));
127     sb.append("\n            ")
128         .append(String.format("%-40s: %-12d", CLIENT_TIMEOUT_EXCEPTION, clientTimeout));
129     sb.append("\n            ").append(String.format("%-40s: %-12d", UNKNOWN_EXCEPTION, unknown));
130
131     return sb.toString();
132
133   }
134
135
136
137 }