From d279ef72d0b634f9fa57b4bce9dffebebf010d83 Mon Sep 17 00:00:00 2001 From: PatrikBuhr Date: Tue, 21 Sep 2021 14:47:59 +0200 Subject: Create a json schema for validation of the configuration of the A1 PMS - A1 Jakarta To create a json schema to be used for validation of the PMS configuration. The schema shall be used by PMS. It can also be used by the end user. The documentation should be updated on how can be done. Issue-ID: CCSDK-3468 Signed-off-by: PatrikBuhr Change-Id: I9932fa42ff40681098764c8dc84ac201bb3fabaf --- .../configuration/ApplicationConfig.java | 4 +++ .../configuration/ApplicationConfigParser.java | 41 ++++++++++++++++++++++ .../controllers/v2/ConfigurationController.java | 17 ++++++--- .../tasks/RefreshConfigTask.java | 2 +- 4 files changed, 58 insertions(+), 6 deletions(-) (limited to 'a1-policy-management/src/main/java') diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/ApplicationConfig.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/ApplicationConfig.java index 3a245190..40988e44 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/ApplicationConfig.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/ApplicationConfig.java @@ -44,6 +44,10 @@ public class ApplicationConfig { @Value("${app.filepath}") private String localConfigurationFilePath; + @Getter + @Value("${app.config-file-schema-path:\"\"}") + private String configurationFileSchemaPath; + @Getter @Value("${app.vardata-directory:null}") private String vardataDirectory; diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/ApplicationConfigParser.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/ApplicationConfigParser.java index a6af2023..3cab8aa0 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/ApplicationConfigParser.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/ApplicationConfigParser.java @@ -24,6 +24,10 @@ 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.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -38,6 +42,7 @@ import javax.validation.constraints.NotNull; import org.immutables.gson.Gson; import org.immutables.value.Value; +import org.json.JSONObject; import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -50,6 +55,11 @@ public class ApplicationConfigParser { private static final String CONFIG = "config"; private static final String CONTROLLER = "controller"; + private final ApplicationConfig applicationConfig; + + public ApplicationConfigParser(ApplicationConfig applicationConfig) { + this.applicationConfig = applicationConfig; + } @Value.Immutable @Gson.TypeAdapters @@ -66,6 +76,8 @@ public class ApplicationConfigParser { public ConfigParserResult parse(JsonObject root) throws ServiceException { + validateJsonObjectAgainstSchema(root); + String dmaapProducerTopicUrl = ""; String dmaapConsumerTopicUrl = ""; @@ -97,6 +109,35 @@ public class ApplicationConfigParser { .build(); } + private void validateJsonObjectAgainstSchema(Object object) throws ServiceException { + if (applicationConfig.getConfigurationFileSchemaPath() == null + || applicationConfig.getConfigurationFileSchemaPath().isEmpty()) { + return; + } + + try { + String schemaAsString = readSchemaFile(); + + JSONObject schemaJSON = new JSONObject(schemaAsString); + var schema = org.everit.json.schema.loader.SchemaLoader.load(schemaJSON); + + String objectAsString = object.toString(); + JSONObject json = new JSONObject(objectAsString); + schema.validate(json); + } catch (Exception e) { + throw new ServiceException("Json schema validation failure: " + e.toString()); + } + } + + private String readSchemaFile() throws IOException { + ClassLoader classLoader = getClass().getClassLoader(); + String filePath = applicationConfig.getConfigurationFileSchemaPath(); + URL url = classLoader.getResource(filePath); + File file = new File(url.getFile()); + return new String(Files.readAllBytes(file.toPath())); + + } + private void checkConfigurationConsistency(List ricConfigs, Map controllerConfigs) throws ServiceException { Set ricUrls = new HashSet<>(); diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationController.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationController.java index b677a405..e07ea284 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationController.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationController.java @@ -33,6 +33,7 @@ import io.swagger.v3.oas.annotations.tags.Tag; import java.io.IOException; import java.util.Optional; +import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig; import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfigParser; import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ConfigurationFile; import org.onap.ccsdk.oran.a1policymanagementservice.controllers.VoidResponse; @@ -57,11 +58,17 @@ public class ConfigurationController { public static final String API_NAME = "Management of configuration"; public static final String API_DESCRIPTION = ""; - @Autowired - ConfigurationFile configurationFile; + private final ConfigurationFile configurationFile; + private final RefreshConfigTask refreshConfigTask; + private final ApplicationConfig applicationConfig; - @Autowired - RefreshConfigTask refreshConfigTask; + ConfigurationController(@Autowired ConfigurationFile configurationFile, + @Autowired RefreshConfigTask refreshConfigTask, @Autowired ApplicationConfig applicationConfig) { + this.configurationFile = configurationFile; + this.refreshConfigTask = refreshConfigTask; + this.applicationConfig = applicationConfig; + + } private static Gson gson = new GsonBuilder() // .create(); // @@ -85,7 +92,7 @@ public class ConfigurationController { validateConfigFileIsUsed(); String configAsString = gson.toJson(configuration); JsonObject configJson = JsonParser.parseString(configAsString).getAsJsonObject(); - ApplicationConfigParser configParser = new ApplicationConfigParser(); + ApplicationConfigParser configParser = new ApplicationConfigParser(applicationConfig); configParser.parse(configJson); configurationFile.writeFile(configJson); logger.info("Configuration changed through REST call."); diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/tasks/RefreshConfigTask.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/tasks/RefreshConfigTask.java index 30279815..6177ee1b 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/tasks/RefreshConfigTask.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/tasks/RefreshConfigTask.java @@ -189,7 +189,7 @@ public class RefreshConfigTask { private Mono parseConfiguration(JsonObject jsonObject) { try { - ApplicationConfigParser parser = new ApplicationConfigParser(); + ApplicationConfigParser parser = new ApplicationConfigParser(this.appConfig); return Mono.just(parser.parse(jsonObject)); } catch (Exception e) { String str = e.toString(); -- cgit 1.2.3-korg