summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openecomp/sparky/dal/rest/RestOperationalStatistics.java
blob: 7b0ca485abb944f13d871ef5187d01e89292d6fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
 * ============LICENSE_START===================================================
 * SPARKY (AAI UI service)
 * ============================================================================
 * Copyright © 2017 AT&T Intellectual Property.
 * Copyright © 2017 Amdocs
 * All rights reserved.
 * ============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=====================================================
 *
 * ECOMP and OpenECOMP are trademarks
 * and service marks of AT&T Intellectual Property.
 */

package org.openecomp.sparky.dal.rest;

import org.openecomp.sparky.analytics.AbstractStatistics;
import org.openecomp.sparky.dal.NetworkTransaction;

/**
 * The Class RestOperationalStatistics.
 */
public class RestOperationalStatistics extends AbstractStatistics {

  private static final String GET_1XX = "GET_1XX";
  private static final String GET_2XX = "GET_2XX";
  private static final String GET_3XX = "GET_3XX";
  private static final String GET_4XX = "GET_4XX";
  private static final String GET_5XX = "GET_5XX";
  private static final String GET_6XX = "GET_6XX";

  private static final String PUT_1XX = "PUT_1XX";
  private static final String PUT_2XX = "PUT_2XX";
  private static final String PUT_3XX = "PUT_3XX";
  private static final String PUT_4XX = "PUT_4XX";
  private static final String PUT_5XX = "PUT_5XX";
  private static final String PUT_6XX = "PUT_6XX";

  private static final String POST_1XX = "POST_1XX";
  private static final String POST_2XX = "POST_2XX";
  private static final String POST_3XX = "POST_3XX";
  private static final String POST_4XX = "POST_4XX";
  private static final String POST_5XX = "POST_5XX";
  private static final String POST_6XX = "POST_6XX";

  private static final String DELETE_1XX = "DELETE_1XX";
  private static final String DELETE_2XX = "DELETE_2XX";
  private static final String DELETE_3XX = "DELETE_3XX";
  private static final String DELETE_4XX = "DELETE_4XX";
  private static final String DELETE_5XX = "DELETE_5XX";
  private static final String DELETE_6XX = "DELETE_6XX";

  /**
   * Creates the counters.
   */
  private void createCounters() {

    addCounter(GET_1XX);
    addCounter(GET_2XX);
    addCounter(GET_3XX);
    addCounter(GET_4XX);
    addCounter(GET_5XX);
    addCounter(GET_6XX);

    addCounter(PUT_1XX);
    addCounter(PUT_2XX);
    addCounter(PUT_3XX);
    addCounter(PUT_4XX);
    addCounter(PUT_5XX);
    addCounter(PUT_6XX);

    addCounter(POST_1XX);
    addCounter(POST_2XX);
    addCounter(POST_3XX);
    addCounter(POST_4XX);
    addCounter(POST_5XX);
    addCounter(POST_6XX);

    addCounter(DELETE_1XX);
    addCounter(DELETE_2XX);
    addCounter(DELETE_3XX);
    addCounter(DELETE_4XX);
    addCounter(DELETE_5XX);
    addCounter(DELETE_6XX);


  }

  /**
   * Gets the result code.
   *
   * @param txn the txn
   * @return the result code
   */
  private int getResultCode(NetworkTransaction txn) {

    if (txn == null) {
      return -1;
    }

    if (txn.getOperationResult() == null) {
      return -1;
    }

    return txn.getOperationResult().getResultCode();

  }

