diff options
author | 2019-03-07 10:53:26 +0530 | |
---|---|---|
committer | 2019-03-07 05:29:50 +0000 | |
commit | 77a725f2d27977f15ce585b2cd73fc357799dfda (patch) | |
tree | 3e141a7dd755e9d25f543810e369887e5a7578f8 /UniversalVesAdapter/src/main/java/org/onap/universalvesadapter | |
parent | 6489bbca90237d869669dbf2e2e677f2e360b72b (diff) |
Upgraded to Support VES 7.0
Upgraded Universal VES Adapter to support VES Version 7.0
Issue-ID: DCAEGEN2-1005
Change-Id: Ib52f71957be8bb16ea84b19c9264d6fa042f2db0
Signed-off-by: Pooja03 <pm00501616@techmahindra.com>
Diffstat (limited to 'UniversalVesAdapter/src/main/java/org/onap/universalvesadapter')
6 files changed, 99 insertions, 67 deletions
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 c161977..f17f3a6 100644 --- a/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/adapter/UniversalEventAdapter.java +++ b/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/adapter/UniversalEventAdapter.java @@ -22,13 +22,14 @@ 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.dcaegen2.ves.domain.VesEvent; +import org.onap.dcaegen2.ves.domain.ves7_0.VesEvent; import org.onap.universalvesadapter.exception.ConfigFileSmooksConversionException; import org.onap.universalvesadapter.exception.VesException; import org.onap.universalvesadapter.service.VESAdapterInitializer; @@ -36,6 +37,7 @@ 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; @@ -58,7 +60,9 @@ import com.google.gson.JsonSyntaxException; 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<String, Smooks> eventToSmooksMapping = new ConcurrentHashMap<>(); @@ -79,7 +83,7 @@ public class UniversalEventAdapter implements GenericAdapter { String result = ""; String configFileData; - String identifier[]= CollectorConfigPropertyRetrival.getProperyArray("identifier"); + String identifier[]= CollectorConfigPropertyRetrival.getProperyArray("identifier",defaultConfigFilelocation ); String defaultMappingFile="defaultMappingFile-"+Thread.currentThread().getName(); try { @@ -89,10 +93,11 @@ public class UniversalEventAdapter implements GenericAdapter { JsonElement results; for(int i=0;i<identifier.length;i++) { - if(body.has(identifier[i])) + JsonObject obj; + if((obj=keyObject(body,identifier[i])).has(identifier[i])) { collectorIdentifierKey=identifier[i]; - results=body.get(identifier[i]); + results=obj.get(identifier[i]); collectorIdentifierValue=results.getAsString(); } @@ -156,5 +161,25 @@ public class UniversalEventAdapter implements GenericAdapter { 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 220c5e0..671fcc3 100644 --- a/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/service/VESAdapterInitializer.java +++ b/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/service/VESAdapterInitializer.java @@ -52,19 +52,28 @@ public class VESAdapterInitializer implements CommandLineRunner, Ordered { private Creator creator; @Autowired private DmaapConfig dmaapConfig; + @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; - public static String configFile = "/opt/app/VESAdapter/conf/kv.json"; - //public static String configFile = "src\\main\\resources\\kv.json"; @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()); @@ -73,7 +82,7 @@ public class VESAdapterInitializer implements CommandLineRunner, Ordered { //checks for DMaaP Host and Port No if( (env.get("DMAAPHOST")==null ||(env.get("MR_DEFAULT_PORT_NUMBER")==null))) { - errorLogger.error("Some docker environment parameter is missing. Sample Usage is -\n sudo docker run -d -p 8085:8085/tcp --env MR_DEFAULT_PORT_NUMBER=3904 --env CONSUL_HOST=10.53.172.109 --env HOSTNAME=mvp-dcaegen2-service-mua --env CONFIG_BINDING_SERVICE=config_binding_service --env DMAAPHOST='10.53.172.156' onap/org.onap.dcaegen2.services.mapper.vesadapter.universalvesadaptor:latest"); + errorLogger.error("DMAAPHOST,MR_DEFAULT_PORT_NUMBER environment parameter is missing. Sample Usage is -\n sudo docker run -d -p 8085:8085/tcp --env MR_DEFAULT_PORT_NUMBER=3904 --env CONSUL_HOST=10.53.172.109 --env HOSTNAME=mvp-dcaegen2-service-mua --env CONFIG_BINDING_SERVICE=config_binding_service --env DMAAPHOST='10.53.172.156' nexus3.onap.org:10001/onap/org.onap.dcaegen2.services.mapper.vesadapter.universalvesadaptor:latest"); System.exit(SpringApplication.exit(applicationContext, () -> {errorLogger.error("Application stoped due missing DMAAPHOST or MR_DEFAULT_PORT_NUMBER environment varibales.Please refer above example for environment varibales to pass ");return-1;})); } @@ -84,13 +93,13 @@ public class VESAdapterInitializer implements CommandLineRunner, Ordered { //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(); + FetchDynamicConfig.cbsCall(defaultConfigFilelocation); } else { debugLogger.info(">>>Static configuration to be used"); } - readJsonToMap(configFile); + readJsonToMap(defaultConfigFilelocation); //prepareDatabase(); //fetchMappingFile(); @@ -118,7 +127,6 @@ public class VESAdapterInitializer implements CommandLineRunner, Ordered { builder.append(line); } result = builder.toString(); - debugLogger.debug(result); } } catch (IOException e) { errorLogger.error("error", e); 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 79317ea..a5b88ec 100644 --- a/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/service/VesService.java +++ b/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/service/VesService.java @@ -40,6 +40,7 @@ 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; /** @@ -56,6 +57,8 @@ public class VesService { private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger"); private boolean isRunning = true; + @Value("${defaultConfigFilelocation}") + private String defaultConfigFilelocation; @Autowired private Creator creator; @Autowired @@ -74,7 +77,7 @@ public class VesService { DMaaPMRPublisher publisher = creator.getDMaaPMRPublisher(); - String topicArray[]= CollectorConfigPropertyRetrival.getProperyArray("subscriberTopic"); + String topicArray[]= CollectorConfigPropertyRetrival.getProperyArray("subscriberTopic",defaultConfigFilelocation); ExecutorService executorService=Executors.newFixedThreadPool(topicArray.length); diff --git a/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/CollectorConfigPropertyRetrival.java b/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/CollectorConfigPropertyRetrival.java index 9de7e63..2262b9b 100644 --- a/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/CollectorConfigPropertyRetrival.java +++ b/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/CollectorConfigPropertyRetrival.java @@ -19,6 +19,7 @@ */ package org.onap.universalvesadapter.utils; +import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; @@ -29,56 +30,59 @@ 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.Value; +import org.springframework.stereotype.Component; - +@Component public class CollectorConfigPropertyRetrival { + @Value("${defaultConfigFilelocation}") + public String defaultConfigFilelocation; + private static final Logger debugLogger = LoggerFactory.getLogger("debugLogger"); + private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger"); + private static JSONArray array; - public static String configFile = "/opt/app/VESAdapter/conf/kv.json"; - //public static String configFile = "src\\main\\resources\\kv.json"; - private static final Logger debugLogger = LoggerFactory.getLogger("debugLogger"); - private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger"); - private static JSONArray array; - - public static JSONArray collectorConfigArray(String configFile){ + public static JSONArray collectorConfigArray(String configFile) { try { - JSONParser parser = new JSONParser(); - FileReader fileReader = new FileReader(configFile); - JSONObject obj = (JSONObject) parser.parse(fileReader); - JSONObject appobj = (JSONObject) obj.get("app_preferences"); - array =(JSONArray) appobj.get("collectors"); - - debugLogger.info("Retrieved JsonArray from Collector Config File"); + JSONParser parser = new JSONParser(); + + File file = new File(ClassLoader.getSystemResource(configFile.trim()).getFile()); + + FileReader fileReader = new FileReader(file); + JSONObject obj = (JSONObject) parser.parse(fileReader); + 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()); + errorLogger.error("ParseException occured at position:", e.getPosition()); } catch (FileNotFoundException e) { - - errorLogger.error("Collector Config File is not found..",e.getMessage()); + + errorLogger.error("Collector Config File is not found..", e.getMessage()); } catch (IOException e) { - - errorLogger.error("Error occured due to :",e.getMessage()); + + errorLogger.error("Error occured due to :", e.getMessage()); } - - + return array; - + } - - - public static String [] getProperyArray(String properyName) { - JSONArray jsonArray =collectorConfigArray(configFile); - - 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"); + + 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; - + } } 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 f515858..af219ce 100644 --- a/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/FetchDynamicConfig.java +++ b/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/FetchDynamicConfig.java @@ -32,17 +32,16 @@ import org.json.JSONObject; import org.json.JSONTokener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; import com.fasterxml.jackson.databind.JsonNode; 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"); - // need to change below path - public static String configFile = "/opt/app/VESAdapter/conf/kv.json"; - //public static String configFile = "src\\main\\resources\\kv.json"; private static String url; public static String retString; public static String retCBSString; @@ -51,7 +50,7 @@ public class FetchDynamicConfig { public FetchDynamicConfig() { } - public static void cbsCall() { + public static void cbsCall(String configFile) { env = System.getenv(); Boolean areEqual; @@ -60,11 +59,11 @@ public class FetchDynamicConfig { // Construct and invoke CBS API to get application Configuration getCBS(); // Verify if data has changed - areEqual = verifyConfigChange(); + areEqual = verifyConfigChange(configFile); if (!areEqual) { FetchDynamicConfig fc = new FetchDynamicConfig(); - fc.writefile(retCBSString); + fc.writefile(retCBSString,configFile); } else { debugLogger.info("New config pull results identical - " + configFile + " NOT refreshed"); } @@ -73,16 +72,11 @@ public class FetchDynamicConfig { private static void getconsul() { url = env.get("CONSUL_HOST") + ":8500/v1/catalog/service/" + env.get("CONFIG_BINDING_SERVICE"); - - // for testing :url="10.12.6.50:8500/v1/catalog/service/config_binding_service"; - //http://10.12.6.44:8500/v1/catlog/service/config_binding_service - retString = executecurl(url); debugLogger.info("CBS details fetched from Consul"); - } - public static boolean verifyConfigChange() { + public static boolean verifyConfigChange(String configFile) { boolean areEqual = false; // Read current data @@ -138,12 +132,12 @@ public class FetchDynamicConfig { } - public void writefile(String retCBSString) { + public void writefile(String retCBSString, String configFile) { debugLogger.info("URL to fetch configuration:" + url); String indentedretstring = (new JSONObject(retCBSString)).toString(4); - try (FileWriter file = new FileWriter(FetchDynamicConfig.configFile)) { + try (FileWriter file = new FileWriter(configFile)) { file.write(indentedretstring); debugLogger.info("Successfully Copied JSON Object to file " + configFile); @@ -189,8 +183,6 @@ public class FetchDynamicConfig { builder.append(line); } result = builder.toString(); - debugLogger.info(result); - reader.close(); ipr.close(); } catch (IOException e) { diff --git a/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/SmooksUtils.java b/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/SmooksUtils.java index e77c965..9b341d5 100644 --- a/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/SmooksUtils.java +++ b/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/SmooksUtils.java @@ -27,7 +27,7 @@ import javax.xml.transform.stream.StreamSource; import org.milyn.Smooks; import org.milyn.container.ExecutionContext; import org.milyn.payload.StringResult; -import org.onap.dcaegen2.ves.domain.VesEvent; +import org.onap.dcaegen2.ves.domain.ves7_0.VesEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; |