diff options
Diffstat (limited to 'UniversalVesAdapter')
-rw-r--r-- | UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/adapter/UniversalEventAdapter.java | 396 | ||||
-rw-r--r-- | UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/service/VESAdapterInitializer.java | 393 | ||||
-rw-r--r-- | UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/service/VesService.java | 388 | ||||
-rw-r--r-- | UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/CollectorConfigPropertyRetrieval.java (renamed from UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/CollectorConfigPropertyRetrival.java) | 332 | ||||
-rw-r--r-- | UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/FetchDynamicConfig.java | 457 | ||||
-rw-r--r-- | UniversalVesAdapter/src/test/java/org/onap/universalvesadapter/adapter/UniversalEventAdapterTest.java | 313 |
6 files changed, 1127 insertions, 1152 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 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<String, Smooks> 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<String, Smooks> 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<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();
- }
-
-}
+/*- + * ============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<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 = 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<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/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<String> list = new LinkedList<String>();
-
-
- /**
- * 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<String, String> dmaapTopics = collectorConfigPropertyRetrival
- .getDmaapTopics("stream_subscriber", "stream_publisher", defaultConfigFilelocation);
-
- ExecutorService executorService = Executors.newFixedThreadPool(dmaapTopics.size());
- for (Map.Entry<String, String> entry : dmaapTopics.entrySet()) {
- String threadName = entry.getKey();
- // subcriber and corresponding publisher topics in a Map
- Map<String, String> subpubTopics = collectorConfigPropertyRetrival
- .getTopics(entry.getKey(), entry.getValue(), defaultConfigFilelocation);
- for (Map.Entry<String, String> 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<String>) list).removeFirst();
- List<String> 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<String> list = new LinkedList<String>(); + + + /** + * 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<String, String> dmaapTopics = collectorConfigPropertyRetrival + .getDmaapTopics("stream_subscriber", "stream_publisher", defaultConfigFilelocation); + + ExecutorService executorService = Executors.newFixedThreadPool(dmaapTopics.size()); + for (Map.Entry<String, String> entry : dmaapTopics.entrySet()) { + String threadName = entry.getKey(); + // subcriber and corresponding publisher topics in a Map + Map<String, String> subpubTopics = collectorConfigPropertyRetrival + .getTopics(entry.getKey(), entry.getValue(), defaultConfigFilelocation); + for (Map.Entry<String, String> 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<String>) list).removeFirst(); + List<String> 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/CollectorConfigPropertyRetrival.java b/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/CollectorConfigPropertyRetrieval.java index 9f96c62..7c9c30e 100644 --- a/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/CollectorConfigPropertyRetrival.java +++ b/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/utils/CollectorConfigPropertyRetrieval.java @@ -1,171 +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 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<String, String> getDmaapTopics(String subscriber, String publisher,
- String defaultConfigFilelocation) {
- JSONArray jsonArray = collectorConfigArray(defaultConfigFilelocation);
-
- Map<String, String> 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<String, String> getTopics(String subscriber, String publisher,
- String defaultConfigFilelocation) {
- Map<String, String> 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;
-
- }
-}
+/*- + * ============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<String, String> getDmaapTopics(String subscriber, String publisher, + String defaultConfigFilelocation) { + JSONArray jsonArray = collectorConfigArray(defaultConfigFilelocation); + + Map<String, String> 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<String, String> getTopics(String subscriber, String publisher, + String defaultConfigFilelocation) { + Map<String, String> 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/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(); + } + +} diff --git a/UniversalVesAdapter/src/test/java/org/onap/universalvesadapter/adapter/UniversalEventAdapterTest.java b/UniversalVesAdapter/src/test/java/org/onap/universalvesadapter/adapter/UniversalEventAdapterTest.java index 233a27a..459047a 100644 --- a/UniversalVesAdapter/src/test/java/org/onap/universalvesadapter/adapter/UniversalEventAdapterTest.java +++ b/UniversalVesAdapter/src/test/java/org/onap/universalvesadapter/adapter/UniversalEventAdapterTest.java @@ -1,158 +1,155 @@ -/*-
- * ============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 static org.junit.Assert.assertNotEquals;
-import static org.junit.Assert.assertNotNull;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.onap.universalvesadapter.Application;
-import org.onap.universalvesadapter.exception.VesException;
-import org.onap.universalvesadapter.service.VESAdapterInitializer;
-import org.onap.universalvesadapter.utils.CollectorConfigPropertyRetrival;
-import org.powermock.api.mockito.PowerMockito;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.boot.test.context.SpringBootTest;
-
-@RunWith(PowerMockRunner.class)
-@SpringBootTest(classes = Application.class)
-public class UniversalEventAdapterTest {
-
- private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger");
-
- @Mock
- private VESAdapterInitializer vesAdapterInitializer;
-
- @InjectMocks
- private UniversalEventAdapter universalVesAdapter;
-
- @Before
- public void init() {
- MockitoAnnotations.initMocks(this);
- }
-
- @PrepareForTest({CollectorConfigPropertyRetrival.class})
- @Test
- public void testtransform() {
-
- StringBuffer incomingJsonString = new StringBuffer("{\n"
- + " \"rule-id\": \"12121\",\n" + " \"notification\": {\n"
- + " \"event-time\": \"2018-03-15T08:15:32.000Z\",\n"
- + " \"notification-id\": \"2541256\",\n"
- + " \"message\": {\n"
- + " \"topic\":\"resource\",\n"
- + " \"object-type\":\"onu\", \n"
- + " \"version\": \"v1\", \n"
- + " \"operation\": \"create\", \n"
- + " \"target\": \"ont/ont=23hk1231231232\", \n"
- + " \"content\": {\n" + " \"onu\": {\n"
- + " \"sn\": \"48575443FA637D1A\",\n"
- + " \"refParentNE\": \"550e8400-e29b-41d4-a716-446655440000\",\n"
- + " \"refParentNeNativeId\": \"FDN\",\n"
- + " \"refParentLTP\": \"8c0db89e-218c-4e18-b0de-7a6788b3dda4\",\n"
- + " \"refParentLTPNativeId\": \"FDN\",\n"
- + " \"onuId\": \"213\",\n"
- + " \"accessID\": \"HG65875420001\"\n"
- + " }\n" + " }\n" + " }\n"
- + " }\n" + "}");
- Map<String, String> testMap = new HashMap<String, String>();
- testMap.put("defaultMappingFile-" + Thread.currentThread().getName(),
- "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
- + "<smooks-resource-list xmlns=\"http://www.milyn.org/xsd/smooks-1.1.xsd\" xmlns:jb=\"http://www.milyn.org/xsd/smooks/javabean-1.4.xsd\" xmlns:json=\"http://www.milyn.org/xsd/smooks/json-1.1.xsd\">\n"
- + " <json:reader rootName=\"vesevent\" keyWhitspaceReplacement=\"-\">\n"
- + " <json:keyMap>\n"
- + " <json:key from=\"date&time\" to=\"date-and-time\" />\n"
- + " </json:keyMap>\n" + " </json:reader>\n"
- + " <jb:bean class=\"org.onap.dcaegen2.ves.domain.ves70.VesEvent\""
- + " beanId=\"vesEvent\" createOnElement=\"vesevent\">\n"
- + " <jb:wiring property=\"event\" beanIdRef=\"event\" />\n"
- + " </jb:bean>\n"
- + " <jb:bean class=\"org.onap.dcaegen2.ves.domain.ves70.Event\" "
- + "beanId=\"event\" createOnElement=\"vesevent\">\n"
- + " <jb:wiring property=\"commonEventHeader\" beanIdRef=\"commonEventHeader\" />\n"
- + " <jb:wiring property=\"pnfRegistrationFields\" beanIdRef=\"pnfRegistrationFields\" />\n"
- + " </jb:bean>\n"
- + " <jb:bean class=\"org.onap.dcaegen2.ves.domain.ves70.CommonEventHeader\""
- + " beanId=\"commonEventHeader\" createOnElement=\"vesevent\">\n"
- + "<jb:expression property=\"version\">"
- + "org.onap.dcaegen2.ves.domain.ves70.CommonEventHeader.Version._4_0_1</jb:expression> \n"
- + "<jb:expression property=\"eventType\">\"pnfRegistration\"</jb:expression>\n"
- + "<jb:expression property=\"vesEventListenerVersion\">"
- + "org.onap.dcaegen2.ves.domain.ves70.CommonEventHeader.VesEventListenerVersion._7_0_1"
- + "</jb:expression>\n"
- + " <jb:expression property=\"eventId\" execOnElement=\"vesevent\">"
- + "\"registration_\"+commonEventHeader.ts1</jb:expression>\n"
- + "<jb:expression property=\"reportingEntityName\">\"VESMapper\"</jb:expression>\n"
- + " <jb:expression property=\"domain\">"
- + "org.onap.dcaegen2.ves.domain.ves70.CommonEventHeader.Domain.PNF_REGISTRATION"
- + "</jb:expression>\n"
- + " <jb:expression property=\"eventName\" execOnElement=\"vesevent\">"
- + "commonEventHeader.domain</jb:expression>\n"
- + "<jb:value property=\"sequence\" data=\"0\" default=\"0\" decoder=\"Long\" />\n"
- + "<jb:expression property=\"lastEpochMicrosec\" execOnElement=\"vesevent\">"
- + "commonEventHeader.ts1</jb:expression>\n"
- + " <jb:expression property=\"startEpochMicrosec\" execOnElement=\"vesevent\">"
- + "commonEventHeader.ts1</jb:expression>\n"
- + " <jb:expression property=\"priority\">"
- + "org.onap.dcaegen2.ves.domain.ves70.CommonEventHeader.Priority.NORMAL</jb:expression>\n"
- + " <jb:value property=\"sourceName\" data=\"notification/message/target\" />\n"
- + " </jb:bean>\n"
- + " <jb:bean class=\"org.onap.dcaegen2.ves.domain.ves70.PnfRegistrationFields\""
- + " beanId=\"pnfRegistrationFields\" createOnElement=\"vesevent\">\n"
- + " \n"
- + " <jb:expression property=\"pnfRegistrationFieldsVersion\">"
- + "org.onap.dcaegen2.ves.domain.ves70.PnfRegistrationFields.PnfRegistrationFieldsVersion._2_0"
- + "</jb:expression>\n"
- + " <jb:value property=\"serialNumber\" data=\"notification/message/content/onu/sn\" />\n"
- + " \n" + " </jb:bean>\n"
- + "</smooks-resource-list>");
-
- try {
- vesAdapterInitializer.setMappingFiles(testMap);
-
- String[] identifiertest = CollectorConfigPropertyRetrival
- .getProperyArray("identifier", "kvTest.json");
- PowerMockito.mockStatic(CollectorConfigPropertyRetrival.class);
- PowerMockito.when(CollectorConfigPropertyRetrival
- .getProperyArray("identifier", null))
- .thenReturn(identifiertest);
-
- String actualResult = universalVesAdapter
- .transform(incomingJsonString.toString());
- assertNotNull(actualResult);
- assertNotEquals("", actualResult);
- } catch (VesException exception) {
- errorLogger.error("Error occurred : {}", exception);
- }
- }
-
-}
+/*- + * ============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 static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import java.util.HashMap; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.universalvesadapter.Application; +import org.onap.universalvesadapter.exception.VesException; +import org.onap.universalvesadapter.service.VESAdapterInitializer; +import org.onap.universalvesadapter.utils.CollectorConfigPropertyRetrieval; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.test.context.SpringBootTest; + +@RunWith(PowerMockRunner.class) +@SpringBootTest(classes = Application.class) +public class UniversalEventAdapterTest { + + private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger"); + + @Mock + private VESAdapterInitializer vesAdapterInitializer; + + @InjectMocks + private UniversalEventAdapter universalVesAdapter; + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + } + + @PrepareForTest({ CollectorConfigPropertyRetrieval.class }) + @Test + public void testtransform() { + + StringBuffer incomingJsonString = new StringBuffer("{\n" + + " \"rule-id\": \"12121\",\n" + " \"notification\": {\n" + + " \"event-time\": \"2018-03-15T08:15:32.000Z\",\n" + + " \"notification-id\": \"2541256\",\n" + + " \"message\": {\n" + + " \"topic\":\"resource\",\n" + + " \"object-type\":\"onu\", \n" + + " \"version\": \"v1\", \n" + + " \"operation\": \"create\", \n" + + " \"target\": \"ont/ont=23hk1231231232\", \n" + + " \"content\": {\n" + " \"onu\": {\n" + + " \"sn\": \"48575443FA637D1A\",\n" + + " \"refParentNE\": \"550e8400-e29b-41d4-a716-446655440000\",\n" + + " \"refParentNeNativeId\": \"FDN\",\n" + + " \"refParentLTP\": \"8c0db89e-218c-4e18-b0de-7a6788b3dda4\",\n" + + " \"refParentLTPNativeId\": \"FDN\",\n" + + " \"onuId\": \"213\",\n" + + " \"accessID\": \"HG65875420001\"\n" + + " }\n" + " }\n" + " }\n" + + " }\n" + "}"); + Map<String, String> testMap = new HashMap<String, String>(); + testMap.put("defaultMappingFile-" + Thread.currentThread().getName(), + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + + "<smooks-resource-list xmlns=\"http://www.milyn.org/xsd/smooks-1.1.xsd\" xmlns:jb=\"http://www.milyn.org/xsd/smooks/javabean-1.4.xsd\" xmlns:json=\"http://www.milyn.org/xsd/smooks/json-1.1.xsd\">\n" + + " <json:reader rootName=\"vesevent\" keyWhitspaceReplacement=\"-\">\n" + + " <json:keyMap>\n" + + " <json:key from=\"date&time\" to=\"date-and-time\" />\n" + + " </json:keyMap>\n" + " </json:reader>\n" + + " <jb:bean class=\"org.onap.dcaegen2.ves.domain.ves70.VesEvent\"" + + " beanId=\"vesEvent\" createOnElement=\"vesevent\">\n" + + " <jb:wiring property=\"event\" beanIdRef=\"event\" />\n" + + " </jb:bean>\n" + + " <jb:bean class=\"org.onap.dcaegen2.ves.domain.ves70.Event\" " + + "beanId=\"event\" createOnElement=\"vesevent\">\n" + + " <jb:wiring property=\"commonEventHeader\" beanIdRef=\"commonEventHeader\" />\n" + + " <jb:wiring property=\"pnfRegistrationFields\" beanIdRef=\"pnfRegistrationFields\" />\n" + + " </jb:bean>\n" + + " <jb:bean class=\"org.onap.dcaegen2.ves.domain.ves70.CommonEventHeader\"" + + " beanId=\"commonEventHeader\" createOnElement=\"vesevent\">\n" + + "<jb:expression property=\"version\">" + + "org.onap.dcaegen2.ves.domain.ves70.CommonEventHeader.Version._4_0_1</jb:expression> \n" + + "<jb:expression property=\"eventType\">\"pnfRegistration\"</jb:expression>\n" + + "<jb:expression property=\"vesEventListenerVersion\">" + + "org.onap.dcaegen2.ves.domain.ves70.CommonEventHeader.VesEventListenerVersion._7_0_1" + + "</jb:expression>\n" + + " <jb:expression property=\"eventId\" execOnElement=\"vesevent\">" + + "\"registration_\"+commonEventHeader.ts1</jb:expression>\n" + + "<jb:expression property=\"reportingEntityName\">\"VESMapper\"</jb:expression>\n" + + " <jb:expression property=\"domain\">" + + "org.onap.dcaegen2.ves.domain.ves70.CommonEventHeader.Domain.PNF_REGISTRATION" + + "</jb:expression>\n" + + " <jb:expression property=\"eventName\" execOnElement=\"vesevent\">" + + "commonEventHeader.domain</jb:expression>\n" + + "<jb:value property=\"sequence\" data=\"0\" default=\"0\" decoder=\"Long\" />\n" + + "<jb:expression property=\"lastEpochMicrosec\" execOnElement=\"vesevent\">" + + "commonEventHeader.ts1</jb:expression>\n" + + " <jb:expression property=\"startEpochMicrosec\" execOnElement=\"vesevent\">" + + "commonEventHeader.ts1</jb:expression>\n" + + " <jb:expression property=\"priority\">" + + "org.onap.dcaegen2.ves.domain.ves70.CommonEventHeader.Priority.NORMAL</jb:expression>\n" + + " <jb:value property=\"sourceName\" data=\"notification/message/target\" />\n" + + " </jb:bean>\n" + + " <jb:bean class=\"org.onap.dcaegen2.ves.domain.ves70.PnfRegistrationFields\"" + + " beanId=\"pnfRegistrationFields\" createOnElement=\"vesevent\">\n" + + " \n" + + " <jb:expression property=\"pnfRegistrationFieldsVersion\">" + + "org.onap.dcaegen2.ves.domain.ves70.PnfRegistrationFields.PnfRegistrationFieldsVersion._2_0" + + "</jb:expression>\n" + + " <jb:value property=\"serialNumber\" data=\"notification/message/content/onu/sn\" />\n" + + " \n" + " </jb:bean>\n" + + "</smooks-resource-list>"); + + try { + vesAdapterInitializer.setMappingFiles(testMap); + + String[] identifiertest = CollectorConfigPropertyRetrieval + .getProperyArray("identifier", "kvTest.json"); + PowerMockito.mockStatic(CollectorConfigPropertyRetrieval.class); + PowerMockito.when(CollectorConfigPropertyRetrieval + .getProperyArray("identifier", null)) + .thenReturn(identifiertest); + + String actualResult = universalVesAdapter + .transform(incomingJsonString.toString()); + assertNotNull(actualResult); + assertNotEquals("", actualResult); + } catch (VesException exception) { + errorLogger.error("Error occurred : {}", exception); + } + } +} |