Structured Exception details for DMI 16/128116/13
authormpriyank <priyank.maheshwari@est.tech>
Mon, 28 Mar 2022 10:17:47 +0000 (15:47 +0530)
committermpriyank <priyank.maheshwari@est.tech>
Mon, 4 Apr 2022 15:41:09 +0000 (21:11 +0530)
- Introduced DmiErrorMessage in API docs with 502 Bad Gateway
- HttpClientRequestException will be thrown which will be exposed as 502 BAD Gateway for the client from NCMP

Issue-ID: CPS-917
Change-Id: Iba8f159e8216bc1f63a9ab86208e5c802437e2e8
Signed-off-by: mpriyank <priyank.maheshwari@est.tech>
cps-ncmp-rest/docs/openapi/components.yaml
cps-ncmp-rest/docs/openapi/ncmp.yml
cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/exceptions/NetworkCmProxyRestExceptionHandler.java
cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/exceptions/NetworkCmProxyRestExceptionHandlerSpec.groovy
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/exception/HttpClientRequestException.java [new file with mode: 0644]
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplSpec.groovy
docs/api/swagger/ncmp/openapi.yaml

index 69225ae..092c0a2 100644 (file)
@@ -30,7 +30,23 @@ components:
           type: string
         details:
           type: string
-
+    # DMI Server Exception Schema
+    DmiErrorMessage:
+      title: DMI Error Message
+      type: object
+      properties:
+        message:
+          type: string
+          example: "Bad Gateway Error Message NCMP"
+        dmi-response:
+          type: object
+          properties:
+            http-code:
+              type: integer
+              example: 400
+            body:
+              type: string
+              example: Bad Request
     # Request Schemas
     RestDmiPluginRegistration:
       type: object
@@ -434,3 +450,14 @@ components:
             status: 500
             message: Internal Server Error
             details: Internal Server Error occurred
+    BadGateway:
+      description: Bad Gateway
+      content:
+        application/json:
+          schema:
+            $ref: "#/components/schemas/DmiErrorMessage"
+          example:
+            message: "Bad Gateway Error Message NCMP"
+            dmi-response:
+              http-code: 400
+              body: "Bad Request"
index a9d08b7..2c9ee24 100755 (executable)
@@ -48,6 +48,8 @@ getResourceDataForPassthroughOperational:
         $ref: 'components.yaml#/components/responses/Forbidden'
       500:
         $ref: 'components.yaml#/components/responses/InternalServerError'
+      502:
+        $ref: 'components.yaml#/components/responses/BadGateway'
 
 resourceDataForPassthroughRunning:
   get:
@@ -80,6 +82,8 @@ resourceDataForPassthroughRunning:
         $ref: 'components.yaml#/components/responses/Forbidden'
       500:
         $ref: 'components.yaml#/components/responses/InternalServerError'
+      502:
+        $ref: 'components.yaml#/components/responses/BadGateway'
   post:
     tags:
       - network-cm-proxy
@@ -116,6 +120,8 @@ resourceDataForPassthroughRunning:
         $ref: 'components.yaml#/components/responses/Forbidden'
       500:
         $ref: 'components.yaml#/components/responses/InternalServerError'
+      502:
+        $ref: 'components.yaml#/components/responses/BadGateway'
 
   put:
     tags:
@@ -153,6 +159,8 @@ resourceDataForPassthroughRunning:
         $ref: 'components.yaml#/components/responses/Forbidden'
       500:
         $ref: 'components.yaml#/components/responses/InternalServerError'
+      502:
+        $ref: 'components.yaml#/components/responses/BadGateway'
 
   patch:
     tags:
@@ -184,6 +192,8 @@ resourceDataForPassthroughRunning:
         $ref: 'components.yaml#/components/responses/Forbidden'
       500:
         $ref: 'components.yaml#/components/responses/InternalServerError'
+      502:
+        $ref: 'components.yaml#/components/responses/BadGateway'
 
   delete:
     tags:
