Merge "Add debugging of REST call"
[policy/drools-applications.git] / controlloop / common / model-impl / sdnr / src / main / java / org / onap / policy / sdnr / util / StatusCodeEnum.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.sdnr.util;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 public enum StatusCodeEnum {
27     ACCEPTED("ACCEPTED"), ERROR("ERROR"), REJECT("REJECT"), SUCCESS("SUCCESS"), FAILURE("FAILURE"),
28     PARTIAL_SUCCESS("PARTIAL SUCCESS"), PARTIAL_FAILURE("PARTIAL FAILURE");
29
30     private String name;
31
32     StatusCodeEnum(final String name) {
33         this.name = name;
34     }
35
36     public String toString() {
37         return this.name;
38     }
39
40     /**
41      * Determine status enum from the code.
42      *
43      * @param statusCode integer code indicating the status
44      * @return enum representation of status code
45      */
46     public static StatusCodeEnum fromStatusCode(final int statusCode) {
47         if (statusCode == 100) {
48             return ACCEPTED;
49         }
50
51         if (statusCode == 200) {
52             return SUCCESS;
53         }
54
55         if (isRejectStatusCode(statusCode)) {
56             return REJECT;
57         }
58
59         if (statusCode == 400) {
60             return ERROR;
61         }
62
63         if (isFailureStatusCode(statusCode)) {
64             return FAILURE;
65         }
66
67         if (statusCode == 500) {
68             return PARTIAL_SUCCESS;
69         }
70
71         if (isPartialFailureStatusCode(statusCode)) {
72             return PARTIAL_FAILURE;
73         }
74
75         return null;
76     }
77
78     private static boolean isRejectStatusCode(final int statusCode) {
79         return statusCode >= 300 && statusCode <= 313;
80     }
81
82     private static boolean isFailureStatusCode(final int statusCode) {
83         return statusCode == 450 || (statusCode >= 401 && statusCode <= 406);
84     }
85
86     private static boolean isPartialFailureStatusCode(final int statusCode) {
87         return statusCode >= 501 && statusCode <= 599;
88     }
89
90 }