Development of NETCONF RPCs for tr-069 adapter to
[oam/tr069-adapter.git] / config-data / src / main / java / org / commscope / tr069adapter / config / service / ConfigurationDataService.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.config.service;
20
21 import java.nio.charset.StandardCharsets;
22 import java.util.List;
23 import java.util.Map.Entry;
24 import java.util.Optional;
25 import java.util.TreeMap;
26
27 import org.commscope.tr069adapter.acs.common.dto.ConfigurationData;
28 import org.commscope.tr069adapter.config.constants.Utility;
29 import org.commscope.tr069adapter.config.exceptions.InvalidConfigurationServiceException;
30 import org.commscope.tr069adapter.config.model.ConfigFileContent;
31 import org.commscope.tr069adapter.config.parser.ConfigurationXMLDataParser;
32 import org.commscope.tr069adapter.config.repository.ConfigurationDataRepository;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Service;
37 import org.springframework.util.StringUtils;
38 import org.springframework.web.multipart.MultipartFile;
39
40 @Service
41 public class ConfigurationDataService {
42   private final Logger logger = LoggerFactory.getLogger(this.getClass());
43
44   @Autowired
45   ConfigurationDataRepository configDataRepository;
46
47   @Autowired
48   ConfigurationXMLDataParser configurationXMLDataParser;
49
50   public void saveConfigFileContent(ConfigFileContent configFileContent) {
51     configDataRepository.save(configFileContent);
52   }
53
54   public Iterable<ConfigFileContent> getAllConfigFileContent() {
55     return configDataRepository.findAll();
56   }
57
58   public Optional<ConfigFileContent> getConfigFileContent(String macId) {
59     return configDataRepository.findById(macId);
60   }
61
62   public Optional<ConfigurationData> getConfigurationData(String macId, String swVersion,
63       String hwVersion) throws InvalidConfigurationServiceException {
64     ConfigurationData configurationData = null;
65     List<ConfigFileContent> configFileContentList = configDataRepository.findByMacId(macId);
66     TreeMap<DeviceVersion, ConfigurationData> configDataMap = new TreeMap<>();
67     if (!configFileContentList.isEmpty()) {
68       logger.debug("Parsing configuration file for device {}", macId);
69       for (ConfigFileContent configFileContent : configFileContentList) {
70         ConfigurationData cfgData = configurationXMLDataParser.parseFile(configFileContent);
71         DeviceVersion dVersion =
72             new DeviceVersion(cfgData.getSoftwareVersion(), cfgData.getHardwareVersion());
73         configDataMap.put(dVersion, cfgData);
74       }
75       DeviceVersion inputVersion = new DeviceVersion(swVersion, hwVersion);
76       Entry<DeviceVersion, ConfigurationData> floorEntry = configDataMap.floorEntry(inputVersion);
77
78       if (null == floorEntry) {
79         macId = macId.replaceAll("[\n|\r|\t]", "_");
80         logger.error("Configuration file is not available for device {}", macId);
81         return Optional.ofNullable(configurationData);
82       }
83
84       DeviceVersion floor = floorEntry.getKey();
85       configurationData = configDataMap.get(floor);
86       logger.debug("Parsing of device configuration file is completed");
87     } else {
88       macId = macId.replaceAll("[\n|\r|\t]", "_");
89       logger.error("Configuration file is not available for device {}", macId);
90       return Optional.ofNullable(configurationData);
91     }
92     return Optional.ofNullable(configurationData);
93   }
94
95   public void saveConfigFileContents(MultipartFile file)
96       throws InvalidConfigurationServiceException {
97     String fileName = StringUtils.cleanPath(file.getOriginalFilename());
98
99     if (fileName.contains("..")) {
100       throw new InvalidConfigurationServiceException(
101           "Filename contains invalid path sequence " + fileName);
102     }
103
104     ConfigFileContent configFileContent = new ConfigFileContent();
105
106     try {
107       configFileContent.setFileContent(new String(file.getBytes(), StandardCharsets.UTF_8));
108       configFileContent.setMacId(Utility.getMacId(fileName));
109
110       if (!configFileContent.getFileContent().contains("<configDataFile>")) {
111         logger.error(
112             "File {} is not a valid configuration file as it doesn't contain tag \"<configDataFile>\"",
113             fileName);
114         throw new InvalidConfigurationServiceException(
115             "File is not a valid configuration file as it doesn't contain tag \"<configDataFile>\"");
116       }
117
118     } catch (Exception e) {
119       throw new InvalidConfigurationServiceException(
120           "Error occurred while reading file content. Reason: " + e.getMessage());
121     }
122
123     configurationXMLDataParser.validateFile(configFileContent);
124
125     ConfigurationData configurationData = configurationXMLDataParser.parseFile(configFileContent);
126     configFileContent.setSwVersion(configurationData.getSoftwareVersion());
127     configFileContent.setHwVersion(configurationData.getHardwareVersion());
128
129     logger.debug("Saving configuration file {} content for device of macId {}", fileName,
130         Utility.getMacId(fileName));
131     ConfigFileContent configFileContentEntity =
132         configDataRepository.findByMacIdAndSwVersionAndHwVersion(configFileContent.getMacId(),
133             configFileContent.getSwVersion(), configFileContent.getHwVersion());
134
135     if (configFileContentEntity != null) {
136       configFileContentEntity.setFileContent(configFileContent.getFileContent());
137       saveConfigFileContent(configFileContentEntity);
138     } else {
139       saveConfigFileContent(configFileContent);
140     }
141
142     logger.debug("Configuration file content saved successfully");
143   }
144
145   class DeviceVersion implements Comparable<DeviceVersion> {
146     private static final long serialVersionUID = -7251276716604249440L;
147     private int svMajorVersion = 0;
148     private int svMinorVersion = 0;
149     private int svPatchVersion = 0;
150     private boolean isGenericVersion = false;
151
152     public DeviceVersion(String swVersion, String hwVersion) {
153       super();
154       setSwVersion(swVersion);
155       this.hwVersion = hwVersion;
156     }
157
158     public String getSwVersion() {
159       return svMajorVersion + "." + svMinorVersion + "." + svPatchVersion;
160     }
161
162     public void setSwVersion(String swVersion) {
163       // TODO: conversion to integers
164
165       if (swVersion.indexOf(".") > 0) {
166         String[] verArray = swVersion.split("\\.");
167
168
169         for (int i = 0; i < verArray.length; i++) {
170
171           if (verArray[i].equals("*")) {
172             verArray[i] = "0";
173           }
174         }
175         svMajorVersion = Integer.parseInt(verArray[0]);
176         svMinorVersion = Integer.parseInt(verArray[1]);
177         svPatchVersion = Integer.parseInt(verArray[2]);
178
179       } else if (swVersion.indexOf("x") > 0) {
180         swVersion = "*";
181       } else if (swVersion.equals("*")) {
182         isGenericVersion = true;
183       }
184     }
185
186     public String getHwVersion() {
187       return hwVersion;
188     }
189
190     public void setHwVersion(String hwVersion) {
191       this.hwVersion = hwVersion;
192     }
193
194     private String hwVersion;
195
196     public int getSvMajorVersion() {
197       return svMajorVersion;
198     }
199
200     public void setSvMajorVersion(int svMajorVersion) {
201       this.svMajorVersion = svMajorVersion;
202     }
203
204     public int getSvMinorVersion() {
205       return svMinorVersion;
206     }
207
208     public void setSvMinorVersion(int svMinorVersion) {
209       this.svMinorVersion = svMinorVersion;
210     }
211
212     public int getSvPatchVersion() {
213       return svPatchVersion;
214     }
215
216     public void setSvPatchVersion(int svPatchVersion) {
217       this.svPatchVersion = svPatchVersion;
218     }
219
220     public boolean isGenericVersion() {
221       return isGenericVersion;
222     }
223
224     public void setGenericVersion(boolean isGenericVersion) {
225       this.isGenericVersion = isGenericVersion;
226     }
227
228     @Override
229     public int compareTo(DeviceVersion o) {
230
231       if (svMajorVersion != o.svMajorVersion) {
232         return (svMajorVersion - o.svMajorVersion);
233       } else if (svMinorVersion != o.svMinorVersion) {
234         return svMinorVersion - o.svMinorVersion;
235       } else {
236         return svPatchVersion - o.svPatchVersion;
237       }
238     }
239   }
240
241 }