@@ -208,6 +218,8 @@ resourceDataForPassthroughRunning:
         $ref: 'components.yaml#/components/responses/NotFound'
       500:
         $ref: 'components.yaml#/components/responses/InternalServerError'
+      502:
+        $ref: 'components.yaml#/components/responses/BadGateway'
 
 fetchModuleReferencesByCmHandle:
   get:
index 0843e97..c723733 100755 (executable)
@@ -24,11 +24,14 @@ import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.onap.cps.ncmp.api.impl.exception.DmiRequestException;
+import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException;
 import org.onap.cps.ncmp.api.impl.exception.InvalidTopicException;
 import org.onap.cps.ncmp.api.impl.exception.NcmpException;
 import org.onap.cps.ncmp.api.impl.exception.ServerNcmpException;
 import org.onap.cps.ncmp.rest.controller.NetworkCmProxyController;
 import org.onap.cps.ncmp.rest.controller.NetworkCmProxyInventoryController;
+import org.onap.cps.ncmp.rest.model.DmiErrorMessage;
+import org.onap.cps.ncmp.rest.model.DmiErrorMessageDmiresponse;
 import org.onap.cps.ncmp.rest.model.ErrorMessage;
 import org.onap.cps.spi.exceptions.CpsException;
 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
@@ -66,6 +69,12 @@ public class NetworkCmProxyRestExceptionHandler {
         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
     }
 
+    @ExceptionHandler({HttpClientRequestException.class})
+    public static ResponseEntity<Object> handleClientRequestExceptions(
+            final HttpClientRequestException httpClientRequestException) {
+        return wrapDmiErrorResponse(HttpStatus.BAD_GATEWAY, httpClientRequestException);
+    }
+
     @ExceptionHandler({DmiRequestException.class, DataValidationException.class, HttpMessageNotReadableException.class,
             InvalidTopicException.class})
     public static ResponseEntity<Object> handleDmiRequestExceptions(final Exception exception) {
@@ -91,8 +100,19 @@ public class NetworkCmProxyRestExceptionHandler {
         } else {
             errorMessage.setDetails(CHECK_LOGS_FOR_DETAILS);
         }
-        errorMessage.setDetails(exception instanceof CpsException ? ((CpsException) exception).getDetails() :
-            CHECK_LOGS_FOR_DETAILS);
+        errorMessage.setDetails(
+                exception instanceof CpsException ? ((CpsException) exception).getDetails() : CHECK_LOGS_FOR_DETAILS);
         return new ResponseEntity<>(errorMessage, status);
     }
+
+    private static ResponseEntity<Object> wrapDmiErrorResponse(final HttpStatus httpStatus,
+            final HttpClientRequestException httpClientRequestException) {
+        final var dmiErrorMessage = new DmiErrorMessage();
+        final var dmiErrorResponse = new DmiErrorMessageDmiresponse();
+        dmiErrorResponse.setHttpCode(httpClientRequestException.getHttpStatus());
+        dmiErrorResponse.setBody(httpClientRequestException.getDetails());
+        dmiErrorMessage.setMessage(httpClientRequestException.getMessage());
+        dmiErrorMessage.setDmiResponse(dmiErrorResponse);
+        return new ResponseEntity<>(dmiErrorMessage, httpStatus);
+    }
 }
index b642370..1f6c384 100644 (file)
 
 package org.onap.cps.ncmp.rest.exceptions
 
-import com.fasterxml.jackson.databind.ObjectMapper
 import groovy.json.JsonSlurper
 import org.mapstruct.factory.Mappers
 import org.onap.cps.TestUtils
 import org.onap.cps.ncmp.api.NetworkCmProxyDataService
 import org.onap.cps.ncmp.api.impl.exception.DmiRequestException
+import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException
 import org.onap.cps.ncmp.api.impl.exception.ServerNcmpException
-import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
 import org.onap.cps.ncmp.rest.controller.NcmpRestInputMapper
 import org.onap.cps.spi.exceptions.CpsException
 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
