aboutsummaryrefslogtreecommitdiffstats
path: root/snmpmapper/src/main/java/org/onap/dcaegen2/services/mapper/snmpmapper/DAO/MappingFileDAOImpl.java
blob: 1fdba150918ac77a5133a4956016ab0a3f80794f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* ============LICENSE_START=======================================================
* ONAP : DCAE
* ================================================================================
* Copyright 2018 TechMahindra
*=================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.onap.dcaegen2.services.mapper.snmpmapper.DAO;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Repository;
import org.springframework.web.multipart.MultipartFile;

/**
 * This Class is use for connection to the database and upload the provided mapping file
 *
 */
@Repository
public class MappingFileDAOImpl implements MappingFileDAO {
	 private static final Logger debugLogger = LoggerFactory.getLogger("debugLogger");
	 private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger");
	 private static final Logger metricsLogger = LoggerFactory.getLogger("metricsLogger");

	@Value("${spring.datasource.url}")
	String url;
	@Value("${spring.datasource.username}")
	String user;
	@Value("${spring.datasource.password}")
	String pwd;
	@Autowired private ApplicationContext applicationContext;

	private static Map<String, String> env;
	@Override
	public String uploadMappingFile(MultipartFile mappingFile, String enterpriseid) throws SQLException, IOException {
		metricsLogger.info("Uploading mapping file");
		env = System.getenv();
		for (Map.Entry<String, String> entry : env.entrySet()) {
			debugLogger.info(entry.getKey() + ":" + entry.getValue());
		}

		if (env.containsKey("CONSUL_HOST") && env.containsKey("CONFIG_BINDING_SERVICE") && env.containsKey("HOSTNAME")) {
				//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
				
			debugLogger.info(">>>Dynamic configuration to be used");
				
				if( (env.get("MR_DMAAPHOST")==null || 
						(env.get("MR_DMAAPHOST")==null || 
						(env.get("MR_DEFAULT_PORT_NUMBER")==null || 
						(env.get("URL_JDBC")==null || 
						(env.get("JDBC_USERNAME")==null || 
						(env.get("JDBC_PASSWORD")==null ))))))) {
					
					
					errorLogger.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");
					System.exit(SpringApplication.exit(applicationContext, () -> {errorLogger.error("Application is stoped please provide the above environment parameter during docker run");return-1;}));
					
				}else {
					
					url=env.get("URL_JDBC");
					user=env.get("JDBC_USERNAME");
					pwd=env.get("JDBC_PASSWORD");
				}
			
		} else {
			debugLogger.info(">>>Static configuration to be used");
		}
		
		
		
		try (Connection con = DriverManager.getConnection(url, user, pwd);
		        PreparedStatement pstmt = con.prepareStatement(
	                    "INSERT INTO mapping_file(enterpriseid, mappingfilecontents, mimetype,  File_Name) VALUES (?, ?, ?, ?)")) {
			metricsLogger.info("Connection established successfully");

			pstmt.setString(1, enterpriseid);
			pstmt.setBytes(2, mappingFile.getBytes());
			pstmt.setString(3, mappingFile.getContentType());
			pstmt.setString(4, mappingFile.getOriginalFilename());

			pstmt.executeUpdate();

		}catch (Exception e) {
			errorLogger.error("Error occured due to :{}" , e.getMessage());
			throw e;
		}
		return "Uploaded successfully";

	}

}