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.universalvesadapter.service;
22 import java.io.BufferedReader;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStreamReader;
27 import java.sql.Connection;
28 import java.sql.DriverManager;
29 import java.sql.PreparedStatement;
30 import java.sql.ResultSet;
31 import java.sql.SQLException;
32 import java.util.HashMap;
34 import org.apache.commons.codec.binary.Hex;
35 import org.onap.universalvesadapter.dmaap.Creator;
36 import org.onap.universalvesadapter.utils.DmaapConfig;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.beans.factory.annotation.Value;
41 import org.springframework.boot.CommandLineRunner;
42 import org.springframework.boot.SpringApplication;
43 import org.springframework.context.ApplicationContext;
44 import org.springframework.core.Ordered;
45 import org.springframework.stereotype.Component;
49 public class VESAdapterInitializer implements CommandLineRunner, Ordered {
50 private static final Logger LOGGER = LoggerFactory.getLogger(VESAdapterInitializer.class);
53 private Creator creator;
55 private DmaapConfig dmaapConfig;
56 @Value("${spring.datasource.url}")
58 @Value("${spring.datasource.username}")
60 @Value("${spring.datasource.password}")
62 @Value("${defaultMappingFilelocation}")
63 String defaultMappingFileLocation;
64 @Value("${MappingFileTableName}")
65 String MappingFileTableName;
66 @Value("${defaultEnterpriseId}")
67 String defaultEnterpriseId;
68 @Value("${server.port}")
71 private static Map<String, String> mappingFiles = new HashMap<String, String>();
72 private static Map<String, String> env;
73 private static String url;
74 public static String retString;
75 public static String retCBSString;
76 public static String configFile = "/opt/app/KV-Configuration.json";
77 byte[] bytesArray = null;
78 @Autowired private ApplicationContext applicationContext;
81 public void run(String... args) throws Exception {
82 env = System.getenv();
83 for (Map.Entry<String, String> entry : env.entrySet()) {
84 LOGGER.info(entry.getKey() + ":" + entry.getValue());
87 if (env.containsKey("CONSUL_HOST") && env.containsKey("CONFIG_BINDING_SERVICE") && env.containsKey("HOSTNAME")) {
88 //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
90 LOGGER.info(">>>Dynamic configuration to be used");
92 if( (env.get("DMAAPHOST")==null ||
93 (env.get("MR_DEFAULT_PORT_NUMBER")==null ||
94 (env.get("URL_JDBC")==null ||
95 (env.get("JDBC_USERNAME")==null ||
96 (env.get("JDBC_PASSWORD")==null )))))) {
99 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");
100 System.exit(SpringApplication.exit(applicationContext, () -> {LOGGER.error("Application stoped due to missing default mapping file");return-1;}));
106 dmaapConfig.setDmaaphost(env.get("DMAAPHOST"));
107 dmaapConfig.setDEFAULT_PORT_NUMBER(Integer.parseInt(env.get("MR_DEFAULT_PORT_NUMBER")));
108 creator.setDmaapConfig(dmaapConfig);
110 dBurl=env.get("URL_JDBC");
111 user=env.get("JDBC_USERNAME");
112 pwd=env.get("JDBC_PASSWORD");
118 LOGGER.info(">>>Static configuration to be used");
123 LOGGER.info("Triggering controller's start url ");
124 executecurl("http://localhost:"+serverPort+"/start");
127 private void getconsul() {
132 public static boolean verifyConfigChange() {
137 public static void getCBS() {
141 public static void writefile(String retCBSString) {
145 private static String executecurl(String url) {
147 LOGGER.info("Running curl command for url:"+url);
148 String[] command = { "curl", "-v", url };
149 ProcessBuilder process = new ProcessBuilder(command);
151 String result = null;
154 InputStreamReader ipr = new InputStreamReader(p.getInputStream());
155 BufferedReader reader = new BufferedReader(ipr);
156 StringBuilder builder = new StringBuilder();
159 while ((line = reader.readLine()) != null) {
160 builder.append(line);
162 result = builder.toString();
167 } catch (IOException e) {
168 LOGGER.error("error", e);
175 public void fetchMappingFile() {
177 try (Connection con = DriverManager.getConnection(dBurl, user, pwd);PreparedStatement pstmt = con.prepareStatement("SELECT * FROM mapping_file");ResultSet rs = pstmt.executeQuery()) {
178 LOGGER.info("Retrieving data from DB");
179 // parsing the column each time is a linear search
180 int column1Pos = rs.findColumn("enterpriseid");
181 int column2Pos = rs.findColumn("mappingfilecontents");
184 String column1 = rs.getString(column1Pos);
185 String column2 = rs.getString(column2Pos);
186 hexString = column2.substring(2);
187 byte[] bytes = Hex.decodeHex(hexString.toCharArray());
188 String data = new String(bytes, "UTF-8");
189 mappingFiles.put(column1, data);
191 LOGGER.info("DB Initialization Completed, Total # Mappingfiles are" + mappingFiles.size());
192 } catch (Exception e) {
193 LOGGER.error("Error occured due to :" + e.getMessage());
200 private void prepareDatabase() throws IOException {
203 LOGGER.info("The Default Mapping file Location:"+defaultMappingFileLocation.trim());
205 if(ClassLoader.getSystemResource(defaultMappingFileLocation.trim())==null){
206 LOGGER.error("Default mapping file " + defaultMappingFileLocation.trim() + " is missing");
207 System.exit(SpringApplication.exit(applicationContext, () -> {LOGGER.error("Application stoped due to missing default mapping file");return-1;}));
210 File file = new File(ClassLoader.getSystemResource(defaultMappingFileLocation.trim()).getFile());
211 FileInputStream fileInputStream=null;
215 bytesArray = new byte[(int) file.length()];
216 fileInputStream = new FileInputStream(file);
217 fileInputStream.read(bytesArray);
219 } catch (IOException e1) {
220 LOGGER.error("Exception Occured while reading the default mapping file ,Cause: " + e1.getMessage(), e1);
221 //exit on missing default mapping file
222 System.exit(SpringApplication.exit(applicationContext, () -> {LOGGER.error("Application stoped due to missing default mapping file");return-1;}));
225 fileInputStream.close();
226 }catch (IOException e) {
227 LOGGER.error("Exception while closing file inputstream" + e.getMessage(), e);
231 try (Connection con = DriverManager.getConnection(dBurl, user, pwd)) {
232 LOGGER.info("Postgresql Connection successful...");
233 LOGGER.debug("Connection object:"+con.toString());
234 //creating table if not exist
235 PreparedStatement pstmt11=con.prepareStatement("CREATE TABLE IF NOT EXISTS public."+MappingFileTableName+"\r\n" +
237 " enterpriseid character varying COLLATE pg_catalog.\"default\" NOT NULL,\r\n" +
238 " mappingfilecontents bytea,\r\n" +
239 " mimetype character varying COLLATE pg_catalog.\"default\",\r\n" +
240 " file_name character varying COLLATE pg_catalog.\"default\",\r\n" +
241 " CONSTRAINT mapping_file_pkey5 PRIMARY KEY (enterpriseid)\r\n" +
244 " OIDS = FALSE\r\n" +
246 "TABLESPACE pg_default;");
247 pstmt11.executeUpdate();
248 LOGGER.info("CREATE TABLE IF NOT EXISTS executed successfully....");
250 if((bytesArray.length>0)&&(!bytesArray.toString().equals(""))) {
251 LOGGER.debug("2Connection object:"+con.toString());
252 PreparedStatement pstmt=con.prepareStatement("INSERT INTO "+MappingFileTableName+"(enterpriseid, mappingfilecontents, mimetype, File_Name) VALUES (?, ?, ?, ?) ON CONFLICT (enterpriseid) DO NOTHING;");
253 pstmt.setString(1,defaultEnterpriseId);
254 pstmt.setBytes(2,bytesArray );
255 pstmt.setString(3,"text/xml");
256 pstmt.setString(4, file.getName());
258 pstmt.executeUpdate();
259 LOGGER.info("Made sure that default mapping file is present in table");
262 LOGGER.error(file.getName()+" is empty");
263 //exit on empty mapping file
264 System.exit(SpringApplication.exit(applicationContext, () -> {LOGGER.error("Application stoped beacuase default mapping file is empty..");return-1;}));
269 } catch (SQLException e) {
270 LOGGER.error("Received exception : " + e.getMessage(), e);
271 //exit on SqlException
272 System.exit(SpringApplication.exit(applicationContext, () -> {LOGGER.error("Application Stoped due to ",e.getCause());return-1;}));
276 public static Map<String, String> getMappingFiles() {
280 public static void setMappingFiles(Map<String, String> mappingFiles) {
281 VESAdapterInitializer.mappingFiles = mappingFiles;
285 public int getOrder() {