Add o-ran-sc/ prefix to ves-agent image name
[oam/tr069-adapter.git] / mapper / src / main / java / org / commscope / tr069adapter / mapper / util / ErrorCodeUtil.java
1 /*\r
2  * ============LICENSE_START========================================================================\r
3  * ONAP : tr-069-adapter\r
4  * =================================================================================================\r
5  * Copyright (C) 2020 CommScope Inc Intellectual Property.\r
6  * =================================================================================================\r
7  * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,\r
8  * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You\r
9  * may obtain a copy of the License at\r
10  *\r
11  * http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,\r
14  * either express or implied. See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  * ===============LICENSE_END=======================================================================\r
17  */\r
18 \r
19 package org.commscope.tr069adapter.mapper.util;\r
20 \r
21 import com.fasterxml.jackson.core.type.TypeReference;\r
22 import com.fasterxml.jackson.databind.ObjectMapper;\r
23 \r
24 import java.io.File;\r
25 import java.io.IOException;\r
26 import java.nio.charset.StandardCharsets;\r
27 import java.util.Map;\r
28 import java.util.Map.Entry;\r
29 \r
30 import javax.annotation.PostConstruct;\r
31 \r
32 import org.apache.commons.io.FileUtils;\r
33 import org.commscope.tr069adapter.mapper.ErrorCodeMetaData;\r
34 import org.slf4j.Logger;\r
35 import org.slf4j.LoggerFactory;\r
36 import org.springframework.stereotype.Component;\r
37 \r
38 @Component\r
39 public class ErrorCodeUtil {\r
40 \r
41 \r
42   private static final Logger LOG = LoggerFactory.getLogger(ErrorCodeUtil.class);\r
43 \r
44   private static final String ERROR_CODE_FILE = "error-code-mapping.json";\r
45   private static Map<String, ErrorCodeMetaData> errorCodeMap;\r
46 \r
47   @PostConstruct\r
48   public static void loadErrorCodeData() {\r
49     File file = new File(ERROR_CODE_FILE);\r
50     try {\r
51       errorCodeMap = getMetaDataAsMap(file);\r
52     } catch (IOException e) {\r
53       LOG.error("Exception : {}", e.getMessage());\r
54     }\r
55   }\r
56 \r
57   public void printErrorCodeMap() {\r
58     for (Entry<String, ErrorCodeMetaData> entry : errorCodeMap.entrySet()) {\r
59       LOG.debug("KEY= {}", entry.getKey());\r
60       LOG.debug("VALUE= {}", entry.getValue());\r
61     }\r
62   }\r
63 \r
64   private static Map<String, ErrorCodeMetaData> getMetaDataAsMap(File file) throws IOException {\r
65 \r
66     String json = FileUtils.readFileToString(file, StandardCharsets.UTF_8);\r
67 \r
68     Map<String, ErrorCodeMetaData> result = null;\r
69     ObjectMapper mapper = new ObjectMapper();\r
70     try {\r
71       result = mapper.readValue(json, new TypeReference<Map<String, ErrorCodeMetaData>>() {});\r
72     } catch (IOException e) {\r
73       LOG.error("IOException while loading device model meta data {}", e.toString());\r
74       LOG.error("Exception : {}", e.getMessage());\r
75     }\r
76     return result;\r
77   }\r
78 \r
79   public ErrorCodeMetaData getErrorCodeMetaData(String errorCode) {\r
80     return errorCodeMap.get(errorCode);\r
81   }\r
82 \r
83 }\r