Merge "Refactored cps-ri package structure"
diff --git a/cps-application/src/main/resources/application.yml b/cps-application/src/main/resources/application.yml
index 8ca6b0f..b2cbe7f 100644
--- a/cps-application/src/main/resources/application.yml
+++ b/cps-application/src/main/resources/application.yml
@@ -189,10 +189,10 @@
                 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 b3aa848..96771e3 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 @@
                     "Invalid Json: " + changeRequestAsJson);
             }
         }
-
         final Map<String, Object> request = new HashMap<>(2);
         request.put("schema", getAssociatedPolicyDataSchemaName(operationType));
         request.put("data", data);
@@ -144,7 +143,7 @@
                                                                final String authorization,
                                                                final String resourceIdentifier,
                                                                final String changeRequestAsJson) {
-        final String serviceBaseUrl = serverAddress + ":" + serverPort;
+
 
         final Map<String, Object> requestAsMap = getSingleRequestAsMap(yangModelCmHandle,
             operationType,
@@ -154,8 +153,9 @@
         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 f79c03d..1e47d47 100644
--- a/docker-compose/docker-compose.yml
+++ b/docker-compose/docker-compose.yml
@@ -21,6 +21,7 @@
   ### 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 @@
       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 @@
     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 f80b998..6b73d98 100644
--- a/docs/api/swagger/policy-executor/openapi.yaml
+++ b/docs/api/swagger/policy-executor/openapi.yaml
@@ -21,13 +21,11 @@
   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 27e7563..69792d7 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 @@
             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 5b3a993..cdd26c9 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.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 @@
                                                      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<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 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 @@
             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 f713a15..8ed5742 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 efb12ac..064e023 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 @@
     }
 
     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 @@
 
     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)
     }