Consul update (via SDK library) to support periodic polling 81/94181/3
authoraleemraja <ar00500721@techmahindra.com>
Tue, 27 Aug 2019 12:32:47 +0000 (18:02 +0530)
committeraleemraja <ar00500721@techmahindra.com>
Tue, 27 Aug 2019 12:32:47 +0000 (18:02 +0530)
Issue-ID: DCAEGEN2-1735
Change-Id: I0aeae0569aa94cec336688e1e70e1498a29473b4
Signed-off-by: aleemraja <ar00500721@techmahindra.com>
UniversalVesAdapter/pom.xml
UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/service/VESAdapterInitializer.java
UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/FetchDynamicConfig.java

index 335bc13..9ce6b47 100644 (file)
@@ -18,6 +18,7 @@
 * limitations under the License.\r
 * ============LICENSE_END=========================================================\r
  -->\r
+\r
 <project xmlns="http://maven.apache.org/POM/4.0.0"\r
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\r
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">\r
                <snapshots.path>content/repositories/snapshots/</snapshots.path>\r
                <releases.path>content/repositories/releases/</releases.path>\r
                <site.path>\r
-            content/sites/site/org/onap/dcaegen2/services/mapper/${project.artifactId}/${project.version}\r
-        </site.path>\r
+                       content/sites/site/org/onap/dcaegen2/services/mapper/${project.artifactId}/${project.version}\r
+               </site.path>\r
                <maven.build.timestamp.format>yyyyMMdd'T'HHmmss</maven.build.timestamp.format>\r
 \r
+               <!-- DCAE SDK version -->\r
+               <sdk.version>1.1.6</sdk.version>\r
+\r
        </properties>\r
        <dependencies>\r
-       <!-- Junit, Mockito, PowerMock -->\r
-       <dependency>\r
-            <groupId>junit</groupId>\r
-            <artifactId>junit</artifactId>\r
-            <version>4.12</version>\r
-            <scope>test</scope>\r
-        </dependency>\r
-       <dependency>\r
-    <groupId>org.mockito</groupId>\r
-    <artifactId>mockito-core</artifactId>\r
-    <version>2.0.5-beta</version>\r
-    <scope>test</scope>\r
-</dependency>\r
+               <!-- Junit, Mockito, PowerMock -->\r
+               <dependency>\r
+                       <groupId>junit</groupId>\r
+                       <artifactId>junit</artifactId>\r
+                       <version>4.12</version>\r
+                       <scope>test</scope>\r
+               </dependency>\r
+               <dependency>\r
+                       <groupId>org.mockito</groupId>\r
+                       <artifactId>mockito-core</artifactId>\r
+                       <version>2.0.5-beta</version>\r
+                       <scope>test</scope>\r
+               </dependency>\r
 \r
-        <dependency>\r
-            <groupId>org.powermock</groupId>\r
-            <artifactId>powermock-api-mockito</artifactId>\r
-            <version>1.6.2</version>\r
-            <scope>test</scope>\r
-        </dependency>\r
-        <dependency>\r
-            <groupId>org.powermock</groupId>\r
-            <artifactId>powermock-module-junit4</artifactId>\r
-            <version>1.6.2</version>\r
-            <scope>test</scope>\r
-        </dependency>\r
-        <dependency>\r
-        <groupId>org.javassist</groupId>\r
-        <artifactId>javassist</artifactId>\r
-        <version>3.18.2-GA</version>\r
-    </dependency>\r
-               <dependency>\r
-                       <groupId>com.googlecode.json-simple</groupId>\r
-                       <artifactId>json-simple</artifactId>\r
-                       <version>1.1.1</version>\r
+               <dependency>\r
+                       <groupId>org.powermock</groupId>\r
+                       <artifactId>powermock-api-mockito</artifactId>\r
+                       <version>1.6.2</version>\r
+                       <scope>test</scope>\r
+               </dependency>\r
+               <dependency>\r
+                       <groupId>org.powermock</groupId>\r
+                       <artifactId>powermock-module-junit4</artifactId>\r
+                       <version>1.6.2</version>\r
+                       <scope>test</scope>\r
+               </dependency>\r
+               <dependency>\r
+                       <groupId>org.javassist</groupId>\r
+                       <artifactId>javassist</artifactId>\r
+                       <version>3.18.2-GA</version>\r
+               </dependency>\r
+               <dependency>\r
+                       <groupId>com.googlecode.json-simple</groupId>\r
+                       <artifactId>json-simple</artifactId>\r
+                       <version>1.1.1</version>\r
                </dependency>\r
                <dependency>\r
                        <groupId>com.google.guava</groupId>\r
                </dependency>\r
                <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->\r
                <dependency>\r
