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