@@ -38,6 +37,7 @@ import org.spockframework.spring.SpringBean
 import org.springframework.beans.factory.annotation.Autowired
 import org.springframework.beans.factory.annotation.Value
 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
+import org.springframework.http.HttpStatus
 import org.springframework.http.MediaType
 import org.springframework.test.web.servlet.MockMvc
 import spock.lang.Shared
@@ -111,6 +111,19 @@ class NetworkCmProxyRestExceptionHandlerSpec extends Specification {
             assertTestResponse(response, BAD_REQUEST, sampleErrorMessage, sampleErrorDetails)
     }
 
+    def 'Failing DMI Request - passthrough scenario'() {
+        given: 'failing DMI request'
+            mockNetworkCmProxyDataService.getResourceDataPassThroughRunningForCmHandle(*_) >> { throw new HttpClientRequestException('Error Message Details NCMP', 'Bad Request from DMI', 400) }
+        when: 'the DMI request is executed'
+            def response = mvc.perform(get("$dataNodeBaseEndpointNcmp/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running?resourceIdentifier=stores:bookstore/categories=100"))
+                .andReturn().response
+        then: 'NCMP service responds with 502 Bad Gateway status'
+            response.status == HttpStatus.BAD_GATEWAY.value()
+        and: 'the NCMP response also contains the original DMI response details'
+            response.contentAsString.contains('400')
+            response.contentAsString.contains('Bad Request from DMI')
+    }
+
     def setupTestException(exception, apiType) {
         if (NCMP == apiType) {
             mockNetworkCmProxyDataService.getYangResourcesModuleReferences(*_) >> { throw exception }
index 576c45c..69e9c7b 100755 (executable)
@@ -47,8 +47,8 @@ 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.exception.InvalidTopicException;
-import org.onap.cps.ncmp.api.impl.exception.ServerNcmpException;
 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations;
 import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations;
 import org.onap.cps.ncmp.api.impl.operations.DmiOperations;
@@ -149,9 +149,8 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService
                                                                final String requestData,
                                                                final String dataType) {
         return handleResponse(
-            dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(
-                cmHandleId, resourceIdentifier, operation, requestData, dataType),
-            "Not able to " + operation + " resource data.");
+                dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier, operation,
+                        requestData, dataType), operation);
     }
 
 
@@ -225,14 +224,13 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService
         registerAndSyncNewCmHandles(createdYangModelCmHandlesList);
     }
 
-    private static Object handleResponse(final ResponseEntity<?> responseEntity,
-                                         final String exceptionMessage) {
+    private static Object handleResponse(final ResponseEntity<?> responseEntity, final OperationEnum operation) {
         if (responseEntity.getStatusCode().is2xxSuccessful()) {
             return responseEntity.getBody();
         } else {
-            throw new ServerNcmpException(exceptionMessage,
-                    "DMI status code: " + responseEntity.getStatusCodeValue()
-                            + ", DMI response body: " + responseEntity.getBody());
+            final String exceptionMessage = "Unable to " + operation.toString() + " resource data.";
+            throw new HttpClientRequestException(exceptionMessage, (String) responseEntity.getBody(),
+                    responseEntity.getStatusCodeValue());
         }
     }
 
@@ -355,6 +353,6 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService
         final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(
                 cmHandleId, resourceIdentifier, optionsParamInQuery, acceptParamInHeader,
                 dataStore, NO_REQUEST_ID, NO_TOPIC);
-        return handleResponse(responseEntity, "Not able to get resource data.");
+        return handleResponse(responseEntity, OperationEnum.READ);
     }
 }