-                   <groupId>org.slf4j</groupId>\r
-                   <artifactId>slf4j-api</artifactId>\r
-                   <version>1.7.25</version>\r
+                       <groupId>org.slf4j</groupId>\r
+                       <artifactId>slf4j-api</artifactId>\r
+                       <version>1.7.25</version>\r
                </dependency>\r
                <dependency>\r
                        <groupId>org.codehaus.groovy</groupId>\r
                        <version>${httpclient.version}</version>\r
                        <scope>compile</scope>\r
                </dependency>\r
-               <!-- <dependency> <groupId>org.onap.aai.event-client</groupId> <artifactId>event-client-dmaap</artifactId>\r
+               <!-- <dependency> <groupId>org.onap.aai.event-client</groupId> <artifactId>event-client-dmaap</artifactId> \r
                        <version>1.2.0</version> </dependency> -->\r
                <dependency>\r
                        <groupId>org.springframework.boot</groupId>\r
                        <optional>true</optional>\r
                        <version>2.1.1.RELEASE</version>\r
                </dependency>\r
+\r
+               <!-- DCAE sdk dependency -->\r
+\r
+               <dependency>\r
+                       <groupId>org.onap.dcaegen2.services.sdk.rest.services</groupId>\r
+                       <artifactId>cbs-client</artifactId>\r
+                       <version>${sdk.version}</version>\r
+               </dependency>\r
+               \r
        </dependencies>\r
 \r
        <build>\r
                        </plugins>\r
                </pluginManagement>\r
                <plugins>\r
-                       <!-- <plugin>\r
-                               <groupId>org.apache.maven.plugins</groupId>\r
-                               <artifactId>maven-surefire-plugin</artifactId>\r
-                               <configuration>\r
-                                       <reuseForks>false</reuseForks>\r
-                                       <forkCount>1</forkCount>\r
-                               </configuration>\r
-                       </plugin> -->\r
+                       <!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> \r
+                               <configuration> <reuseForks>false</reuseForks> <forkCount>1</forkCount> </configuration> \r
+                               </plugin> -->\r
 \r
 \r
 \r
                                <artifactId>docker-maven-plugin</artifactId>\r
                                <version>1.0.0</version>\r
                                <configuration>\r
-                                        <skipDockerBuild>false</skipDockerBuild>\r
+                                       <skipDockerBuild>false</skipDockerBuild>\r
                                        <serverId>${onap.nexus.dockerregistry.daily}</serverId>\r
                                        <imageName>${onap.nexus.dockerregistry.daily}/${docker.image.name}</imageName>\r
                                        <imageTags>\r
                                        <entryPoint>bin/run.sh</entryPoint>\r
 \r
                                </configuration>\r
