Adding name and revision tag for yang resources output 81/123681/5
authorniamhcore <niamh.core@est.tech>
Mon, 30 Aug 2021 08:31:53 +0000 (09:31 +0100)
committerniamhcore <niamh.core@est.tech>
Tue, 31 Aug 2021 10:51:11 +0000 (11:51 +0100)
Issue-ID: CPS-589
Signed-off-by: niamhcore <niamh.core@est.tech>
Change-Id: I43f6971f416c6aa3ac1b1a56626930ad16288680

pom.xml
src/main/java/org/onap/cps/ncmp/dmi/service/DmiServiceImpl.java
src/test/groovy/org/onap/cps/ncmp/dmi/service/DmiServiceImplSpec.groovy

diff --git a/pom.xml b/pom.xml
index c4bce42..7204244 100644 (file)
--- a/pom.xml
+++ b/pom.xml
             <groupId>net.minidev</groupId>
             <artifactId>json-smart</artifactId>
         </dependency>
+        <dependency>
+            <groupId>com.google.code.gson</groupId>
+            <artifactId>gson</artifactId>
+        </dependency>
     </dependencies>
     <build>
         <resources>
index b4f0cac..216190e 100644 (file)
@@ -22,6 +22,8 @@ package org.onap.cps.ncmp.dmi.service;
 
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.gson.Gson;
+import com.google.gson.JsonObject;
 import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -29,6 +31,7 @@ import java.util.Map;
 import javax.validation.constraints.NotNull;
 import lombok.extern.slf4j.Slf4j;
 import net.minidev.json.JSONArray;
+import net.minidev.json.JSONObject;
 import org.apache.groovy.parser.antlr4.util.StringUtils;
 import org.onap.cps.ncmp.dmi.config.DmiPluginConfig.DmiPluginProperties;
 import org.onap.cps.ncmp.dmi.exception.CmHandleRegistrationException;
