a91e5c67c0b861d1e5ed341098a65c4f18706e60
[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)) {
178                         LOGGER.info("Retrieving data from DB");
179                         PreparedStatement pstmt = con.prepareStatement("SELECT * FROM mapping_file");
180                         ResultSet rs = pstmt.executeQuery();
181                         // parsing the column each time is a linear search
182                         int column1Pos = rs.findColumn("enterpriseid");
183                         int column2Pos = rs.findColumn("mappingfilecontents");
184                         String hexString;
185                         while (rs.next()) {
186                                 String column1 = rs.getString(column1Pos);
187                                 String column2 = rs.getString(column2Pos);
188                                 hexString = column2.substring(2);
189                                 byte[] bytes = Hex.decodeHex(hexString.toCharArray());
190                                 String data = new String(bytes, "UTF-8");
191                                 mappingFiles.put(column1, data);
192                         }
193                         LOGGER.info("DB Initialization Completed, Total # Mappingfiles are" + mappingFiles.size());
194                 } catch (Exception e) {
195                         LOGGER.error("Error occured due to :" + e.getMessage());
196                         e.printStackTrace();
197                 }
198
199         }
200         
201         
202          private void prepareDatabase() throws IOException {       
203                  
204      
205          LOGGER.info("The Default Mapping file Location:"+defaultMappingFileLocation.trim());
206                  
207                  if(ClassLoader.getSystemResource(defaultMappingFileLocation.trim())==null){
208                          LOGGER.error("Default mapping file " + defaultMappingFileLocation.trim() + " is missing");
209                   System.exit(SpringApplication.exit(applicationContext, () -> {LOGGER.error("Application stoped due to missing default mapping file");return-1;}));
210                  }
211                  
212                  File file = new File(ClassLoader.getSystemResource(defaultMappingFileLocation.trim()).getFile());
213               FileInputStream fileInputStream=null;
214               
215                  try 
216                  {
217                  bytesArray = new byte[(int) file.length()];
218                  fileInputStream = new FileInputStream(file);
219              fileInputStream.read(bytesArray);
220                      
221           } catch (IOException e1) {
222            LOGGER.error("Exception Occured while reading the default mapping file ,Cause: " + e1.getMessage(), e1);                    
223             //exit on missing default mapping file 
224             System.exit(SpringApplication.exit(applicationContext, () -> {LOGGER.error("Application stoped due to missing default mapping file");return-1;}));      
225            }finally {
226                    try {
227                                         fileInputStream.close();
228                                 }catch (IOException e) {
229                                         LOGGER.error("Exception while closing file inputstream" + e.getMessage(), e);
230                                 }
231             }
232                  
233                try (Connection con = DriverManager.getConnection(dBurl, user, pwd)) {
234                      LOGGER.info("Postgresql Connection successful...");
235                      LOGGER.debug("Connection object:"+con.toString());
236               //creating table if not exist
237              PreparedStatement pstmt11=con.prepareStatement("CREATE TABLE IF NOT EXISTS public."+MappingFileTableName+"\r\n" + 
238                      "(\r\n" + 
239                      "    enterpriseid character varying COLLATE pg_catalog.\"default\" NOT NULL,\r\n" + 
240                      "    mappingfilecontents bytea,\r\n" + 
241                      "    mimetype character varying COLLATE pg_catalog.\"default\",\r\n" + 
242                      "    file_name character varying COLLATE pg_catalog.\"default\",\r\n" + 
243                      "    CONSTRAINT mapping_file_pkey5 PRIMARY KEY (enterpriseid)\r\n" + 
244                      ")\r\n" + 
245                      "WITH (\r\n" + 
246                      "    OIDS = FALSE\r\n" + 
247                      ")\r\n" + 
248                      "TABLESPACE pg_default;");
249              pstmt11.executeUpdate();
250              LOGGER.info("CREATE TABLE IF NOT EXISTS executed successfully....");
251              
252              if((bytesArray.length>0)&&(!bytesArray.toString().equals(""))) {
253                 LOGGER.debug("2Connection object:"+con.toString());
254                  PreparedStatement pstmt=con.prepareStatement("INSERT INTO "+MappingFileTableName+"(enterpriseid, mappingfilecontents, mimetype,  File_Name) VALUES (?, ?, ?, ?) ON CONFLICT (enterpriseid) DO NOTHING;");
255                  pstmt.setString(1,defaultEnterpriseId);
256                  pstmt.setBytes(2,bytesArray );
257                  pstmt.setString(3,"text/xml");
258                  pstmt.setString(4, file.getName());
259        
260                  pstmt.executeUpdate();
261                  LOGGER.info("Made sure that default mapping file is present in table");
262              }
263              else {
264                  LOGGER.error(file.getName()+" is empty");
265                   //exit on empty mapping file
266                   System.exit(SpringApplication.exit(applicationContext, () -> {LOGGER.error("Application stoped beacuase default mapping file is empty..");return-1;}));
267              }
268               
269               
270               
271                } catch (SQLException e) {
272                      LOGGER.error("Received exception : " + e.getMessage(), e);
273                      //exit on SqlException
274                      System.exit(SpringApplication.exit(applicationContext, () -> {LOGGER.error("Application Stoped due to ",e.getCause());return-1;}));
275                }
276           
277          }
278         public static Map<String, String> getMappingFiles() {
279                 return mappingFiles;
280         }
281
282         public static void setMappingFiles(Map<String, String> mappingFiles) {
283                 VESAdapterInitializer.mappingFiles = mappingFiles;
284         }
285
286         @Override
287         public int getOrder() {
288                 return 0;
289         }
290
291 }