Development of NETCONF RPCs for tr-069 adapter to
[oam/tr069-adapter.git] / mapper / src / main / java / org / commscope / tr069adapter / mapper / util / MOMetaDataUtil.java
1 /*
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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=======================================================================
17  */
18
19 package org.commscope.tr069adapter.mapper.util;
20
21 import java.io.File;
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;
29 import java.util.Map;
30 import java.util.Set;
31
32 import javax.annotation.PostConstruct;
33
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;
43
44 @Component
45 public class MOMetaDataUtil {
46
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";
55
56   @Autowired
57   DeviceVersionManager versionManager;
58
59   @PostConstruct
60   public void loadMetaData() {
61
62     try {
63       LOG.info("Loading mapper schema");
64       if (metaDataMap != null)
65         metaDataMap.clear();
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());
72     }
73   }
74
75   private void getMetaDataAsMap(String fileLocation) throws IOException {
76     metaDataMap = new HashMap<>();
77     metaDataReverseMap = new HashMap<>();
78
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<>();
83
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("#")) {
91             continue;
92           }
93           if (line != null && line.split(",").length >= 3) {
94             parseMetaDataLine(line, profileMetaDataMap, profileMetaDataReverseMap);
95           }
96         }
97       }
98       metaDataMap.put(profileDefinition.getProfileId(), profileMetaDataMap);
99       metaDataReverseMap.put(profileDefinition.getProfileId(), profileMetaDataReverseMap);
100     }
101   }
102
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]))
112         isTabluar = true;
113       if ("ReadOnly".equalsIgnoreCase(dataAttr[1]))
114         isReadOnly = true;
115     } else if ("TabularObject".equalsIgnoreCase(split[2])) {
116       isTabObject = true;
117     }
118     prepareMOMetaData(isReadOnly, isTabluar, split, isTabObject, profileMetaDataMap,
119         profileMetaDataReverseMap);
120   }
121
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) {
127       dataType = split[3];
128     }
129     if (isTabObject) {
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]);
137       }
138       profileMetaDataMap.put(logMessage, metaTabData);
139     }
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]);
143     }
144     profileMetaDataMap.put(split[1], metaData);
145     profileMetaDataReverseMap.put(split[0], split[1]);
146   }
147
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);
153   }
154
155   public MOMetaData getMetaDataByTR69Name(String moName, String swVersion, String hwVersion) {
156     String moNameInGnrForm = moName.replaceAll(INDEX_REGEX, STRING_I);
157
158     String profileId = versionManager.getAssociatedProfileId(swVersion, hwVersion);
159     Map<String, String> profileReverseMetaData = metaDataReverseMap.get(profileId);
160     Map<String, MOMetaData> profileMetaData = metaDataMap.get(profileId);
161
162     String netconfMoName = profileReverseMetaData.get(moNameInGnrForm);
163     return profileMetaData.get(netconfMoName);
164   }
165
166   public String getNetconfNameByTR69NameWithIndexes(String moName, String swVersion,
167       String hwVersion) {
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)
173         : netConfNMoName;
174   }
175
176   public String getTR069NameByNetconfNameWithIndexes(String netconfName, String swVersion,
177       String hwVersion) {
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)
183         : tr069MoName;
184   }
185
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()));
199       }
200     }
201     for (MOMetaData metaData : allMatchedChilds) {
202       ParameterDTO param = new ParameterDTO();
203       String paramName = metaData.getMoName();
204
205       param.setParamName(paramName);
206       param.setDataType(metaData.getDataType());
207       result.add(param);
208     }
209     return result;
210   }
211
212   public List<ParameterDTO> getDeviceSupportedChildParameters() {
213     List<ParameterDTO> result = new ArrayList<>();
214
215     ParameterDTO param1 = new ParameterDTO();
216     param1.setParamName("Device.DeviceInfo.Description");
217     param1.setParamValue("Internal");
218     param1.setDataType("string");
219     result.add(param1);
220     return result;
221   }
222
223   public static String getTR69MOByReplacingIndexes(String netconfMo, String tr69Mo) {
224
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] + ".");
229       }
230     }
231     return tr69Mo;
232   }
233
234   public static String getNetConfMOByReplacingIndexes(String netconfMo, String tr69Mo) {
235
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] + ".");
240       }
241     }
242     return netconfMo;
243   }
244
245   public String getNetconfXPathNameByTR69NameWithIndexes(String paramName, String swVersion,
246       String hwVersion) {
247     String netconfName = getNetconfNameByTR69NameWithIndexes(paramName, swVersion, hwVersion);
248     if (netconfName == null)
249       return 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);
257       else {
258         nodeName.append(tokenizer);
259         nodeName.append(token);
260       }
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 + "]");
266       } else {
267         sb.append("/" + token);
268       }
269     }
270     return sb.toString();
271   }
272
273 }