@@ -94,12 +97,12 @@ public class DmiServiceImpl implements DmiService {
 
     @Override
     public String getModuleResources(final String cmHandle, final List<ModuleReference> moduleReferences) {
-        final JSONArray getModuleResponses = new JSONArray();
+        final var getModuleResponses = new JSONArray();
         for (final var moduleReference : moduleReferences) {
             final var moduleRequest = createModuleRequest(moduleReference);
-            final var responseEntity = sdncOperations.getModuleResource(cmHandle, moduleRequest);
+            final ResponseEntity responseEntity = sdncOperations.getModuleResource(cmHandle, moduleRequest);
             if (responseEntity.getStatusCode() == HttpStatus.OK) {
-                getModuleResponses.add(responseEntity.getBody());
+                getModuleResponses.add(toJsonObject(moduleReference, responseEntity));
             } else if (responseEntity.getStatusCode() == HttpStatus.NOT_FOUND) {
                 log.error("SDNC did not return a module resource for the given cmHandle {}", cmHandle);
                 throw new ModuleResourceNotFoundException(cmHandle,
@@ -249,4 +252,26 @@ public class DmiServiceImpl implements DmiService {
         }
         return moduleRequest;
     }
+
+    private JSONObject toJsonObject(final ModuleReference moduleReference,
+        final ResponseEntity<String> response) {
+        final var jsonObject = new JSONObject();
+        jsonObject.put("moduleName", moduleReference.getName());
+        jsonObject.put("revision", moduleReference.getRevision());
+        jsonObject.put("yangSource", extractYangSourceFromBody(response));
+        return jsonObject;
+    }
+
+    private String extractYangSourceFromBody(final ResponseEntity<String> responseEntity) {
+        final var responseBodyAsJsonObject = new Gson().fromJson(responseEntity.getBody(), JsonObject.class);
+        if (responseBodyAsJsonObject.getAsJsonObject("ietf-netconf-monitoring:output") == null
+            || responseBodyAsJsonObject.getAsJsonObject("ietf-netconf-monitoring:output")
+                .getAsJsonPrimitive("data") == null) {
+            log.error("Error occurred when trying to parse the response body from sdnc {}", responseEntity.getBody());
+            throw new ModuleResourceNotFoundException(responseEntity.getBody(),
+                "Error occurred when trying to parse the response body from sdnc.");
+        }
+        return responseBodyAsJsonObject.getAsJsonObject("ietf-netconf-monitoring:output").getAsJsonPrimitive("data")
+            .toString();
+    }
 }
\ No newline at end of file
index f793532..1d2cf7f 100644 (file)
@@ -144,9 +144,9 @@ class DmiServiceImplSpec extends Specification {
         when: 'get module resources is invoked with the given cm handle and a module list'
             def result = objectUnderTest.getModuleResources(cmHandle, moduleList)
         then: 'get modules resources is called once with the expected cm handle and request body'
-            1 * mockSdncOperations.getModuleResource(cmHandle, expectedRequestBody) >> new ResponseEntity<String>('sdnc-response-body', HttpStatus.OK)
-        and: 'the result is a sdnc response wrapped inside an array'
-            assert result == '["sdnc-response-body"]'
+            1 * mockSdncOperations.getModuleResource(cmHandle, expectedRequestBody) >> new ResponseEntity<String>('{"ietf-netconf-monitoring:output": {"data": "some-data"}}', HttpStatus.OK)
+        and: 'the result is an array containing one json object with the expected name, revision and yang-source'
+            assert result == '[{"yangSource":"\\"some-data\\"","moduleName":"NAME","revision":"REVISION"}]'
     }
 
     def 'Get multiple module resources.'() {
@@ -158,10 +158,27 @@ class DmiServiceImplSpec extends Specification {
         when: 'get module resources is invoked with the given cm handle and a module list'
             def result = objectUnderTest.getModuleResources(cmHandle, moduleList)
         then: 'get modules resources is called twice'
-            2 * mockSdncOperations.getModuleResource(cmHandle, _) >>> [new ResponseEntity<String>('sdnc-response-body1', HttpStatus.OK),
-                                                                   new ResponseEntity<String>('sdnc-response-body2', HttpStatus.OK)]
-        and: 'the response is the list of all module resource schemas returned by sdnc'
-            assert result == '["sdnc-response-body1","sdnc-response-body2"]'
+            2 * mockSdncOperations.getModuleResource(cmHandle, _) >>> [new ResponseEntity<String>('{"ietf-netconf-monitoring:output": {"data": "some-data1"}}', HttpStatus.OK),
+                                                                   new ResponseEntity<String>('{"ietf-netconf-monitoring:output": {"data": "some-data2"}}', HttpStatus.OK)]
+        and: 'the result is an array containing json objects with the expected name, revision and yang-source'
+            assert result == '[{"yangSource":"\\"some-data1\\"","moduleName":"name-1","revision":"revision-1"},{"yangSource":"\\"some-data2\\"","moduleName":"name-2","revision":"revision-2"}]'
+    }
+
+    def 'Get a module resource with module resource not found exception for #scenario.'() {
+        given: 'a cmHandle and module reference list'
+            def cmHandle = 'some-cmHandle'
+            def moduleReference = new ModuleReference(name: 'NAME',revision: 'REVISION')
+            def moduleList = [moduleReference]
+        when: 'get module resources is invoked with the given cm handle and a module list'
+             objectUnderTest.getModuleResources(cmHandle, moduleList)
+        then: 'get modules resources is called once with a response body that contains no data'
+            1 * mockSdncOperations.getModuleResource(cmHandle, _) >> new ResponseEntity<String>(responseBody, HttpStatus.OK)
+        and: 'a module resource not found exception is thrown'
+            thrown(ModuleResourceNotFoundException)
+        where: 'the following values are returned'
+            scenario                                                              | responseBody
+            'a response body containing no data object'                           | '{"ietf-netconf-monitoring:output": {"null": "some-data"}}'
+            'a response body containing no ietf-netconf-monitoring:output object' | '{"null": {"data": "some-data"}}'
     }
 
     def 'Get module resources when sdnc returns #scenario response.'() {