Consul update (via SDK library) to support periodic polling
[dcaegen2/services/mapper.git] / UniversalVesAdapter / src / main / java / org / onap / universalvesadapter / service / VESAdapterInitializer.java
index 2ac4fe9..e7e4705 100644 (file)
@@ -23,13 +23,24 @@ package org.onap.universalvesadapter.service;
 import java.io.BufferedReader;\r
 import java.io.IOException;\r
 import java.io.InputStreamReader;\r
+import java.time.Duration;\r
 import java.util.HashMap;\r
 import java.util.Map;\r
 import java.util.Map.Entry;\r
 import java.util.Set;\r
 \r
+import org.apache.http.HttpResponse;\r
+import org.apache.http.client.ClientProtocolException;\r
+import org.apache.http.client.HttpClient;\r
+import org.apache.http.client.methods.HttpGet;\r
+import org.apache.http.impl.client.HttpClientBuilder;\r
 import org.json.simple.JSONArray;\r
 import org.json.simple.JSONObject;\r
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClientFactory;\r
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsRequests;\r
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest;\r
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;\r
+import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;\r
 import org.onap.universalvesadapter.utils.CollectorConfigPropertyRetrival;\r
 import org.onap.universalvesadapter.utils.FetchDynamicConfig;\r
 import org.slf4j.Logger;\r