-                               <!--<executions>\r
-                                       <execution>\r
-                                               <id>build-image</id>\r
-                                               <phase>package</phase>\r
-                                               <goals>\r
-                                                       <goal>build</goal>\r
-                                               </goals>\r
-                                       </execution>\r
-                                       <execution>\r
-                                               <id>tag-and-push-image-latest</id>\r
-                                               <phase>deploy</phase>\r
-                                               <goals>\r
-                                                       <goal>tag</goal>\r
-                                               </goals>\r
-                                               <configuration>\r
-                                                       <image>${docker.image.name}:latest</image>\r
-                                                       <newName>${onap.nexus.dockerregistry.daily}/${docker.image.name}:latest</newName>\r
-                                                       <pushImage>true</pushImage>\r
-                                               </configuration>\r
-                                       </execution>\r
-                                       <execution>\r
-                                               <id>tag-and-push-image-with-version</id>\r
-                                               <phase>deploy</phase>\r
-                                               <goals>\r
-                                                       <goal>tag</goal>\r
-                                               </goals>\r
-                                               <configuration>\r
-                                                       <image>${docker.image.name}:latest</image>\r
-                                                       <newName>${onap.nexus.dockerregistry.daily}/${docker.image.name}:${project.version}\r
-                                                       </newName>\r
-                                                       <pushImage>true</pushImage>\r
-                                               </configuration>\r
-                                       </execution>\r
-                                       <execution>\r
-                                               <id>tag-and-push-image-with-version-and-date</id>\r
-                                               <phase>deploy</phase>\r
-                                               <goals>\r
-                                                       <goal>tag</goal>\r
-                                               </goals>\r
-                                               <configuration>\r
-                                                       <image>${docker.image.name}:latest</image>\r
-                                                       <newName>\r
-                                                               ${onap.nexus.dockerregistry.daily}/${docker.image.name}:${project.version}-${maven.build.timestamp}Z\r
-                                                       </newName>\r
-                                                       <pushImage>true</pushImage>\r
-                                               </configuration>\r
-                                       </execution>\r
-                               </executions>-->\r
+                               <!--<executions> <execution> <id>build-image</id> <phase>package</phase> \r
+                                       <goals> <goal>build</goal> </goals> </execution> <execution> <id>tag-and-push-image-latest</id> \r
+                                       <phase>deploy</phase> <goals> <goal>tag</goal> </goals> <configuration> <image>${docker.image.name}:latest</image> \r
+                                       <newName>${onap.nexus.dockerregistry.daily}/${docker.image.name}:latest</newName> \r
+                                       <pushImage>true</pushImage> </configuration> </execution> <execution> <id>tag-and-push-image-with-version</id> \r
+                                       <phase>deploy</phase> <goals> <goal>tag</goal> </goals> <configuration> <image>${docker.image.name}:latest</image> \r
+                                       <newName>${onap.nexus.dockerregistry.daily}/${docker.image.name}:${project.version} \r
+                                       </newName> <pushImage>true</pushImage> </configuration> </execution> <execution> \r
+                                       <id>tag-and-push-image-with-version-and-date</id> <phase>deploy</phase> <goals> \r
+                                       <goal>tag</goal> </goals> <configuration> <image>${docker.image.name}:latest</image> \r
+                                       <newName> ${onap.nexus.dockerregistry.daily}/${docker.image.name}:${project.version}-${maven.build.timestamp}Z \r
+                                       </newName> <pushImage>true</pushImage> </configuration> </execution> </executions> -->\r
                        </plugin>\r
                        <plugin>\r
                                <groupId>org.springframework.boot</groupId>\r
                                                        </sources>\r
                                                </configuration>\r
                                        </execution>\r
-                                       <!-- <execution> <id>regex-property</id> <goals> <goal>regex-property</goal>\r
-                                               </goals> <configuration> <name>docker.version</name> <value>${project.version}</value>\r
-                                               <regex>(^[0-9]+.[0-9]+.[0-9]+$)</regex> <replacement>$1-STAGING</replacement>\r
+                                       <!-- <execution> <id>regex-property</id> <goals> <goal>regex-property</goal> \r
+                                               </goals> <configuration> <name>docker.version</name> <value>${project.version}</value> \r
+                                               <regex>(^[0-9]+.[0-9]+.[0-9]+$)</regex> <replacement>$1-STAGING</replacement> \r
                                                <failIfNoMatch>false</failIfNoMatch> </configuration> </execution> -->\r
                                </executions>\r
                        </plugin>\r
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
index 022a586..9493fc6 100644 (file)
@@ -28,11 +28,19 @@ import java.io.IOException;
 import java.io.InputStreamReader;\r
 import java.io.PrintWriter;\r
 import java.nio.file.Files;\r
-import java.util.Map;\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.JSONArray;\r
 import org.json.JSONObject;\r
 import org.json.JSONTokener;\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.slf4j.Logger;\r
 import org.slf4j.LoggerFactory;\r
 import org.springframework.stereotype.Component;\r
