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.config.service;
\r
21 import java.nio.charset.StandardCharsets;
\r
22 import java.util.Optional;
\r
24 import org.commscope.tr069adapter.config.constants.Utility;
\r
25 import org.commscope.tr069adapter.config.dto.ConfigurationData;
\r
26 import org.commscope.tr069adapter.config.exceptions.InvalidConfigurationServiceException;
\r
27 import org.commscope.tr069adapter.config.model.ConfigFileContent;
\r
28 import org.commscope.tr069adapter.config.parser.ConfigurationXMLDataParser;
\r
29 import org.commscope.tr069adapter.config.repository.ConfigurationDataRepository;
\r
30 import org.slf4j.Logger;
\r
31 import org.slf4j.LoggerFactory;
\r
32 import org.springframework.beans.factory.annotation.Autowired;
\r
33 import org.springframework.stereotype.Service;
\r
34 import org.springframework.util.StringUtils;
\r
35 import org.springframework.web.multipart.MultipartFile;
\r
38 public class ConfigurationDataService {
\r
39 private final Logger logger = LoggerFactory.getLogger(this.getClass());
\r
42 ConfigurationDataRepository configDataRepository;
\r
45 ConfigurationXMLDataParser configurationXMLDataParser;
\r
47 public void saveConfigFileContent(ConfigFileContent configFileContent) {
\r
48 configDataRepository.save(configFileContent);
\r
51 public Iterable<ConfigFileContent> getAllConfigFileContent() {
\r
52 return configDataRepository.findAll();
\r
55 public Optional<ConfigFileContent> getConfigFileContent(String macId) {
\r
56 return configDataRepository.findById(macId);
\r
59 public Optional<ConfigurationData> getConfigurationData(String macId)
\r
60 throws InvalidConfigurationServiceException {
\r
61 ConfigurationData configurationData = null;
\r
62 Optional<ConfigFileContent> configFileContent = configDataRepository.findById(macId);
\r
64 if (configFileContent.isPresent()) {
\r
65 logger.debug("Parsing configuration file for device {}", macId);
\r
66 configurationData = configurationXMLDataParser.parseFile(configFileContent.get());
\r
67 logger.debug("Parsing of device configuration file is completed");
\r
69 logger.error("Configuration file is not available for device {}", macId);
\r
70 return Optional.ofNullable(configurationData);
\r
72 return Optional.ofNullable(configurationData);
\r
75 public void saveConfigFileContents(MultipartFile file)
\r
76 throws InvalidConfigurationServiceException {
\r
77 String fileName = StringUtils.cleanPath(file.getOriginalFilename());
\r
79 if (fileName.contains("..")) {
\r
80 throw new InvalidConfigurationServiceException(
\r
81 "Filename contains invalid path sequence " + fileName);
\r
84 ConfigFileContent configFileContent = new ConfigFileContent();
\r
87 configFileContent.setFileContent(new String(file.getBytes(), StandardCharsets.UTF_8));
\r
88 configFileContent.setMacId(Utility.getMacId(fileName));
\r
90 if (!configFileContent.getFileContent().contains("<configDataFile>")) {
\r
92 "File {} is not a valid configuration file as it doesn't contain tag \"<configDataFile>\"",
\r
94 throw new InvalidConfigurationServiceException(
\r
95 "File is not a valid configuration file as it doesn't contain tag \"<configDataFile>\"");
\r
98 } catch (Exception e) {
\r
99 throw new InvalidConfigurationServiceException(
\r
100 "Error occurred while reading file content. Reason: " + e.getMessage());
\r
103 configurationXMLDataParser.validateFile(configFileContent);
\r
105 logger.debug("Saving configuration file {} content for device of macId {}", fileName,
\r
106 Utility.getMacId(fileName));
\r
107 saveConfigFileContent(configFileContent);
\r
108 logger.debug("Configuration file content saved successfully");
\r