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