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.config.service;
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;
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;
41 public class ConfigurationDataService {
42 private final Logger logger = LoggerFactory.getLogger(this.getClass());
45 ConfigurationDataRepository configDataRepository;
48 ConfigurationXMLDataParser configurationXMLDataParser;
50 public void saveConfigFileContent(ConfigFileContent configFileContent) {
51 configDataRepository.save(configFileContent);
54 public Iterable<ConfigFileContent> getAllConfigFileContent() {
55 return configDataRepository.findAll();
58 public Optional<ConfigFileContent> getConfigFileContent(String macId) {
59 return configDataRepository.findById(macId);
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);
75 DeviceVersion inputVersion = new DeviceVersion(swVersion, hwVersion);
76 Entry<DeviceVersion, ConfigurationData> floorEntry = configDataMap.floorEntry(inputVersion);
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);
84 DeviceVersion floor = floorEntry.getKey();
85 configurationData = configDataMap.get(floor);
86 logger.debug("Parsing of device configuration file is completed");
88 macId = macId.replaceAll("[\n|\r|\t]", "_");
89 logger.error("Configuration file is not available for device {}", macId);
90 return Optional.ofNullable(configurationData);
92 return Optional.ofNullable(configurationData);
95 public void saveConfigFileContents(MultipartFile file)
96 throws InvalidConfigurationServiceException {
97 String fileName = StringUtils.cleanPath(file.getOriginalFilename());
99 if (fileName.contains("..")) {
100 throw new InvalidConfigurationServiceException(
101 "Filename contains invalid path sequence " + fileName);
104 ConfigFileContent configFileContent = new ConfigFileContent();
107 configFileContent.setFileContent(new String(file.getBytes(), StandardCharsets.UTF_8));
108 configFileContent.setMacId(Utility.getMacId(fileName));
110 if (!configFileContent.getFileContent().contains("<configDataFile>")) {
112 "File {} is not a valid configuration file as it doesn't contain tag \"<configDataFile>\"",
114 throw new InvalidConfigurationServiceException(
115 "File is not a valid configuration file as it doesn't contain tag \"<configDataFile>\"");
118 } catch (Exception e) {
119 throw new InvalidConfigurationServiceException(
120 "Error occurred while reading file content. Reason: " + e.getMessage());
123 configurationXMLDataParser.validateFile(configFileContent);
125 ConfigurationData configurationData = configurationXMLDataParser.parseFile(configFileContent);
126 configFileContent.setSwVersion(configurationData.getSoftwareVersion());
127 configFileContent.setHwVersion(configurationData.getHardwareVersion());
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());
135 if (configFileContentEntity != null) {
136 configFileContentEntity.setFileContent(configFileContent.getFileContent());
137 saveConfigFileContent(configFileContentEntity);
139 saveConfigFileContent(configFileContent);
142 logger.debug("Configuration file content saved successfully");
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;
152 public DeviceVersion(String swVersion, String hwVersion) {
154 setSwVersion(swVersion);
155 this.hwVersion = hwVersion;
158 public String getSwVersion() {
159 return svMajorVersion + "." + svMinorVersion + "." + svPatchVersion;
162 public void setSwVersion(String swVersion) {
163 // TODO: conversion to integers
165 if (swVersion.indexOf(".") > 0) {
166 String[] verArray = swVersion.split("\\.");
169 for (int i = 0; i < verArray.length; i++) {
171 if (verArray[i].equals("*")) {
175 svMajorVersion = Integer.parseInt(verArray[0]);
176 svMinorVersion = Integer.parseInt(verArray[1]);
177 svPatchVersion = Integer.parseInt(verArray[2]);
179 } else if (swVersion.indexOf("x") > 0) {
181 } else if (swVersion.equals("*")) {
182 isGenericVersion = true;
186 public String getHwVersion() {
190 public void setHwVersion(String hwVersion) {
191 this.hwVersion = hwVersion;
194 private String hwVersion;
196 public int getSvMajorVersion() {
197 return svMajorVersion;
200 public void setSvMajorVersion(int svMajorVersion) {
201 this.svMajorVersion = svMajorVersion;
204 public int getSvMinorVersion() {
205 return svMinorVersion;
208 public void setSvMinorVersion(int svMinorVersion) {
209 this.svMinorVersion = svMinorVersion;
212 public int getSvPatchVersion() {
213 return svPatchVersion;
216 public void setSvPatchVersion(int svPatchVersion) {
217 this.svPatchVersion = svPatchVersion;
220 public boolean isGenericVersion() {
221 return isGenericVersion;
224 public void setGenericVersion(boolean isGenericVersion) {
225 this.isGenericVersion = isGenericVersion;
229 public int compareTo(DeviceVersion o) {
231 if (svMajorVersion != o.svMajorVersion) {
232 return (svMajorVersion - o.svMajorVersion);
233 } else if (svMinorVersion != o.svMinorVersion) {
234 return svMinorVersion - o.svMinorVersion;
236 return svPatchVersion - o.svPatchVersion;