Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / dal / rest / RestOperationalStatistics.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.rest;
27
28 import org.openecomp.sparky.analytics.AbstractStatistics;
29 import org.openecomp.sparky.dal.NetworkTransaction;
30
31 /**
32  * The Class RestOperationalStatistics.
33  */
34 public class RestOperationalStatistics extends AbstractStatistics {
35
36   private static final String GET_1XX = "GET_1XX";
37   private static final String GET_2XX = "GET_2XX";
38   private static final String GET_3XX = "GET_3XX";
39   private static final String GET_4XX = "GET_4XX";
40   private static final String GET_5XX = "GET_5XX";
41   private static final String GET_6XX = "GET_6XX";
42
43   private static final String PUT_1XX = "PUT_1XX";
44   private static final String PUT_2XX = "PUT_2XX";
45   private static final String PUT_3XX = "PUT_3XX";
46   private static final String PUT_4XX = "PUT_4XX";
47   private static final String PUT_5XX = "PUT_5XX";
48   private static final String PUT_6XX = "PUT_6XX";
49
50   private static final String POST_1XX = "POST_1XX";
51   private static final String POST_2XX = "POST_2XX";
52   private static final String POST_3XX = "POST_3XX";
53   private static final String POST_4XX = "POST_4XX";
54   private static final String POST_5XX = "POST_5XX";
55   private static final String POST_6XX = "POST_6XX";
56
57   private static final String DELETE_1XX = "DELETE_1XX";
58   private static final String DELETE_2XX = "DELETE_2XX";
59   private static final String DELETE_3XX = "DELETE_3XX";
60   private static final String DELETE_4XX = "DELETE_4XX";
61   private static final String DELETE_5XX = "DELETE_5XX";
62   private static final String DELETE_6XX = "DELETE_6XX";
63
64   /**
65    * Creates the counters.
66    */
67   private void createCounters() {
68
69     addCounter(GET_1XX);
70     addCounter(GET_2XX);
71     addCounter(GET_3XX);
72     addCounter(GET_4XX);
73     addCounter(GET_5XX);
74     addCounter(GET_6XX);
75
76     addCounter(PUT_1XX);
77     addCounter(PUT_2XX);
78     addCounter(PUT_3XX);
79     addCounter(PUT_4XX);
80     addCounter(PUT_5XX);
81     addCounter(PUT_6XX);
82
83     addCounter(POST_1XX);
84     addCounter(POST_2XX);
85     addCounter(POST_3XX);
86     addCounter(POST_4XX);
87     addCounter(POST_5XX);
88     addCounter(POST_6XX);
89
90     addCounter(DELETE_1XX);
91     addCounter(DELETE_2XX);
92     addCounter(DELETE_3XX);
93     addCounter(DELETE_4XX);
94     addCounter(DELETE_5XX);
95     addCounter(DELETE_6XX);
96
97
98   }
99
100   /**
101    * Gets the result code.
102    *
103    * @param txn the txn
104    * @return the result code
105    */
106   private int getResultCode(NetworkTransaction txn) {
107
108     if (txn == null) {
109       return -1;
110     }
111
112     if (txn.getOperationResult() == null) {
113       return -1;
114     }
115
116     return txn.getOperationResult().getResultCode();
117
118   }
119
120   /**
121    * Update counters.
122    *
123    * @param txn the txn
124    */
125   public void updateCounters(NetworkTransaction txn) {
126
127     if (txn == null) {
128       return;
129     }
130
131     int rc = getResultCode(txn);
132
133     switch (txn.getOperationType()) {
134
135       case GET: {
136
137         if (100 <= rc && rc <= 199) {
138           pegCounter(GET_1XX);
139         } else if (200 <= rc && rc <= 299) {
140           pegCounter(GET_2XX);
141         } else if (300 <= rc && rc <= 399) {
142           pegCounter(GET_3XX);
143         } else if (400 <= rc && rc <= 499) {
144           pegCounter(GET_4XX);
145         } else if (500 <= rc && rc <= 599) {
146           pegCounter(GET_5XX);
147         } else if (600 <= rc && rc <= 699) {
148           pegCounter(GET_6XX);
149         }
150
151         break;
152       }
153
154       case PUT: {
155
156         if (100 <= rc && rc <= 199) {
157           pegCounter(PUT_1XX);
158         } else if (200 <= rc && rc <= 299) {
159           pegCounter(PUT_2XX);
160         } else if (300 <= rc && rc <= 399) {
161           pegCounter(PUT_3XX);
162         } else if (400 <= rc && rc <= 499) {
163           pegCounter(PUT_4XX);
164         } else if (500 <= rc && rc <= 599) {
165           pegCounter(PUT_5XX);
166         } else if (600 <= rc && rc <= 699) {
167           pegCounter(PUT_6XX);
168         }
169
170         break;
171       }
172
173       case POST: {
174
175         if (100 <= rc && rc <= 199) {
176           pegCounter(POST_1XX);
177         } else if (200 <= rc && rc <= 299) {
178           pegCounter(POST_2XX);
179         } else if (300 <= rc && rc <= 399) {
180           pegCounter(POST_3XX);
181         } else if (400 <= rc && rc <= 499) {
182           pegCounter(POST_4XX);
183         } else if (500 <= rc && rc <= 599) {
184           pegCounter(POST_5XX);
185         } else if (600 <= rc && rc <= 699) {
186           pegCounter(POST_6XX);
187         }
188
189         break;
190       }
191
192       case DELETE: {
193
194         if (100 <= rc && rc <= 199) {
195           pegCounter(DELETE_1XX);
196         } else if (200 <= rc && rc <= 299) {
197           pegCounter(DELETE_2XX);
198         } else if (300 <= rc && rc <= 399) {
199           pegCounter(DELETE_3XX);
200         } else if (400 <= rc && rc <= 499) {
201           pegCounter(DELETE_4XX);
202         } else if (500 <= rc && rc <= 599) {
203           pegCounter(DELETE_5XX);
204         } else if (600 <= rc && rc <= 699) {
205           pegCounter(DELETE_6XX);
206         }
207
208         break;
209       }
210
211       default: {
212         // not expecting anything else yet
213       }
214
215     }
216
217   }
218
219   /**
220    * Instantiates a new rest operational statistics.
221    */
222   public RestOperationalStatistics() {
223     createCounters();
224   }
225
226   public String getStatisticsReport() {
227
228     StringBuilder sb = new StringBuilder(128);
229
230     sb.append("\n            ")
231         .append(String.format(
232             "%-12s 1XX: %-12d 2XX: %-12d 3XX: %-12d 4XX: %-12d 5XX: %-12d 6XX: %-12d ",
233             HttpMethod.DELETE, getCounterValue(DELETE_1XX), getCounterValue(DELETE_2XX),
234             getCounterValue(DELETE_3XX), getCounterValue(DELETE_4XX), getCounterValue(DELETE_5XX),
235             getCounterValue(DELETE_6XX)));
236
237     sb.append("\n            ").append(String.format(
238         "%-12s 1XX: %-12d 2XX: %-12d 3XX: %-12d 4XX: %-12d 5XX: %-12d 6XX: %-12d ", HttpMethod.PUT,
239         getCounterValue(PUT_1XX), getCounterValue(PUT_2XX), getCounterValue(PUT_3XX),
240         getCounterValue(PUT_4XX), getCounterValue(PUT_5XX), getCounterValue(PUT_6XX)));
241
242     sb.append("\n            ").append(String.format(
243         "%-12s 1XX: %-12d 2XX: %-12d 3XX: %-12d 4XX: %-12d 5XX: %-12d 6XX: %-12d ", HttpMethod.POST,
244         getCounterValue(POST_1XX), getCounterValue(POST_2XX), getCounterValue(POST_3XX),
245         getCounterValue(POST_4XX), getCounterValue(POST_5XX), getCounterValue(POST_6XX)));
246
247     sb.append("\n            ").append(String.format(
248         "%-12s 1XX: %-12d 2XX: %-12d 3XX: %-12d 4XX: %-12d 5XX: %-12d 6XX: %-12d ", HttpMethod.GET,
249         getCounterValue(GET_1XX), getCounterValue(GET_2XX), getCounterValue(GET_3XX),
250         getCounterValue(GET_4XX), getCounterValue(GET_5XX), getCounterValue(GET_6XX)));
251
252     return sb.toString();
253   }
254
255
256 }