  /**
   * Update counters.
   *
   * @param txn the txn
   */
  public void updateCounters(NetworkTransaction txn) {

    if (txn == null) {
      return;
    }

    int rc = getResultCode(txn);

    switch (txn.getOperationType()) {

      case GET: {

        if (100 <= rc && rc <= 199) {
          pegCounter(GET_1XX);
        } else if (200 <= rc && rc <= 299) {
          pegCounter(GET_2XX);
        } else if (300 <= rc && rc <= 399) {
          pegCounter(GET_3XX);
        } else if (400 <= rc && rc <= 499) {
          pegCounter(GET_4XX);
        } else if (500 <= rc && rc <= 599) {
          pegCounter(GET_5XX);
        } else if (600 <= rc && rc <= 699) {
          pegCounter(GET_6XX);
        }

        break;
      }

      case PUT: {

        if (100 <= rc && rc <= 199) {
          pegCounter(PUT_1XX);
        } else if (200 <= rc && rc <= 299) {
          pegCounter(PUT_2XX);
        } else if (300 <= rc && rc <= 399) {
          pegCounter(PUT_3XX);
        } else if (400 <= rc && rc <= 499) {
          pegCounter(PUT_4XX);
        } else if (500 <= rc && rc <= 599) {
          pegCounter(PUT_5XX);
        } else if (600 <= rc && rc <= 699) {
          pegCounter(PUT_6XX);
        }

        break;
      }

      case POST: {

        if (100 <= rc && rc <= 199) {
          pegCounter(POST_1XX);
        } else if (200 <= rc && rc <= 299) {
          pegCounter(POST_2XX);
        } else if (300 <= rc && rc <= 399) {
          pegCounter(POST_3XX);
        } else if (400 <= rc && rc <= 499) {
          pegCounter(POST_4XX);
        } else if (500 <= rc && rc <= 599) {
          pegCounter(POST_5XX);
        } else if (600 <= rc && rc <= 699) {
          pegCounter(POST_6XX);
        }

        break;
      }

      case DELETE: {

        if (100 <= rc && rc <= 199) {
          pegCounter(DELETE_1XX);
        } else if (200 <= rc && rc <= 299) {
          pegCounter(DELETE_2XX);
        } else if (300 <= rc && rc <= 399) {
          pegCounter(DELETE_3XX);
        } else if (400 <= rc && rc <= 499) {
          pegCounter(DELETE_4XX);
        } else if (500 <= rc && rc <= 599) {
          pegCounter(DELETE_5XX);
        } else if (600 <= rc && rc <= 699) {
          pegCounter(DELETE_6XX);
        }

        break;
      }

      default: {
        // not expecting anything else yet
      }

    }

  }

  /**
   * Instantiates a new rest operational statistics.
   */
  public RestOperationalStatistics() {
    createCounters();
  }

  public String getStatisticsReport() {

    StringBuilder sb = new StringBuilder(128);

    sb.append("\n            ")
        .append(String.format(
            "%-12s 1XX: %-12d 2XX: %-12d 3XX: %-12d 4XX: %-12d 5XX: %-12d 6XX: %-12d ",
            HttpMethod.DELETE, getCounterValue(DELETE_1XX), getCounterValue(DELETE_2XX),
            getCounterValue(DELETE_3XX), getCounterValue(DELETE_4XX), getCounterValue(DELETE_5XX),
            getCounterValue(DELETE_6XX)));

    sb.append("\n            ").append(String.format(
        "%-12s 1XX: %-12d 2XX: %-12d 3XX: %-12d 4XX: %-12d 5XX: %-12d 6XX: %-12d ", HttpMethod.PUT,
        getCounterValue(PUT_1XX), getCounterValue(PUT_2XX), getCounterValue(PUT_3XX),
        getCounterValue(PUT_4XX), getCounterValue(PUT_5XX), getCounterValue(PUT_6XX)));

    sb.append("\n            ").append(String.format(
        "%-12s 1XX: %-12d 2XX: %-12d 3XX: %-12d 4XX: %-12d 5XX: %-12d 6XX: %-12d ", HttpMethod.POST,
        getCounterValue(POST_1XX), getCounterValue(POST_2XX), getCounterValue(POST_3XX),
        getCounterValue(POST_4XX), getCounterValue(POST_5XX), getCounterValue(POST_6XX)));

    sb.append("\n            ").append(String.format(
        "%-12s 1XX: %-12d 2XX: %-12d 3XX: %-12d 4XX: %-12d 5XX: %-12d 6XX: %-12d ", HttpMethod.GET,
        getCounterValue(GET_1XX), getCounterValue(GET_2XX), getCounterValue(GET_3XX),
        getCounterValue(GET_4XX), getCounterValue(GET_5XX), getCounterValue(GET_6XX)));

    return sb.toString();
  }


}