From ec84ff94604ab1d31ae6b05fee7c3bc7bca30291 Mon Sep 17 00:00:00 2001 From: Parshad Patel Date: Wed, 17 Jul 2019 19:57:57 +0900 Subject: Fix sonar issues in UniversalVesAdapter Fix Either log or rethrow this exception Use try-with-resources or close this "FileWriter" in a "finally" clause Use a logger to log this exception Rename CollectorConfigPropertyRetrival.java to CollectorConfigPropertyRetrieval Issue-ID: DCAEGEN2-1384 Change-Id: If38042bd192da2ca30b5d6eaf3288e4e83ef4682 Signed-off-by: Parshad Patel --- .../adapter/UniversalEventAdapter.java | 396 +++++++++--------- .../service/VESAdapterInitializer.java | 393 +++++++++--------- .../universalvesadapter/service/VesService.java | 388 ++++++++--------- .../utils/CollectorConfigPropertyRetrieval.java | 161 ++++++++ .../utils/CollectorConfigPropertyRetrival.java | 171 -------- .../utils/FetchDynamicConfig.java | 457 ++++++++++----------- 6 files changed, 972 insertions(+), 994 deletions(-) create mode 100644 UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/CollectorConfigPropertyRetrieval.java delete mode 100644 UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/CollectorConfigPropertyRetrival.java (limited to 'UniversalVesAdapter/src/main') diff --git a/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/adapter/UniversalEventAdapter.java b/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/adapter/UniversalEventAdapter.java index 483b19b..7e41f38 100644 --- a/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/adapter/UniversalEventAdapter.java +++ b/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/adapter/UniversalEventAdapter.java @@ -1,200 +1,196 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : DCAE - * ================================================================================ - * Copyright 2018-2019 TechMahindra - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.universalvesadapter.adapter; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.Iterator; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import javax.annotation.PreDestroy; - -import org.milyn.Smooks; -import org.onap.universalvesadapter.exception.ConfigFileSmooksConversionException; -import org.onap.universalvesadapter.exception.VesException; -import org.onap.universalvesadapter.service.VESAdapterInitializer; -import org.onap.universalvesadapter.utils.CollectorConfigPropertyRetrival; -import org.onap.universalvesadapter.utils.SmooksUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; -import org.xml.sax.SAXException; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.gson.Gson; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; -import com.google.gson.JsonSyntaxException; - -/** - * Default implementation of the Generic Adapter - * - * @author kmalbari - * - */ - -@Component -public class UniversalEventAdapter implements GenericAdapter { - private static final Logger debugLogger = LoggerFactory.getLogger("debugLogger"); - private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger"); - - @Value("${defaultConfigFilelocation}") - private String defaultConfigFilelocation; - private String collectorIdentifierValue; - private String collectorIdentifierKey; - private Map eventToSmooksMapping = new ConcurrentHashMap<>(); - - public UniversalEventAdapter() { - - } - - /** - * transforms JSON to VES format and and returns the ves Event - * - * @param IncomingJason,eventType - * @return ves Event - */ - @Override - public String transform(String incomingJsonString) - throws ConfigFileSmooksConversionException, VesException { - String result = ""; - String configFileData; - - String identifier[] = CollectorConfigPropertyRetrival.getProperyArray("identifier", - defaultConfigFilelocation); - String defaultMappingFile = - "defaultMappingFile-" + Thread.currentThread().getName(); - try { - - Gson gson = new Gson(); - JsonObject body = gson.fromJson(incomingJsonString, JsonObject.class); - - JsonElement results; - for (int i = 0; i < identifier.length; i++) { - JsonObject obj; - if ((obj = keyObject(body, identifier[i])).has(identifier[i])) { - collectorIdentifierKey = identifier[i]; - results = obj.get(identifier[i]); - collectorIdentifierValue = results.getAsString(); - - } - - } - // collectorIdentifierValue = collectorIdentifierValue.substring(0, - // collectorIdentifierValue.length() - 4); - if (collectorIdentifierKey.equals("notify OID")) { - collectorIdentifierValue = collectorIdentifierValue.substring(0, - collectorIdentifierValue.length() - 4); - } - - - if (VESAdapterInitializer.getMappingFiles() - .containsKey(collectorIdentifierValue)) { - configFileData = VESAdapterInitializer.getMappingFiles() - .get(collectorIdentifierValue); - debugLogger.debug( - "Using Mapping file as Mapping file is available for collector identifier:{}", - collectorIdentifierValue); - - } else { - - configFileData = VESAdapterInitializer.getMappingFiles() - .get(defaultMappingFile); - - debugLogger.debug( - "Using Default Mapping file as Mapping file is not available for Enterprise Id / identifer ID:{}", - collectorIdentifierValue); - } - - Smooks smooksTemp = new Smooks(new ByteArrayInputStream( - configFileData.getBytes(StandardCharsets.UTF_8))); - eventToSmooksMapping.put(collectorIdentifierKey, smooksTemp); - - Object vesEvent = SmooksUtils.getTransformedObjectForInput(smooksTemp, - incomingJsonString); - debugLogger.info("Incoming json transformed to VES format successfully:" - + Thread.currentThread().getName()); - ObjectMapper objectMapper = new ObjectMapper(); - result = objectMapper.writeValueAsString(vesEvent); - debugLogger.info("Serialized VES json"); - } catch (JsonProcessingException exception) { - throw new VesException("Unable to convert pojo to VES format, Reason :{}", - exception); - } catch (SAXException | IOException exception) { - // Invalid Mapping file - exception.printStackTrace(); - errorLogger.error("Dropping this Trap :{},Reason:{}", incomingJsonString, - exception.getMessage()); - - } catch (JsonSyntaxException exception) { - // Invalid Trap - errorLogger.error("Dropping this Invalid json Trap :{}, Reason:{}", - incomingJsonString, exception); - } catch (JsonParseException exception) { - // Invalid Trap - errorLogger.error("Dropping this Invalid json Trap :{}, Reason:{}", - incomingJsonString, exception); - } catch (RuntimeException exception) { - - exception.printStackTrace(); - errorLogger.error("Dropping this Trap :{},Reason:{}", incomingJsonString, - exception.getMessage()); - - } - return result; - } - - /** - * Closes all open smooks' instances before bean is destroyed - */ - @PreDestroy - public void destroy() { - for (Smooks smooks : eventToSmooksMapping.values()) - smooks.close(); - debugLogger.warn("All Smooks objects closed"); - } - - public JsonObject keyObject(JsonObject object, String searchedKey) { - boolean exists = object.has(searchedKey); - JsonObject jsonObject = object; - - if (!exists) { - Iterator keys = object.keySet().iterator(); - while (keys.hasNext()) { - String key = (String) keys.next(); - if (object.get(key) instanceof JsonObject) { - - jsonObject = (JsonObject) object.get(key); - JsonObject obj = keyObject(jsonObject, searchedKey); - exists = obj.has(searchedKey); - } - } - } - - return jsonObject; - } - -} +/*- + * ============LICENSE_START======================================================= + * ONAP : DCAE + * ================================================================================ + * Copyright 2018-2019 TechMahindra + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.universalvesadapter.adapter; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import javax.annotation.PreDestroy; +import org.milyn.Smooks; +import org.onap.universalvesadapter.exception.ConfigFileSmooksConversionException; +import org.onap.universalvesadapter.exception.VesException; +import org.onap.universalvesadapter.service.VESAdapterInitializer; +import org.onap.universalvesadapter.utils.CollectorConfigPropertyRetrieval; +import org.onap.universalvesadapter.utils.SmooksUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; +import org.xml.sax.SAXException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.JsonSyntaxException; + +/** + * Default implementation of the Generic Adapter + * + * @author kmalbari + * + */ + +@Component +public class UniversalEventAdapter implements GenericAdapter { + private static final Logger debugLogger = LoggerFactory.getLogger("debugLogger"); + private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger"); + + @Value("${defaultConfigFilelocation}") + private String defaultConfigFilelocation; + private String collectorIdentifierValue; + private String collectorIdentifierKey; + private Map eventToSmooksMapping = new ConcurrentHashMap<>(); + + public UniversalEventAdapter() { + + } + + /** + * transforms JSON to VES format and and returns the ves Event + * + * @param IncomingJason,eventType + * @return ves Event + */ + @Override + public String transform(String incomingJsonString) + throws ConfigFileSmooksConversionException, VesException { + String result = ""; + String configFileData; + + String identifier[] = CollectorConfigPropertyRetrieval.getProperyArray("identifier", + defaultConfigFilelocation); + String defaultMappingFile = + "defaultMappingFile-" + Thread.currentThread().getName(); + try { + + Gson gson = new Gson(); + JsonObject body = gson.fromJson(incomingJsonString, JsonObject.class); + + JsonElement results; + for (int i = 0; i < identifier.length; i++) { + JsonObject obj; + if ((obj = keyObject(body, identifier[i])).has(identifier[i])) { + collectorIdentifierKey = identifier[i]; + results = obj.get(identifier[i]); + collectorIdentifierValue = results.getAsString(); + + } + + } + // collectorIdentifierValue = collectorIdentifierValue.substring(0, + // collectorIdentifierValue.length() - 4); + if (collectorIdentifierKey.equals("notify OID")) { + collectorIdentifierValue = collectorIdentifierValue.substring(0, + collectorIdentifierValue.length() - 4); + } + + if (VESAdapterInitializer.getMappingFiles() + .containsKey(collectorIdentifierValue)) { + configFileData = VESAdapterInitializer.getMappingFiles() + .get(collectorIdentifierValue); + debugLogger.debug( + "Using Mapping file as Mapping file is available for collector identifier:{}", + collectorIdentifierValue); + + } else { + + configFileData = VESAdapterInitializer.getMappingFiles() + .get(defaultMappingFile); + + debugLogger.debug( + "Using Default Mapping file as Mapping file is not available for Enterprise Id / identifer ID:{}", + collectorIdentifierValue); + } + + Smooks smooksTemp = new Smooks(new ByteArrayInputStream( + configFileData.getBytes(StandardCharsets.UTF_8))); + eventToSmooksMapping.put(collectorIdentifierKey, smooksTemp); + + Object vesEvent = SmooksUtils.getTransformedObjectForInput(smooksTemp, + incomingJsonString); + debugLogger.info("Incoming json transformed to VES format successfully:" + + Thread.currentThread().getName()); + ObjectMapper objectMapper = new ObjectMapper(); + result = objectMapper.writeValueAsString(vesEvent); + debugLogger.info("Serialized VES json"); + } catch (JsonProcessingException exception) { + throw new VesException("Unable to convert pojo to VES format, Reason :{}", + exception); + } catch (SAXException | IOException exception) { + // Invalid Mapping file + exception.printStackTrace(); + errorLogger.error("Dropping this Trap :{},Reason:{}", incomingJsonString, + exception.getMessage()); + + } catch (JsonSyntaxException exception) { + // Invalid Trap + errorLogger.error("Dropping this Invalid json Trap :{}, Reason:{}", + incomingJsonString, exception); + } catch (JsonParseException exception) { + // Invalid Trap + errorLogger.error("Dropping this Invalid json Trap :{}, Reason:{}", + incomingJsonString, exception); + } catch (RuntimeException exception) { + + exception.printStackTrace(); + errorLogger.error("Dropping this Trap :{},Reason:{}", incomingJsonString, + exception.getMessage()); + + } + return result; + } + + /** + * Closes all open smooks' instances before bean is destroyed + */ + @PreDestroy + public void destroy() { + for (Smooks smooks : eventToSmooksMapping.values()) + smooks.close(); + debugLogger.warn("All Smooks objects closed"); + } + + public JsonObject keyObject(JsonObject object, String searchedKey) { + boolean exists = object.has(searchedKey); + JsonObject jsonObject = object; + + if (!exists) { + Iterator keys = object.keySet().iterator(); + while (keys.hasNext()) { + String key = (String) keys.next(); + if (object.get(key) instanceof JsonObject) { + + jsonObject = (JsonObject) object.get(key); + JsonObject obj = keyObject(jsonObject, searchedKey); + exists = obj.has(searchedKey); + } + } + } + + return jsonObject; + } + +} 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 e7e4705..85c5f42 100644 --- a/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/service/VESAdapterInitializer.java +++ b/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/service/VESAdapterInitializer.java @@ -1,197 +1,196 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : DCAE - * ================================================================================ - * Copyright 2018-2019 TechMahindra - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -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; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.CommandLineRunner; -import org.springframework.boot.SpringApplication; -import org.springframework.context.ApplicationContext; -import org.springframework.core.Ordered; -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 mappingFiles = new HashMap(); - - // 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> set = obj3.entrySet(); - - for (Entry 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 getMappingFiles() { - return mappingFiles; - } - - public static void setMappingFiles(Map 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(); - } - -} +/*- + * ============LICENSE_START======================================================= + * ONAP : DCAE + * ================================================================================ + * Copyright 2018-2019 TechMahindra + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +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.CollectorConfigPropertyRetrieval; +import org.onap.universalvesadapter.utils.FetchDynamicConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.context.ApplicationContext; +import org.springframework.core.Ordered; +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 mappingFiles = new HashMap(); + + // 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 = CollectorConfigPropertyRetrieval.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> set = obj3.entrySet(); + + for (Entry 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 getMappingFiles() { + return mappingFiles; + } + + public static void setMappingFiles(Map 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/service/VesService.java b/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/service/VesService.java index 644c348..26d4303 100644 --- a/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/service/VesService.java +++ b/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/service/VesService.java @@ -1,194 +1,194 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : DCAE - * ================================================================================ - * Copyright 2018-2019 TechMahindra - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.universalvesadapter.service; - -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -import org.onap.universalvesadapter.adapter.UniversalEventAdapter; -import org.onap.universalvesadapter.dmaap.Creator; -import org.onap.universalvesadapter.dmaap.MRPublisher.DMaaPMRPublisher; -import org.onap.universalvesadapter.dmaap.MRSubcriber.DMaaPMRSubscriber; -import org.onap.universalvesadapter.exception.DMaapException; -import org.onap.universalvesadapter.exception.MapperConfigException; -import org.onap.universalvesadapter.exception.VesException; -import org.onap.universalvesadapter.utils.CollectorConfigPropertyRetrival; -import org.onap.universalvesadapter.utils.DmaapConfig; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; - -/** - * Service that starts the universal ves adapter module to listen for events - * - * @author kmalbari - * - */ -/** - * @author PM00501616 - * - */ - -@Component -public class VesService { - - private static final Logger metricsLogger = LoggerFactory.getLogger("metricsLogger"); - private static final Logger debugLogger = LoggerFactory.getLogger("debugLogger"); - private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger"); - - private boolean isRunning = true; - @Value("${defaultConfigFilelocation}") - private String defaultConfigFilelocation; - @Autowired - private Creator creator; - @Autowired - private UniversalEventAdapter eventAdapter; - @Autowired - private DmaapConfig dmaapConfig; - @Autowired - private CollectorConfigPropertyRetrival collectorConfigPropertyRetrival; - private static List list = new LinkedList(); - - - /** - * method triggers universal VES adapter module. - */ - public void start() throws MapperConfigException { - debugLogger.info("Creating Subcriber and Publisher with creator............."); - String topicName = null; - String publisherTopic = null; - // Hashmap of subscriber and publisher details in correspondence to the respective - // collectors in kv file - Map dmaapTopics = collectorConfigPropertyRetrival - .getDmaapTopics("stream_subscriber", "stream_publisher", defaultConfigFilelocation); - - ExecutorService executorService = Executors.newFixedThreadPool(dmaapTopics.size()); - for (Map.Entry entry : dmaapTopics.entrySet()) { - String threadName = entry.getKey(); - // subcriber and corresponding publisher topics in a Map - Map subpubTopics = collectorConfigPropertyRetrival - .getTopics(entry.getKey(), entry.getValue(), defaultConfigFilelocation); - for (Map.Entry entry2 : subpubTopics.entrySet()) { - topicName = entry2.getKey(); - publisherTopic = entry2.getValue(); - } - - - // Publisher and subcriber as per each collector - DMaaPMRSubscriber subcriber = creator.getDMaaPMRSubscriber(topicName); - - DMaaPMRPublisher publisher = creator.getDMaaPMRPublisher(publisherTopic); - debugLogger.info( - "Created scriber topic:" + topicName + "publisher topic:" + publisherTopic); - - executorService.submit(new Runnable() { - - @Override - public void run() { - - Thread.currentThread().setName(threadName); - metricsLogger.info("fetch and publish from and to Dmaap started:" - + Thread.currentThread().getName()); - int pollingInternalInt = dmaapConfig.getPollingInterval(); - debugLogger.info( - "The Polling Interval in Milli Second is :{}" + pollingInternalInt); - debugLogger.info("starting subscriber & publisher thread:{}", - Thread.currentThread().getName()); - while (true) { - synchronized (this) { - for (String incomingJsonString : subcriber.fetchMessages() - .getFetchedMessages()) { - list.add(incomingJsonString); - - } - - if (list.isEmpty()) { - try { - Thread.sleep(pollingInternalInt); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - debugLogger.debug("number of messages to be converted :{}", - list.size()); - - if (!list.isEmpty()) { - String val = ((LinkedList) list).removeFirst(); - List messages = new ArrayList<>(); - String vesEvent = processReceivedJson(val); - if (vesEvent != null - && (!(vesEvent.isEmpty() || vesEvent.equals("")))) { - messages.add(vesEvent); - publisher.publish(messages); - - metricsLogger - .info("Message successfully published to DMaaP Topic-\n" - + vesEvent); - } - } - } - } - } - }); - } - - - - } - - /** - * method stops universal ves adapter module - */ - public void stop() { - isRunning = false; - } - - - /** - * method for processing the incoming json to ves - * - * @param incomingJsonString - * @return ves - */ - private String processReceivedJson(String incomingJsonString) { - String outgoingJsonString = null; - if (!"".equals(incomingJsonString)) { - - try { - - outgoingJsonString = eventAdapter.transform(incomingJsonString); - - } catch (VesException exception) { - errorLogger.error("Received exception : {},{}" + exception.getMessage(), exception); - debugLogger.warn("APPLICATION WILL BE SHUTDOWN UNTIL ABOVE ISSUE IS RESOLVED."); - } catch (DMaapException e) { - errorLogger.error("Received exception : {}", e.getMessage()); - } - } - return outgoingJsonString; - } -} +/*- + * ============LICENSE_START======================================================= + * ONAP : DCAE + * ================================================================================ + * Copyright 2018-2019 TechMahindra + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.universalvesadapter.service; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import org.onap.universalvesadapter.adapter.UniversalEventAdapter; +import org.onap.universalvesadapter.dmaap.Creator; +import org.onap.universalvesadapter.dmaap.MRPublisher.DMaaPMRPublisher; +import org.onap.universalvesadapter.dmaap.MRSubcriber.DMaaPMRSubscriber; +import org.onap.universalvesadapter.exception.DMaapException; +import org.onap.universalvesadapter.exception.MapperConfigException; +import org.onap.universalvesadapter.exception.VesException; +import org.onap.universalvesadapter.utils.CollectorConfigPropertyRetrieval; +import org.onap.universalvesadapter.utils.DmaapConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +/** + * Service that starts the universal ves adapter module to listen for events + * + * @author kmalbari + * + */ +/** + * @author PM00501616 + * + */ + +@Component +public class VesService { + + private static final Logger metricsLogger = LoggerFactory.getLogger("metricsLogger"); + private static final Logger debugLogger = LoggerFactory.getLogger("debugLogger"); + private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger"); + + private boolean isRunning = true; + @Value("${defaultConfigFilelocation}") + private String defaultConfigFilelocation; + @Autowired + private Creator creator; + @Autowired + private UniversalEventAdapter eventAdapter; + @Autowired + private DmaapConfig dmaapConfig; + @Autowired + private CollectorConfigPropertyRetrieval collectorConfigPropertyRetrival; + private static List list = new LinkedList(); + + + /** + * method triggers universal VES adapter module. + */ + public void start() throws MapperConfigException { + debugLogger.info("Creating Subcriber and Publisher with creator............."); + String topicName = null; + String publisherTopic = null; + // Hashmap of subscriber and publisher details in correspondence to the respective + // collectors in kv file + Map dmaapTopics = collectorConfigPropertyRetrival + .getDmaapTopics("stream_subscriber", "stream_publisher", defaultConfigFilelocation); + + ExecutorService executorService = Executors.newFixedThreadPool(dmaapTopics.size()); + for (Map.Entry entry : dmaapTopics.entrySet()) { + String threadName = entry.getKey(); + // subcriber and corresponding publisher topics in a Map + Map subpubTopics = collectorConfigPropertyRetrival + .getTopics(entry.getKey(), entry.getValue(), defaultConfigFilelocation); + for (Map.Entry entry2 : subpubTopics.entrySet()) { + topicName = entry2.getKey(); + publisherTopic = entry2.getValue(); + } + + + // Publisher and subcriber as per each collector + DMaaPMRSubscriber subcriber = creator.getDMaaPMRSubscriber(topicName); + + DMaaPMRPublisher publisher = creator.getDMaaPMRPublisher(publisherTopic); + debugLogger.info( + "Created scriber topic:" + topicName + "publisher topic:" + publisherTopic); + + executorService.submit(new Runnable() { + + @Override + public void run() { + + Thread.currentThread().setName(threadName); + metricsLogger.info("fetch and publish from and to Dmaap started:" + + Thread.currentThread().getName()); + int pollingInternalInt = dmaapConfig.getPollingInterval(); + debugLogger.info( + "The Polling Interval in Milli Second is :{}" + pollingInternalInt); + debugLogger.info("starting subscriber & publisher thread:{}", + Thread.currentThread().getName()); + while (true) { + synchronized (this) { + for (String incomingJsonString : subcriber.fetchMessages() + .getFetchedMessages()) { + list.add(incomingJsonString); + + } + + if (list.isEmpty()) { + try { + Thread.sleep(pollingInternalInt); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + debugLogger.debug("number of messages to be converted :{}", + list.size()); + + if (!list.isEmpty()) { + String val = ((LinkedList) list).removeFirst(); + List messages = new ArrayList<>(); + String vesEvent = processReceivedJson(val); + if (vesEvent != null + && (!(vesEvent.isEmpty() || vesEvent.equals("")))) { + messages.add(vesEvent); + publisher.publish(messages); + + metricsLogger + .info("Message successfully published to DMaaP Topic-\n" + + vesEvent); + } + } + } + } + } + }); + } + + + + } + + /** + * method stops universal ves adapter module + */ + public void stop() { + isRunning = false; + } + + + /** + * method for processing the incoming json to ves + * + * @param incomingJsonString + * @return ves + */ + private String processReceivedJson(String incomingJsonString) { + String outgoingJsonString = null; + if (!"".equals(incomingJsonString)) { + + try { + + outgoingJsonString = eventAdapter.transform(incomingJsonString); + + } catch (VesException exception) { + errorLogger.error("Received exception : {},{}" + exception.getMessage(), exception); + debugLogger.warn("APPLICATION WILL BE SHUTDOWN UNTIL ABOVE ISSUE IS RESOLVED."); + } catch (DMaapException e) { + errorLogger.error("Received exception : {}", e.getMessage()); + } + } + return outgoingJsonString; + } +} diff --git a/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/CollectorConfigPropertyRetrieval.java b/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/CollectorConfigPropertyRetrieval.java new file mode 100644 index 0000000..7c9c30e --- /dev/null +++ b/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/CollectorConfigPropertyRetrieval.java @@ -0,0 +1,161 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : DCAE + * ================================================================================ + * Copyright 2018-2019 TechMahindra + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.universalvesadapter.utils; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.file.Files; +import java.util.HashMap; +import java.util.Map; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.util.ResourceUtils; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +@Component +public class CollectorConfigPropertyRetrieval { + + private static final Logger debugLogger = LoggerFactory.getLogger("debugLogger"); + private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger"); + private static JSONArray array; + @Autowired + private DmaapConfig dmaapConfig; + + public static JSONArray collectorConfigArray(String configFile) { + try { + JSONParser parser = new JSONParser(); + String content = readFile(configFile); + JSONObject obj = (JSONObject) parser.parse(content); + JSONObject appobj = (JSONObject) obj.get("app_preferences"); + array = (JSONArray) appobj.get("collectors"); + + debugLogger.info("Retrieved JsonArray from Collector Config File"); + + } catch (ParseException e) { + errorLogger.error("ParseException occured at position:", e); + } + + return array; + + } + + public static String[] getProperyArray(String propertyName, String defaultConfigFilelocation) { + JSONArray jsonArray = collectorConfigArray(defaultConfigFilelocation); + + String[] propertyArray = new String[jsonArray.size()]; + + for (int k = 0; k < jsonArray.size(); k++) { + + JSONObject collJson = (JSONObject) jsonArray.get(k); + + propertyArray[k] = (String) collJson.get(propertyName); + } + debugLogger.info("returning {} array from Collector Config", propertyName); + return propertyArray; + + } + + public Map getDmaapTopics(String subscriber, String publisher, + String defaultConfigFilelocation) { + JSONArray jsonArray = collectorConfigArray(defaultConfigFilelocation); + + Map dmaapTopics = new HashMap<>(); + + for (int k = 0; k < jsonArray.size(); k++) { + + JSONObject collJson = (JSONObject) jsonArray.get(k); + + dmaapTopics.put(collJson.get(subscriber).toString(), + collJson.get(publisher).toString()); + + } + debugLogger.info("returning Dmaap topics from Collector Config"); + return dmaapTopics; + + } + + public Map getTopics(String subscriber, String publisher, + String defaultConfigFilelocation) { + Map dmaapTopics = new HashMap<>(); + + try { + + ObjectMapper objectMapper = new ObjectMapper(); + String content = readFile(defaultConfigFilelocation); + // read JSON like DOM Parser + JsonNode rootNode = objectMapper.readTree(content); + JsonNode subscriberUrl = rootNode.path("streams_subscribes").path(subscriber) + .path("dmaap_info").path("topic_url"); + JsonNode publisherUrl = rootNode.path("streams_publishes").path(publisher) + .path("dmaap_info").path("topic_url"); + + dmaapTopics.put(getTopicName(subscriberUrl.asText()), + getTopicName(publisherUrl.asText())); + setDmaapConfig(subscriberUrl.asText()); + } catch (IOException ex) { + errorLogger.error("IOException occured:", ex); + } catch (URISyntaxException e) { + errorLogger.error("Invalid URI :", e); + } + + return dmaapTopics; + } + + public String getTopicName(String url) throws URISyntaxException { + URI uri = new URI(url); + String path = uri.getPath(); + String idStr = path.substring(path.lastIndexOf('/') + 1); + return idStr; + } + + public void setDmaapConfig(String url) throws URISyntaxException { + URI uri = new URI(url); + dmaapConfig.setDmaaphost(uri.getHost()); + dmaapConfig.setDEFAULT_PORT_NUMBER(uri.getPort()); + } + + 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); + } + + return content; + } +} diff --git a/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/CollectorConfigPropertyRetrival.java b/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/CollectorConfigPropertyRetrival.java deleted file mode 100644 index 9f96c62..0000000 --- a/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/CollectorConfigPropertyRetrival.java +++ /dev/null @@ -1,171 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : DCAE - * ================================================================================ - * Copyright 2018-2019 TechMahindra - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.universalvesadapter.utils; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; -import java.nio.file.Files; -import java.util.HashMap; -import java.util.Map; - -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; -import org.springframework.util.ResourceUtils; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; - -@Component -public class CollectorConfigPropertyRetrival { - - - private static final Logger debugLogger = LoggerFactory.getLogger("debugLogger"); - private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger"); - private static JSONArray array; - @Autowired - private DmaapConfig dmaapConfig; - - public static JSONArray collectorConfigArray(String configFile) { - try { - JSONParser parser = new JSONParser(); - String content = readFile(configFile); - JSONObject obj = (JSONObject) parser.parse(content); - JSONObject appobj = (JSONObject) obj.get("app_preferences"); - array = (JSONArray) appobj.get("collectors"); - - debugLogger.info("Retrieved JsonArray from Collector Config File"); - - } catch (ParseException e) { - errorLogger.error("ParseException occured at position:", e.getPosition()); - } - - - return array; - - } - - public static String[] getProperyArray(String properyName, String defaultConfigFilelocation) { - JSONArray jsonArray = collectorConfigArray(defaultConfigFilelocation); - - String[] propertyArray = new String[jsonArray.size()]; - - for (int k = 0; k < jsonArray.size(); k++) { - - JSONObject collJson = (JSONObject) jsonArray.get(k); - - propertyArray[k] = (String) collJson.get(properyName); - } - debugLogger.info("returning " + properyName + " array from Collector Config"); - return propertyArray; - - } - - public Map getDmaapTopics(String subscriber, String publisher, - String defaultConfigFilelocation) { - JSONArray jsonArray = collectorConfigArray(defaultConfigFilelocation); - - Map dmaapTopics = new HashMap<>(); - - for (int k = 0; k < jsonArray.size(); k++) { - - JSONObject collJson = (JSONObject) jsonArray.get(k); - - dmaapTopics.put(collJson.get(subscriber).toString(), - collJson.get(publisher).toString()); - - } - debugLogger.info("returning Dmaap topics from Collector Config"); - return dmaapTopics; - - } - - public Map getTopics(String subscriber, String publisher, - String defaultConfigFilelocation) { - Map dmaapTopics = new HashMap<>(); - - try { - - ObjectMapper objectMapper = new ObjectMapper(); - String content = readFile(defaultConfigFilelocation); - // read JSON like DOM Parser - JsonNode rootNode = objectMapper.readTree(content); - JsonNode subscriberUrl = rootNode.path("streams_subscribes").path(subscriber) - .path("dmaap_info").path("topic_url"); - JsonNode publisherUrl = rootNode.path("streams_publishes").path(publisher) - .path("dmaap_info").path("topic_url"); - - dmaapTopics.put(getTopicName(subscriberUrl.asText()), - getTopicName(publisherUrl.asText())); - setDmaapConfig(subscriberUrl.asText()); - } catch (IOException ex) { - errorLogger.error("IOException occured:" + ex.getMessage()); - - } catch (URISyntaxException e) { - - errorLogger.error("Invalid URI :" + e.getInput() + ": " + e.getReason()); - } - - return dmaapTopics; - - } - - public String getTopicName(String url) throws URISyntaxException { - URI uri = new URI(url); - String path = uri.getPath(); - String idStr = path.substring(path.lastIndexOf('/') + 1); - return idStr; - - } - - public void setDmaapConfig(String url) throws URISyntaxException { - URI uri = new URI(url); - dmaapConfig.setDmaaphost(uri.getHost()); - dmaapConfig.setDEFAULT_PORT_NUMBER(uri.getPort()); - - } - - 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()); - } - - return content; - - } -} 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 9493fc6..0c6155c 100644 --- a/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/FetchDynamicConfig.java +++ b/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/FetchDynamicConfig.java @@ -1,232 +1,225 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : DCAE - * ================================================================================ - * Copyright 2019 TechMahindra - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.universalvesadapter.utils; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.PrintWriter; -import java.nio.file.Files; - -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; -import org.springframework.util.ResourceUtils; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; - -@Component -public class FetchDynamicConfig { - - // @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(); - } - -} +/*- + * ============LICENSE_START======================================================= + * ONAP : DCAE + * ================================================================================ + * Copyright 2019 TechMahindra + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.universalvesadapter.utils; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.nio.file.Files; +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; +import org.springframework.util.ResourceUtils; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +@Component +public class FetchDynamicConfig { + + // @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 - {} NOT refreshed", configFile); + } + } + + 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); + + } + + 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 {} with configuartions received from CBS", + configFile); + + File file2 = ResourceUtils.getFile("classpath:" + configFile); + try (FileWriter fstream = new FileWriter(file2, false); + PrintWriter printWriter = new PrintWriter(fstream)) { + printWriter.print(indentedretstring); + } + + 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); + } + } + + 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); + } catch (Exception e) { + errorLogger.error("Exception occured , reason:", e); + } + + 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); + } catch (UnsupportedOperationException e) { + debugLogger.debug("FetchDynamicConfig : fetchResultFromDestination : UnsupportedOperationException thrown.", + e); + } catch (IOException e) { + debugLogger.debug("FetchDynamicConfig : fetchResultFromDestination : IOException thrown.", e); + } + + debugLogger.debug("FetchDynamicConfig : fetchResultFromDestination : END"); + return sb.toString(); + } + +} -- cgit 1.2.3-korg