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