From cc26209ff5fbe036caf0dde162ff6079bd58bd9d Mon Sep 17 00:00:00 2001 From: Taka Date: Sat, 10 Feb 2018 15:48:24 -0500 Subject: refactor AnsibleMessageParser for nesting try Change-Id: Idd86134a034d5f49baff34fd0d056820f45e1121 Issue-ID: APPC-596 Signed-off-by: Taka --- .../ansible/model/AnsibleMessageParser.java | 115 +++++++++++---------- 1 file changed, 58 insertions(+), 57 deletions(-) (limited to 'appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main') diff --git a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleMessageParser.java b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleMessageParser.java index c11966860..0e19384e6 100644 --- a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleMessageParser.java +++ b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleMessageParser.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP : APPC * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs * ============================================================================= @@ -178,76 +178,77 @@ public class AnsibleMessageParser { public AnsibleResult parseGetResponse(String input) throws APPCException { AnsibleResult ansibleResult = new AnsibleResult(); - int finalCode = AnsibleResultCodes.FINAL_SUCCESS.getValue(); try { JSONObject postResponse = new JSONObject(input); + ansibleResult = parseGetResponseNested(ansibleResult, postResponse); + } catch (JSONException e) { + ansibleResult = new AnsibleResult(AnsibleResultCodes.INVALID_PAYLOAD.getValue(), + "Error parsing response = " + input + ". Error = " + e.getMessage(), ""); + } + return ansibleResult; + } - int codeStatus = postResponse.getInt(STATUS_CODE_KEY); - String messageStatus = postResponse.getString(STATUS_MESSAGE_KEY); + private AnsibleResult parseGetResponseNested(AnsibleResult ansibleResult, JSONObject postRsp) throws APPCException { - boolean valCode = - AnsibleResultCodes.CODE.checkValidCode(AnsibleResultCodes.FINALRESPONSE.getValue(), codeStatus); + int codeStatus = postRsp.getInt(STATUS_CODE_KEY); + String messageStatus = postRsp.getString(STATUS_MESSAGE_KEY); + int finalCode = AnsibleResultCodes.FINAL_SUCCESS.getValue(); - if (!valCode) { - throw new APPCException("Invalid FinalResponse code = " + codeStatus + " received. MUST be one of " - + AnsibleResultCodes.CODE.getValidCodes(AnsibleResultCodes.FINALRESPONSE.getValue())); - } + boolean valCode = + AnsibleResultCodes.CODE.checkValidCode(AnsibleResultCodes.FINALRESPONSE.getValue(), codeStatus); - ansibleResult.setStatusCode(codeStatus); - ansibleResult.setStatusMessage(messageStatus); - LOGGER.info("Received response with code = {}, Message = {}", codeStatus, messageStatus); - - if (!postResponse.isNull("Results")) { - - // Results are available. process them - // Results is a dictionary of the form - // {host :{status:s, group:g, message:m, hostname:h}, ...} - LOGGER.info("Processing results in response"); - JSONObject results = postResponse.getJSONObject("Results"); - LOGGER.info("Get JSON dictionary from Results .."); - Iterator hosts = results.keys(); - LOGGER.info("Iterating through hosts"); - - while (hosts.hasNext()) { - String host = hosts.next(); - LOGGER.info("Processing host = {}", host); - - try { - JSONObject hostResponse = results.getJSONObject(host); - int subCode = hostResponse.getInt(STATUS_CODE_KEY); - String message = hostResponse.getString(STATUS_MESSAGE_KEY); - - LOGGER.info("Code = {}, Message = {}", subCode, message); - - if (subCode != 200 || !message.equals("SUCCESS")) { - finalCode = AnsibleResultCodes.REQ_FAILURE.getValue(); - } - } catch (JSONException e) { - ansibleResult.setStatusCode(AnsibleResultCodes.INVALID_RESPONSE.getValue()); - ansibleResult.setStatusMessage(String.format( - "Error processing response message = %s from host %s", results.getString(host), host)); - break; - } - } + if (!valCode) { + throw new APPCException("Invalid FinalResponse code = " + codeStatus + " received. MUST be one of " + + AnsibleResultCodes.CODE.getValidCodes(AnsibleResultCodes.FINALRESPONSE.getValue())); + } - ansibleResult.setStatusCode(finalCode); + ansibleResult.setStatusCode(codeStatus); + ansibleResult.setStatusMessage(messageStatus); + LOGGER.info("Received response with code = {}, Message = {}", codeStatus, messageStatus); - // We return entire Results object as message - ansibleResult.setResults(results.toString()); + if (!postRsp.isNull("Results")) { - } else { - ansibleResult.setStatusCode(AnsibleResultCodes.INVALID_RESPONSE.getValue()); - ansibleResult.setStatusMessage("Results not found in GET for response"); - } + // Results are available. process them + // Results is a dictionary of the form + // {host :{status:s, group:g, message:m, hostname:h}, ...} + LOGGER.info("Processing results in response"); + JSONObject results = postRsp.getJSONObject("Results"); + LOGGER.info("Get JSON dictionary from Results .."); + Iterator hosts = results.keys(); + LOGGER.info("Iterating through hosts"); + while (hosts.hasNext()) { + String host = hosts.next(); + LOGGER.info("Processing host = {}", host); - } catch (JSONException e) { - ansibleResult = new AnsibleResult(AnsibleResultCodes.INVALID_PAYLOAD.getValue(), - "Error parsing response = " + input + ". Error = " + e.getMessage(), ""); - } + try { + JSONObject hostResponse = results.getJSONObject(host); + int subCode = hostResponse.getInt(STATUS_CODE_KEY); + String message = hostResponse.getString(STATUS_MESSAGE_KEY); + + LOGGER.info("Code = {}, Message = {}", subCode, message); + if (subCode != 200 || !message.equals("SUCCESS")) { + finalCode = AnsibleResultCodes.REQ_FAILURE.getValue(); + } + } catch (JSONException e) { + ansibleResult.setStatusCode(AnsibleResultCodes.INVALID_RESPONSE.getValue()); + ansibleResult.setStatusMessage(String.format( + "Error processing response message = %s from host %s", results.getString(host), host)); + break; + } + } + ansibleResult.setStatusCode(finalCode); + + // We return entire Results object as message + ansibleResult.setResults(results.toString()); + + } else { + ansibleResult.setStatusCode(AnsibleResultCodes.INVALID_RESPONSE.getValue()); + ansibleResult.setStatusMessage("Results not found in GET for response"); + } return ansibleResult; } -- cgit 1.2.3-korg