2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright 2018 TechMahindra
6 *=================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.dcaegen2.services.mapper.snmpmapper.DAO;
22 import java.io.IOException;
23 import java.sql.Connection;
24 import java.sql.DriverManager;
25 import java.sql.PreparedStatement;
26 import java.sql.SQLException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.beans.factory.annotation.Value;
32 import org.springframework.boot.SpringApplication;
33 import org.springframework.context.ApplicationContext;
34 import org.springframework.stereotype.Repository;
35 import org.springframework.web.multipart.MultipartFile;
38 * This Class is use for connection to the database and upload the provided mapping file
42 public class MappingFileDAOImpl implements MappingFileDAO {
43 private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
45 @Value("${spring.datasource.url}")
47 @Value("${spring.datasource.username}")
49 @Value("${spring.datasource.password}")
51 @Autowired private ApplicationContext applicationContext;
53 private static Map<String, String> env;
55 public String uploadMappingFile(MultipartFile mappingFile, String enterpriseid) throws SQLException, IOException {
57 env = System.getenv();
58 for (Map.Entry<String, String> entry : env.entrySet()) {
59 LOGGER.info(entry.getKey() + ":" + entry.getValue());
62 if (env.containsKey("CONSUL_HOST") && env.containsKey("CONFIG_BINDING_SERVICE") && env.containsKey("HOSTNAME")) {
63 //TODO - Add logic to talk to Consul and CBS to get the configuration. For now, we will refer to configuration coming from docker env parameters
65 LOGGER.info(">>>Dynamic configuration to be used");
67 if( (env.get("MR_DMAAPHOST")==null ||
68 (env.get("MR_DMAAPHOST")==null ||
69 (env.get("MR_DEFAULT_PORT_NUMBER")==null ||
70 (env.get("URL_JDBC")==null ||
71 (env.get("JDBC_USERNAME")==null ||
72 (env.get("JDBC_PASSWORD")==null ))))))) {
75 LOGGER.error("Some docker environment parameter is missing. Sample Usage is -\n sudo docker run -d -p 8085:8085/tcp --env URL_JDBC=jdbc:postgresql://10.53.172.129:5432/dummy --env JDBC_USERNAME=ngpuser --env JDBC_PASSWORD=root --env MR_DMAAPHOST=10.10.10.10 --env MR_DEFAULT_PORT_NUMBER=3904 --env CONSUL_HOST=10.53.172.109 --env HOSTNAME=mvp-dcaegen2-collectors-ves --env CONFIG_BINDING_SERVICE=config_binding_service -e DMAAPHOST='10.53.172.156' onap/org.onap.dcaegen2.services.mapper.vesadapter.universalvesadaptor:latest");
76 System.exit(SpringApplication.exit(applicationContext, () -> {LOGGER.error("Application is stoped please provide the above environment parameter during docker run");return-1;}));
80 url=env.get("URL_JDBC");
81 user=env.get("JDBC_USERNAME");
82 pwd=env.get("JDBC_PASSWORD");
86 LOGGER.info(">>>Static configuration to be used");
91 try (Connection con = DriverManager.getConnection(url, user, pwd);
92 PreparedStatement pstmt = con.prepareStatement(
93 "INSERT INTO mapping_file(enterpriseid, mappingfilecontents, mimetype, File_Name) VALUES (?, ?, ?, ?)")) {
94 LOGGER.debug("Connection established successfully");
96 pstmt.setString(1, enterpriseid);
97 pstmt.setBytes(2, mappingFile.getBytes());
98 pstmt.setString(3, mappingFile.getContentType());
99 pstmt.setString(4, mappingFile.getOriginalFilename());
101 pstmt.executeUpdate();
103 }catch (Exception e) {
104 LOGGER.error("Error occured due to :" + e.getMessage());
106 return "Uploaded successfully";