Fix bug throwing exception when first event is collected 42/112642/5
authorMichal Banka <michal.banka@nokia.com>
Mon, 14 Sep 2020 21:04:37 +0000 (23:04 +0200)
committerMichal Banka <michal.banka@nokia.com>
Tue, 15 Sep 2020 09:26:56 +0000 (11:26 +0200)
- Problem: When running app from jar (e.g. in docker env) ClassLoader
badly interprete classpath as root of app jar, while resources are
located in jar under BOOT-INF/classes/ in Spring Boot apps.

- Solution: Moved file from resources to etc directory so ClassLoader
isn't needed. Filepath of api_version_description.json which
previously was badly resolved now is configured in collector.properties.

Change-Id: I690394cc59e16c95f5902045efc3fdaf13bf9112
Signed-off-by: Michal Banka <michal.banka@nokia.com>
Issue-ID: DCAEGEN2-2426

Changelog.md
dpo/data-formats/ConsulConfig.json
dpo/spec/vescollector-componentspec.json
etc/api_version_description.json [new file with mode: 0644]
etc/collector.properties
src/main/java/org/onap/dcae/ApplicationSettings.java
src/main/java/org/onap/dcae/common/HeaderUtils.java
src/main/java/org/onap/dcae/restapi/VesRestController.java
src/main/resources/api_version_config.json [deleted file]
src/test/java/org/onap/dcae/common/JsonDataLoader.java
src/test/java/org/onap/dcae/restapi/VesRestControllerTest.java

