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