\ No newline at end of file
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/exception/HttpClientRequestException.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/exception/HttpClientRequestException.java
new file mode 100644 (file)
index 0000000..9d307e5
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2022 Nordix Foundation
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.ncmp.api.impl.exception;
+
+import lombok.Getter;
+
+/**
+ * Http Client Request exception for passthrough scenarios.
+ */
+@Getter
+public class HttpClientRequestException extends NcmpException {
+
+    private static final long serialVersionUID = 6659897770659834797L;
+    final Integer httpStatus;
+
+    /**
+     * Constructor to form exception for passthrough scenarios.
+     *
+     * @param message    message details from NCMP
+     * @param details    response body from the client available as details
+     * @param httpStatus http status code from the client
+     */
+    public HttpClientRequestException(final String message, final String details, final Integer httpStatus) {
+        super(message, details);
+        this.httpStatus = httpStatus;
+    }
+}
index c21d7e7..06b2032 100644 (file)
@@ -22,6 +22,7 @@
 
 package org.onap.cps.ncmp.api.impl
 
+import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException
 import org.onap.cps.ncmp.api.impl.exception.InvalidTopicException
 import org.onap.cps.ncmp.api.impl.operations.YangModelCmHandleRetriever
 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
@@ -40,7 +41,6 @@ import com.fasterxml.jackson.databind.ObjectMapper
 import org.onap.cps.api.CpsAdminService
 import org.onap.cps.api.CpsDataService
 import org.onap.cps.api.CpsModuleService
-import org.onap.cps.ncmp.api.impl.exception.ServerNcmpException
 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations
 import org.onap.cps.spi.FetchDescendantsOption
 import org.onap.cps.spi.model.DataNode
@@ -98,9 +98,9 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
                 'testResourceId', CREATE,
                 '{some-json}', 'application/json')
         then: 'exception is thrown'
-            def exceptionThrown = thrown(ServerNcmpException.class)
-        and: 'details contains (not found) error code: 404'
-            exceptionThrown.details.contains('404')
+            def exceptionThrown = thrown(HttpClientRequestException.class)
+        and: 'http status (not found) error code: 404'
+            exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value()
     }
 
     def 'Get resource data for pass-through operational from DMI.'() {
@@ -141,9 +141,10 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
                     'testAcceptParam',
                     OPTIONS_PARAM,
                     NO_TOPIC)
-        then: 'exception is thrown with the expected details'
-            def exceptionThrown = thrown(ServerNcmpException.class)
-            exceptionThrown.details == 'DMI status code: 404, DMI response body: NOK-json'
+        then: 'exception is thrown with the expected response code and details'
+            def exceptionThrown = thrown(HttpClientRequestException.class)
+            exceptionThrown.details.contains('NOK-json')
+            exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value()
     }
 
     def 'Get resource data for pass-through operational from DMI return NOK response.'() {
@@ -166,8 +167,9 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
                     OPTIONS_PARAM,
                     NO_TOPIC)
         then: 'exception is thrown'
-            def exceptionThrown = thrown(ServerNcmpException.class)
-        and: 'details contains the original response'
+            def exceptionThrown = thrown(HttpClientRequestException.class)
+        and: 'details contain the original response'
+            exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value()
             exceptionThrown.details.contains('NOK-json')
     }
 
@@ -213,9 +215,10 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
                     OPTIONS_PARAM,
                     NO_TOPIC)
         then: 'exception is thrown'
-            def exceptionThrown = thrown(ServerNcmpException.class)
-        and: 'details contains the original response'
+            def exceptionThrown = thrown(HttpClientRequestException.class)
+        and: 'details contain the original response'
             exceptionThrown.details.contains('NOK-json')
+            exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value()
     }
 
     def 'DMI Operational data request with #scenario'() {
@@ -340,12 +343,12 @@ class NetworkCmProxyDataServiceImplSpec extends Specification {
                 '{some-json}',
                 'application/json')
         then: 'an exception is thrown with the expected error message details with correct operation'
-            def exceptionThrown = thrown(ServerNcmpException.class)
+            def exceptionThrown = thrown(HttpClientRequestException.class)
             exceptionThrown.getMessage().contains(expectedResponseMessage)
         where:
             scenario | givenOperation || expectedResponseMessage
-            'CREATE' | CREATE         || 'Not able to create resource data.'
-            'READ'   | READ           || 'Not able to read resource data.'
-            'UPDATE' | UPDATE         || 'Not able to update resource data.'
+            'CREATE' | CREATE         || 'Unable to create resource data.'
+            'READ'   | READ           || 'Unable to read resource data.'
+            'UPDATE' | UPDATE         || 'Unable to update resource data.'
     }
 }
