fixing warnings from checkstyle in common-app-api
[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 static final int ERROR_CODE_SUBSTRING = 3;
39     private int errorCode;
40
41     EcompLoggerErrorCode(int errorCode) {
42         this.errorCode = errorCode;
43     }
44
45     public int getErrorCode() {
46         return errorCode;
47     }
48
49     public static EcompLoggerErrorCode getByValue(String ecompErrorCode) {
50         String errorPrefix = parseCode(ecompErrorCode);
51         Optional<EcompLoggerErrorCode> optionalCode = Arrays.stream(values()).filter(v -> isCode(v, errorPrefix)).findFirst();
52         return optionalCode.orElse(UNKNOWN_ERROR);
53     }
54
55     private static boolean isCode(EcompLoggerErrorCode ecompLoggerErrorCode, String errorPrefix) {
56         return String.valueOf(ecompLoggerErrorCode.getErrorCode()).contains(errorPrefix);
57     }
58
59     private static String parseCode(String errorCode) {
60         try {
61             return errorCode.substring("E_".length(), ERROR_CODE_SUBSTRING);
62         } catch (StringIndexOutOfBoundsException ex) {
63             return UNKNOWN_ERROR.name();
64         }
65     }
66
67
68 }