From d1a7dad5af98a7f3397f8535ba88129a0f7dce11 Mon Sep 17 00:00:00 2001
From: ToineSiebelink <toine.siebelink@est.tech>
Date: Fri, 27 May 2022 10:58:52 +0100
Subject: Handle RestTemplate Error handling so NCMP cna report correct server
 status

- Moved resposne handling to DmRestClient
- Clean up incorrect/redundant tests in CmProxyDataServiceImplSpec

Issue-ID: CPS-1056
Signed-off-by: ToineSiebelink <toine.siebelink@est.tech>
Change-Id: I78ba87d6af7255d85758893cfef237d75227875a
---
 .../api/impl/NetworkCmProxyDataServiceImpl.java    | 60 ++++++++--------------
 .../cps/ncmp/api/impl/client/DmiRestClient.java    | 18 +++++--
 .../api/impl/operations/DmiDataOperations.java     |  4 +-
 .../api/impl/operations/DmiModelOperations.java    |  2 +-
 4 files changed, 38 insertions(+), 46 deletions(-)

(limited to 'cps-ncmp-service/src/main/java/org')

diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java
index 0e748c7fe7..240e3c57d1 100755
--- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java
+++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java
@@ -46,7 +46,6 @@ import org.onap.cps.api.CpsAdminService;
 import org.onap.cps.api.CpsDataService;
 import org.onap.cps.api.CpsModuleService;
 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
-import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException;
 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations;
 import org.onap.cps.ncmp.api.impl.operations.DmiOperations;
 import org.onap.cps.ncmp.api.impl.operations.YangModelCmHandleRetriever;
@@ -114,9 +113,12 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService
                                                         final String optionsParamInQuery,
                                                         final String topicParamInQuery,
                                                         final String requestId) {
-        CpsValidator.validateNameCharacters(cmHandleId);
-        return getResourceDataResponse(cmHandleId, resourceIdentifier,
-                DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL, optionsParamInQuery, topicParamInQuery, requestId);
+        final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(cmHandleId,
+            resourceIdentifier,
+            optionsParamInQuery,
+            DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL,
+            requestId, topicParamInQuery);
+        return responseEntity.getBody();
     }
 
     @Override
@@ -125,21 +127,23 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService
                                                                final String optionsParamInQuery,
                                                                final String topicParamInQuery,
                                                                final String requestId) {
-        CpsValidator.validateNameCharacters(cmHandleId);
-        return getResourceDataResponse(cmHandleId, resourceIdentifier,
-                DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING, optionsParamInQuery, topicParamInQuery, requestId);
+        final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(cmHandleId,
+            resourceIdentifier,
+            optionsParamInQuery,
+            DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING,
+            requestId, topicParamInQuery);
+        return responseEntity.getBody();
     }
 
     @Override
     public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
-                                                               final String resourceIdentifier,
-                                                               final OperationEnum operation,
-                                                               final String requestData,
-                                                               final String dataType) {
+                                                                 final String resourceIdentifier,
+                                                                 final OperationEnum operation,
+                                                                 final String requestData,
+                                                                 final String dataType) {
         CpsValidator.validateNameCharacters(cmHandleId);
-        return handleResponse(
-                dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier, operation,
-                        requestData, dataType), operation);
+        return dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier, operation,
+            requestData, dataType);
     }
 
 
