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