aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoraleemraja <ar00500721@techmahindra.com>2019-08-27 18:02:47 +0530
committeraleemraja <ar00500721@techmahindra.com>2019-08-27 18:02:47 +0530
commitfa5d1e2af268c0c60a39faa5b41a40d165f6767c (patch)
tree02b3fa55e55627803ab7c11df2ffa23c92142377
parent00be6997121e38e2b91a00203bc49119b11e58a4 (diff)
Consul update (via SDK library) to support periodic polling
Issue-ID: DCAEGEN2-1735 Change-Id: I0aeae0569aa94cec336688e1e70e1498a29473b4 Signed-off-by: aleemraja <ar00500721@techmahindra.com>
-rw-r--r--UniversalVesAdapter/pom.xml172
-rw-r--r--UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/service/VESAdapterInitializer.java353
-rw-r--r--UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/FetchDynamicConfig.java361
3 files changed, 408 insertions, 478 deletions
diff --git a/UniversalVesAdapter/pom.xml b/UniversalVesAdapter/pom.xml
index 335bc13..9ce6b47 100644
--- a/UniversalVesAdapter/pom.xml
+++ b/UniversalVesAdapter/pom.xml
@@ -18,6 +18,7 @@
* limitations under the License.
* ============LICENSE_END=========================================================
-->
+
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
@@ -50,47 +51,50 @@
<snapshots.path>content/repositories/snapshots/</snapshots.path>
<releases.path>content/repositories/releases/</releases.path>
<site.path>
- content/sites/site/org/onap/dcaegen2/services/mapper/${project.artifactId}/${project.version}
- </site.path>
+ content/sites/site/org/onap/dcaegen2/services/mapper/${project.artifactId}/${project.version}
+ </site.path>
<maven.build.timestamp.format>yyyyMMdd'T'HHmmss</maven.build.timestamp.format>
+ <!-- DCAE SDK version -->
+ <sdk.version>1.1.6</sdk.version>
+
</properties>
<dependencies>
- <!-- Junit, Mockito, PowerMock -->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.mockito</groupId>
- <artifactId>mockito-core</artifactId>
- <version>2.0.5-beta</version>
- <scope>test</scope>
-</dependency>
+ <!-- Junit, Mockito, PowerMock -->
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.12</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <version>2.0.5-beta</version>
+ <scope>test</scope>
+ </dependency>
- <dependency>
- <groupId>org.powermock</groupId>
- <artifactId>powermock-api-mockito</artifactId>
- <version>1.6.2</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.powermock</groupId>
- <artifactId>powermock-module-junit4</artifactId>
- <version>1.6.2</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.javassist</groupId>
- <artifactId>javassist</artifactId>
- <version>3.18.2-GA</version>
- </dependency>
- <dependency>
- <groupId>com.googlecode.json-simple</groupId>
- <artifactId>json-simple</artifactId>
- <version>1.1.1</version>
+ <dependency>
+ <groupId>org.powermock</groupId>
+ <artifactId>powermock-api-mockito</artifactId>
+ <version>1.6.2</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.powermock</groupId>
+ <artifactId>powermock-module-junit4</artifactId>
+ <version>1.6.2</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.javassist</groupId>
+ <artifactId>javassist</artifactId>
+ <version>3.18.2-GA</version>
+ </dependency>
+ <dependency>
+ <groupId>com.googlecode.json-simple</groupId>
+ <artifactId>json-simple</artifactId>
+ <version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
@@ -185,9 +189,9 @@
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>1.7.25</version>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
@@ -248,7 +252,7 @@
<version>${httpclient.version}</version>
<scope>compile</scope>
</dependency>
- <!-- <dependency> <groupId>org.onap.aai.event-client</groupId> <artifactId>event-client-dmaap</artifactId>
+ <!-- <dependency> <groupId>org.onap.aai.event-client</groupId> <artifactId>event-client-dmaap</artifactId>
<version>1.2.0</version> </dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
@@ -256,6 +260,15 @@
<optional>true</optional>
<version>2.1.1.RELEASE</version>
</dependency>
+
+ <!-- DCAE sdk dependency -->
+
+ <dependency>
+ <groupId>org.onap.dcaegen2.services.sdk.rest.services</groupId>
+ <artifactId>cbs-client</artifactId>
+ <version>${sdk.version}</version>
+ </dependency>
+
</dependencies>
<build>
@@ -273,14 +286,9 @@
</plugins>
</pluginManagement>
<plugins>
- <!-- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-surefire-plugin</artifactId>
- <configuration>
- <reuseForks>false</reuseForks>
- <forkCount>1</forkCount>
- </configuration>
- </plugin> -->
+ <!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId>
+ <configuration> <reuseForks>false</reuseForks> <forkCount>1</forkCount> </configuration>
+ </plugin> -->
@@ -309,7 +317,7 @@
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
- <skipDockerBuild>false</skipDockerBuild>
+ <skipDockerBuild>false</skipDockerBuild>
<serverId>${onap.nexus.dockerregistry.daily}</serverId>
<imageName>${onap.nexus.dockerregistry.daily}/${docker.image.name}</imageName>
<imageTags>
@@ -341,54 +349,18 @@
<entryPoint>bin/run.sh</entryPoint>
</configuration>
- <!--<executions>
- <execution>
- <id>build-image</id>
- <phase>package</phase>
- <goals>
- <goal>build</goal>
- </goals>
- </execution>
- <execution>
- <id>tag-and-push-image-latest</id>
- <phase>deploy</phase>
- <goals>
- <goal>tag</goal>
- </goals>
- <configuration>
- <image>${docker.image.name}:latest</image>
- <newName>${onap.nexus.dockerregistry.daily}/${docker.image.name}:latest</newName>
- <pushImage>true</pushImage>
- </configuration>
- </execution>
- <execution>
- <id>tag-and-push-image-with-version</id>
- <phase>deploy</phase>
- <goals>
- <goal>tag</goal>
- </goals>
- <configuration>
- <image>${docker.image.name}:latest</image>
- <newName>${onap.nexus.dockerregistry.daily}/${docker.image.name}:${project.version}
- </newName>
- <pushImage>true</pushImage>
- </configuration>
- </execution>
- <execution>
- <id>tag-and-push-image-with-version-and-date</id>
- <phase>deploy</phase>
- <goals>
- <goal>tag</goal>
- </goals>
- <configuration>
- <image>${docker.image.name}:latest</image>
- <newName>
- ${onap.nexus.dockerregistry.daily}/${docker.image.name}:${project.version}-${maven.build.timestamp}Z
- </newName>
- <pushImage>true</pushImage>
- </configuration>
- </execution>
- </executions>-->
+ <!--<executions> <execution> <id>build-image</id> <phase>package</phase>
+ <goals> <goal>build</goal> </goals> </execution> <execution> <id>tag-and-push-image-latest</id>
+ <phase>deploy</phase> <goals> <goal>tag</goal> </goals> <configuration> <image>${docker.image.name}:latest</image>
+ <newName>${onap.nexus.dockerregistry.daily}/${docker.image.name}:latest</newName>
+ <pushImage>true</pushImage> </configuration> </execution> <execution> <id>tag-and-push-image-with-version</id>
+ <phase>deploy</phase> <goals> <goal>tag</goal> </goals> <configuration> <image>${docker.image.name}:latest</image>
+ <newName>${onap.nexus.dockerregistry.daily}/${docker.image.name}:${project.version}
+ </newName> <pushImage>true</pushImage> </configuration> </execution> <execution>
+ <id>tag-and-push-image-with-version-and-date</id> <phase>deploy</phase> <goals>
+ <goal>tag</goal> </goals> <configuration> <image>${docker.image.name}:latest</image>
+ <newName> ${onap.nexus.dockerregistry.daily}/${docker.image.name}:${project.version}-${maven.build.timestamp}Z
+ </newName> <pushImage>true</pushImage> </configuration> </execution> </executions> -->
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
@@ -422,9 +394,9 @@
</sources>
</configuration>
</execution>
- <!-- <execution> <id>regex-property</id> <goals> <goal>regex-property</goal>
- </goals> <configuration> <name>docker.version</name> <value>${project.version}</value>
- <regex>(^[0-9]+.[0-9]+.[0-9]+$)</regex> <replacement>$1-STAGING</replacement>
+ <!-- <execution> <id>regex-property</id> <goals> <goal>regex-property</goal>
+ </goals> <configuration> <name>docker.version</name> <value>${project.version}</value>
+ <regex>(^[0-9]+.[0-9]+.[0-9]+$)</regex> <replacement>$1-STAGING</replacement>
<failIfNoMatch>false</failIfNoMatch> </configuration> </execution> -->
</executions>
</plugin>
diff --git a/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/service/VESAdapterInitializer.java b/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/service/VESAdapterInitializer.java
index 2ac4fe9..e7e4705 100644
--- a/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/service/VESAdapterInitializer.java
+++ b/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/service/VESAdapterInitializer.java
@@ -23,13 +23,24 @@ package org.onap.universalvesadapter.service;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
+import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.HttpClientBuilder;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClientFactory;
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsRequests;
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest;
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
+import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
import org.onap.universalvesadapter.utils.CollectorConfigPropertyRetrival;
import org.onap.universalvesadapter.utils.FetchDynamicConfig;
import org.slf4j.Logger;
@@ -45,208 +56,142 @@ import org.springframework.stereotype.Component;
// AdapterInitializer
@Component
public class VESAdapterInitializer implements CommandLineRunner, Ordered {
- private static final Logger debugLogger = LoggerFactory.getLogger("debugLogger");
- private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger");
-
- @Value("${defaultConfigFilelocation}")
- String defaultConfigFilelocation;
- @Value("${server.port}")
- String serverPort;
-
- private static Map<String, String> mappingFiles = new HashMap<String, String>();
- private static Map<String, String> env;
-
- @Autowired
- private ApplicationContext applicationContext;
-
- @Override
- public void run(String... args) throws Exception {
- debugLogger.info("The Default Config file Location:" + defaultConfigFilelocation.trim());
-
- if (ClassLoader.getSystemResource(defaultConfigFilelocation.trim()) == null) {
- errorLogger.error(
- "Default Config file " + defaultConfigFilelocation.trim() + " is missing");
- System.exit(SpringApplication.exit(applicationContext, () -> {
- errorLogger.error("Application stoped due to missing default Config file");
- return -1;
- }));
- }
- env = System.getenv();
- for (Map.Entry<String, String> entry : env.entrySet()) {
- debugLogger.debug(entry.getKey() + ":" + entry.getValue());
- }
-
- // check for consul details
- if (env.containsKey("CONSUL_HOST") && env.containsKey("CONFIG_BINDING_SERVICE")
- && env.containsKey("HOSTNAME")) {
- debugLogger.info(">>>Dynamic configuration to be used");
- FetchDynamicConfig.cbsCall(defaultConfigFilelocation);
-
- } else {
- debugLogger.info(">>>Static configuration to be used");
-
- }
- readJsonToMap(defaultConfigFilelocation);
-
- // prepareDatabase();
- // fetchMappingFile();
-
- debugLogger.info("Triggering controller's start url ");
- executecurl("http://localhost:" + serverPort + "/start");
- }
-
-
- private static String executecurl(String url) {
-
- debugLogger.info("Running curl command for url:{}", url);
- String[] command = {"curl", "-v", url};
- ProcessBuilder process = new ProcessBuilder(command);
- Process p;
- String result = null;
- try {
- p = process.start();
- try (InputStreamReader ipr = new InputStreamReader(p.getInputStream());
- BufferedReader reader = new BufferedReader(ipr)) {
- StringBuilder builder = new StringBuilder();
- String line;
-
- while ((line = reader.readLine()) != null) {
- builder.append(line);
- }
- result = builder.toString();
- }
- } catch (IOException e) {
- errorLogger.error("error", e);
- }
- return result;
-
- }
-
- private void readJsonToMap(String configFile) {
- try {
- JSONArray collectorArray =
- CollectorConfigPropertyRetrival.collectorConfigArray(configFile);
-
- for (int i = 0; i < collectorArray.size(); i++) {
- JSONObject obj2 = (JSONObject) collectorArray.get(i);
-
- if (obj2.containsKey("mapping-files")) {
-
- JSONArray a1 = (JSONArray) obj2.get("mapping-files");
-
- for (int j = 0; j < a1.size(); j++) {
- JSONObject obj3 = (JSONObject) a1.get(j);
- Set<Entry<String, String>> set = obj3.entrySet();
-
- for (Entry<String, String> entry : set) {
-
- mappingFiles.put(entry.getKey(), entry.getValue());
- }
- }
-
- }
- }
-
- } catch (Exception e) {
- e.printStackTrace();
- errorLogger.error("Exception occured while reading Collector config file cause: ",
- e.getCause());
- }
-
- }
-
-
- /*
- * private void prepareDatabase() throws IOException {
- *
- *
- * debugLogger.info("The Default Mapping file Location:" + defaultMappingFileLocation.trim());
- *
- * if (ClassLoader.getSystemResource(defaultMappingFileLocation.trim()) == null) {
- * errorLogger.error( "Default mapping file " + defaultMappingFileLocation.trim() +
- * " is missing"); System.exit(SpringApplication.exit(applicationContext, () -> {
- * errorLogger.error("Application stoped due to missing default mapping file"); return -1; }));
- * }
- *
- * File file = new File(
- * ClassLoader.getSystemResource(defaultMappingFileLocation.trim()).getFile());
- *
- * try (FileInputStream fileInputStream = new FileInputStream(file)) { bytesArray = new
- * byte[(int) file.length()]; fileInputStream.read(bytesArray);
- *
- * } catch (IOException e1) {
- * errorLogger.error("Exception Occured while reading the default mapping file ,Cause: " +
- * e1.getMessage(), e1); // exit on missing default mapping file
- * System.exit(SpringApplication.exit(applicationContext, () -> {
- * errorLogger.error("Application stoped due to missing default mapping file"); return -1; }));
- * }
- *
- * try (Connection con = DriverManager.getConnection(dBurl, user, pwd); // creating table if not
- * exist PreparedStatement pstmt11 = con.prepareStatement("CREATE TABLE IF NOT EXISTS public." +
- * MappingFileTableName + "\r\n" + "(\r\n" +
- * " enterpriseid character varying COLLATE pg_catalog.\"default\" NOT NULL,\r\n" +
- * " mappingfilecontents bytea,\r\n" +
- * " mimetype character varying COLLATE pg_catalog.\"default\",\r\n" +
- * " file_name character varying COLLATE pg_catalog.\"default\",\r\n" +
- * " CONSTRAINT mapping_file_pkey5 PRIMARY KEY (enterpriseid)\r\n" + ")\r\n" + "WITH (\r\n" +
- * " OIDS = FALSE\r\n" + ")\r\n" + "TABLESPACE pg_default;")) {
- *
- * metricsLogger.info("Postgresql Connection successful...");
- * debugLogger.debug("Connection object:{}" , con.toString());
- *
- * pstmt11.executeUpdate();
- * debugLogger.info("CREATE TABLE IF NOT EXISTS executed successfully....");
- *
- * if ((bytesArray.length > 0) && (!Arrays.toString(bytesArray).equals(""))) {
- *
- * try (PreparedStatement pstmt = con.prepareStatement("INSERT INTO " + MappingFileTableName +
- * "(enterpriseid, mappingfilecontents, mimetype, File_Name) VALUES (?, ?, ?, ?) ON CONFLICT (enterpriseid) DO NOTHING;"
- * )) { pstmt.setString(1, defaultEnterpriseId); pstmt.setBytes(2, bytesArray);
- * pstmt.setString(3, "text/xml"); pstmt.setString(4, file.getName());
- *
- * pstmt.executeUpdate();
- * debugLogger.info("Made sure that default mapping file is present in table"); } } else {
- * errorLogger.error(file.getName() + " is empty"); // exit on empty mapping file
- * System.exit(SpringApplication.exit(applicationContext, () -> {
- * errorLogger.error("Application stoped beacuase default mapping file is empty.."); return -1;
- * })); }
- *
- * } catch (SQLException e) { errorLogger.error("Received exception : " + e.getMessage(), e); //
- * exit on SqlException System.exit(SpringApplication.exit(applicationContext, () -> {
- * errorLogger.error("Application Stoped due to ", e.getCause()); return -1; })); }
- *
- * }
- */
- /*
- * public void fetchMappingFile() {
- *
- * try (Connection con = DriverManager.getConnection(dBurl, user, pwd)) {
- * debugLogger.info("Retrieving data from DB"); PreparedStatement pstmt =
- * con.prepareStatement("SELECT * FROM mapping_file"); ResultSet rs = pstmt.executeQuery(); //
- * parsing the column each time is a linear search int column1Pos =
- * rs.findColumn("enterpriseid"); int column2Pos = rs.findColumn("mappingfilecontents"); String
- * hexString; while (rs.next()) { String column1 = rs.getString(column1Pos); String column2 =
- * rs.getString(column2Pos); hexString = column2.substring(2); byte[] bytes =
- * Hex.decodeHex(hexString.toCharArray()); String data = new String(bytes, "UTF-8");
- * mappingFiles.put(column1, data); }
- * debugLogger.info("DB Initialization Completed, Total # Mappingfiles are" +
- * mappingFiles.size()); } catch (Exception e) { errorLogger.error("Error occured due to :" +
- * e.getMessage()); e.printStackTrace(); }
- *
- * }
- */
-
- public static Map<String, String> getMappingFiles() {
- return mappingFiles;
- }
-
- public static void setMappingFiles(Map<String, String> mappingFiles) {
- VESAdapterInitializer.mappingFiles = mappingFiles;
- }
-
- @Override
- public int getOrder() {
- return 0;
- }
-
+ private static final Logger debugLogger = LoggerFactory.getLogger("debugLogger");
+ private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger");
+
+ @Value("${defaultConfigFilelocation}")
+ String defaultConfigFilelocation;
+ @Value("${server.port}")
+ String serverPort;
+
+ private static Map<String, String> mappingFiles = new HashMap<String, String>();
+
+ // Generate RequestID and InvocationID which will be used when logging and in
+ // HTTP requests
+ final RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();
+ final CbsRequest request = CbsRequests.getConfiguration(diagnosticContext);
+
+ // Read necessary properties from the environment
+ final EnvProperties env = EnvProperties.fromEnvironment();
+
+ // Polling properties :
+ final Duration initialDelay = Duration.ofSeconds(5);
+ final Duration period = Duration.ofMinutes(1);
+
+ @Autowired
+ private ApplicationContext applicationContext;
+
+ @Override
+ public void run(String... args) throws Exception {
+ debugLogger.info("The Default Config file Location:" + defaultConfigFilelocation.trim());
+
+ if (ClassLoader.getSystemResource(defaultConfigFilelocation.trim()) == null) {
+ errorLogger.error("Default Config file " + defaultConfigFilelocation.trim() + " is missing");
+ System.exit(SpringApplication.exit(applicationContext, () -> {
+ errorLogger.error("Application stoped due to missing default Config file");
+ return -1;
+ }));
+ }
+
+ // Create the client and use it to get the configuration
+ CbsClientFactory.createCbsClient(env).flatMapMany(cbsClient -> cbsClient.updates(request, initialDelay, period))
+ .subscribe(jsonObject -> {
+
+ // If env details not fetched static configuration file will be used
+ if (env.consulHost() != null && env.cbsName() != null && env.appName() != null) {
+ debugLogger.info(">>>Dynamic configuration to be used");
+ FetchDynamicConfig.cbsCall(defaultConfigFilelocation);
+ }
+
+ readJsonToMap(defaultConfigFilelocation);
+
+ debugLogger.info("Triggering controller's start url ");
+ fetchResultFromDestination("http://localhost:" + serverPort + "/start");
+
+ }, throwable -> {
+ debugLogger.warn("Cannot Connect", throwable);
+ });
+
+ }
+
+ /**
+ * gets the configuration details from JSON an puts those in the mapping data
+ * structure for further processing.
+ *
+ * @param configFile: String
+ */
+ private void readJsonToMap(String configFile) {
+ try {
+ JSONArray collectorArray = CollectorConfigPropertyRetrival.collectorConfigArray(configFile);
+
+ for (int i = 0; i < collectorArray.size(); i++) {
+ JSONObject obj2 = (JSONObject) collectorArray.get(i);
+
+ if (obj2.containsKey("mapping-files")) {
+
+ JSONArray a1 = (JSONArray) obj2.get("mapping-files");
+
+ for (int j = 0; j < a1.size(); j++) {
+ JSONObject obj3 = (JSONObject) a1.get(j);
+ Set<Entry<String, String>> set = obj3.entrySet();
+
+ for (Entry<String, String> entry : set) {
+
+ mappingFiles.put(entry.getKey(), entry.getValue());
+ }
+ }
+
+ }
+ }
+
+ } catch (Exception e) {
+ // e.printStackTrace();
+ errorLogger.error(
+ " Class VESAdapterInitializer: method readJsonToMap: Exception occured while reading Collector config file cause: ",
+ e.getCause());
+ }
+ }
+
+ public static Map<String, String> getMappingFiles() {
+ return mappingFiles;
+ }
+
+ public static void setMappingFiles(Map<String, String> mappingFiles) {
+ VESAdapterInitializer.mappingFiles = mappingFiles;
+ }
+
+ @Override
+ public int getOrder() {
+ return 0;
+ }
+
+ private static String fetchResultFromDestination(String url) {
+ debugLogger.debug("VESAdapterInitializer:: fetchResultFromDestination :: START");
+ String line = "";
+ StringBuffer sb = new StringBuffer();
+ try {
+ HttpClient client = HttpClientBuilder.create().build();
+ HttpGet request = new HttpGet(url);
+ HttpResponse response = client.execute(request);
+ BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
+
+ while ((line = rd.readLine()) != null) {
+ sb.append(line);
+ sb.append('\n');
+ }
+ } catch (ClientProtocolException e) {
+ debugLogger.debug("VESAdapterInitializer:: fetchResultFromDestination :: ClientProtocolException thrown "
+ + e.getMessage());
+ } catch (UnsupportedOperationException e) {
+ debugLogger
+ .debug("VESAdapterInitializer:: fetchResultFromDestination :: UnsupportedOperationException thrown "
+ + e.getMessage());
+ } catch (IOException e) {
+ debugLogger.debug(
+ "VESAdapterInitializer:: fetchResultFromDestination :: IOException thrown " + e.getMessage());
+ }
+ debugLogger.debug("VESAdapterInitializer:: fetchResultFromDestination :: END");
+ return sb.toString();
+ }
+
}
diff --git a/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/FetchDynamicConfig.java b/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/FetchDynamicConfig.java
index 022a586..9493fc6 100644
--- a/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/FetchDynamicConfig.java
+++ b/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/FetchDynamicConfig.java
@@ -28,11 +28,19 @@ import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.file.Files;
-import java.util.Map;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.HttpClientBuilder;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsRequests;
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest;
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
+import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@@ -43,177 +51,182 @@ import com.fasterxml.jackson.databind.ObjectMapper;
@Component
public class FetchDynamicConfig {
-
- private static final Logger debugLogger = LoggerFactory.getLogger("debugLogger");
- private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger");
-
- private static String url;
- public static String retString;
- public static String retCBSString;
- private static Map<String, String> env;
-
- public FetchDynamicConfig() {}
-
- public static void cbsCall(String configFile) {
-
- env = System.getenv();
- Boolean areEqual;
- // Call consul api and identify the CBS Service address and port
- getconsul();
- // Construct and invoke CBS API to get application Configuration
- getCBS();
- // Verify if data has changed
- areEqual = verifyConfigChange(configFile);
-
- if (!areEqual) {
- FetchDynamicConfig fc = new FetchDynamicConfig();
- fc.writefile(retCBSString, configFile);
- } else {
- debugLogger
- .info("New config pull results identical - " + configFile + " NOT refreshed");
- }
- }
-
- private static void getconsul() {
- url = env.get("CONSUL_HOST") + ":8500/v1/catalog/service/"
- + env.get("CONFIG_BINDING_SERVICE");
- retString = executecurl(url);
- debugLogger.info("CBS details fetched from Consul");
- }
-
- public static boolean verifyConfigChange(String configFile) {
-
- boolean areEqual = false;
- // Read current data
- try {
-
- File f = new File(ClassLoader.getSystemResource(configFile.trim()).getFile());
-
- if (f.exists() && !f.isDirectory()) {
- debugLogger.info(
- "Comparing local configuration with the configuration fethed from CBS ");
-
- String jsonData = readFile(configFile);
- JSONObject jsonObject = new JSONObject(jsonData);
-
- ObjectMapper mapper = new ObjectMapper();
-
- JsonNode tree1 = mapper.readTree(jsonObject.toString());
- JsonNode tree2 = mapper.readTree(retCBSString);
- areEqual = tree1.equals(tree2);
- debugLogger.info("Comparison value:" + areEqual);
- } else {
- debugLogger.info("First time config file read: " + configFile);
- }
-
- } catch (IOException e) {
- errorLogger.error("Comparison with new fetched data failed" + e.getMessage());
-
- }
-
- return areEqual;
-
- }
-
- public static void getCBS() {
-
- // consul return as array
- JSONTokener temp = new JSONTokener(retString);
- JSONObject cbsjobj = (JSONObject) new JSONArray(temp).get(0);
-
- String urlPart1 = null;
- if (cbsjobj.has("ServiceAddress") && cbsjobj.has("ServicePort")) {
-
- urlPart1 = cbsjobj.getString("ServiceAddress") + ":" + cbsjobj.getInt("ServicePort");
-
- }
- debugLogger.info("CONFIG_BINDING_SERVICE HOST:PORT is " + urlPart1);
-
- if (env.containsKey("HOSTNAME")) {
- url = urlPart1 + "/service_component/" + env.get("HOSTNAME");
- retCBSString = executecurl(url);
- debugLogger.info("Configuration fetched from CBS successfully..");
- } else if (env.containsKey("SERVICE_NAME")) {
- url = urlPart1 + "/service_component/" + env.get("SERVICE_NAME");
- retCBSString = executecurl(url);
- debugLogger.info("Configuration fetched from CBS successfully..");
- } else {
- errorLogger.error(
- "Service name environment variable - HOSTNAME/SERVICE_NAME not found within container ");
- }
-
- }
-
- public void writefile(String retCBSString, String configFile) {
-
- String indentedretstring = (new JSONObject(retCBSString)).toString(4);
- File f = new File(ClassLoader.getSystemResource(configFile.trim()).getFile());
- try {
- debugLogger.info("Overwriting local configuration file " + configFile
- + " with configuartions received from CBS");
-
-
- File file2 = ResourceUtils.getFile("classpath:" + configFile);
- FileWriter fstream = new FileWriter(file2, false);
- PrintWriter printWriter = new PrintWriter(fstream);
- printWriter.print(indentedretstring);
- printWriter.close();
- fstream.close();
-
- debugLogger.info("New Config successfully written to local file to " + configFile);
- } catch (IOException e) {
- errorLogger.error("Error in writing configuration into local KV file " + configFile
- + retString + e.getMessage());
- e.printStackTrace();
- }
-
- }
-
- public static String readFile(String configFileName) {
- String content = null;
- File file = null;
-
- try {
- file = ResourceUtils.getFile("classpath:" + configFileName);
- content = new String(Files.readAllBytes(file.toPath()));
- } catch (FileNotFoundException e) {
- errorLogger.error("colud not find file :",configFileName);
-
- } catch (IOException e) {
- errorLogger.error("unable to read the file , reason:", e.getCause());
- } catch (Exception e) {
- errorLogger.error("Exception occured , reason:", e.getMessage());
- }
-
- return content;
-
- }
-
- private static String executecurl(String url) {
-
- String[] command = {"curl", "-v", url};
- ProcessBuilder process = new ProcessBuilder(command);
- Process p;
- String result = null;
- try {
- p = process.start();
- InputStreamReader ipr = new InputStreamReader(p.getInputStream());
- BufferedReader reader = new BufferedReader(ipr);
- StringBuilder builder = new StringBuilder();
- String line;
-
- while ((line = reader.readLine()) != null) {
- builder.append(line);
- }
- result = builder.toString();
- reader.close();
- ipr.close();
- } catch (IOException e) {
- errorLogger.error("error", e);
- e.printStackTrace();
- }
- return result;
-
- }
-
+
+ // @Value("${defaultProtocol}")
+ static String defaultProtocol = "http";
+
+ private static final Logger debugLogger = LoggerFactory.getLogger("debugLogger");
+ private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger");
+
+ private static String url;
+ public static String retString;
+ public static String retCBSString;
+
+ // Generate RequestID and InvocationID which will be used when logging and in
+ // HTTP requests
+ final RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();
+ final CbsRequest request = CbsRequests.getConfiguration(diagnosticContext);
+
+ // Read necessary properties from the environment
+ static final EnvProperties env = EnvProperties.fromEnvironment();
+
+ public FetchDynamicConfig() {
+
+ }
+
+ public static void cbsCall(String configFile) {
+
+ Boolean areEqual;
+ // Call consul api and identify the CBS Service address and port
+ getconsul();
+ // Construct and invoke CBS API to get application Configuration
+ getCBS();
+ // Verify if data has changed
+ areEqual = verifyConfigChange(configFile);
+
+ if (!areEqual) {
+ FetchDynamicConfig fc = new FetchDynamicConfig();
+ if (retCBSString!=null) {
+ fc.writefile(retCBSString, configFile);
+ } else {
+ debugLogger.debug("No content recieved from server");
+ }
+
+ } else {
+ debugLogger.info("New config pull results identical - " + configFile + " NOT refreshed");
+ }
+ }
+
+ private static void getconsul() {
+ url = defaultProtocol + "://" + env.consulHost() + ":" + env.consulPort() + "/v1/catalog/service/"
+ + env.cbsName();
+ retString = fetchResultFromDestination(url);
+ debugLogger.info("CBS details fetched from Consul");
+ }
+
+ public static boolean verifyConfigChange(String configFile) {
+
+ boolean areEqual = false;
+ // Read current data
+ try {
+
+ File f = new File(ClassLoader.getSystemResource(configFile.trim()).getFile());
+
+ if (f.exists() && !f.isDirectory()) {
+ debugLogger.info("Comparing local configuration with the configuration fethed from CBS ");
+
+ String jsonData = readFile(configFile);
+ JSONObject jsonObject = new JSONObject(jsonData);
+
+ ObjectMapper mapper = new ObjectMapper();
+
+ JsonNode tree1 = mapper.readTree(jsonObject.toString());
+ JsonNode tree2 = mapper.readTree(retCBSString);
+ areEqual = tree1.equals(tree2);
+ debugLogger.info("Comparison value:" + areEqual);
+ } else {
+ debugLogger.info("First time config file read: " + configFile);
+ }
+
+ } catch (IOException e) {
+ errorLogger.error("Comparison with new fetched data failed" + e.getMessage());
+
+ }
+
+ return areEqual;
+
+ }
+
+ public static void getCBS() {
+
+ // consul return as array
+ JSONTokener temp = new JSONTokener(retString);
+ JSONObject cbsjobj = (JSONObject) new JSONArray(temp).get(0);
+
+ String urlPart1 = null;
+ if (cbsjobj.has("ServiceAddress") && cbsjobj.has("ServicePort")) {
+
+ urlPart1 = cbsjobj.getString("ServiceAddress") + ":" + cbsjobj.getInt("ServicePort");
+
+ }
+ debugLogger.info("CONFIG_BINDING_SERVICE HOST:PORT is " + urlPart1);
+
+ if (env.appName() != null) {
+ url = defaultProtocol + "://" + urlPart1 + "/service_component/" + env.appName();
+ retCBSString = fetchResultFromDestination(url);
+ debugLogger.info("Configuration fetched from CBS successfully..");
+ } else {
+ errorLogger.error("Service name environment variable - APP_NAME/SERVICE_NAME not found within container ");
+ }
+
+ }
+
+ public void writefile(String retCBSString, String configFile) {
+
+ String indentedretstring = (new JSONObject(retCBSString)).toString(4);
+ try {
+ debugLogger.info(
+ "Overwriting local configuration file " + configFile + " with configuartions received from CBS");
+
+ File file2 = ResourceUtils.getFile("classpath:" + configFile);
+ FileWriter fstream = new FileWriter(file2, false);
+ PrintWriter printWriter = new PrintWriter(fstream);
+ printWriter.print(indentedretstring);
+ printWriter.close();
+ fstream.close();
+
+ debugLogger.info("New Config successfully written to local file to " + configFile);
+ } catch (IOException e) {
+ errorLogger.error(
+ "Error in writing configuration into local KV file " + configFile + retString + e.getMessage());
+ e.printStackTrace();
+ }
+
+ }
+
+ public static String readFile(String configFileName) {
+ String content = null;
+ File file = null;
+
+ try {
+ file = ResourceUtils.getFile("classpath:" + configFileName);
+ content = new String(Files.readAllBytes(file.toPath()));
+ } catch (FileNotFoundException e) {
+ errorLogger.error("colud not find file :", configFileName);
+
+ } catch (IOException e) {
+ errorLogger.error("unable to read the file , reason:", e.getCause());
+ } catch (Exception e) {
+ errorLogger.error("Exception occured , reason:", e.getMessage());
+ }
+
+ return content;
+ }
+
+ private static String fetchResultFromDestination(String url) {
+ debugLogger.debug("FetchDynamicConfig : fetchResultFromDestination : START");
+
+ StringBuffer sb = new StringBuffer();
+ try {
+ HttpClient client = HttpClientBuilder.create().build();
+ HttpGet request = new HttpGet(url);
+ HttpResponse response = client.execute(request);
+ BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
+ String line = "";
+ while ((line = rd.readLine()) != null) {
+ sb.append(line);
+ sb.append('\n');
+ }
+ } catch (ClientProtocolException e) {
+ debugLogger.debug("FetchDynamicConfig : fetchResultFromDestination : ClientProtocolException thrown."+e.getMessage());
+ } catch (UnsupportedOperationException e) {
+ debugLogger.debug("FetchDynamicConfig : fetchResultFromDestination : UnsupportedOperationException thrown."+e.getMessage());
+ } catch (IOException e) {
+ debugLogger.debug("FetchDynamicConfig : fetchResultFromDestination : IOException thrown."+e.getMessage());
+ }
+
+ debugLogger.debug("FetchDynamicConfig : fetchResultFromDestination : END");
+ return sb.toString();
+ }
+
}