index 5a6a600..606b69f 100644 (file)
@@ -131,6 +131,18 @@ paths:
                 status: 500
                 message: Internal Server Error
                 details: Internal Server Error occurred
+        "502":
+          description: Bad Gateway
+          content:
+            application/json:
+              schema:
+                $ref: '#/components/schemas/DmiErrorMessage'
+              example:
+                message: "Bad Gateway Error Message NCMP"
+                dmi-response:
+                  http-code: 400
+                  body: Bad Request
+
   /v1/ch/{cm-handle}/data/ds/ncmp-datastore:passthrough-running:
     get:
       tags:
@@ -256,6 +268,17 @@ paths:
                 status: 500
                 message: Internal Server Error
                 details: Internal Server Error occurred
+        "502":
+          description: Bad Gateway
+          content:
+            application/json:
+              schema:
+                $ref: '#/components/schemas/DmiErrorMessage'
+              example:
+                message: "Bad Gateway Error Message NCMP"
+                dmi-response:
+                  http-code: 400
+                  body: Bad Request
     put:
       tags:
       - network-cm-proxy
@@ -362,6 +385,17 @@ paths:
                 status: 500
                 message: Internal Server Error
                 details: Internal Server Error occurred
+        "502":
+          description: Bad Gateway
+          content:
+            application/json:
+              schema:
+                $ref: '#/components/schemas/DmiErrorMessage'
+              example:
+                message: "Bad Gateway Error Message NCMP"
+                dmi-response:
+                  http-code: 400
+                  body: Bad Request
     post:
       tags:
       - network-cm-proxy
@@ -464,6 +498,17 @@ paths:
                 status: 500
                 message: Internal Server Error
                 details: Internal Server Error occurred
+        "502":
+          description: Bad Gateway
+          content:
+            application/json:
+              schema:
+                $ref: '#/components/schemas/DmiErrorMessage'
+              example:
+                message: "Bad Gateway Error Message NCMP"
+                dmi-response:
+                  http-code: 400
+                  body: Bad Request
     delete:
       tags:
       - network-cm-proxy
@@ -561,6 +606,17 @@ paths:
                 status: 500
                 message: Internal Server Error
                 details: Internal Server Error occurred
+        "502":
+          description: Bad Gateway
+          content:
+            application/json:
+              schema:
+                $ref: '#/components/schemas/DmiErrorMessage'
+              example:
+                message: "Bad Gateway Error Message NCMP"
+                dmi-response:
+                  http-code: 400
+                  body: Bad Request
     patch:
       tags:
       - network-cm-proxy
@@ -661,6 +717,17 @@ paths:
                 status: 500
                 message: Internal Server Error
                 details: Internal Server Error occurred
+        "502":
+          description: Bad Gateway
+          content:
+            application/json:
+              schema:
+                $ref: '#/components/schemas/DmiErrorMessage'
+              example:
+                message: "Bad Gateway Error Message NCMP"
+                dmi-response:
+                  http-code: 400
+                  body: Bad Request
   /v1/ch/{cm-handle}/modules:
     get:
       tags:
@@ -873,6 +940,23 @@ components:
           type: string
         details:
           type: string
+    # DMI Server Exception Schema
+    DmiErrorMessage:
+      title: DMI Error Message
+      type: object
+      properties:
+        message:
+          type: string
+          example: "Bad Gateway Error Message NCMP"
+        dmi-response:
+          type: object
+          properties:
+            http-code:
+              type: integer
+              example: 400
+            body:
+              type: string
+              example: Bad Request
     RestModuleReference:
       title: Module reference details
       type: object