Create a json schema for validation of the configuration of the A1 PMS - A1 Jakarta 77/124777/2
authorPatrikBuhr <patrik.buhr@est.tech>
Fri, 8 Oct 2021 12:08:59 +0000 (14:08 +0200)
committerPatrikBuhr <patrik.buhr@est.tech>
Mon, 11 Oct 2021 13:58:16 +0000 (15:58 +0200)
Enabling the feature by default and fixing so that the schema can be loaded from the jar.

Issue-ID: CCSDK-3468
Signed-off-by: PatrikBuhr <patrik.buhr@est.tech>
Change-Id: I49a41b9124b74d5019200cc9b6eb48c5d46b043d

a1-policy-management/config/application.yaml
a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/ApplicationConfigParser.java
a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/MockPolicyManagementService.java
a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/ApplicationConfigParserTest.java
a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationControllerTest.java

index e1a778c..d02c0a8 100644 (file)
@@ -74,5 +74,7 @@ app:
     http.proxy-type: HTTP
   # path where the service can store data
   vardata-directory: /var/policy-management-service
-  config-file-schema-path:
+  # the config-file-schema-path referres to a location in the jar file. If this property is empty or missing, 
+  # no schema validation will be executed. 
+  config-file-schema-path: /application_configuration_schema.json
 
index 3cab8aa..726d67f 100644 (file)
 
 package org.onap.ccsdk.oran.a1policymanagementservice.configuration;
 
+import com.google.common.io.CharStreams;
 import com.google.gson.JsonArray;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 
-import java.io.File;
 import java.io.IOException;
-import java.net.URL;
-import java.nio.file.Files;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -46,6 +47,7 @@ import org.json.JSONObject;
 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpStatus;
 
 /**
  * Parser for the Json representing of the component configuration.
@@ -129,13 +131,15 @@ public class ApplicationConfigParser {
         }
     }
 
-    private String readSchemaFile() throws IOException {
-        ClassLoader classLoader = getClass().getClassLoader();
+    private String readSchemaFile() throws IOException, ServiceException {
         String filePath = applicationConfig.getConfigurationFileSchemaPath();
-        URL url = classLoader.getResource(filePath);
-        File file = new File(url.getFile());
-        return new String(Files.readAllBytes(file.toPath()));
-
+        InputStream in = getClass().getResourceAsStream(filePath);
+        logger.debug("Reading application schema file from: {} with: {}", filePath, in);
+        if (in == null) {
+            throw new ServiceException("Could not read application configuration schema file: " + filePath,
+                    HttpStatus.INTERNAL_SERVER_ERROR);
+        }
+        return CharStreams.toString(new InputStreamReader(in, StandardCharsets.UTF_8));
     }
 
     private void checkConfigurationConsistency(List<RicConfig> ricConfigs,
index b007640..3cb1d91 100644 (file)
@@ -58,7 +58,8 @@ import org.springframework.util.StringUtils;
 @TestPropertySource(properties = { //
         "server.ssl.key-store=./config/keystore.jks", //
         "app.webclient.trust-store=./config/truststore.jks", //
-        "app.vardata-directory=./target"})
+        "app.vardata-directory=./target", //
+        "app.config-file-schema-path=/application_configuration_schema.json"})
 @SuppressWarnings("java:S3577") // Class name should start or end with Test. This is not a test class per se,
                                 // but a mock
                                 // of the server.
index d12fd6b..4a4e8a3 100644 (file)
@@ -55,7 +55,7 @@ class ApplicationConfigParserTest {
         JsonObject jsonRootObject = getJsonRootObject();
 
         when(applicationConfigMock.getConfigurationFileSchemaPath())
-                .thenReturn("application_configuration_schema.json");
+                .thenReturn("/application_configuration_schema.json");
 
         ApplicationConfigParser.ConfigParserResult result = parserUnderTest.parse(jsonRootObject);
 
index d280555..b4ff696 100644 (file)
@@ -61,7 +61,7 @@ import reactor.test.StepVerifier;
         "server.ssl.key-store=./config/keystore.jks", //
         "app.webclient.trust-store=./config/truststore.jks", //
         "app.vardata-directory=./target", //
-        "app.config-file-schema-path=application_configuration_schema.json" //
+        "app.config-file-schema-path=/application_configuration_schema.json" //
 })
 class ConfigurationControllerTest {
     @Autowired