From 6191d41c8d8ee20f0388172faf57e2bf8bbf8862 Mon Sep 17 00:00:00 2001 From: ToineSiebelink Date: Mon, 16 Sep 2024 15:42:30 +0100 Subject: Policy Executor API: Add Prefix in Yaml - Remove servers from yaml. - Add prefix to path in yaml. - Remove basepath prefix from Controller Impl. - Added policy server port to 8093. - Exposed docker env. vars for policy services. Issue-ID: CPS-2291 Change-Id: Idcc9e23f9d63bad2480537dc8d39523fe82b4b83 Signed-off-by: ToineSiebelink Signed-off-by: sourabh_sourabh --- cps-application/src/main/resources/application.yml | 6 +++--- .../impl/data/policyexecutor/PolicyExecutor.java | 8 ++++---- docker-compose/docker-compose.yml | 4 +++- docs/api/swagger/policy-executor/openapi.yaml | 4 +--- .../cps/integration/base/PolicyDispatcher.groovy | 2 +- .../controller/PolicyExecutorStubController.java | 22 +++++++++------------- .../src/main/resources/application.yml | 5 ++--- .../PolicyExecutorStubControllerSpec.groovy | 8 ++------ 8 files changed, 25 insertions(+), 34 deletions(-) diff --git a/cps-application/src/main/resources/application.yml b/cps-application/src/main/resources/application.yml index 8ca6b0fe02..b2cbe7ff4d 100644 --- a/cps-application/src/main/resources/application.yml +++ b/cps-application/src/main/resources/application.yml @@ -189,10 +189,10 @@ logging: cps: INFO ncmp: policy-executor: - enabled: false + enabled: ${POLICY_SERVICE_ENABLED:false} server: - address: http://localhost - port: 8785 + address: ${POLICY_SERVICE_URL:http://policy-executor-stub} + port: ${POLICY_SERVICE_PORT:8093} httpclient: all-services: maximumInMemorySizeInMegabytes: 16 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 b3aa848394..96771e30f1 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 @@ -121,7 +121,6 @@ public class PolicyExecutor { "Invalid Json: " + changeRequestAsJson); } } - final Map request = new HashMap<>(2); request.put("schema", getAssociatedPolicyDataSchemaName(operationType)); request.put("data", data); @@ -144,7 +143,7 @@ public class PolicyExecutor { final String authorization, final String resourceIdentifier, final String changeRequestAsJson) { - final String serviceBaseUrl = serverAddress + ":" + serverPort; + final Map requestAsMap = getSingleRequestAsMap(yangModelCmHandle, operationType, @@ -154,8 +153,9 @@ public class PolicyExecutor { final Object bodyAsObject = createBodyAsObject(Collections.singletonList(requestAsMap)); final UrlTemplateParameters urlTemplateParameters = RestServiceUrlTemplateBuilder.newInstance() - .fixedPathSegment("execute") - .createUrlTemplateParameters(serviceBaseUrl, ""); + .fixedPathSegment("execute") + .createUrlTemplateParameters(String.format("%s:%s", serverAddress, serverPort), + "policy-executor/api"); return policyExecutorWebClient.post() .uri(urlTemplateParameters.urlTemplate(), urlTemplateParameters.urlVariables()) diff --git a/docker-compose/docker-compose.yml b/docker-compose/docker-compose.yml index f79c03dbab..1e47d47382 100644 --- a/docker-compose/docker-compose.yml +++ b/docker-compose/docker-compose.yml @@ -21,6 +21,7 @@ services: ### docker-compose --profile dmi-service up -d -> run CPS services incl. dmi-plugin ### ### docker-compose --profile dmi-stub --profile monitoring up -d -> run CPS with stubbed dmi-plugin (for registration performance testing) ### docker-compose --profile dmi-stub --profile tracing up -d -> run CPS with stubbed dmi-plugin (for open telemetry tracing testing make ONAP_TRACING_ENABLED "true" later "http://localhost:16686" can be accessed from browser) + ### docker-compose --profile dmi-stub --profile policy-executor-stub up -d -> run CPS with stubbed dmi-plugin and policy executor stub (for policy executor service testing make POLICY_SERVICE_ENABLED "true") ### to disable notifications make notification.enabled to false & comment out kafka/zookeeper services ### dbpostgresql: @@ -58,6 +59,7 @@ services: ONAP_TRACING_ENABLED: 'false' ONAP_OTEL_SAMPLER_JAEGER_REMOTE_ENDPOINT: http://jaeger-service:14250 ONAP_OTEL_EXPORTER_ENDPOINT: http://jaeger-service:4317 + POLICY_SERVICE_ENABLED: 'false' restart: unless-stopped depends_on: - dbpostgresql @@ -153,7 +155,7 @@ services: container_name: policy-executor-stub image: ${DOCKER_REPO:-nexus3.onap.org:10003}/onap/policy-executor-stub:latest ports: - - 8785:8080 + - 8785:8093 restart: unless-stopped profiles: - policy-executor-stub diff --git a/docs/api/swagger/policy-executor/openapi.yaml b/docs/api/swagger/policy-executor/openapi.yaml index f80b998369..6b73d98ed6 100644 --- a/docs/api/swagger/policy-executor/openapi.yaml +++ b/docs/api/swagger/policy-executor/openapi.yaml @@ -21,13 +21,11 @@ info: title: Policy Executor description: "Allows NCMP to execute a policy defined by a third party implementation before proceeding with a CM operation" version: 1.0.0 -servers: - - url: /policy-executor/api tags: - name: policy-executor description: "Execute all your policies" paths: - /v1/{action}: + /policy-executor/api/v1/{action}: post: description: "Fire a Policy action" operationId: executePolicyAction diff --git a/integration-test/src/test/groovy/org/onap/cps/integration/base/PolicyDispatcher.groovy b/integration-test/src/test/groovy/org/onap/cps/integration/base/PolicyDispatcher.groovy index 27e7563e61..69792d7ed8 100644 --- a/integration-test/src/test/groovy/org/onap/cps/integration/base/PolicyDispatcher.groovy +++ b/integration-test/src/test/groovy/org/onap/cps/integration/base/PolicyDispatcher.groovy @@ -45,7 +45,7 @@ class PolicyDispatcher extends Dispatcher { return new MockResponse().setResponseCode(401) } - if (recordedRequest.path != '/v1/execute') { + if (recordedRequest.path != '/policy-executor/api/v1/execute') { return new MockResponse().setResponseCode(400) } 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 5b3a9931a0..cdd26c96e9 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,12 @@ 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 lombok.extern.slf4j.Slf4j; import org.onap.cps.policyexecutor.stub.api.PolicyExecutorApi; import org.onap.cps.policyexecutor.stub.model.NcmpDelete; import org.onap.cps.policyexecutor.stub.model.PolicyExecutionRequest; @@ -34,18 +34,15 @@ import org.onap.cps.policyexecutor.stub.model.Request; import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatusCode; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController -@RequestMapping("${rest.api.policy-executor-base-path}") @RequiredArgsConstructor +@Slf4j public class PolicyExecutorStubController implements PolicyExecutorApi { private final ObjectMapper objectMapper; - private static final Pattern ERROR_CODE_PATTERN = Pattern.compile("(\\d{3})"); - private int decisionCounter = 0; @Override @@ -53,25 +50,24 @@ public class PolicyExecutorStubController implements PolicyExecutorApi { final String action, final PolicyExecutionRequest policyExecutionRequest, final String authorization) { + log.info("Stub Policy Executor Invoked (only supports 'delete' operations)"); 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())) { + log.info("1st Request Schema:{}", firstRequest.getSchema()); + if (firstRequest.getSchema().contains("ncmp-delete-schema:1.0.0")) { return handleNcmpDeleteSchema(firstRequest); } + log.warn("This stub only supports 'delete' operations"); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } private ResponseEntity 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 NcmpDelete ncmpDelete = objectMapper.convertValue(request.getData(), NcmpDelete.class); final String targetIdentifier = ncmpDelete.getTargetIdentifier(); + if (targetIdentifier == null) { return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } @@ -97,7 +93,7 @@ public class PolicyExecutorStubController implements PolicyExecutorApi { decision = "deny"; message = "Only FDNs containing 'cps-is-great' are allowed"; } - + log.info("Decision: {} ({})", decision, message); final PolicyExecutionResponse policyExecutionResponse = new PolicyExecutionResponse(decisionId, decision, message); diff --git a/policy-executor-stub/src/main/resources/application.yml b/policy-executor-stub/src/main/resources/application.yml index f713a157fe..8ed5742a41 100644 --- a/policy-executor-stub/src/main/resources/application.yml +++ b/policy-executor-stub/src/main/resources/application.yml @@ -15,6 +15,5 @@ # SPDX-License-Identifier: Apache-2.0 # ============LICENSE_END========================================================= -rest: - api: - policy-executor-base-path: /policy-executor/api +server: + port: 8093 diff --git a/policy-executor-stub/src/test/groovy/org/onap/cps/policyexecutor/stub/controller/PolicyExecutorStubControllerSpec.groovy b/policy-executor-stub/src/test/groovy/org/onap/cps/policyexecutor/stub/controller/PolicyExecutorStubControllerSpec.groovy index efb12ac619..064e0234a3 100644 --- a/policy-executor-stub/src/test/groovy/org/onap/cps/policyexecutor/stub/controller/PolicyExecutorStubControllerSpec.groovy +++ b/policy-executor-stub/src/test/groovy/org/onap/cps/policyexecutor/stub/controller/PolicyExecutorStubControllerSpec.groovy @@ -108,15 +108,11 @@ class PolicyExecutorStubControllerSpec extends Specification { } def 'Execute policy action with invalid json for request data.'() { - given: 'a policy execution request' - def request = new Request('ncmp-delete-schema:1.0.0', 'invalid json') - def policyExecutionRequest = new PolicyExecutionRequest('some decision type', [request]) - def requestBody = objectMapper.writeValueAsString(policyExecutionRequest) when: 'request is posted' def response = mockMvc.perform(post(url) .header('Authorization','some string') .contentType(MediaType.APPLICATION_JSON) - .content(requestBody)) + .content('invalid json')) .andReturn().response then: 'response status is Bad Request' assert response.status == HttpStatus.BAD_REQUEST.value() @@ -143,7 +139,7 @@ class PolicyExecutorStubControllerSpec extends Specification { def createRequestBody(decisionType, schema, targetIdentifier) { def ncmpDelete = new NcmpDelete(targetIdentifier: targetIdentifier) - def request = new Request(schema, objectMapper.writeValueAsString(ncmpDelete)) + def request = new Request(schema, ncmpDelete) def policyExecutionRequest = new PolicyExecutionRequest(decisionType, [request]) return objectMapper.writeValueAsString(policyExecutionRequest) } -- cgit 1.2.3-korg