* ========================LICENSE_START=================================
* ONAP : ccsdk oran
* ======================================================================
- * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
+ * Copyright (C) 2024-2025 OpenInfra Foundation Europe. 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.
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.RequiredArgsConstructor;
+import org.everit.json.schema.loader.SchemaLoader;
+import org.json.JSONObject;
+import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyInformation;
import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyObjectInformation;
@RequiredArgsConstructor
public class Helper {
+ private final ApplicationConfig applicationConfig;
+
@Autowired
private TokenService tokenService;
return true;
}
+ private boolean policyTypeSchemaValidation(Policy policy, PolicyType policyType) {
+ try {
+ JSONObject schemaJson = new JSONObject(policyType.getSchema());
+ var schema = SchemaLoader.load(schemaJson);
+ JSONObject policyJson = new JSONObject(policy.getJson());
+
+ // PUT request body is not automatically deserialized - so we manually extract the desired policy object
+ if (policyJson.has("policyObject")) {
+ policyJson = policyJson.getJSONObject("policyObject");
+ }
+
+ schema.validate(policyJson);
+ logger.info("Policy type schema validation successful");
+ return true; // Validation passed
+ } catch (Exception e) {
+ logger.error("Policy type schema validation failed", e);
+ return false; // Validation failed
+ }
+ }
+
+ public Boolean performPolicySchemaValidation(Policy policy, PolicyType policyType) {
+
+ switch (applicationConfig.getValidatePolicyInstanceSchema()) {
+ case INFO:
+ if (policyTypeSchemaValidation(policy, policyType)) {
+ return true;
+ }
+ logger.info("Policy Schema validation failed but not enforced.");
+ return true;
+ case WARN:
+ if (policyTypeSchemaValidation(policy, policyType)) {
+ return true;
+ }
+ logger.warn("Policy Schema validation failed but not enforced.");
+ return true;
+ case FAIL:
+ if (policyTypeSchemaValidation(policy, policyType)) {
+ return true;
+ }
+ logger.error("Policy Schema validation failed.");
+ return false;
+ default:
+ logger.info("Policy schema validation disabled.");
+ return true;
+ }
+ }
+
public String policyIdGeneration(PolicyObjectInformation policyObjectInfo) {
if (policyObjectInfo.getPolicyId() == null || policyObjectInfo.getPolicyId().isEmpty() ||
policyObjectInfo.getPolicyId().isBlank())
void init() {
testHelperTest.port = port;
this.applicationConfig.setAuthProviderUrl(testHelperTest.baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL);
+ this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.NONE);
}
@AfterEach
testHelperTest.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1-policy-management/v1/policies/"));
}
+
+ @Test
+ @DisplayName("test Create Policy Success when schema validation set to FAIL")
+ void testPolicyTypeSchemaValidationFail() throws Exception {
+ this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.FAIL);
+ String nonRtRicId = "ric.1";
+ String policyTypeName = "type1_1.2.3";
+ String url = "/policies";
+ testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
+ String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "");
+ Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
+ testHelperTest.testSuccessResponse(responseMono, HttpStatus.CREATED, responseBody ->
+ responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}"));
+ testHelperTest.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1-policy-management/v1/policies/"));
+ }
+
+
+ @Test
+ @DisplayName("test Create Policy Success when schema validation set to INFO")
+ void testPolicyTypeSchemaValidationInfo() throws Exception {
+ this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.INFO);
+ String nonRtRicId = "ric.1";
+ String policyTypeName = "type1_1.2.3";
+ String url = "/policies";
+ testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
+ String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "");
+ Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
+ testHelperTest.testSuccessResponse(responseMono, HttpStatus.CREATED, responseBody ->
+ responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}"));
+ testHelperTest.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1-policy-management/v1/policies/"));
+ }
+
+
+ @Test
+ @DisplayName("test Create Policy Success when schema validation set to WARN")
+ void testPolicyTypeSchemaValidationWarn() throws Exception {
+ this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.WARN);
+ String nonRtRicId = "ric.1";
+ String policyTypeName = "type1_1.2.3";
+ String url = "/policies";
+ testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
+ String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "");
+ Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
+ testHelperTest.testSuccessResponse(responseMono, HttpStatus.CREATED, responseBody ->
+ responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}"));
+ testHelperTest.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1-policy-management/v1/policies/"));
+ }
+
+ @Test
+ @DisplayName("test bad Create Policy when schema validation set to FAIL")
+ void testBadPolicyTypeSchemaValidationFail() throws Exception {
+ this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.FAIL);
+ String nonRtRicId = "ric.1";
+ String policyTypeName = "type1_1.2.3";
+ String url = "/policies";
+ testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
+ String policyBody = testHelperTest.postBadPolicyBody(nonRtRicId, policyTypeName, "");
+ Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
+ testHelperTest.testErrorCode(responseMono, HttpStatus.BAD_REQUEST, "Policy Type Schema validation failed");
+ }
+
+ @Test
+ @DisplayName("test bad Create Policy when schema validation set to INFO")
+ void testBadPolicyTypeSchemaValidationInfo() throws Exception {
+ this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.INFO);
+ String nonRtRicId = "ric.1";
+ String policyTypeName = "type1_1.2.3";
+ String url = "/policies";
+ testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
+ String policyBody = testHelperTest.postBadPolicyBody(nonRtRicId, policyTypeName, "");
+ Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
+ testHelperTest.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1-policy-management/v1/policies/"));
+ }
+
+ @Test
+ @DisplayName("test bad Create Policy when schema validation set to WARN")
+ void testBadPolicyTypeSchemaValidationWarn() throws Exception {
+ this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.WARN);
+ String nonRtRicId = "ric.1";
+ String policyTypeName = "type1_1.2.3";
+ String url = "/policies";
+ testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
+ String policyBody = testHelperTest.postBadPolicyBody(nonRtRicId, policyTypeName, "");
+ Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
+ testHelperTest.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1-policy-management/v1/policies/"));
+ }
+
@Test
@DisplayName("test Create Policy with PolicyID sending")
void testPostPolicyWithPolicyID() throws Exception {
responseBody.contains("{\"scope\":{\"ueId\":\"ue5200\",\"qosId\":\"qos5200\"},\"qosObjectives\":{\"priorityLevel\":5200.0}"));
}
+
+ @Test
+ @DisplayName("test Update Policy Success when schema validation set to FAIL")
+ void testUpdatePolicyTypeSchemaValidationFail() throws Exception {
+ this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.FAIL);
+ String nonRtRicId = "ric.1";
+ String policyTypeName = "type1_1.2.3";
+ String url = "/policies";
+ testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
+ String policyBodyForPost = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "policyOne");
+ testHelperTest.restClientV3().postForEntity(url, policyBodyForPost).block();
+ String policyBodyForPut = testHelperTest.putPolicyBody(nonRtRicId, policyTypeName, "policyOne", "ue5200",
+ "qos5200", "5200.0");
+ testHelperTest.restClientV3().putForEntity(url+"/policyOne", policyBodyForPut).block();
+ Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url+"/policyOne");
+ testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody ->
+ responseBody.contains("{\"scope\":{\"ueId\":\"ue5200\",\"qosId\":\"qos5200\"},\"qosObjectives\":{\"priorityLevel\":5200.0}"));
+ }
+
+
+ @Test
+ @DisplayName("test Update Policy Success when schema validation set to INFO")
+ void testUpdatePolicyTypeSchemaValidationInfo() throws Exception {
+ this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.INFO);
+ String nonRtRicId = "ric.1";
+ String policyTypeName = "type1_1.2.3";
+ String url = "/policies";
+ testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
+ String policyBodyForPost = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "policyOne");
+ testHelperTest.restClientV3().postForEntity(url, policyBodyForPost).block();
+ String policyBodyForPut = testHelperTest.putPolicyBody(nonRtRicId, policyTypeName, "policyOne", "ue5200",
+ "qos5200", "5200.0");
+ testHelperTest.restClientV3().putForEntity(url+"/policyOne", policyBodyForPut).block();
+ Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url+"/policyOne");
+ testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody ->
+ responseBody.contains("{\"scope\":{\"ueId\":\"ue5200\",\"qosId\":\"qos5200\"},\"qosObjectives\":{\"priorityLevel\":5200.0}"));
+ }
+
+
+ @Test
+ @DisplayName("test Update Policy Success when schema validation set to WARN")
+ void testUpdatePolicyTypeSchemaValidationWarn() throws Exception {
+ this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.WARN);
+ String nonRtRicId = "ric.1";
+ String policyTypeName = "type1_1.2.3";
+ String url = "/policies";
+ testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
+ String policyBodyForPost = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "policyOne");
+ testHelperTest.restClientV3().postForEntity(url, policyBodyForPost).block();
+ String policyBodyForPut = testHelperTest.putPolicyBody(nonRtRicId, policyTypeName, "policyOne", "ue5200",
+ "qos5200", "5200.0");
+ testHelperTest.restClientV3().putForEntity(url+"/policyOne", policyBodyForPut).block();
+ Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url+"/policyOne");
+ testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody ->
+ responseBody.contains("{\"scope\":{\"ueId\":\"ue5200\",\"qosId\":\"qos5200\"},\"qosObjectives\":{\"priorityLevel\":5200.0}"));
+ }
+
+ @Test
+ @DisplayName("test bad Update Policy when schema validation set to FAIL")
+ void testUpdateBadPolicyTypeSchemaValidationFail() throws Exception {
+ this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.FAIL);
+ String nonRtRicId = "ric.1";
+ String policyTypeName = "type1_1.2.3";
+ String url = "/policies";
+ testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
+ String policyBodyForPost = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "policyOne");
+ testHelperTest.restClientV3().postForEntity(url, policyBodyForPost).block();
+ String policyBodyForPut = testHelperTest.putBadPolicyBody(nonRtRicId, policyTypeName, "policyOne", "ue5200",
+ "qos5200", "5200.0", "bar");
+ Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().putForEntity(url+"/policyOne", policyBodyForPut);
+ testHelperTest.testErrorCode(responseMono, HttpStatus.BAD_REQUEST, "Policy Type Schema validation failed");
+ }
+
+ @Test
+ @DisplayName("test bad Update Policy when schema validation set to WARN")
+ void testUpdateBadPolicyTypeSchemaValidationWarn() throws Exception {
+ this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.WARN);
+ String nonRtRicId = "ric.1";
+ String policyTypeName = "type1_1.2.3";
+ String url = "/policies";
+ testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
+ String policyBodyForPost = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "policyOne");
+ testHelperTest.restClientV3().postForEntity(url, policyBodyForPost).block();
+ String policyBodyForPut = testHelperTest.putBadPolicyBody(nonRtRicId, policyTypeName, "policyOne", "ue5200",
+ "qos5200", "5200.0", "bar");
+ Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().putForEntity(url+"/policyOne", policyBodyForPut);
+ testHelperTest.testSuccessResponse(responseMono, HttpStatus.OK, responseBody ->
+ responseBody.contains("{\"scope\":{\"ueId\":\"ue5200\",\"qosId\":\"qos5200\",\"foo\":\"bar\"},\"qosObjectives\":{\"priorityLevel\":5200.0}}"));
+ }
+
+ @Test
+ @DisplayName("test bad Update Policy when schema validation set to INFO")
+ void testUpdateBadPolicyTypeSchemaValidationInfo() throws Exception {
+ this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.INFO);
+ String nonRtRicId = "ric.1";
+ String policyTypeName = "type1_1.2.3";
+ String url = "/policies";
+ testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
+ String policyBodyForPost = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "policyOne");
+ testHelperTest.restClientV3().postForEntity(url, policyBodyForPost).block();
+ String policyBodyForPut = testHelperTest.putBadPolicyBody(nonRtRicId, policyTypeName, "policyOne", "ue5200",
+ "qos5200", "5200.0", "bar");
+ Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().putForEntity(url+"/policyOne", policyBodyForPut);
+ testHelperTest.testSuccessResponse(responseMono, HttpStatus.OK, responseBody ->
+ responseBody.contains("{\"scope\":{\"ueId\":\"ue5200\",\"qosId\":\"qos5200\",\"foo\":\"bar\"},\"qosObjectives\":{\"priorityLevel\":5200.0}}"));
+ }
+
private void postPolicyWithTokenAndVerify(String clientId, String serviceId, String result) throws IOException {
testHelperTest.addPolicyType("type1_1.2.3", "ric.1");
String policyBody = testHelperTest.postPolicyBody("ric.1", "type1_1.2.3", "1");
* ========================LICENSE_START=================================
* ONAP : ccsdk oran
* ======================================================================
- * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
+ * Copyright (C) 2024-2025 OpenInfra Foundation Europe. 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.
return gson.toJson(policyObjectInfo);
}
+ public String postBadPolicyBody(String nearRtRicId, String policyTypeName, String policyId) {
+ PolicyObjectInformation policyObjectInfo = new PolicyObjectInformation(nearRtRicId, dummyBadPolicyObject(), policyTypeName);
+ if (policyId != null && !policyId.isEmpty() && !policyId.isBlank())
+ policyObjectInfo.setPolicyId(policyId);
+ return gson.toJson(policyObjectInfo);
+ }
+
public String putPolicyBody(String nearRtRicId, String policyTypeName, String policyId, String ueId, String qosId,
String priorityLevel) {
PolicyObjectInformation policyObjectInfo = new PolicyObjectInformation(nearRtRicId, dummyPolicyObjectForPut(
return gson.toJson(policyObjectInfo);
}
+ public String putBadPolicyBody(String nearRtRicId, String policyTypeName, String policyId, String ueId, String qosId,
+ String priorityLevel, String foo) {
+ PolicyObjectInformation policyObjectInfo = new PolicyObjectInformation(nearRtRicId, dummyBadPolicyObjectForPut(
+ ueId, qosId, priorityLevel, foo), policyTypeName);
+ if (policyId != null && !policyId.isEmpty() && !policyId.isBlank())
+ policyObjectInfo.setPolicyId(policyId);
+ return gson.toJson(policyObjectInfo);
+ }
+
public PolicyObjectInformation policyObjectInfo(String nearRtRicId, String policyTypeName) {
return gson.fromJson(postPolicyBody(nearRtRicId, policyTypeName, ""), PolicyObjectInformation.class);
}
" }").getAsJsonObject();
}
+ public JsonObject dummyBadPolicyObjectForPut(String... values) {
+ return JsonParser.parseString("{\n" +
+ " \"scope\": {\n" +
+ " \"ueId\": \"" + values[0] + "\",\n" +
+ " \"qosId\": \"" + values[1] + "\",\n" +
+ " \"foo\": \"" + values[3] + "\"\n" +
+ " },\n" +
+ " \"qosObjectives\": {\n" +
+ " \"priorityLevel\": " + values[2] + "\n" +
+ " }\n" +
+ " }").getAsJsonObject();
+ }
+
public JsonObject dummyPolicyObject() {
return JsonParser.parseString("{\n" +
" \"scope\": {\n" +
" }").getAsJsonObject();
}
+ public JsonObject dummyBadPolicyObject() {
+ return JsonParser.parseString("{\n" +
+ " \"scope\": {\n" +
+ " \"ueId\": \"ue5100\",\n" +
+ " \"qosId\": \"qos5100\",\n" +
+ " \"foo\": \"bar\"\n" +
+ " },\n" +
+ " \"qosObjectives\": {\n" +
+ " \"priorityLevel\": 5100.0\n" +
+ " }\n" +
+ " }").getAsJsonObject();
+ }
+
public Policy buidTestPolicy(PolicyObjectInformation policyInfo, String id) throws Exception{
return Policy.builder().ric(rics.getRic(policyInfo.getNearRtRicId()))
.type(policyTypes.getType(policyInfo.getPolicyTypeId()))