Initial source code
[oam/tr069-adapter.git] / mapper / src / main / java / org / commscope / tr069adapter / mapper / util / ErrorCodeUtil.java
diff --git a/mapper/src/main/java/org/commscope/tr069adapter/mapper/util/ErrorCodeUtil.java b/mapper/src/main/java/org/commscope/tr069adapter/mapper/util/ErrorCodeUtil.java
new file mode 100644 (file)
index 0000000..13f9a6a
--- /dev/null
@@ -0,0 +1,83 @@
+/*\r
+ * ============LICENSE_START========================================================================\r
+ * ONAP : tr-069-adapter\r
+ * =================================================================================================\r
+ * Copyright (C) 2020 CommScope Inc Intellectual Property.\r
+ * =================================================================================================\r
+ * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,\r
+ * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You\r
+ * may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,\r
+ * either express or implied. See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ===============LICENSE_END=======================================================================\r
+ */\r
+\r
+package org.commscope.tr069adapter.mapper.util;\r
+\r
+import com.fasterxml.jackson.core.type.TypeReference;\r
+import com.fasterxml.jackson.databind.ObjectMapper;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.nio.charset.StandardCharsets;\r
+import java.util.Map;\r
+import java.util.Map.Entry;\r
+\r
+import javax.annotation.PostConstruct;\r
+\r
+import org.apache.commons.io.FileUtils;\r
+import org.commscope.tr069adapter.mapper.ErrorCodeMetaData;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.springframework.stereotype.Component;\r
+\r
+@Component\r
+public class ErrorCodeUtil {\r
+\r
+\r
+  private static final Logger LOG = LoggerFactory.getLogger(ErrorCodeUtil.class);\r
+\r
+  private static final String ERROR_CODE_FILE = "error-code-mapping.json";\r
+  private static Map<String, ErrorCodeMetaData> errorCodeMap;\r
+\r
+  @PostConstruct\r
+  public static void loadErrorCodeData() {\r
+    File file = new File(ERROR_CODE_FILE);\r
+    try {\r
+      errorCodeMap = getMetaDataAsMap(file);\r
+    } catch (IOException e) {\r
+      LOG.error("Exception : {}", e.getMessage());\r
+    }\r
+  }\r
+\r
+  public void printErrorCodeMap() {\r
+    for (Entry<String, ErrorCodeMetaData> entry : errorCodeMap.entrySet()) {\r
+      LOG.debug("KEY= {}", entry.getKey());\r
+      LOG.debug("VALUE= {}", entry.getValue());\r
+    }\r
+  }\r
+\r
+  private static Map<String, ErrorCodeMetaData> getMetaDataAsMap(File file) throws IOException {\r
+\r
+    String json = FileUtils.readFileToString(file, StandardCharsets.UTF_8);\r
+\r
+    Map<String, ErrorCodeMetaData> result = null;\r
+    ObjectMapper mapper = new ObjectMapper();\r
+    try {\r
+      result = mapper.readValue(json, new TypeReference<Map<String, ErrorCodeMetaData>>() {});\r
+    } catch (IOException e) {\r
+      LOG.error("IOException while loading device model meta data {}", e.toString());\r
+      LOG.error("Exception : {}", e.getMessage());\r
+    }\r
+    return result;\r
+  }\r
+\r
+  public ErrorCodeMetaData getErrorCodeMetaData(String errorCode) {\r
+    return errorCodeMap.get(errorCode);\r
+  }\r
+\r
+}\r