diff options
author | ToineSiebelink <toine.siebelink@est.tech> | 2024-12-09 11:22:29 +0000 |
---|---|---|
committer | ToineSiebelink <toine.siebelink@est.tech> | 2024-12-10 18:33:49 +0000 |
commit | 9c4745535aeb1e68e1a3c8fdda358dbcbb673362 (patch) | |
tree | 0e01214a546223d35a23cd7b932d208e389c1757 /cps-ncmp-service/src/main/java | |
parent | f325ca432bae326cc279c85907543ccf3f45400c (diff) |
Policy Executor API Review Board Comments
- Implemented Guild review comments in API
- Updated Stub to reflect new API and 'support' all operations
- Updated production code to use new API
- Updated Semi-Integration Tests
Issue-ID: CPS-2479
Change-Id: Ibe307b0d859312b534009a384e9f71e1ea2affe0
Signed-off-by: ToineSiebelink <toine.siebelink@est.tech>
Diffstat (limited to 'cps-ncmp-service/src/main/java')
-rw-r--r-- | cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/data/policyexecutor/PolicyExecutor.java | 73 |
1 files changed, 36 insertions, 37 deletions
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/data/policyexecutor/PolicyExecutor.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/data/policyexecutor/PolicyExecutor.java index af4331893d..38105329d1 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/data/policyexecutor/PolicyExecutor.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/data/policyexecutor/PolicyExecutor.java @@ -26,9 +26,9 @@ import com.fasterxml.jackson.databind.ObjectMapper; import java.net.UnknownHostException; import java.time.Duration; import java.time.temporal.ChronoUnit; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.concurrent.TimeoutException; import lombok.RequiredArgsConstructor; @@ -68,6 +68,10 @@ public class PolicyExecutor { @Value("${ncmp.policy-executor.httpclient.all-services.readTimeoutInSeconds:30}") private long readTimeoutInSeconds; + private static final String CHANGE_REQUEST_FORMAT = "cm-legacy"; + private static final String PERMISSION_BASE_PATH = "operation-permission"; + private static final String REQUEST_PATH = "permissions"; + @Qualifier("policyExecutorWebClient") private final WebClient policyExecutorWebClient; @@ -110,38 +114,33 @@ public class PolicyExecutor { } } - private Map<String, Object> getSingleRequestAsMap(final YangModelCmHandle yangModelCmHandle, - final OperationType operationType, - final String resourceIdentifier, - final String changeRequestAsJson) { - final Map<String, Object> data = new HashMap<>(4); - data.put("cmHandleId", yangModelCmHandle.getId()); - data.put("resourceIdentifier", resourceIdentifier); - data.put("targetIdentifier", yangModelCmHandle.getAlternateId()); + private Map<String, Object> getSingleOperationAsMap(final YangModelCmHandle yangModelCmHandle, + final OperationType operationType, + final String resourceIdentifier, + final String changeRequestAsJson) { + final Map<String, Object> operationAsMap = new HashMap<>(5); + operationAsMap.put("operation", operationType.getOperationName()); + operationAsMap.put("entityHandleId", yangModelCmHandle.getId()); + operationAsMap.put("resourceIdentifier", resourceIdentifier); + operationAsMap.put("targetIdentifier", yangModelCmHandle.getAlternateId()); if (!OperationType.DELETE.equals(operationType)) { try { final Object changeRequestAsObject = objectMapper.readValue(changeRequestAsJson, Object.class); - data.put("cmChangeRequest", changeRequestAsObject); + operationAsMap.put("changeRequest", changeRequestAsObject); } catch (final JsonProcessingException e) { throw new NcmpException("Cannot convert Change Request data to Object", "Invalid Json: " + changeRequestAsJson); } } - final Map<String, Object> request = new HashMap<>(2); - request.put("schema", getAssociatedPolicyDataSchemaName(operationType)); - request.put("data", data); - return request; - } - - private static String getAssociatedPolicyDataSchemaName(final OperationType operationType) { - return "urn:cps:org.onap.cps.ncmp.policy-executor.ncmp-" + operationType.getOperationName() + "-schema:1.0.0"; + return operationAsMap; } - private Object createBodyAsObject(final List<Object> requests) { - final Map<String, Object> bodyAsMap = new HashMap<>(2); - bodyAsMap.put("decisionType", "allow"); - bodyAsMap.put("requests", requests); - return bodyAsMap; + private Object createBodyAsObject(final Map<String, Object> operationAsMap) { + final Collection<Map<String, Object>> operations = Collections.singletonList(operationAsMap); + final Map<String, Object> permissionRequestAsMap = new HashMap<>(2); + permissionRequestAsMap.put("changeRequestFormat", CHANGE_REQUEST_FORMAT); + permissionRequestAsMap.put("operations", operations); + return permissionRequestAsMap; } private ResponseEntity<JsonNode> getPolicyExecutorResponse(final YangModelCmHandle yangModelCmHandle, @@ -149,17 +148,16 @@ public class PolicyExecutor { final String authorization, final String resourceIdentifier, final String changeRequestAsJson) { - final Map<String, Object> requestAsMap = getSingleRequestAsMap(yangModelCmHandle, + final Map<String, Object> operationAsMap = getSingleOperationAsMap(yangModelCmHandle, operationType, resourceIdentifier, changeRequestAsJson); - final Object bodyAsObject = createBodyAsObject(Collections.singletonList(requestAsMap)); + final Object bodyAsObject = createBodyAsObject(operationAsMap); final UrlTemplateParameters urlTemplateParameters = RestServiceUrlTemplateBuilder.newInstance() - .fixedPathSegment("execute") - .createUrlTemplateParameters(String.format("%s:%s", serverAddress, serverPort), - "policy-executor/api"); + .fixedPathSegment(REQUEST_PATH) + .createUrlTemplateParameters(String.format("%s:%s", serverAddress, serverPort), PERMISSION_BASE_PATH); return policyExecutorWebClient.post() .uri(urlTemplateParameters.urlTemplate(), urlTemplateParameters.urlVariables()) @@ -172,23 +170,23 @@ public class PolicyExecutor { } private static void processSuccessResponse(final JsonNode responseBody) { - final String decisionId = responseBody.path("decisionId").asText("unknown id"); - final String decision = responseBody.path("decision").asText("unknown"); + final String id = responseBody.path("id").asText("unknown id"); + final String permissionResult = responseBody.path("permissionResult").asText("unknown"); final String messageFromPolicyExecutor = responseBody.path("message").asText(); - processDecision(decisionId, decision, messageFromPolicyExecutor, NO_ERROR); + processDecision(id, permissionResult, messageFromPolicyExecutor, NO_ERROR); } - private static void processDecision(final String decisionId, - final String decision, + private static void processDecision(final String id, + final String permissionResult, final String details, final Throwable optionalCauseOfError) { - log.trace("Policy Executor decision id: {} ", decisionId); - if ("allow".equals(decision)) { + log.trace("Policy Executor Decision id: {} ", id); + if ("allow".equals(permissionResult)) { log.trace("Operation allowed."); } else { - log.warn("Policy Executor decision: {}", decision); + log.warn("Policy Executor permission result: {}", permissionResult); log.warn("Policy Executor message: {}", details); - final String message = "Operation not allowed. Decision id " + decisionId + " : " + decision; + final String message = "Operation not allowed. Decision id " + id + " : " + permissionResult; throw new PolicyExecutorException(message, details, optionalCauseOfError); } } @@ -196,6 +194,7 @@ public class PolicyExecutor { private void processException(final RuntimeException runtimeException) { if (runtimeException instanceof WebClientResponseException) { final WebClientResponseException webClientResponseException = (WebClientResponseException) runtimeException; + log.warn("HTTP Error Message: {}", webClientResponseException.getMessage()); final int httpStatusCode = webClientResponseException.getStatusCode().value(); processFallbackResponse("Policy Executor returned HTTP Status code " + httpStatusCode + ".", webClientResponseException); |