Fix sonar issue: try with resources
[dcaegen2/services/mapper.git] / UniversalVesAdapter / src / main / java / org / onap / universalvesadapter / service / VESAdapterInitializer.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.universalvesadapter.service;
21
22 import java.io.BufferedReader;
23 import java.io.File;
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;
33 import java.util.Map;
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;
46
47 //AdapterInitializer
48 @Component
49 public class VESAdapterInitializer implements CommandLineRunner, Ordered {
50         private static final Logger LOGGER = LoggerFactory.getLogger(VESAdapterInitializer.class);
51         
52         @Autowired
53         private Creator creator;
54         @Autowired
55         private DmaapConfig dmaapConfig;
56         @Value("${spring.datasource.url}")
57         String dBurl;
58         @Value("${spring.datasource.username}")
59         String user;
60         @Value("${spring.datasource.password}")
61         String pwd;
62         @Value("${defaultMappingFilelocation}")
63         String defaultMappingFileLocation;
64         @Value("${MappingFileTableName}")
65         String MappingFileTableName;
66         @Value("${defaultEnterpriseId}")
67         String defaultEnterpriseId;
68         @Value("${server.port}")
69         String serverPort;
70
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;
79     
80         @Override
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());
85                 }
86
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
89                         
90                         LOGGER.info(">>>Dynamic configuration to be used");
91                         
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 )))))) {
97                                 
98                                 
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;}));
101                                 
102                         }else {
103                                 
104                                 
105                                 
106                                 dmaapConfig.setDmaaphost(env.get("DMAAPHOST"));
107                                 dmaapConfig.setDEFAULT_PORT_NUMBER(Integer.parseInt(env.get("MR_DEFAULT_PORT_NUMBER")));
108                                 creator.setDmaapConfig(dmaapConfig);
109                                 
110                                 dBurl=env.get("URL_JDBC");
111                                 user=env.get("JDBC_USERNAME");
112                                 pwd=env.get("JDBC_PASSWORD");
113                         }
114                         
115                         
116         
117                 } else {
118                         LOGGER.info(">>>Static configuration to be used");
119                 }
120                 prepareDatabase();
121                 fetchMappingFile();
122                 
123                 LOGGER.info("Triggering controller's start url ");
124                 executecurl("http://localhost:"+serverPort+"/start");
125         }
126
127         private void getconsul() { 
128                 
129                 //TODO
130         }
131         
132         public static boolean verifyConfigChange() {
133                 //TODO
134                 return false;
135         }
136
137         public static void getCBS() {
138                 //TODO
139         }
140
141         public static void writefile(String retCBSString) {
142                 //TODO
143         }
144         
145         private static String executecurl(String url) {
146
147                 LOGGER.info("Running curl command for url:"+url);
148                 String[] command = { "curl", "-v", url };
149                 ProcessBuilder process = new ProcessBuilder(command);
150                 Process p;
151                 String result = null;
152                 try {
153                         p = process.start();
154                         InputStreamReader ipr = new InputStreamReader(p.getInputStream());
155                         BufferedReader reader = new BufferedReader(ipr);
156                         StringBuilder builder = new StringBuilder();
157                         String line;
158
159                         while ((line = reader.readLine()) != null) {
160                                 builder.append(line);
161                         }
162                         result = builder.toString();
163                         LOGGER.info(result);
164
165                         reader.close();
166                         ipr.close();
167                 } catch (IOException e) {
168                         LOGGER.error("error", e);
169                         e.printStackTrace();
170                 }
171                 return result;
172
173         }
174
175         public void fetchMappingFile() {
176                 
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");
182                         String hexString;
183                         while (rs.next()) {
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);
190                         }
191                         LOGGER.info("DB Initialization Completed, Total # Mappingfiles are" + mappingFiles.size());
192                 } catch (Exception e) {
193                         LOGGER.error("Error occured due to :" + e.getMessage());
194                         e.printStackTrace();
195                 }
196
197         }
198         
199         
200          private void prepareDatabase() throws IOException {       
201                  
202      
203          LOGGER.info("The Default Mapping file Location:"+defaultMappingFileLocation.trim());
204                  
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;}));
208                  }
209                  
210                  File file = new File(ClassLoader.getSystemResource(defaultMappingFileLocation.trim()).getFile());
211               FileInputStream fileInputStream=null;
212               
213                  try 
214                  {
215                  bytesArray = new byte[(int) file.length()];
216                  fileInputStream = new FileInputStream(file);
217              fileInputStream.read(bytesArray);
218                      
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;}));      
223            }finally {
224                    try {
225                                         fileInputStream.close();
226                                 }catch (IOException e) {
227                                         LOGGER.error("Exception while closing file inputstream" + e.getMessage(), e);
228                                 }
229             }
230                  
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" + 
236                      "(\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" + 
242                      ")\r\n" + 
243                      "WITH (\r\n" + 
244                      "    OIDS = FALSE\r\n" + 
245                      ")\r\n" + 
246                      "TABLESPACE pg_default;");
247              pstmt11.executeUpdate();
248              LOGGER.info("CREATE TABLE IF NOT EXISTS executed successfully....");
249              
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());
257        
258                  pstmt.executeUpdate();
259                  LOGGER.info("Made sure that default mapping file is present in table");
260              }
261              else {
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;}));
265              }
266               
267               
268               
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;}));
273                }
274           
275          }
276         public static Map<String, String> getMappingFiles() {
277                 return mappingFiles;
278         }
279
280         public static void setMappingFiles(Map<String, String> mappingFiles) {
281                 VESAdapterInitializer.mappingFiles = mappingFiles;
282         }
283
284         @Override
285         public int getOrder() {
286                 return 0;
287         }
288
289 }