+/*\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 java.io.File;\r
+import java.io.IOException;\r
+import java.nio.charset.StandardCharsets;\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.HashMap;\r
+import java.util.HashSet;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+import javax.annotation.PostConstruct;\r
+\r
+import org.apache.commons.io.FileUtils;\r
+import org.commscope.tr069adapter.acs.common.ParameterDTO;\r
+import org.commscope.tr069adapter.mapper.MOMetaData;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.springframework.stereotype.Component;\r
+\r
+@Component\r
+public class MOMetaDataUtil {\r
+\r
+ private static final String STRING_I = ".{i}.";\r
+ private static final Logger LOG = LoggerFactory.getLogger(MOMetaDataUtil.class);\r
+ private static final String GENERIC_INDEX_REGEX = "\\.\\{[i-l]\\}\\.";\r
+ private static final String INDEX_REGEX = "\\.[0-9]{1,}\\.";\r
+ private static final String MO_META_DATA_FILE_LOCATION = "mapper-schema";\r
+ private Map<String, MOMetaData> metaDataMap;\r
+ private Map<String, String> metaDataReverseMap;\r
+ public static final String ORAN_SW_MGMT_URI = "urn:o-ran:software-management:1.0";\r
+ private static HashMap<String, String> nameSpaces = new HashMap<>();\r
+\r
+ @PostConstruct\r
+ public void loadMetaData() {\r
+ try {\r
+ LOG.info("Loading mapper schema");\r
+ if (metaDataMap != null)\r
+ metaDataMap.clear();\r
+ if (metaDataReverseMap != null)\r
+ metaDataReverseMap.clear();\r
+ getMetaDataAsMap(MO_META_DATA_FILE_LOCATION);\r
+ LOG.info("Loading mapper schema successfully completed");\r
+ } catch (IOException e) {\r
+ LOG.error("Exception : {}", e.getMessage());\r
+ }\r
+ }\r
+\r
+ private void getMetaDataAsMap(String fileLocation) throws IOException {\r
+ metaDataMap = new HashMap<>();\r
+ metaDataReverseMap = new HashMap<>();\r
+ Collection<File> files = FileUtils.listFiles(new File(fileLocation), null, false);\r
+\r
+ for (File file : files) {\r
+ LOG.info("Loading mapper schema from {}", file.getName());\r
+ List<String> lines = FileUtils.readLines(file, StandardCharsets.UTF_8);\r
+ for (String line : lines) {\r
+ if (line != null && line.startsWith("#")) {\r
+ continue;\r
+ }\r
+ if (line != null && line.split(",").length >= 3) {\r
+ String[] split = line.split(",");\r
+ boolean isReadOnly = false;\r
+ boolean isTabluar = false;\r
+ boolean isTabObject = false;\r
+ String dataType = "";\r
+\r
+ if (split[2].contains("-")) {\r
+ String[] dataAttr = split[2].split("-");\r
+ if ("Tabular".equalsIgnoreCase(dataAttr[0]))\r
+ isTabluar = true;\r
+ if ("ReadOnly".equalsIgnoreCase(dataAttr[1]))\r
+ isReadOnly = true;\r
+ } else if ("TabularObject".equalsIgnoreCase(split[2])) {\r
+ isTabObject = true;\r
+ }\r
+ if (split.length > 3) {\r
+ dataType = split[3];\r
+ }\r
+ if (isTabObject) {\r
+ String logMessage = split[1].substring(0, split[1].length() - 5);\r
+ LOG.info("Adding Parent Objects {}", logMessage);\r
+ String substring = split[0].substring(0, split[0].length() - 4);\r
+ MOMetaData metaTabData =\r
+ new MOMetaData(substring, dataType, isReadOnly, isTabluar, isTabObject);\r
+ if ((split.length > 4 && split[4] != null) && split[4].trim().length() > 0) {\r
+ metaTabData.setURI(split[4]);\r
+ }\r
+ metaDataMap.put(logMessage, metaTabData);\r
+ }\r
+ MOMetaData metaData =\r
+ new MOMetaData(split[0], dataType, isReadOnly, isTabluar, isTabObject);\r
+ if ((split.length > 4 && split[4] != null) && split[4].trim().length() > 0) {\r
+ metaData.setURI(split[4]);\r
+ }\r
+ metaDataMap.put(split[1], metaData);\r
+ metaDataReverseMap.put(split[0], split[1]);\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ public MOMetaData getMetaDataByNetConfName(String moName) {\r
+ String moNameInGnrForm = moName.replaceAll(INDEX_REGEX, STRING_I);\r
+ return metaDataMap.get(moNameInGnrForm);\r
+ }\r
+\r
+ public MOMetaData getMetaDataByTR69Name(String moName) {\r
+ String moNameInGnrForm = moName.replaceAll(INDEX_REGEX, STRING_I);\r
+ String netconfMoName = metaDataReverseMap.get(moNameInGnrForm);\r
+ return metaDataMap.get(netconfMoName);\r
+ }\r
+\r
+ public String getNetconfNameByTR69Name(String moName) {\r
+ String moNameInGnrForm = moName.replaceAll(INDEX_REGEX, STRING_I);\r
+ return metaDataReverseMap.get(moNameInGnrForm);\r
+ }\r
+\r
+ public String getNetconfNameByTR69NameWithIndexes(String moName) {\r
+ String moNameInGnrForm = moName.replaceAll(INDEX_REGEX, STRING_I);\r
+ String netConfNMoName = metaDataReverseMap.get(moNameInGnrForm);\r
+ return netConfNMoName != null ? getNetConfMOByReplacingIndexes(netConfNMoName, moName)\r
+ : netConfNMoName;\r
+ }\r
+\r
+ public Set<String> getAllMONames() {\r
+ return metaDataMap.keySet();\r
+ }\r
+\r
+ public String getNameSpace(String nodeName) {\r
+ return nameSpaces.get(nodeName);\r
+ }\r
+\r
+ public List<ParameterDTO> getSupportedChildParameters(List<ParameterDTO> parameters) {\r
+ List<ParameterDTO> result = new ArrayList<>();\r
+ Set<MOMetaData> allMatchedChilds = new HashSet<>();\r
+ for (ParameterDTO param : parameters) {\r
+ String parentMONameInGnrc = param.getParamName().replaceAll(INDEX_REGEX, STRING_I);\r
+ MOMetaData moData = metaDataMap.get(parentMONameInGnrc);\r
+ if (moData != null) {\r
+ allMatchedChilds.add(new MOMetaData(\r
+ getTR69MOByReplacingIndexes(param.getParamName(), moData.getMoName()),\r
+ moData.getDataType(), moData.isReadOnly(), moData.isTabluar(), moData.isTabluarObj()));\r
+ }\r
+ }\r
+ for (MOMetaData metaData : allMatchedChilds) {\r
+ ParameterDTO param = new ParameterDTO();\r
+ String paramName = metaData.getMoName();\r
+\r
+ param.setParamName(paramName);\r
+ param.setDataType(metaData.getDataType());\r
+ result.add(param);\r
+ }\r
+ return result;\r
+ }\r
+\r
+ public List<ParameterDTO> getDeviceSupportedChildParameters() {\r
+ List<ParameterDTO> result = new ArrayList<>();\r
+\r
+ ParameterDTO param1 = new ParameterDTO();\r
+ param1.setParamName("Device.DeviceInfo.Description");\r
+ param1.setParamValue("Internal");\r
+ param1.setDataType("string");\r
+ result.add(param1);\r
+ return result;\r
+ }\r
+\r
+ public static String getTR69MOByReplacingIndexes(String netconfMo, String tr69Mo) {\r
+\r
+ String[] split = netconfMo.split("\\.");\r
+ for (int i = 0; i < split.length; i++) {\r
+ if (split[i].matches("[0-9]{1,}")) {\r
+ tr69Mo = tr69Mo.replaceFirst(GENERIC_INDEX_REGEX, "." + split[i] + ".");\r
+ }\r
+ }\r
+\r
+ return tr69Mo;\r
+\r
+ }\r
+\r
+ public static String getNetConfMOByReplacingIndexes(String netconfMo, String tr69Mo) {\r
+\r
+ String[] split = tr69Mo.split("\\.");\r
+ for (int i = 0; i < split.length; i++) {\r
+ if (split[i].matches("[0-9]{1,}")) {\r
+ netconfMo = netconfMo.replaceFirst(GENERIC_INDEX_REGEX, "." + split[i] + ".");\r
+ }\r
+ }\r
+\r
+ return netconfMo;\r
+\r
+ }\r
+\r
+}\r