Merge "VES Heartbeat and Software Management Feature"
[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.model.ErrorCodeDetails;\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   private static final Logger LOG = LoggerFactory.getLogger(ErrorCodeUtil.class);\r
42 \r
43   private static final String ERROR_CODE_FILE = "error-code-mapping.json";\r
44   private static Map<String, ErrorCodeDetails> errorCodeMap;\r
45 \r
46   @PostConstruct\r
47   public static void loadErrorCodeData() {\r
48     File file = new File(ERROR_CODE_FILE);\r
49     try {\r
50       errorCodeMap = getMetaDataAsMap(file);\r
51     } catch (IOException e) {\r
52       LOG.error("Exception : {}", e.getMessage());\r
53     }\r
54   }\r
55 \r
56   public void printErrorCodeMap() {\r
57     for (Entry<String, ErrorCodeDetails> entry : errorCodeMap.entrySet()) {\r
58       LOG.debug("KEY= {}", entry.getKey());\r
59       LOG.debug("VALUE= {}", entry.getValue());\r
60     }\r
61   }\r
62 \r
63   private static Map<String, ErrorCodeDetails> getMetaDataAsMap(File file) throws IOException {\r
64 \r
65     String json = FileUtils.readFileToString(file, StandardCharsets.UTF_8);\r
66 \r
67     Map<String, ErrorCodeDetails> result = null;\r
68     ObjectMapper mapper = new ObjectMapper();\r
69     try {\r
70       result = mapper.readValue(json, new TypeReference<Map<String, ErrorCodeDetails>>() {});\r
71     } catch (IOException e) {\r
72       LOG.error("IOException while loading device model meta data {}", e.toString());\r
73       LOG.error("Exception : {}", e.getMessage());\r
74     }\r
75     return result;\r
76   }\r
77 \r
78   public ErrorCodeDetails getErrorCodeMetaData(String errorCode) {\r
79     return errorCodeMap.get(errorCode);\r
80   }\r
81 \r
82 }\r