From 9dc83ae69c8ae15a3a79d17feba0f4c7da81daa0 Mon Sep 17 00:00:00 2001 From: sourabh_sourabh Date: Mon, 19 Feb 2024 03:42:54 +0000 Subject: Advance DMI Stub to change module set (tags) used by cm handle - Renamed json response file name based on module set tag into Dmi plugin stub. - Added in memory map to hold the information of cm handle per module set tag int dmi plugin stub. - Exposed a REST api to perform diff. operations on memory map to replicate dmi plugin stub. Issue-ID: CPS-2079 Change-Id: I7818fa4c47be9a02068976fd116a1dbc36b134ef Signed-off-by: sourabh_sourabh --- .../stub/controller/DmiRestStubController.java | 92 ++++++++++++++++++---- 1 file changed, 78 insertions(+), 14 deletions(-) (limited to 'dmi-plugin-demo-and-csit-stub/dmi-plugin-demo-and-csit-stub-service/src/main/java/org/onap') diff --git a/dmi-plugin-demo-and-csit-stub/dmi-plugin-demo-and-csit-stub-service/src/main/java/org/onap/cps/ncmp/dmi/rest/stub/controller/DmiRestStubController.java b/dmi-plugin-demo-and-csit-stub/dmi-plugin-demo-and-csit-stub-service/src/main/java/org/onap/cps/ncmp/dmi/rest/stub/controller/DmiRestStubController.java index b536c75fb6..c57cf0ecb0 100644 --- a/dmi-plugin-demo-and-csit-stub/dmi-plugin-demo-and-csit-stub-service/src/main/java/org/onap/cps/ncmp/dmi/rest/stub/controller/DmiRestStubController.java +++ b/dmi-plugin-demo-and-csit-stub/dmi-plugin-demo-and-csit-stub-service/src/main/java/org/onap/cps/ncmp/dmi/rest/stub/controller/DmiRestStubController.java @@ -28,7 +28,9 @@ import io.cloudevents.CloudEvent; import io.cloudevents.core.builder.CloudEventBuilder; import java.net.URI; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.UUID; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -49,8 +51,11 @@ import org.springframework.core.io.ResourceLoader; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.kafka.core.KafkaTemplate; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; @@ -63,23 +68,81 @@ import org.springframework.web.bind.annotation.RestController; @Slf4j public class DmiRestStubController { + private static final String DEFAULT_TAG = "tagD"; + private static final String dataOperationEventType = "org.onap.cps.ncmp.events.async1_0_0.DataOperationEvent"; + private static final Map moduleSetTagPerCmHandleId = new HashMap<>(); private final KafkaTemplate cloudEventKafkaTemplate; private final ObjectMapper objectMapper; private final ApplicationContext applicationContext; - @Value("${app.ncmp.async-m2m.topic}") private String ncmpAsyncM2mTopic; - @Value("${delay.module-references-delay-ms}") private long moduleReferencesDelayMs; - @Value("${delay.module-resources-delay-ms}") private long moduleResourcesDelayMs; - @Value("${delay.data-for-cm-handle-delay-ms}") private long dataForCmHandleDelayMs; - private String dataOperationEventType = "org.onap.cps.ncmp.events.async1_0_0.DataOperationEvent"; + /** + * This code defines a REST API endpoint for adding new the module set tag mapping. The endpoint receives the + * cmHandleId and moduleSetTag as request body and add into moduleSetTagPerCmHandleId map with the provided + * values. + * + * @param requestBody map of cmHandleId and moduleSetTag + * @return a ResponseEntity object containing the updated moduleSetTagPerCmHandleId map as the response body + */ + @PostMapping("/v1/tagMapping") + public ResponseEntity> addTagForMapping(@RequestBody final Map requestBody) { + moduleSetTagPerCmHandleId.putAll(requestBody); + return new ResponseEntity<>(requestBody, HttpStatus.CREATED); + } + + /** + * This code defines a GET endpoint of module set tag mapping. + * + * @return The map represents the module set tag mapping. + */ + @GetMapping("/v1/tagMapping") + public ResponseEntity> getTagMapping() { + return ResponseEntity.ok(moduleSetTagPerCmHandleId); + } + + /** + * This code defines a GET endpoint of module set tag by cm handle ID. + * + * @return The map represents the module set tag mapping filtered by cm handle ID. + */ + @GetMapping("/v1/tagMapping/ch/{cmHandleId}") + public ResponseEntity getTagMappingByCmHandleId(@PathVariable final String cmHandleId) { + return ResponseEntity.ok(moduleSetTagPerCmHandleId.get(cmHandleId)); + } + + /** + * This code defines a REST API endpoint for updating the module set tag mapping. The endpoint receives the + * cmHandleId and moduleSetTag as request body and updates the moduleSetTagPerCmHandleId map with the provided + * values. + * + * @param requestBody map of cmHandleId and moduleSetTag + * @return a ResponseEntity object containing the updated moduleSetTagPerCmHandleId map as the response body + */ + + @PutMapping("/v1/tagMapping") + public ResponseEntity> updateTagMapping(@RequestBody final Map requestBody) { + moduleSetTagPerCmHandleId.putAll(requestBody); + return ResponseEntity.noContent().build(); + } + + /** + * It contains a method to delete an entry from the moduleSetTagPerCmHandleId map. + * The method takes a cmHandleId as a parameter and removes the corresponding entry from the map. + * + * @return a ResponseEntity containing the updated map. + */ + @DeleteMapping("/v1/tagMapping/ch/{cmHandleId}") + public ResponseEntity deleteTagMappingByCmHandleId(@PathVariable final String cmHandleId) { + moduleSetTagPerCmHandleId.remove(cmHandleId); + return ResponseEntity.ok(String.format("Mapping of %s is deleted successfully", cmHandleId)); + } /** * Get all modules for given cm handle. @@ -224,17 +287,18 @@ public class DmiRestStubController { } private String getModuleResourceResponse(final String cmHandleId, final String moduleResponseType) { - final String nodeType = cmHandleId.split("-")[0]; - final String moduleResponseFilePath = String.format("module/%s%s", nodeType, moduleResponseType); + if (moduleSetTagPerCmHandleId.isEmpty()) { + log.info("Using default module responses of type ietfYang"); + return ResourceFileReaderUtil.getResourceFileContent(applicationContext.getResource( + ResourceLoader.CLASSPATH_URL_PREFIX + + String.format("module/ietfYang-%s", moduleResponseType))); + } + final String moduleSetTag = moduleSetTagPerCmHandleId.getOrDefault(cmHandleId, DEFAULT_TAG); + final String moduleResponseFilePath = String.format("module/%s-%s", moduleSetTag, moduleResponseType); final Resource moduleResponseResource = applicationContext.getResource( ResourceLoader.CLASSPATH_URL_PREFIX + moduleResponseFilePath); - if (moduleResponseResource.exists()) { - log.info("Using requested node type: {}", nodeType); - return ResourceFileReaderUtil.getResourceFileContent(moduleResponseResource); - } - log.info("Using default node type: ietfYang"); - return ResourceFileReaderUtil.getResourceFileContent(applicationContext.getResource( - ResourceLoader.CLASSPATH_URL_PREFIX + "module/ietfYang" + moduleResponseType)); + log.info("Using module responses from : {}", moduleResponseFilePath); + return ResourceFileReaderUtil.getResourceFileContent(moduleResponseResource); } private void delay(final long milliseconds) { -- cgit 1.2.3-korg