Config from docker env params
[dcaegen2/services/mapper.git] / snmpmapper / src / main / java / org / onap / dcaegen2 / services / mapper / snmpmapper / DAO / MappingFileDAOImpl.java
1 /*
2 * ============LICENSE_START=======================================================
3 * ONAP : DCAE
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
10 *
11 *     http://www.apache.org/licenses/LICENSE-2.0
12 *
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=========================================================
19 */
20 package org.onap.dcaegen2.services.mapper.snmpmapper.DAO;
21
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;
27 import java.util.Map;
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;
36
37 /**
38  * This Class is use for connection to the database and upload the provided mapping file
39  *
40  */
41 @Repository
42 public class MappingFileDAOImpl implements MappingFileDAO {
43         private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
44
45         @Value("${spring.datasource.url}")
46         String url;
47         @Value("${spring.datasource.username}")
48         String user;
49         @Value("${spring.datasource.password}")
50         String pwd;
51         @Autowired private ApplicationContext applicationContext;
52
53         private static Map<String, String> env;
54         @Override
55         public String uploadMappingFile(MultipartFile mappingFile, String enterpriseid) throws SQLException, IOException {
56                 
57                 env = System.getenv();
58                 for (Map.Entry<String, String> entry : env.entrySet()) {
59                         LOGGER.info(entry.getKey() + ":" + entry.getValue());
60                 }
61
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
64                                 
65                                 LOGGER.info(">>>Dynamic configuration to be used");
66                                 
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 ))))))) {
73                                         
74                                         
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;}));
77                                         
78                                 }else {
79                                         
80                                         url=env.get("URL_JDBC");
81                                         user=env.get("JDBC_USERNAME");
82                                         pwd=env.get("JDBC_PASSWORD");
83                                 }
84                         
85                 } else {
86                         LOGGER.info(">>>Static configuration to be used");
87                 }
88                 
89                 
90                 
91                 try (Connection con = DriverManager.getConnection(url, user, pwd)) {
92                         LOGGER.debug("Connection established successfully");
93                         PreparedStatement pstmt = con.prepareStatement(
94                                         "INSERT INTO mapping_file(enterpriseid, mappingfilecontents, mimetype,  File_Name) VALUES (?, ?, ?, ?)");
95
96                         pstmt.setString(1, enterpriseid);
97                         pstmt.setBytes(2, mappingFile.getBytes());
98                         pstmt.setString(3, mappingFile.getContentType());
99                         pstmt.setString(4, mappingFile.getOriginalFilename());
100
101                         pstmt.executeUpdate();
102
103                 }catch (Exception e) {
104                         LOGGER.error("Error occured due to :" + e.getMessage());
105                         e.printStackTrace();
106                 }
107                 return "Uploaded successfully";
108
109         }
110
111 }