Added oparent to sdc main
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / log / enums / EcompLoggerErrorCode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.common.log.enums;
22
23 import java.util.Arrays;
24 import java.util.Optional;
25
26 /**
27  * Created by dd4296 on 12/26/2017.
28  */
29 public enum EcompLoggerErrorCode {
30     SUCCESS(0),
31     PERMISSION_ERROR(100),
32     AVAILABILITY_TIMEOUTS_ERROR(200),
33     DATA_ERROR(300),
34     SCHEMA_ERROR(400),
35     BUSINESS_PROCESS_ERROR(500),
36     UNKNOWN_ERROR(900);
37
38     private int errorCode;
39
40     EcompLoggerErrorCode(int errorCode) {
41         this.errorCode = errorCode;
42     }
43
44     public int getErrorCode() {
45         return errorCode;
46     }
47
48     public static EcompLoggerErrorCode getByValue(String ecompErrorCode) {
49         String errorPrefix = parseCode(ecompErrorCode);
50         Optional<EcompLoggerErrorCode> optionalCode = Arrays.stream(values()).filter(v->isCode(v, errorPrefix)).findFirst();
51         return optionalCode.orElse(UNKNOWN_ERROR);
52     }
53
54     private static boolean isCode(EcompLoggerErrorCode ecompLoggerErrorCode, String errorPrefix) {
55         return String.valueOf(ecompLoggerErrorCode.getErrorCode()).contains(errorPrefix);
56     }
57
58     private static String parseCode(String errorCode) {
59         try {
60             return errorCode.substring("E_".length(), 3);
61         } catch (StringIndexOutOfBoundsException ex) {
62             return UNKNOWN_ERROR.name();
63         }
64     }
65
66
67 }