From 9c2a9c760f86793cdcd7aad7dc90ce77d48f1487 Mon Sep 17 00:00:00 2001 From: shudhansu-shekhar <131247852+shudhansu-shekhar@users.noreply.github.com> Date: Tue, 5 Nov 2024 18:40:48 +0530 Subject: [PATCH] Improve efficiency how result of event generation is handled (#233) * Modify /generate end-point to work for Gen1 templates * Improve efficiency how result of event generation is handled --- CHANGELOG.md | 3 ++ pom.xml | 2 +- .../controller/RemremGenerateController.java | 37 +++++++------------ 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 600fd80..c7b37e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 2.1.14 +- Made changes to /generate end-point to improve efficiency how result of event generation is handled + ## 2.1.13 - Made changes to /generate end-point to work for Gen1 templates diff --git a/pom.xml b/pom.xml index ba300a5..76c4add 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ - 2.1.13 + 2.1.14 2.2.7 eiffel-remrem-generate diff --git a/service/src/main/java/com/ericsson/eiffel/remrem/generate/controller/RemremGenerateController.java b/service/src/main/java/com/ericsson/eiffel/remrem/generate/controller/RemremGenerateController.java index a416319..f0da45c 100644 --- a/service/src/main/java/com/ericsson/eiffel/remrem/generate/controller/RemremGenerateController.java +++ b/service/src/main/java/com/ericsson/eiffel/remrem/generate/controller/RemremGenerateController.java @@ -21,7 +21,6 @@ import com.ericsson.eiffel.remrem.protocol.MsgService; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.*; @@ -33,7 +32,6 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.PropertySource; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.RequestEntity; @@ -173,9 +171,9 @@ public ResponseEntity generate(final String msgProtocol, final String msgType int failedCount = 0; for (JsonElement element : inputEventJsonArray) { try { - JsonObject generatedEvent = (processEvent(msgProtocol, msgType, + JsonObject generatedEvent = generateEvent(msgProtocol, msgType, failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit, - okToLeaveOutInvalidOptionalFields, element.getAsJsonObject())); + okToLeaveOutInvalidOptionalFields, element.getAsJsonObject()); generatedEventResults.add(generatedEvent); successCount++; } catch (ProtocolHandlerNotFoundException e) { @@ -202,20 +200,9 @@ public ResponseEntity generate(final String msgProtocol, final String msgType } else if (inputData.isJsonObject()) { JsonObject inputJsonObject = inputData.getAsJsonObject(); - JsonObject processedJson = processEvent(msgProtocol, msgType, failIfMultipleFound, failIfNoneFound, + JsonObject processedJson = generateEvent(msgProtocol, msgType, failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit, okToLeaveOutInvalidOptionalFields, inputJsonObject); - if (!processedJson.has(JSON_STATUS_CODE)) { - HttpStatus status = HttpStatus.OK; - return new ResponseEntity<>(processedJson, status); - } else if (processedJson.has(JSON_STATUS_CODE)) { - String statusValue = processedJson.get(JSON_STATUS_CODE).toString(); - HttpStatus status = HttpStatus.resolve(Integer.parseInt(statusValue)); - return new ResponseEntity<>(processedJson, status); - } else { - String errorMessage = "There is no status value in the response " + processedJson; - log.error(errorMessage); - return createResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR, errorMessage, JSON_ERROR_STATUS); - } + return new ResponseEntity<>(processedJson, HttpStatus.OK); } else { return createResponseEntity(HttpStatus.BAD_REQUEST, "Invalid JSON format,expected either single template or array of templates", @@ -270,6 +257,10 @@ public void initializeResponse(HttpStatus status, String resultMessage, String e */ private ResponseEntity handleException(Exception e) { String exceptionMessage = e.getMessage(); + if (e instanceof ProtocolHandlerNotFoundException) { + return createResponseEntity(HttpStatus.SERVICE_UNAVAILABLE, exceptionMessage, JSON_ERROR_STATUS); + } + if (e instanceof REMGenerateException) { List statusList = List.of( HttpStatus.NOT_ACCEPTABLE, HttpStatus.EXPECTATION_FAILED, HttpStatus.SERVICE_UNAVAILABLE, @@ -302,17 +293,16 @@ private ResponseEntity handleException(Exception e) { * @param jsonObject The content of the message which is used in creating the event details. * @return JsonObject generated event */ - public JsonObject processEvent(String msgProtocol, String msgType, Boolean failIfMultipleFound, - Boolean failIfNoneFound, Boolean lookupInExternalERs, int lookupLimit, - Boolean okToLeaveOutInvalidOptionalFields, JsonObject jsonObject) throws REMGenerateException, JsonSyntaxException { + public JsonObject generateEvent(String msgProtocol, String msgType, Boolean failIfMultipleFound, + Boolean failIfNoneFound, Boolean lookupInExternalERs, int lookupLimit, + Boolean okToLeaveOutInvalidOptionalFields, JsonObject jsonObject) throws REMGenerateException, JsonSyntaxException { JsonElement parsedResponse; JsonObject event = erLookup(jsonObject, failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit); MsgService msgService = getMessageService(msgProtocol); if (msgService == null) { - return createResponseEntity(HttpStatus.SERVICE_UNAVAILABLE, - "No protocol service has been found registered", JSON_ERROR_STATUS).getBody(); + throw new ProtocolHandlerNotFoundException("Handler of Eiffel protocol '" + msgProtocol + "' not found"); } String response = msgService.generateMsg(msgType, event, isLenientEnabled(okToLeaveOutInvalidOptionalFields)); parsedResponse = JsonParser.parseString(response); @@ -320,9 +310,8 @@ public JsonObject processEvent(String msgProtocol, String msgType, Boolean failI if (parsedJson.has(JSON_ERROR_MESSAGE_FIELD)) { throw new REMGenerateException(response); - } else { - return parsedJson; } + return parsedJson; } private JsonObject erLookup(final JsonObject bodyJson, Boolean failIfMultipleFound, Boolean failIfNoneFound,