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