@@ -43,177 +51,182 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 \r
 @Component\r
 public class FetchDynamicConfig {\r
-    \r
-    private static final Logger debugLogger = LoggerFactory.getLogger("debugLogger");\r
-    private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger");\r
-    \r
-    private static String url;\r
-    public static String retString;\r
-    public static String retCBSString;\r
-    private static Map<String, String> env;\r
-    \r
-    public FetchDynamicConfig() {}\r
-    \r
-    public static void cbsCall(String configFile) {\r
-        \r
-        env = System.getenv();\r
-        Boolean areEqual;\r
-        // Call consul api and identify the CBS Service address and port\r
-        getconsul();\r
-        // Construct and invoke CBS API to get application Configuration\r
-        getCBS();\r
-        // Verify if data has changed\r
-        areEqual = verifyConfigChange(configFile);\r
-        \r
-        if (!areEqual) {\r
-            FetchDynamicConfig fc = new FetchDynamicConfig();\r
-            fc.writefile(retCBSString, configFile);\r
-        } else {\r
-            debugLogger\r
-                    .info("New config pull results identical -  " + configFile + " NOT refreshed");\r
-        }\r
-    }\r
-    \r
-    private static void getconsul() {\r
-        url = env.get("CONSUL_HOST") + ":8500/v1/catalog/service/"\r
-                + env.get("CONFIG_BINDING_SERVICE");\r
-        retString = executecurl(url);\r
-        debugLogger.info("CBS details fetched from Consul");\r
-    }\r
-    \r
-    public static boolean verifyConfigChange(String configFile) {\r
-        \r
-        boolean areEqual = false;\r
-        // Read current data\r
-        try {\r
-            \r
-            File f = new File(ClassLoader.getSystemResource(configFile.trim()).getFile());\r
-            \r
-            if (f.exists() && !f.isDirectory()) {\r
-                debugLogger.info(\r
-                        "Comparing local configuration with the configuration fethed from CBS ");\r
-                \r
-                String jsonData = readFile(configFile);\r
-                JSONObject jsonObject = new JSONObject(jsonData);\r
-                \r
-                ObjectMapper mapper = new ObjectMapper();\r
-                \r
-                JsonNode tree1 = mapper.readTree(jsonObject.toString());\r
-                JsonNode tree2 = mapper.readTree(retCBSString);\r
-                areEqual = tree1.equals(tree2);\r
-                debugLogger.info("Comparison value:" + areEqual);\r
-            } else {\r
-                debugLogger.info("First time config file read: " + configFile);\r
-            }\r
-            \r
-        } catch (IOException e) {\r
-            errorLogger.error("Comparison with new fetched data failed" + e.getMessage());\r
-            \r
-        }\r
-        \r
-        return areEqual;\r
-        \r
-    }\r
-    \r
-    public static void getCBS() {\r
-        \r
-        // consul return as array\r
-        JSONTokener temp = new JSONTokener(retString);\r
-        JSONObject cbsjobj = (JSONObject) new JSONArray(temp).get(0);\r
-        \r
-        String urlPart1 = null;\r
-        if (cbsjobj.has("ServiceAddress") && cbsjobj.has("ServicePort")) {\r
-            \r
-            urlPart1 = cbsjobj.getString("ServiceAddress") + ":" + cbsjobj.getInt("ServicePort");\r
-            \r
-        }\r
-        debugLogger.info("CONFIG_BINDING_SERVICE HOST:PORT is " + urlPart1);\r
-        \r
-        if (env.containsKey("HOSTNAME")) {\r
-            url = urlPart1 + "/service_component/" + env.get("HOSTNAME");\r
-            retCBSString = executecurl(url);\r
-            debugLogger.info("Configuration fetched from CBS successfully..");\r
-        } else if (env.containsKey("SERVICE_NAME")) {\r
-            url = urlPart1 + "/service_component/" + env.get("SERVICE_NAME");\r
-            retCBSString = executecurl(url);\r
-            debugLogger.info("Configuration fetched from CBS successfully..");\r
-        } else {\r
-            errorLogger.error(\r
-                    "Service name environment variable - HOSTNAME/SERVICE_NAME not found within container ");\r
-        }\r
-        \r
-    }\r
-    \r
-    public void writefile(String retCBSString, String configFile) {\r
-        \r
-        String indentedretstring = (new JSONObject(retCBSString)).toString(4);\r
-        File f = new File(ClassLoader.getSystemResource(configFile.trim()).getFile());\r
-        try {\r
-            debugLogger.info("Overwriting local configuration file " + configFile\r
-                    + " with configuartions received from CBS");\r
-            \r
-            \r
-            File file2 = ResourceUtils.getFile("classpath:" + configFile);\r
-            FileWriter fstream = new FileWriter(file2, false);\r
-            PrintWriter printWriter = new PrintWriter(fstream);\r
-            printWriter.print(indentedretstring);\r
-            printWriter.close();\r
-            fstream.close();\r
-            \r
-            debugLogger.info("New Config successfully written to local file to " + configFile);\r
-        } catch (IOException e) {\r
-            errorLogger.error("Error in writing configuration into local KV file " + configFile\r
-                    + retString + e.getMessage());\r
-            e.printStackTrace();\r
-        }\r
-        \r
-    }\r
-    \r
-    public static String readFile(String configFileName) {\r
-        String content = null;\r
-        File file = null;\r
-        \r
-        try {\r
-            file = ResourceUtils.getFile("classpath:" + configFileName);\r
-            content = new String(Files.readAllBytes(file.toPath()));\r
-        } catch (FileNotFoundException e) {\r
-            errorLogger.error("colud not find file :",configFileName);\r
-            \r
-        } catch (IOException e) {\r
-            errorLogger.error("unable to read the file , reason:", e.getCause());\r
-        } catch (Exception e) {\r
-            errorLogger.error("Exception occured , reason:", e.getMessage());\r
-        }\r
-        \r
-        return content;\r
-        \r
-    }\r
-    \r
-    private static String executecurl(String url) {\r
-        \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
-            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
-            reader.close();\r
-            ipr.close();\r
-        } catch (IOException e) {\r
-            errorLogger.error("error", e);\r
-            e.printStackTrace();\r
-        }\r
-        return result;\r
-        \r
-    }\r
-    \r
+\r
+       // @Value("${defaultProtocol}")\r
+       static String defaultProtocol = "http";\r
+\r
+       private static final Logger debugLogger = LoggerFactory.getLogger("debugLogger");\r
+       private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger");\r
+\r
+       private static String url;\r
+       public static String retString;\r
+       public static String retCBSString;\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
+       static final EnvProperties env = EnvProperties.fromEnvironment();\r
+\r
+       public FetchDynamicConfig() {\r
+\r
+       }\r
+\r
+       public static void cbsCall(String configFile) {\r
+\r
+               Boolean areEqual;\r
+               // Call consul api and identify the CBS Service address and port\r
+               getconsul();\r
+               // Construct and invoke CBS API to get application Configuration\r
+               getCBS();\r
+               // Verify if data has changed\r
+               areEqual = verifyConfigChange(configFile);\r
+\r
+               if (!areEqual) {\r
+                       FetchDynamicConfig fc = new FetchDynamicConfig();\r
+                       if (retCBSString!=null) {\r
+                               fc.writefile(retCBSString, configFile);\r
+                       } else {\r
+                               debugLogger.debug("No content recieved from server");\r
+                       }\r
+                       \r
+               } else {\r
+                       debugLogger.info("New config pull results identical -  " + configFile + " NOT refreshed");\r
+               }\r
+       }\r
+\r
+       private static void getconsul() {\r
+               url = defaultProtocol + "://" + env.consulHost() + ":" + env.consulPort() + "/v1/catalog/service/"\r
+                               + env.cbsName();\r
+               retString = fetchResultFromDestination(url);\r
+               debugLogger.info("CBS details fetched from Consul");\r
+       }\r
+\r
+       public static boolean verifyConfigChange(String configFile) {\r
+\r
+               boolean areEqual = false;\r
+               // Read current data\r
+               try {\r
+\r
+                       File f = new File(ClassLoader.getSystemResource(configFile.trim()).getFile());\r
+\r
+                       if (f.exists() && !f.isDirectory()) {\r
+                               debugLogger.info("Comparing local configuration with the configuration fethed from CBS ");\r
+\r
+                               String jsonData = readFile(configFile);\r
+                               JSONObject jsonObject = new JSONObject(jsonData);\r
+\r
+                               ObjectMapper mapper = new ObjectMapper();\r
+\r
+                               JsonNode tree1 = mapper.readTree(jsonObject.toString());\r
+                               JsonNode tree2 = mapper.readTree(retCBSString);\r
+                               areEqual = tree1.equals(tree2);\r
+                               debugLogger.info("Comparison value:" + areEqual);\r
+                       } else {\r
+                               debugLogger.info("First time config file read: " + configFile);\r
+                       }\r
+\r
+               } catch (IOException e) {\r
+                       errorLogger.error("Comparison with new fetched data failed" + e.getMessage());\r
+\r
+               }\r
+\r
+               return areEqual;\r
+\r
+       }\r
+\r
+       public static void getCBS() {\r
+\r
+               // consul return as array\r
+               JSONTokener temp = new JSONTokener(retString);\r
+               JSONObject cbsjobj = (JSONObject) new JSONArray(temp).get(0);\r
+\r
+               String urlPart1 = null;\r
+               if (cbsjobj.has("ServiceAddress") && cbsjobj.has("ServicePort")) {\r
+\r
+                       urlPart1 = cbsjobj.getString("ServiceAddress") + ":" + cbsjobj.getInt("ServicePort");\r
+\r
+               }\r
+               debugLogger.info("CONFIG_BINDING_SERVICE HOST:PORT is " + urlPart1);\r
+\r
+               if (env.appName() != null) {\r
+                       url = defaultProtocol + "://" + urlPart1 + "/service_component/" + env.appName();\r
+                       retCBSString = fetchResultFromDestination(url);\r
+                       debugLogger.info("Configuration fetched from CBS successfully..");\r
+               } else {\r
+                       errorLogger.error("Service name environment variable - APP_NAME/SERVICE_NAME not found within container ");\r
+               }\r
+\r
+       }\r
+\r
+       public void writefile(String retCBSString, String configFile) {\r
+\r
+               String indentedretstring = (new JSONObject(retCBSString)).toString(4);\r
+               try {\r
+                       debugLogger.info(\r
+                                       "Overwriting local configuration file " + configFile + " with configuartions received from CBS");\r
+\r
+                       File file2 = ResourceUtils.getFile("classpath:" + configFile);\r
+                       FileWriter fstream = new FileWriter(file2, false);\r
+                       PrintWriter printWriter = new PrintWriter(fstream);\r
+                       printWriter.print(indentedretstring);\r
+                       printWriter.close();\r
+                       fstream.close();\r
+\r
+                       debugLogger.info("New Config successfully written to local file to " + configFile);\r
+               } catch (IOException e) {\r
+                       errorLogger.error(\r
+                                       "Error in writing configuration into local KV file " + configFile + retString + e.getMessage());\r
+                       e.printStackTrace();\r
+               }\r
+\r
+       }\r
+\r
+       public static String readFile(String configFileName) {\r
+               String content = null;\r
+               File file = null;\r
+\r
+               try {\r
+                       file = ResourceUtils.getFile("classpath:" + configFileName);\r
+                       content = new String(Files.readAllBytes(file.toPath()));\r
+               } catch (FileNotFoundException e) {\r
+                       errorLogger.error("colud not find file :", configFileName);\r
+\r
+               } catch (IOException e) {\r
+                       errorLogger.error("unable to read the file , reason:", e.getCause());\r
+               } catch (Exception e) {\r
+                       errorLogger.error("Exception occured , reason:", e.getMessage());\r
+               }\r
+\r
+               return content;\r
+       }\r
+\r
+       private static String fetchResultFromDestination(String url) {\r
+               debugLogger.debug("FetchDynamicConfig : fetchResultFromDestination : START");\r
+\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
+                       String line = "";\r
+                       while ((line = rd.readLine()) != null) {\r
+                               sb.append(line);\r
+                               sb.append('\n');\r
+                       }\r
+               } catch (ClientProtocolException e) {\r
+                       debugLogger.debug("FetchDynamicConfig : fetchResultFromDestination : ClientProtocolException thrown."+e.getMessage());\r
+               } catch (UnsupportedOperationException e) {\r
+                       debugLogger.debug("FetchDynamicConfig : fetchResultFromDestination : UnsupportedOperationException thrown."+e.getMessage());\r
+               } catch (IOException e) {\r
+                       debugLogger.debug("FetchDynamicConfig : fetchResultFromDestination : IOException thrown."+e.getMessage());\r
+               }\r
+\r
+               debugLogger.debug("FetchDynamicConfig : fetchResultFromDestination : END");\r
+               return sb.toString();\r
+       }\r
+\r
 }\r