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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
19 package org.commscope.tr069adapter.mapper.util;
\r
21 import com.fasterxml.jackson.core.type.TypeReference;
\r
22 import com.fasterxml.jackson.databind.ObjectMapper;
\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
30 import javax.annotation.PostConstruct;
\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
39 public class ErrorCodeUtil {
\r
41 private static final Logger LOG = LoggerFactory.getLogger(ErrorCodeUtil.class);
\r
43 private static final String ERROR_CODE_FILE = "error-code-mapping.json";
\r
44 private static Map<String, ErrorCodeDetails> errorCodeMap;
\r
47 public static void loadErrorCodeData() {
\r
48 File file = new File(ERROR_CODE_FILE);
\r
50 errorCodeMap = getMetaDataAsMap(file);
\r
51 } catch (IOException e) {
\r
52 LOG.error("Exception : {}", e.getMessage());
\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
63 private static Map<String, ErrorCodeDetails> getMetaDataAsMap(File file) throws IOException {
\r
65 String json = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
\r
67 Map<String, ErrorCodeDetails> result = null;
\r
68 ObjectMapper mapper = new ObjectMapper();
\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
78 public ErrorCodeDetails getErrorCodeMetaData(String errorCode) {
\r
79 return errorCodeMap.get(errorCode);
\r