diff options
author | ToineSiebelink <toine.siebelink@est.tech> | 2024-07-29 17:45:52 +0100 |
---|---|---|
committer | ToineSiebelink <toine.siebelink@est.tech> | 2024-07-31 17:18:58 +0100 |
commit | 53375821fa2d156785a92ef57ef7948389260cc1 (patch) | |
tree | f7b63a0db182791f3fa2b73ea97355e692939e41 /policy-executor-stub/src/main | |
parent | 9d8e6852b2ebfffca8204557a005aba27c1fc581 (diff) |
Refactor OpenAPI Policy Executor
- replace payload with request(s)
- replace payloadType with schema (one schema for each operation)
- include conflict error response in OpenAPI
- introduce 4 schemas in NCMP (doc module) for create, update, patch & delete
- udpate stub & test to follow new API and use one schema for testign purposes
Issue-ID: CPS-2335
Change-Id: Ifc40062ae83429a9ffba350ec3bcc28cb7147293
Signed-off-by: ToineSiebelink <toine.siebelink@est.tech>
Diffstat (limited to 'policy-executor-stub/src/main')
-rw-r--r-- | policy-executor-stub/src/main/java/org/onap/cps/policyexecutor/stub/controller/PolicyExecutorStubController.java | 47 |
1 files changed, 40 insertions, 7 deletions
diff --git a/policy-executor-stub/src/main/java/org/onap/cps/policyexecutor/stub/controller/PolicyExecutorStubController.java b/policy-executor-stub/src/main/java/org/onap/cps/policyexecutor/stub/controller/PolicyExecutorStubController.java index a5ec6dcac9..5b3a9931a0 100644 --- a/policy-executor-stub/src/main/java/org/onap/cps/policyexecutor/stub/controller/PolicyExecutorStubController.java +++ b/policy-executor-stub/src/main/java/org/onap/cps/policyexecutor/stub/controller/PolicyExecutorStubController.java @@ -20,12 +20,17 @@ package org.onap.cps.policyexecutor.stub.controller; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; +import lombok.RequiredArgsConstructor; import org.onap.cps.policyexecutor.stub.api.PolicyExecutorApi; +import org.onap.cps.policyexecutor.stub.model.NcmpDelete; import org.onap.cps.policyexecutor.stub.model.PolicyExecutionRequest; import org.onap.cps.policyexecutor.stub.model.PolicyExecutionResponse; +import org.onap.cps.policyexecutor.stub.model.Request; import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatusCode; import org.springframework.http.ResponseEntity; @@ -34,9 +39,13 @@ import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("${rest.api.policy-executor-base-path}") +@RequiredArgsConstructor public class PolicyExecutorStubController implements PolicyExecutorApi { - private final Pattern errorCodePattern = Pattern.compile("(\\d{3})"); + private final ObjectMapper objectMapper; + + private static final Pattern ERROR_CODE_PATTERN = Pattern.compile("(\\d{3})"); + private int decisionCounter = 0; @Override @@ -44,31 +53,55 @@ public class PolicyExecutorStubController implements PolicyExecutorApi { final String action, final PolicyExecutionRequest policyExecutionRequest, final String authorization) { - if (policyExecutionRequest.getPayload().isEmpty()) { + if (policyExecutionRequest.getRequests().isEmpty()) { + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + final Request firstRequest = policyExecutionRequest.getRequests().iterator().next(); + if ("ncmp-delete-schema:1.0.0".equals(firstRequest.getSchema())) { + return handleNcmpDeleteSchema(firstRequest); + } + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + + private ResponseEntity<PolicyExecutionResponse> handleNcmpDeleteSchema(final Request request) { + final NcmpDelete ncmpDelete; + try { + ncmpDelete = objectMapper.readValue((String) request.getData(), NcmpDelete.class); + } catch (final JsonProcessingException e) { return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } - final String firstTargetFdn = policyExecutionRequest.getPayload().iterator().next().getTargetFdn(); + final String targetIdentifier = ncmpDelete.getTargetIdentifier(); + if (targetIdentifier == null) { + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } - final Matcher matcher = errorCodePattern.matcher(firstTargetFdn); + final Matcher matcher = ERROR_CODE_PATTERN.matcher(targetIdentifier); if (matcher.find()) { final int errorCode = Integer.parseInt(matcher.group(1)); return new ResponseEntity<>(HttpStatusCode.valueOf(errorCode)); } + return createPolicyExecutionResponse(targetIdentifier); + } + + private ResponseEntity<PolicyExecutionResponse> createPolicyExecutionResponse(final String targetIdentifier) { final String decisionId = String.valueOf(++decisionCounter); final String decision; final String message; - if (firstTargetFdn.toLowerCase(Locale.getDefault()).contains("cps-is-great")) { - decision = "permit"; + if (targetIdentifier.toLowerCase(Locale.getDefault()).contains("cps-is-great")) { + decision = "allow"; message = "All good"; } else { decision = "deny"; - message = "Only FDNs containing 'cps-is-great' are permitted"; + message = "Only FDNs containing 'cps-is-great' are allowed"; } + final PolicyExecutionResponse policyExecutionResponse = new PolicyExecutionResponse(decisionId, decision, message); + return ResponseEntity.ok(policyExecutionResponse); } + } |