index 49697aa..35f5006 100644 (file)
@@ -30,3 +30,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
         - [DCAEGEN2-2264](https://jira.onap.org/browse/DCAEGEN2-2264) - Post stndDefined implementation fixes  
 ## [1.7.5] - 09/09/2020
         - [DCAEGEN2-2264](https://jira.onap.org/browse/DCAEGEN2-2264) - Update schema-map.json
+        - [DCAEGEN2-2426](https://jira.onap.org/browse/DCAEGEN2-2426) - Fix bug throwing exception when first event is collected
\ No newline at end of file
index f227619..168eda0 100644 (file)
@@ -76,6 +76,7 @@
   "collector.service.secure.port": "8443",
   "auth.method": "noAuth",
   "collector.keystore.file.location": "/opt/app/VESCollector/etc/keystore",
+  "collector.description.api.version.location": "etc/api_version_description.json",
   "services_calls": [],
   "header.authlist": "sample1,$2a$10$0buh.2WeYwN868YMwnNNEuNEAMNYVU9.FSMJGyIKV3dGET/7oGOi6"
 }
index 5c3e4d9..0d53dc8 100644 (file)
     },
     {
       "name": "collector.dmaap.streamid",
-      "value": "fault=ves-fault|syslog=ves-syslog|heartbeat=ves-heartbeat|measurementsForVfScaling=ves-measurement|measurement=ves-measurement|mobileFlow=ves-mobileflow|other=ves-other|stateChange=ves-statechange|thresholdCrossingAlert=ves-thresholdCrossingAlert|voiceQuality=ves-voicequality|sipSignaling=ves-sipsignaling|notification=ves-notification|pnfRegistration=ves-pnfRegistration|perf3gpp=ves-perf3gpp|stndDefined=ves-other",
+      "value": "fault=ves-fault|syslog=ves-syslog|heartbeat=ves-heartbeat|measurementsForVfScaling=ves-measurement|mobileFlow=ves-mobileflow|other=ves-other|stateChange=ves-statechange|thresholdCrossingAlert=ves-thresholdCrossingAlert|voiceQuality=ves-voicequality|sipSignaling=ves-sipsignaling|notification=ves-notification|pnfRegistration=ves-pnfRegistration|3GPP-FaultSupervision=ves-3gpp-fault-supervision|3GPP-Heartbeat=ves-3gpp-heartbeat|3GPP-Provisioning=ves-3gpp-provisioning|3GPP-PerformanceAssurance=ves-3gpp-performance-assurance",
       "description": "domain-to-streamid mapping used by VESCollector to distributes events based on domain. Both primary and secondary config_key are included for resilency (multiple streamid can be included commma separated). The streamids MUST match to topic config_keys. For single site without resiliency deployment - configkeys with -secondary suffix can be removed",
       "sourced_at_deployment": true,
       "policy_editable": false,
       "sourced_at_deployment": false,
       "policy_editable": false,
       "designer_editable": false
+    },
+    {
+      "name": "collector.description.api.version.location",
+      "value": "etc/api_version_description.json",
+      "description": "Path to the file containing description of api versions",
+      "sourced_at_deployment": false,
+      "policy_editable": false,
+      "designer_editable": false
     }
   ],
   "auxilary": {
diff --git a/etc/api_version_description.json b/etc/api_version_description.json
new file mode 100644 (file)
index 0000000..f2c7f1c
--- /dev/null
@@ -0,0 +1,7 @@
+{
+  "apiVersion": 
+  {
+    "eventListener": ["4.7.2","5.4.1","7.2"],
+    "eventListener_eventBatch": ["4.7.2","5.4.1","7.2"]
+  }
+}
index 7482c5a..10ae9c2 100755 (executable)
@@ -65,6 +65,9 @@ event.externalSchema.stndDefinedDataPath=$.event.stndDefinedFields.data
 collector.dmaap.streamid=fault=ves-fault|syslog=ves-syslog|heartbeat=ves-heartbeat|measurementsForVfScaling=ves-measurement|mobileFlow=ves-mobileflow|other=ves-other|stateChange=ves-statechange|thresholdCrossingAlert=ves-thresholdCrossingAlert|voiceQuality=ves-voicequality|sipSignaling=ves-sipsignaling|notification=ves-notification|pnfRegistration=ves-pnfRegistration|3GPP-FaultSupervision=ves-3gpp-fault-supervision|3GPP-Heartbeat=ves-3gpp-heartbeat|3GPP-Provisioning=ves-3gpp-provisioning|3GPP-PerformanceAssurance=ves-3gpp-performance-assurance\r
 collector.dmaapfile=./etc/DmaapConfig.json\r
 \r
+## Path to the file containing description of api versions\r
+collector.description.api.version.location=etc/api_version_description.json\r
+\r
 ## Event transformation Flag - when set expects configurable transformation\r
 ## defined under ./etc/eventTransform.json\r
 ## Enabled by default; to disable set to 0\r
index 7d5c7db..9462a38 100644 (file)
@@ -196,6 +196,10 @@ public class ApplicationSettings {
         return eventTransformations;
     }
 
+    public String getApiVersionDescriptionFilepath() {
+        return properties.getString("collector.description.api.version.location", "etc/api_version_description.json");
+    }
+
     private void loadPropertiesFromFile() {
         try {
             properties.load(configurationFileLocation);
index c046fb4..81277d0 100644 (file)
@@ -3,7 +3,7 @@
  * PROJECT
  * ================================================================================
  * Copyright (C) 2019 VMware, Inc. All rights reserved.
- * Copyright (C) 2019 Nokia. All rights reserved.s
+ * Copyright (C) 2019-2020 Nokia. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -23,39 +23,33 @@ package org.onap.dcae.common;
 
 import java.util.Collections;
 import java.util.Map;
-import java.util.Objects;
 import java.util.stream.Collectors;
 import javax.servlet.http.HttpServletRequest;
 import org.springframework.http.HttpHeaders;
 import org.springframework.stereotype.Component;
 
 /**
- * @author nil
+ * A class with methods used in HTTP header management.
  */
 @Component
 public class HeaderUtils {
 
-  public String getApiVerFilePath(String fileName) {
-    return Objects.requireNonNull(ClassLoader.getSystemClassLoader().getResource(fileName))
-        .getPath();
-  }
+    public String getRestApiIdentify(String uri) {
+        return isBatchRequest(uri) ? "eventListener_eventBatch" : "eventListener";
+    }
 
-  public String getRestApiIdentify(String uri) {
-    return isBatchRequest(uri) ? "eventListener_eventBatch" : "eventListener";
-  }
+    public Map<String, String> extractHeaders(HttpServletRequest request) {
+        return Collections.list(request.getHeaderNames()).stream()
+                .collect(Collectors.toMap(h -> h, request::getHeader));
+    }
 
-  public Map<String, String> extractHeaders(HttpServletRequest request) {
-    return Collections.list(request.getHeaderNames()).stream()
-        .collect(Collectors.toMap(h -> h, request::getHeader));
-  }
+    public HttpHeaders fillHeaders(Map<String, String> headers) {
+        HttpHeaders httpHeaders = new HttpHeaders();
+        httpHeaders.setAll(headers);
+        return httpHeaders;
+    }
 
-  public HttpHeaders fillHeaders(Map<String, String> headers) {
-    HttpHeaders httpHeaders = new HttpHeaders();
-    httpHeaders.setAll(headers);
-    return httpHeaders;
-  }
-
-  private boolean isBatchRequest(String request) {
-    return request.contains("eventBatch");
-  }
+    private boolean isBatchRequest(String request) {
+        return request.contains("eventBatch");
+    }
 }
index de0392e..0a5930f 100644 (file)
@@ -137,7 +137,7 @@ public class VesRestController {
     private CustomHeaderUtils createHeaderUtils(String version, HttpServletRequest request) {
         return new CustomHeaderUtils(version.toLowerCase().replace("v", ""),
                 headerUtils.extractHeaders(request),
-                headerUtils.getApiVerFilePath("api_version_config.json"),
+                settings.getApiVersionDescriptionFilepath(),
                 headerUtils.getRestApiIdentify(request.getRequestURI()));
 
     }
diff --git a/src/main/resources/api_version_config.json b/src/main/resources/api_version_config.json
deleted file mode 100644 (file)
index 23f585f..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-{
-  "apiVersion": 
-  {
-    "eventListener": ["4.7.2","5.4.1","7.0.1"],
-    "eventListener_eventBatch": ["4.7.2","5.4.1","7.0.1"]
-  }
-}
index 2ea59aa..8c2fdd6 100644 (file)
@@ -20,7 +20,9 @@
 package org.onap.dcae.common;
 
 import java.io.IOException;
+import java.net.URL;
 import java.nio.file.Files;
+import java.nio.file.Path;
 import java.nio.file.Paths;
 
 /**
@@ -35,15 +37,15 @@ public final class JsonDataLoader {
     }
 
     /**
-     * This method is validating given event using schema adn throws exception if event is not valid
+     * This method is validating given event using schema and throws exception when event is invalid
      *
      * @param path to file that will be loaded
      * @return contend of the file located under path, given in parameters, as string
      * @throws IOException when file under given path was not found
      */
     public static String loadContent(String path) throws IOException {
-        return new String(
-                Files.readAllBytes(Paths.get(JsonDataLoader.class.getResource(path).getPath()))
-        );
+        URL resource = JsonDataLoader.class.getResource(path);
+        Path resourcePath = Paths.get(resource.getPath());
+        return new String(Files.readAllBytes(resourcePath));
     }
 }
index 0a03c1a..ce7e09d 100644 (file)
@@ -315,9 +315,7 @@ public class VesRestControllerTest {
 
     private void configureHeadersForEventListener() {
         when(headerUtils.getRestApiIdentify(anyString())).thenReturn("eventListener");
-        when(headerUtils.getApiVerFilePath(anyString())).thenReturn(
-                this.getClass().getResource("/api_version_config.json").getPath()
-        );
+        when(applicationSettings.getApiVersionDescriptionFilepath()).thenReturn("etc/api_version_description.json");
     }
 
     private void verifyThatTransformedEventWasSend(DMaaPEventPublisher eventPublisher, String eventBeforeTransformation) {