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 java.io.File;
\r
22 import java.io.IOException;
\r
23 import java.nio.charset.StandardCharsets;
\r
24 import java.util.ArrayList;
\r
25 import java.util.Collection;
\r
26 import java.util.HashMap;
\r
27 import java.util.HashSet;
\r
28 import java.util.List;
\r
29 import java.util.Map;
\r
30 import java.util.Set;
\r
32 import javax.annotation.PostConstruct;
\r
34 import org.apache.commons.io.FileUtils;
\r
35 import org.commscope.tr069adapter.acs.common.ParameterDTO;
\r
36 import org.commscope.tr069adapter.mapper.MOMetaData;
\r
37 import org.slf4j.Logger;
\r
38 import org.slf4j.LoggerFactory;
\r
39 import org.springframework.stereotype.Component;
\r
42 public class MOMetaDataUtil {
\r
44 private static final String STRING_I = ".{i}.";
\r
45 private static final Logger LOG = LoggerFactory.getLogger(MOMetaDataUtil.class);
\r
46 private static final String GENERIC_INDEX_REGEX = "\\.\\{[i-l]\\}\\.";
\r
47 private static final String INDEX_REGEX = "\\.[0-9]{1,}\\.";
\r
48 private static final String MO_META_DATA_FILE_LOCATION = "mapper-schema";
\r
49 private Map<String, MOMetaData> metaDataMap;
\r
50 private Map<String, String> metaDataReverseMap;
\r
51 public static final String ORAN_SW_MGMT_URI = "urn:o-ran:software-management:1.0";
\r
52 private static HashMap<String, String> nameSpaces = new HashMap<>();
\r
55 public void loadMetaData() {
\r
57 LOG.info("Loading mapper schema");
\r
58 if (metaDataMap != null)
\r
59 metaDataMap.clear();
\r
60 if (metaDataReverseMap != null)
\r
61 metaDataReverseMap.clear();
\r
62 getMetaDataAsMap(MO_META_DATA_FILE_LOCATION);
\r
63 LOG.info("Loading mapper schema successfully completed");
\r
64 } catch (IOException e) {
\r
65 LOG.error("Exception : {}", e.getMessage());
\r
69 private void getMetaDataAsMap(String fileLocation) throws IOException {
\r
70 metaDataMap = new HashMap<>();
\r
71 metaDataReverseMap = new HashMap<>();
\r
72 Collection<File> files = FileUtils.listFiles(new File(fileLocation), null, false);
\r
74 for (File file : files) {
\r
75 LOG.info("Loading mapper schema from {}", file.getName());
\r
76 List<String> lines = FileUtils.readLines(file, StandardCharsets.UTF_8);
\r
77 for (String line : lines) {
\r
78 if (line != null && line.startsWith("#")) {
\r
81 if (line != null && line.split(",").length >= 3) {
\r
82 String[] split = line.split(",");
\r
83 boolean isReadOnly = false;
\r
84 boolean isTabluar = false;
\r
85 boolean isTabObject = false;
\r
86 String dataType = "";
\r
88 if (split[2].contains("-")) {
\r
89 String[] dataAttr = split[2].split("-");
\r
90 if ("Tabular".equalsIgnoreCase(dataAttr[0]))
\r
92 if ("ReadOnly".equalsIgnoreCase(dataAttr[1]))
\r
94 } else if ("TabularObject".equalsIgnoreCase(split[2])) {
\r
97 if (split.length > 3) {
\r
98 dataType = split[3];
\r
101 String logMessage = split[1].substring(0, split[1].length() - 5);
\r
102 LOG.info("Adding Parent Objects {}", logMessage);
\r
103 String substring = split[0].substring(0, split[0].length() - 4);
\r
104 MOMetaData metaTabData =
\r
105 new MOMetaData(substring, dataType, isReadOnly, isTabluar, isTabObject);
\r
106 if ((split.length > 4 && split[4] != null) && split[4].trim().length() > 0) {
\r
107 metaTabData.setURI(split[4]);
\r
109 metaDataMap.put(logMessage, metaTabData);
\r
111 MOMetaData metaData =
\r
112 new MOMetaData(split[0], dataType, isReadOnly, isTabluar, isTabObject);
\r
113 if ((split.length > 4 && split[4] != null) && split[4].trim().length() > 0) {
\r
114 metaData.setURI(split[4]);
\r
116 metaDataMap.put(split[1], metaData);
\r
117 metaDataReverseMap.put(split[0], split[1]);
\r
123 public MOMetaData getMetaDataByNetConfName(String moName) {
\r
124 String moNameInGnrForm = moName.replaceAll(INDEX_REGEX, STRING_I);
\r
125 return metaDataMap.get(moNameInGnrForm);
\r
128 public MOMetaData getMetaDataByTR69Name(String moName) {
\r
129 String moNameInGnrForm = moName.replaceAll(INDEX_REGEX, STRING_I);
\r
130 String netconfMoName = metaDataReverseMap.get(moNameInGnrForm);
\r
131 return metaDataMap.get(netconfMoName);
\r
134 public String getNetconfNameByTR69Name(String moName) {
\r
135 String moNameInGnrForm = moName.replaceAll(INDEX_REGEX, STRING_I);
\r
136 return metaDataReverseMap.get(moNameInGnrForm);
\r
139 public String getNetconfNameByTR69NameWithIndexes(String moName) {
\r
140 String moNameInGnrForm = moName.replaceAll(INDEX_REGEX, STRING_I);
\r
141 String netConfNMoName = metaDataReverseMap.get(moNameInGnrForm);
\r
142 return netConfNMoName != null ? getNetConfMOByReplacingIndexes(netConfNMoName, moName)
\r
146 public Set<String> getAllMONames() {
\r
147 return metaDataMap.keySet();
\r
150 public String getNameSpace(String nodeName) {
\r
151 return nameSpaces.get(nodeName);
\r
154 public List<ParameterDTO> getSupportedChildParameters(List<ParameterDTO> parameters) {
\r
155 List<ParameterDTO> result = new ArrayList<>();
\r
156 Set<MOMetaData> allMatchedChilds = new HashSet<>();
\r
157 for (ParameterDTO param : parameters) {
\r
158 String parentMONameInGnrc = param.getParamName().replaceAll(INDEX_REGEX, STRING_I);
\r
159 MOMetaData moData = metaDataMap.get(parentMONameInGnrc);
\r
160 if (moData != null) {
\r
161 allMatchedChilds.add(new MOMetaData(
\r
162 getTR69MOByReplacingIndexes(param.getParamName(), moData.getMoName()),
\r
163 moData.getDataType(), moData.isReadOnly(), moData.isTabluar(), moData.isTabluarObj()));
\r
166 for (MOMetaData metaData : allMatchedChilds) {
\r
167 ParameterDTO param = new ParameterDTO();
\r
168 String paramName = metaData.getMoName();
\r
170 param.setParamName(paramName);
\r
171 param.setDataType(metaData.getDataType());
\r
177 public List<ParameterDTO> getDeviceSupportedChildParameters() {
\r
178 List<ParameterDTO> result = new ArrayList<>();
\r
180 ParameterDTO param1 = new ParameterDTO();
\r
181 param1.setParamName("Device.DeviceInfo.Description");
\r
182 param1.setParamValue("Internal");
\r
183 param1.setDataType("string");
\r
184 result.add(param1);
\r
188 public static String getTR69MOByReplacingIndexes(String netconfMo, String tr69Mo) {
\r
190 String[] split = netconfMo.split("\\.");
\r
191 for (int i = 0; i < split.length; i++) {
\r
192 if (split[i].matches("[0-9]{1,}")) {
\r
193 tr69Mo = tr69Mo.replaceFirst(GENERIC_INDEX_REGEX, "." + split[i] + ".");
\r
201 public static String getNetConfMOByReplacingIndexes(String netconfMo, String tr69Mo) {
\r
203 String[] split = tr69Mo.split("\\.");
\r
204 for (int i = 0; i < split.length; i++) {
\r
205 if (split[i].matches("[0-9]{1,}")) {
\r
206 netconfMo = netconfMo.replaceFirst(GENERIC_INDEX_REGEX, "." + split[i] + ".");
\r