@@ -45,208 +56,142 @@ import org.springframework.stereotype.Component;
 // AdapterInitializer\r
 @Component\r
 public class VESAdapterInitializer implements CommandLineRunner, Ordered {\r
-    private static final Logger debugLogger = LoggerFactory.getLogger("debugLogger");\r
-    private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger");\r
-    \r
-    @Value("${defaultConfigFilelocation}")\r
-    String defaultConfigFilelocation;\r
-    @Value("${server.port}")\r
-    String serverPort;\r
-    \r
-    private static Map<String, String> mappingFiles = new HashMap<String, String>();\r
-    private static Map<String, String> env;\r
-    \r
-    @Autowired\r
-    private ApplicationContext applicationContext;\r
-    \r
-    @Override\r
-    public void run(String... args) throws Exception {\r
-        debugLogger.info("The Default Config file Location:" + defaultConfigFilelocation.trim());\r
-        \r
-        if (ClassLoader.getSystemResource(defaultConfigFilelocation.trim()) == null) {\r
-            errorLogger.error(\r
-                    "Default Config file " + defaultConfigFilelocation.trim() + " is missing");\r
-            System.exit(SpringApplication.exit(applicationContext, () -> {\r
-                errorLogger.error("Application stoped due to missing default Config file");\r
-                return -1;\r
-            }));\r
-        }\r
-        env = System.getenv();\r
-        for (Map.Entry<String, String> entry : env.entrySet()) {\r
-            debugLogger.debug(entry.getKey() + ":" + entry.getValue());\r
-        }\r
-        \r
-        // check for consul details\r
-        if (env.containsKey("CONSUL_HOST") && env.containsKey("CONFIG_BINDING_SERVICE")\r
-                && env.containsKey("HOSTNAME")) {\r
-            debugLogger.info(">>>Dynamic configuration to be used");\r
-            FetchDynamicConfig.cbsCall(defaultConfigFilelocation);\r
-            \r
-        } else {\r
-            debugLogger.info(">>>Static configuration to be used");\r
-            \r
-        }\r
-        readJsonToMap(defaultConfigFilelocation);\r
-        \r
-        // prepareDatabase();\r
-        // fetchMappingFile();\r
-        \r
-        debugLogger.info("Triggering controller's start url ");\r
-        executecurl("http://localhost:" + serverPort + "/start");\r
-    }\r
-    \r
-    \r
-    private static String executecurl(String url) {\r
-        \r
-        debugLogger.info("Running curl command for url:{}", url);\r
-        String[] command = {"curl", "-v", url};\r
-        ProcessBuilder process = new ProcessBuilder(command);\r
-        Process p;\r
-        String result = null;\r
-        try {\r
-            p = process.start();\r
-            try (InputStreamReader ipr = new InputStreamReader(p.getInputStream());\r
-                    BufferedReader reader = new BufferedReader(ipr)) {\r
-                StringBuilder builder = new StringBuilder();\r
-                String line;\r
-                \r
-                while ((line = reader.readLine()) != null) {\r
-                    builder.append(line);\r
-                }\r
-                result = builder.toString();\r
-            }\r
-        } catch (IOException e) {\r
-            errorLogger.error("error", e);\r
-        }\r
-        return result;\r
-        \r
-    }\r
-    \r
-    private void readJsonToMap(String configFile) {\r
-        try {\r
-            JSONArray collectorArray =\r
-                    CollectorConfigPropertyRetrival.collectorConfigArray(configFile);\r
-            \r
-            for (int i = 0; i < collectorArray.size(); i++) {\r
-                JSONObject obj2 = (JSONObject) collectorArray.get(i);\r
-                \r
-                if (obj2.containsKey("mapping-files")) {\r
-                    \r
-                    JSONArray a1 = (JSONArray) obj2.get("mapping-files");\r
-                    \r
-                    for (int j = 0; j < a1.size(); j++) {\r
-                        JSONObject obj3 = (JSONObject) a1.get(j);\r
-                        Set<Entry<String, String>> set = obj3.entrySet();\r
-                        \r
-                        for (Entry<String, String> entry : set) {\r
-                            \r
-                            mappingFiles.put(entry.getKey(), entry.getValue());\r
-                        }\r
-                    }\r
-                    \r
-                }\r
-            }\r
-            \r
-        } catch (Exception e) {\r
-            e.printStackTrace();\r
-            errorLogger.error("Exception occured while reading Collector config file cause: ",\r
-                    e.getCause());\r
-        }\r
-        \r
-    }\r
-    \r
-    \r
-    /*\r
-     * private void prepareDatabase() throws IOException {\r
-     * \r
-     * \r
-     * debugLogger.info("The Default Mapping file Location:" + defaultMappingFileLocation.trim());\r
-     * \r
-     * if (ClassLoader.getSystemResource(defaultMappingFileLocation.trim()) == null) {\r
-     * errorLogger.error( "Default mapping file " + defaultMappingFileLocation.trim() +\r
-     * " is missing"); System.exit(SpringApplication.exit(applicationContext, () -> {\r
-     * errorLogger.error("Application stoped due to missing default mapping file"); return -1; }));\r
-     * }\r
-     * \r
-     * File file = new File(\r
-     * ClassLoader.getSystemResource(defaultMappingFileLocation.trim()).getFile());\r
-     * \r
-     * try (FileInputStream fileInputStream = new FileInputStream(file)) { bytesArray = new\r
-     * byte[(int) file.length()]; fileInputStream.read(bytesArray);\r
-     * \r
-     * } catch (IOException e1) {\r
-     * errorLogger.error("Exception Occured while reading the default mapping file ,Cause: " +\r
-     * e1.getMessage(), e1); // exit on missing default mapping file\r
-     * System.exit(SpringApplication.exit(applicationContext, () -> {\r
-     * errorLogger.error("Application stoped due to missing default mapping file"); return -1; }));\r
-     * }\r
-     * \r
-     * try (Connection con = DriverManager.getConnection(dBurl, user, pwd); // creating table if not\r
-     * exist PreparedStatement pstmt11 = con.prepareStatement("CREATE TABLE IF NOT EXISTS public." +\r
-     * MappingFileTableName + "\r\n" + "(\r\n" +\r
-     * "    enterpriseid character varying COLLATE pg_catalog.\"default\" NOT NULL,\r\n" +\r
-     * "    mappingfilecontents bytea,\r\n" +\r
-     * "    mimetype character varying COLLATE pg_catalog.\"default\",\r\n" +\r
-     * "    file_name character varying COLLATE pg_catalog.\"default\",\r\n" +\r
-     * "    CONSTRAINT mapping_file_pkey5 PRIMARY KEY (enterpriseid)\r\n" + ")\r\n" + "WITH (\r\n" +\r
-     * "    OIDS = FALSE\r\n" + ")\r\n" + "TABLESPACE pg_default;")) {\r
-     * \r
-     * metricsLogger.info("Postgresql Connection successful...");\r
-     * debugLogger.debug("Connection object:{}" , con.toString());\r
-     * \r
-     * pstmt11.executeUpdate();\r
-     * debugLogger.info("CREATE TABLE IF NOT EXISTS executed successfully....");\r
-     * \r
-     * if ((bytesArray.length > 0) && (!Arrays.toString(bytesArray).equals(""))) {\r
-     * \r
-     * try (PreparedStatement pstmt = con.prepareStatement("INSERT INTO " + MappingFileTableName +\r
-     * "(enterpriseid, mappingfilecontents, mimetype,  File_Name) VALUES (?, ?, ?, ?) ON CONFLICT (enterpriseid) DO NOTHING;"\r
-     * )) { pstmt.setString(1, defaultEnterpriseId); pstmt.setBytes(2, bytesArray);\r
-     * pstmt.setString(3, "text/xml"); pstmt.setString(4, file.getName());\r
-     * \r
-     * pstmt.executeUpdate();\r
-     * debugLogger.info("Made sure that default mapping file is present in table"); } } else {\r
-     * errorLogger.error(file.getName() + " is empty"); // exit on empty mapping file\r
-     * System.exit(SpringApplication.exit(applicationContext, () -> {\r
-     * errorLogger.error("Application stoped beacuase default mapping file is empty.."); return -1;\r
-     * })); }\r
-     * \r
-     * } catch (SQLException e) { errorLogger.error("Received exception : " + e.getMessage(), e); //\r
-     * exit on SqlException System.exit(SpringApplication.exit(applicationContext, () -> {\r
-     * errorLogger.error("Application Stoped due to ", e.getCause()); return -1; })); }\r
-     * \r
-     * }\r
-     */\r
-    /*\r
-     * public void fetchMappingFile() {\r
-     * \r
-     * try (Connection con = DriverManager.getConnection(dBurl, user, pwd)) {\r
-     * debugLogger.info("Retrieving data from DB"); PreparedStatement pstmt =\r
-     * con.prepareStatement("SELECT * FROM mapping_file"); ResultSet rs = pstmt.executeQuery(); //\r
-     * parsing the column each time is a linear search int column1Pos =\r
-     * rs.findColumn("enterpriseid"); int column2Pos = rs.findColumn("mappingfilecontents"); String\r
-     * hexString; while (rs.next()) { String column1 = rs.getString(column1Pos); String column2 =\r
-     * rs.getString(column2Pos); hexString = column2.substring(2); byte[] bytes =\r
-     * Hex.decodeHex(hexString.toCharArray()); String data = new String(bytes, "UTF-8");\r
-     * mappingFiles.put(column1, data); }\r
-     * debugLogger.info("DB Initialization Completed, Total # Mappingfiles are" +\r
-     * mappingFiles.size()); } catch (Exception e) { errorLogger.error("Error occured due to :" +\r
-     * e.getMessage()); e.printStackTrace(); }\r
-     * \r
-     * }\r
-     */\r
-    \r
-    public static Map<String, String> getMappingFiles() {\r
-        return mappingFiles;\r
-    }\r
-    \r
-    public static void setMappingFiles(Map<String, String> mappingFiles) {\r
-        VESAdapterInitializer.mappingFiles = mappingFiles;\r
-    }\r
-    \r
-    @Override\r
-    public int getOrder() {\r
-        return 0;\r
-    }\r
-    \r
+       private static final Logger debugLogger = LoggerFactory.getLogger("debugLogger");\r
+       private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger");\r
+\r
+       @Value("${defaultConfigFilelocation}")\r
+       String defaultConfigFilelocation;\r
+       @Value("${server.port}")\r
+       String serverPort;\r
+\r
+       private static Map<String, String> mappingFiles = new HashMap<String, String>();\r
+\r
+       // Generate RequestID and InvocationID which will be used when logging and in\r
+       // HTTP requests\r
+       final RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();\r
+       final CbsRequest request = CbsRequests.getConfiguration(diagnosticContext);\r
+\r
+       // Read necessary properties from the environment\r
+       final EnvProperties env = EnvProperties.fromEnvironment();\r
+\r
+       // Polling properties :\r
+       final Duration initialDelay = Duration.ofSeconds(5);\r
+       final Duration period = Duration.ofMinutes(1);\r
+\r
+       @Autowired\r
+       private ApplicationContext applicationContext;\r
+\r
+       @Override\r
+       public void run(String... args) throws Exception {\r
+               debugLogger.info("The Default Config file Location:" + defaultConfigFilelocation.trim());\r
+\r
+               if (ClassLoader.getSystemResource(defaultConfigFilelocation.trim()) == null) {\r
+                       errorLogger.error("Default Config file " + defaultConfigFilelocation.trim() + " is missing");\r
+                       System.exit(SpringApplication.exit(applicationContext, () -> {\r
+                               errorLogger.error("Application stoped due to missing default Config file");\r
+                               return -1;\r
+                       }));\r
+               }\r
+\r
+               // Create the client and use it to get the configuration\r
+               CbsClientFactory.createCbsClient(env).flatMapMany(cbsClient -> cbsClient.updates(request, initialDelay, period))\r
+                               .subscribe(jsonObject -> {\r
+\r
+                                       // If env details not fetched static configuration file will be used\r
+                                       if (env.consulHost() != null && env.cbsName() != null && env.appName() != null) {\r
+                                               debugLogger.info(">>>Dynamic configuration to be used");\r
+                                               FetchDynamicConfig.cbsCall(defaultConfigFilelocation);\r
+                                       }\r
+\r
+                                       readJsonToMap(defaultConfigFilelocation);\r
+\r
+                                       debugLogger.info("Triggering controller's start url ");\r
+                                       fetchResultFromDestination("http://localhost:" + serverPort + "/start");\r
+\r
+                               }, throwable -> {\r
+                                       debugLogger.warn("Cannot Connect", throwable);\r
+                               });\r
+\r
+       }\r
+\r
+       /**\r
+        * gets the configuration details from JSON an puts those in the mapping data\r
+        * structure for further processing.\r
+        * \r
+        * @param configFile: String\r
+        */\r
+       private void readJsonToMap(String configFile) {\r
+               try {\r
+                       JSONArray collectorArray = CollectorConfigPropertyRetrival.collectorConfigArray(configFile);\r
+\r
+                       for (int i = 0; i < collectorArray.size(); i++) {\r
+                               JSONObject obj2 = (JSONObject) collectorArray.get(i);\r
+\r
+                               if (obj2.containsKey("mapping-files")) {\r
+\r
+                                       JSONArray a1 = (JSONArray) obj2.get("mapping-files");\r
+\r
+                                       for (int j = 0; j < a1.size(); j++) {\r
+                                               JSONObject obj3 = (JSONObject) a1.get(j);\r
+                                               Set<Entry<String, String>> set = obj3.entrySet();\r
+\r
+                                               for (Entry<String, String> entry : set) {\r
+\r
+                                                       mappingFiles.put(entry.getKey(), entry.getValue());\r
+                                               }\r
+                                       }\r
+\r
+                               }\r
+                       }\r
+\r
+               } catch (Exception e) {\r
+                       // e.printStackTrace();\r
+                       errorLogger.error(\r
+                                       " Class VESAdapterInitializer: method readJsonToMap: Exception occured while reading Collector config file cause: ",\r
+                                       e.getCause());\r
+               }\r
+       }\r
+\r
+       public static Map<String, String> getMappingFiles() {\r
+               return mappingFiles;\r
+       }\r
+\r
+       public static void setMappingFiles(Map<String, String> mappingFiles) {\r
+               VESAdapterInitializer.mappingFiles = mappingFiles;\r
+       }\r
+\r
+       @Override\r
+       public int getOrder() {\r
+               return 0;\r
+       }\r
+\r
+       private static String fetchResultFromDestination(String url) {\r
+               debugLogger.debug("VESAdapterInitializer:: fetchResultFromDestination :: START");\r
+               String line = "";\r
+               StringBuffer sb = new StringBuffer();\r
+               try {\r
+                       HttpClient client = HttpClientBuilder.create().build();\r
+                       HttpGet request = new HttpGet(url);\r
+                       HttpResponse response = client.execute(request);\r
+                       BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));\r
+\r
+                       while ((line = rd.readLine()) != null) {\r
+                               sb.append(line);\r
+                               sb.append('\n');\r
+                       }\r
+               } catch (ClientProtocolException e) {\r
+                       debugLogger.debug("VESAdapterInitializer:: fetchResultFromDestination :: ClientProtocolException thrown "\r
+                                       + e.getMessage());\r
+               } catch (UnsupportedOperationException e) {\r
+                       debugLogger\r
+                                       .debug("VESAdapterInitializer:: fetchResultFromDestination :: UnsupportedOperationException thrown "\r
+                                                       + e.getMessage());\r
+               } catch (IOException e) {\r
+                       debugLogger.debug(\r
+                                       "VESAdapterInitializer:: fetchResultFromDestination :: IOException thrown " + e.getMessage());\r
+               }\r
+               debugLogger.debug("VESAdapterInitializer:: fetchResultFromDestination :: END");\r
+               return sb.toString();\r
+       }\r
+\r
 }\r