2 * ============LICENSE_START========================================================================
3 * ONAP : tr-069-adapter
4 * =================================================================================================
5 * Copyright (C) 2020 CommScope Inc Intellectual Property.
6 * =================================================================================================
7 * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You
9 * may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14 * either express or implied. See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ===============LICENSE_END=======================================================================
19 package org.commscope.tr069adapter.mapper.util;
22 import java.io.IOException;
23 import java.nio.charset.StandardCharsets;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.List;
32 import javax.annotation.PostConstruct;
34 import org.apache.commons.io.FileUtils;
35 import org.commscope.tr069adapter.acs.common.ParameterDTO;
36 import org.commscope.tr069adapter.common.deviceversion.DeviceVersionManager;
37 import org.commscope.tr069adapter.common.deviceversion.ProfileDefinition;
38 import org.commscope.tr069adapter.mapper.MOMetaData;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.stereotype.Component;
45 public class MOMetaDataUtil {
47 private static final String STRING_I = ".{i}.";
48 private static final Logger LOG = LoggerFactory.getLogger(MOMetaDataUtil.class);
49 private static final String GENERIC_INDEX_REGEX = "\\.\\{[i-l]\\}\\.";
50 private static final String INDEX_REGEX = "\\.[0-9]{1,}\\.";
51 private static final String MO_META_DATA_FILE_LOCATION = "mapper-schema";
52 private Map<String, Map<String, MOMetaData>> metaDataMap;
53 private Map<String, Map<String, String>> metaDataReverseMap;
54 public static final String ORAN_SW_MGMT_URI = "urn:o-ran:software-management:1.0";
57 DeviceVersionManager versionManager;
60 public void loadMetaData() {
63 LOG.info("Loading mapper schema");
64 if (metaDataMap != null)
66 if (metaDataReverseMap != null)
67 metaDataReverseMap.clear();
68 getMetaDataAsMap(MO_META_DATA_FILE_LOCATION);
69 LOG.info("Loading mapper schema successfully completed");
70 } catch (IOException e) {
71 LOG.error("Exception : {}", e.getMessage());
75 private void getMetaDataAsMap(String fileLocation) throws IOException {
76 metaDataMap = new HashMap<>();
77 metaDataReverseMap = new HashMap<>();
79 List<ProfileDefinition> profiles = versionManager.getSupportedProfileDefinitions();
80 for (ProfileDefinition profileDefinition : profiles) {
81 Map<String, MOMetaData> profileMetaDataMap = new HashMap<>();
82 Map<String, String> profileMetaDataReverseMap = new HashMap<>();
84 Collection<File> files = FileUtils.listFiles(
85 new File(fileLocation + "/" + profileDefinition.getCsdmMappingPath()), null, false);
86 for (File file : files) {
87 LOG.info("Loading mapper schema from {}", file.getName());
88 List<String> lines = FileUtils.readLines(file, StandardCharsets.UTF_8);
89 for (String line : lines) {
90 if (line != null && line.startsWith("#")) {
93 if (line != null && line.split(",").length >= 3) {
94 parseMetaDataLine(line, profileMetaDataMap, profileMetaDataReverseMap);
98 metaDataMap.put(profileDefinition.getProfileId(), profileMetaDataMap);
99 metaDataReverseMap.put(profileDefinition.getProfileId(), profileMetaDataReverseMap);
103 private static void parseMetaDataLine(String line, Map<String, MOMetaData> profileMetaDataMap,
104 Map<String, String> profileMetaDataReverseMap) {
105 String[] split = line.split(",");
106 boolean isReadOnly = false;
107 boolean isTabluar = false;
108 boolean isTabObject = false;
109 if (split[2].contains("-")) {
110 String[] dataAttr = split[2].split("-");
111 if ("Tabular".equalsIgnoreCase(dataAttr[0]))
113 if ("ReadOnly".equalsIgnoreCase(dataAttr[1]))
115 } else if ("TabularObject".equalsIgnoreCase(split[2])) {
118 prepareMOMetaData(isReadOnly, isTabluar, split, isTabObject, profileMetaDataMap,
119 profileMetaDataReverseMap);
122 private static void prepareMOMetaData(boolean isReadOnly, boolean isTabluar, String[] split,
123 boolean isTabObject, Map<String, MOMetaData> profileMetaDataMap,
124 Map<String, String> profileMetaDataReverseMap) {
125 String dataType = "";
126 if (split.length > 3) {
130 String logMessage = split[1].substring(0, split[1].length() - 5);
131 LOG.info("Adding Parent Objects {}", logMessage);
132 String substring = split[0].substring(0, split[0].length() - 4);
133 MOMetaData metaTabData =
134 new MOMetaData(substring, dataType, isReadOnly, isTabluar, isTabObject);
135 if ((split.length > 4 && split[4] != null) && split[4].trim().length() > 0) {
136 metaTabData.setURI(split[4]);
138 profileMetaDataMap.put(logMessage, metaTabData);
140 MOMetaData metaData = new MOMetaData(split[0], dataType, isReadOnly, isTabluar, isTabObject);
141 if ((split.length > 4 && split[4] != null) && split[4].trim().length() > 0) {
142 metaData.setURI(split[4]);
144 profileMetaDataMap.put(split[1], metaData);
145 profileMetaDataReverseMap.put(split[0], split[1]);
148 public MOMetaData getMetaDataByNetConfName(String moName, String swVersion, String hwVersion) {
149 String moNameInGnrForm = moName.replaceAll(INDEX_REGEX, STRING_I);
150 String profileId = versionManager.getAssociatedProfileId(swVersion, hwVersion);
151 Map<String, MOMetaData> metaData = metaDataMap.get(profileId);
152 return metaData.get(moNameInGnrForm);
155 public MOMetaData getMetaDataByTR69Name(String moName, String swVersion, String hwVersion) {
156 String moNameInGnrForm = moName.replaceAll(INDEX_REGEX, STRING_I);
158 String profileId = versionManager.getAssociatedProfileId(swVersion, hwVersion);
159 Map<String, String> profileReverseMetaData = metaDataReverseMap.get(profileId);
160 Map<String, MOMetaData> profileMetaData = metaDataMap.get(profileId);
162 String netconfMoName = profileReverseMetaData.get(moNameInGnrForm);
163 return profileMetaData.get(netconfMoName);
166 public String getNetconfNameByTR69NameWithIndexes(String moName, String swVersion,
168 String moNameInGnrForm = moName.replaceAll(INDEX_REGEX, STRING_I);
169 String profileId = versionManager.getAssociatedProfileId(swVersion, hwVersion);
170 Map<String, String> reverseMetaData = metaDataReverseMap.get(profileId);
171 String netConfNMoName = reverseMetaData.get(moNameInGnrForm);
172 return netConfNMoName != null ? getNetConfMOByReplacingIndexes(netConfNMoName, moName)
176 public String getTR069NameByNetconfNameWithIndexes(String netconfName, String swVersion,
178 String moNameInGnrForm = netconfName.replaceAll(INDEX_REGEX, STRING_I);
179 String profileId = versionManager.getAssociatedProfileId(swVersion, hwVersion);
180 Map<String, MOMetaData> profileMetaData = metaDataMap.get(profileId);
181 String tr069MoName = profileMetaData.get(moNameInGnrForm).getMoName();
182 return tr069MoName != null ? getTR69MOByReplacingIndexes(netconfName, tr069MoName)
186 public List<ParameterDTO> getSupportedChildParameters(List<ParameterDTO> parameters,
187 String swVersion, String hwVersion) {
188 List<ParameterDTO> result = new ArrayList<>();
189 Set<MOMetaData> allMatchedChilds = new HashSet<>();
190 String profileId = versionManager.getAssociatedProfileId(swVersion, hwVersion);
191 Map<String, MOMetaData> profileMetaData = metaDataMap.get(profileId);
192 for (ParameterDTO param : parameters) {
193 String parentMONameInGnrc = param.getParamName().replaceAll(INDEX_REGEX, STRING_I);
194 MOMetaData moData = profileMetaData.get(parentMONameInGnrc);
195 if (moData != null) {
196 allMatchedChilds.add(new MOMetaData(
197 getTR69MOByReplacingIndexes(param.getParamName(), moData.getMoName()),
198 moData.getDataType(), moData.isReadOnly(), moData.isTabluar(), moData.isTabluarObj()));
201 for (MOMetaData metaData : allMatchedChilds) {
202 ParameterDTO param = new ParameterDTO();
203 String paramName = metaData.getMoName();
205 param.setParamName(paramName);
206 param.setDataType(metaData.getDataType());
212 public List<ParameterDTO> getDeviceSupportedChildParameters() {
213 List<ParameterDTO> result = new ArrayList<>();
215 ParameterDTO param1 = new ParameterDTO();
216 param1.setParamName("Device.DeviceInfo.Description");
217 param1.setParamValue("Internal");
218 param1.setDataType("string");
223 public static String getTR69MOByReplacingIndexes(String netconfMo, String tr69Mo) {
225 String[] split = netconfMo.split("\\.");
226 for (int i = 0; i < split.length; i++) {
227 if (split[i].matches("[0-9]{1,}")) {
228 tr69Mo = tr69Mo.replaceFirst(GENERIC_INDEX_REGEX, "." + split[i] + ".");
234 public static String getNetConfMOByReplacingIndexes(String netconfMo, String tr69Mo) {
236 String[] split = tr69Mo.split("\\.");
237 for (int i = 0; i < split.length; i++) {
238 if (split[i].matches("[0-9]{1,}")) {
239 netconfMo = netconfMo.replaceFirst(GENERIC_INDEX_REGEX, "." + split[i] + ".");
245 public String getNetconfXPathNameByTR69NameWithIndexes(String paramName, String swVersion,
247 String netconfName = getNetconfNameByTR69NameWithIndexes(paramName, swVersion, hwVersion);
248 if (netconfName == null)
250 String[] splitArray = netconfName.split("\\.");
251 StringBuilder sb = new StringBuilder();
252 StringBuilder nodeName = new StringBuilder();
253 String tokenizer = ".";
254 for (String token : splitArray) {
255 if (nodeName.length() == 0)
256 nodeName.append(token);
258 nodeName.append(tokenizer);
259 nodeName.append(token);
261 MOMetaData metaData = getMetaDataByNetConfName(nodeName.toString(), swVersion, hwVersion);
262 if (null != metaData && null != metaData.getURI()) {
263 sb.append("/" + token + "[" + "@xmlns=" + metaData.getURI() + "]");
264 } else if (token.matches("[0-9]*")) {
265 sb.append("[" + token + "]");
267 sb.append("/" + token);
270 return sb.toString();