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