diff options
author | niamhcore <niamh.core@est.tech> | 2021-08-30 09:31:53 +0100 |
---|---|---|
committer | niamhcore <niamh.core@est.tech> | 2021-08-31 11:51:11 +0100 |
commit | 6d7e304474717735610f02941900f780a8c93862 (patch) | |
tree | 0bffb344356e7a46f648d5591328921ecf92b1a6 /src/main | |
parent | 776c1451ddb2eb676520f8d34103b9c889a4ea26 (diff) |
Adding name and revision tag for yang resources output
Issue-ID: CPS-589
Signed-off-by: niamhcore <niamh.core@est.tech>
Change-Id: I43f6971f416c6aa3ac1b1a56626930ad16288680
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/org/onap/cps/ncmp/dmi/service/DmiServiceImpl.java | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/src/main/java/org/onap/cps/ncmp/dmi/service/DmiServiceImpl.java b/src/main/java/org/onap/cps/ncmp/dmi/service/DmiServiceImpl.java index b4f0cac5..216190e3 100644 --- a/src/main/java/org/onap/cps/ncmp/dmi/service/DmiServiceImpl.java +++ b/src/main/java/org/onap/cps/ncmp/dmi/service/DmiServiceImpl.java @@ -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 |