@@ -171,7 +175,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService
         });
 
         return cpsAdminService.queryCmHandles(jsonObjectMapper.convertToValueType(cmHandleQueryApiParameters,
-                org.onap.cps.spi.model.CmHandleQueryParameters.class));
+            org.onap.cps.spi.model.CmHandleQueryParameters.class));
     }
 
     /**
@@ -243,7 +247,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService
         final String schemaSetName = moduleSyncService.syncAndCreateSchemaSet(yangModelCmHandle);
         final String anchorName = yangModelCmHandle.getId();
         cpsAdminService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetName,
-                anchorName);
+            anchorName);
     }
 
     protected List<CmHandleRegistrationResponse> parseAndRemoveCmHandlesInDmiRegistration(
@@ -286,17 +290,6 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService
         }
     }
 
-    private Object getResourceDataResponse(final String cmHandleId,
-                                           final String resourceIdentifier,
-                                           final DmiOperations.DataStoreEnum dataStore,
-                                           final String optionsParamInQuery,
-                                           final String topicParamInQuery,
-                                           final String requestId) {
-        final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(
-                cmHandleId, resourceIdentifier, optionsParamInQuery, dataStore, requestId, topicParamInQuery);
-        return handleResponse(responseEntity, OperationEnum.READ);
-    }
-
     private void setDmiProperties(final List<YangModelCmHandle.Property> dmiProperties,
                                   final NcmpServiceCmHandle ncmpServiceCmHandle) {
         final Map<String, String> dmiPropertiesMap = new LinkedHashMap<>(dmiProperties.size());
@@ -318,7 +311,6 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService
         }
     }
 
-
     private CmHandleRegistrationResponse registerAndSyncNewCmHandle(final YangModelCmHandle yangModelCmHandle) {
         try {
             final String cmHandleJsonData = String.format("{\"cm-handles\":[%s]}",
@@ -335,14 +327,4 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService
         }
     }
 
-    private static Object handleResponse(final ResponseEntity<?> responseEntity, final OperationEnum operation) {
-        if (responseEntity.getStatusCode().is2xxSuccessful()) {
-            return responseEntity.getBody();
-        } else {
-            final String exceptionMessage = "Unable to " + operation.toString() + " resource data.";
-            throw new HttpClientRequestException(exceptionMessage, (String) responseEntity.getBody(),
-                responseEntity.getStatusCodeValue());
-        }
-    }
-
-}
\ No newline at end of file
+}
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/client/DmiRestClient.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/client/DmiRestClient.java
index f1bb95f34e..d457f2601b 100644
--- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/client/DmiRestClient.java
+++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/client/DmiRestClient.java
@@ -1,6 +1,6 @@
 /*
  *  ============LICENSE_START=======================================================
- *  Copyright (C) 2021 Nordix Foundation
+ *  Copyright (C) 2021-2022 Nordix Foundation
  *  Modifications Copyright (C) 2022 Bell Canada
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
@@ -23,11 +23,14 @@ package org.onap.cps.ncmp.api.impl.client;
 
 import lombok.AllArgsConstructor;
 import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration.DmiProperties;
+import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException;
+import org.onap.cps.ncmp.api.impl.operations.DmiRequestBody;
 import org.springframework.http.HttpEntity;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Component;
+import org.springframework.web.client.HttpStatusCodeException;
 import org.springframework.web.client.RestTemplate;
 
 @Component
@@ -37,17 +40,24 @@ public class DmiRestClient {
     private RestTemplate restTemplate;
     private DmiProperties dmiProperties;
 
-
     /**
      * Sends POST operation to DMI with json body containing module references.
      * @param dmiResourceUrl dmi resource url
      * @param jsonData json data body
+     * @param operation the type of operation being executed (for error reporting only)
      * @return response entity of type String
      */
     public ResponseEntity<Object> postOperationWithJsonData(final String dmiResourceUrl,
-                                                            final String jsonData) {
+                                                            final String jsonData,
+                                                            final DmiRequestBody.OperationEnum operation) {
         final var httpEntity = new HttpEntity<>(jsonData, configureHttpHeaders(new HttpHeaders()));
-        return restTemplate.postForEntity(dmiResourceUrl, httpEntity, Object.class);
+        try {
+            return restTemplate.postForEntity(dmiResourceUrl, httpEntity, Object.class);
+        } catch (final HttpStatusCodeException httpStatusCodeException) {
+            final String exceptionMessage = "Unable to " + operation.toString() + " resource data.";
+            throw new HttpClientRequestException(exceptionMessage, httpStatusCodeException.getResponseBodyAsString(),
+                httpStatusCodeException.getRawStatusCode());
+        }
     }
 
     private HttpHeaders configureHttpHeaders(final HttpHeaders httpHeaders) {
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiDataOperations.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiDataOperations.java
index f145379406..8e2c0946a2 100644
--- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiDataOperations.java
+++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiDataOperations.java
@@ -83,7 +83,7 @@ public class DmiDataOperations extends DmiOperations {
                 dmiServiceUrlBuilder.populateQueryParams(resourceId, optionsParamInQuery,
                 topicParamInQuery), dmiServiceUrlBuilder.populateUriVariables(
                         yangModelCmHandle, cmHandleId, dataStore));
-        return dmiRestClient.postOperationWithJsonData(dmiResourceDataUrl, jsonBody);
+        return dmiRestClient.postOperationWithJsonData(dmiResourceDataUrl, jsonBody, READ);
     }
 
     /**
@@ -116,7 +116,7 @@ public class DmiDataOperations extends DmiOperations {
             dmiServiceUrlBuilder.getDmiDatastoreUrl(dmiServiceUrlBuilder.populateQueryParams(resourceId,
                     null, null),
                 dmiServiceUrlBuilder.populateUriVariables(yangModelCmHandle, cmHandleId, PASSTHROUGH_RUNNING));
-        return dmiRestClient.postOperationWithJsonData(dmiUrl, jsonBody);
+        return dmiRestClient.postOperationWithJsonData(dmiUrl, jsonBody, operation);
     }
 
 }
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiModelOperations.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiModelOperations.java
index b033af87cd..7ab5798694 100644
--- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiModelOperations.java
+++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiModelOperations.java
@@ -107,7 +107,7 @@ public class DmiModelOperations extends DmiOperations {
                                                                   final String cmHandle,
                                                                   final String resourceName) {
         final String dmiResourceDataUrl = getDmiResourceUrl(dmiServiceName, cmHandle, resourceName);
-        return dmiRestClient.postOperationWithJsonData(dmiResourceDataUrl, jsonData);
+        return dmiRestClient.postOperationWithJsonData(dmiResourceDataUrl, jsonData, DmiRequestBody.OperationEnum.READ);
     }
 
     private static String getRequestBodyToFetchYangResources(final Collection<ModuleReference> newModuleReferences,
-- 
cgit 1.2.3-korg