diff options
author | Modaboina, Kusumakumari (km583p) <km583p@att.com> | 2019-12-11 13:59:04 -0500 |
---|---|---|
committer | Takamune Cho <takamune.cho@att.com> | 2020-01-10 22:29:35 +0000 |
commit | 70d72d23f7ec9d221c8f44d9e66c177d71f79551 (patch) | |
tree | d432f8f9dfc3f4d051b1311c92ade01d756f1617 /appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org | |
parent | 933a4b3ba8190fad9cf3a97d3a72b8ac00eaa69e (diff) |
ansible chnages for adding configdata and bugfixes
Change-Id: Ide9528baa569815198b18951af7f355c7c7e54d8
Issue-ID: APPC-1786
Signed-off-by: Modaboina, Kusumakumari (km583p) <km583p@att.com>
Diffstat (limited to 'appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org')
3 files changed, 95 insertions, 14 deletions
diff --git a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/impl/AnsibleAdapterImpl.java b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/impl/AnsibleAdapterImpl.java index 576a576c4..4c59b4e18 100644 --- a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/impl/AnsibleAdapterImpl.java +++ b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/impl/AnsibleAdapterImpl.java @@ -520,6 +520,8 @@ public class AnsibleAdapterImpl implements AnsibleAdapter { String message = StringUtils.EMPTY; String results = StringUtils.EMPTY; String output = StringUtils.EMPTY; + String configData = StringUtils.EMPTY; + String finalResponse = StringUtils.EMPTY; try { // Try to retrieve the test results (modify the URL for that) AnsibleResult testResult = queryServer(reqUri, params.get("User"), @@ -535,10 +537,19 @@ public class AnsibleAdapterImpl implements AnsibleAdapter { message = testResult.getStatusMessage(); results = testResult.getResults(); output = testResult.getOutput(); - ctx.setAttribute(OUTPUT_ATTRIBUTE_NAME, output); + configData = testResult.getconfigData(); + if ((StringUtils.isBlank(output) ) ||(output.trim().equalsIgnoreCase("{}"))) { + finalResponse = results; + } else { + finalResponse = output; + } + logger.info("configData from ansible's response = " + configData); + ctx.setAttribute("device-running-config", configData); } logger.info("Request response = " + message); } catch (APPCException e) { + ctx.setAttribute(RESULTS_ATTRIBUTE_NAME, results); + ctx.setAttribute(OUTPUT_ATTRIBUTE_NAME, finalResponse); doFailure(ctx, AnsibleResultCodes.UNKNOWN_EXCEPTION.getValue(), "Exception encountered retrieving result : " + e.getMessage()); return; @@ -554,16 +565,16 @@ public class AnsibleAdapterImpl implements AnsibleAdapter { logger.info(String.format("Ansible Request %s finished with Result %s, Message = %s", params.get("Id"), OUTCOME_FAILURE, message)); ctx.setAttribute(RESULTS_ATTRIBUTE_NAME, results); - ctx.setAttribute(OUTPUT_ATTRIBUTE_NAME, output); + ctx.setAttribute(OUTPUT_ATTRIBUTE_NAME, finalResponse); doFailure(ctx, code, message); return; } // In case of 200,400,FINISHED return 400 - ctx.setAttribute(RESULT_CODE_ATTRIBUTE_NAME, "400"); + ctx.setAttribute(RESULT_CODE_ATTRIBUTE_NAME, Integer.toString(400)); ctx.setAttribute(MESSAGE_ATTRIBUTE_NAME, message); ctx.setAttribute(RESULTS_ATTRIBUTE_NAME, results); - ctx.setAttribute(OUTPUT_ATTRIBUTE_NAME, output); + ctx.setAttribute(OUTPUT_ATTRIBUTE_NAME, finalResponse); ctx.setStatus(OUTCOME_SUCCESS); } 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 ba6577328..fc38b550a 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 @@ -68,6 +68,7 @@ public class AnsibleMessageParser { private static final String TIMEOUT_OPT_KEY = "Timeout"; private static final String VERSION_OPT_KEY = "Version"; private static final String INVENTORY_NAMES_OPT_KEY = "InventoryNames"; + private static final String EXTRAVARS_OPT_KEY ="ExtraVars"; private static final String ACTION_OPT_KEY = "Action"; private static final String OUTPUT_OPT_KEY = "Output"; private static final String JSON_ERROR_MESSAGE = "JSONException: Error parsing response"; @@ -217,7 +218,7 @@ public class AnsibleMessageParser { int codeStatus = postRsp.getInt(STATUS_CODE_KEY); String messageStatus = postRsp.getString(STATUS_MESSAGE_KEY); int finalCode = AnsibleResultCodes.FINAL_SUCCESS.getValue(); - + JSONObject config = null; boolean valCode = AnsibleResultCodes.CODE.checkValidCode(AnsibleResultCodes.FINALRESPONSE.getValue(), codeStatus); @@ -228,6 +229,7 @@ public class AnsibleMessageParser { ansibleResult.setStatusCode(codeStatus); ansibleResult.setStatusMessage(messageStatus); + ansibleResult.setconfigData("UNKNOWN"); LOGGER.info("Received response with code = {}, Message = {}", codeStatus, messageStatus); if (!postRsp.isNull("Results")) { @@ -255,6 +257,17 @@ public class AnsibleMessageParser { if (subCode != 200 || !(("SUCCESS").equals(message))) { finalCode = AnsibleResultCodes.REQ_FAILURE.getValue(); } + if ((hostResponse.optJSONObject("Output")) != null) { + if ((hostResponse.optJSONObject("Output").optJSONObject("info")) != null) { + if ((hostResponse.optJSONObject("Output").optJSONObject("info") + .optJSONObject("configData")) != null) { + config = hostResponse.optJSONObject("Output").optJSONObject("info") + .optJSONObject("configData"); + + ansibleResult.setconfigData(config.toString()); + } + } + } } catch (JSONException e) { LOGGER.error(JSON_ERROR_MESSAGE, e); ansibleResult.setStatusCode(AnsibleResultCodes.INVALID_RESPONSE.getValue()); @@ -300,6 +313,8 @@ public class AnsibleMessageParser { switch (key) { case TIMEOUT_OPT_KEY: + if (dataIsVariable(payload)) + break; int timeout = Integer.parseInt(payload); if (timeout < 0) { throw new NumberFormatException(" : specified negative integer for timeout = " + payload); @@ -314,22 +329,28 @@ public class AnsibleMessageParser { } break; case VERSION_OPT_KEY: + if (dataIsVariable(payload)) + break; case INVENTORY_NAMES_OPT_KEY: jsonPayload.put(key, payload); break; case LOCAL_PARAMETERS_OPT_KEY: case ENV_PARAMETERS_OPT_KEY: - JSONObject paramsJson = new JSONObject(payload); - jsonPayload.put(key, paramsJson); - break; - + case EXTRAVARS_OPT_KEY: + JSONObject paramsJson = new JSONObject(payload); + jsonDataIsVariable(paramsJson); + jsonPayload.put(key, paramsJson); + break; case NODE_LIST_OPT_KEY: - JSONArray paramsArray = new JSONArray(payload); - jsonPayload.put(key, paramsArray); - break; - - case FILE_PARAMETERS_OPT_KEY: + if(payload.startsWith("$")) + break; + JSONArray paramsArray = new JSONArray(payload); + jsonPayload.put(key, paramsArray); + break; + case FILE_PARAMETERS_OPT_KEY: + if (dataIsVariable(payload)) + break; jsonPayload.put(key, getFilePayload(payload)); break; @@ -357,5 +378,45 @@ public class AnsibleMessageParser { "Ansible: Mandatory AnsibleAdapter key %s not found in parameters provided by calling agent !", key)); } + + if (StringUtils.startsWith(params.get(key), "$")) { + throw new APPCException(String.format( + "Ansible: Mandatory AnsibleAdapter key %s is a variable", + key)); + } + } + + + private boolean varObjContainsNoData(Object obj) { + if (obj instanceof String) { + if (StringUtils.startsWith(obj.toString(), "$") || StringUtils.isEmpty(obj.toString())) + return true; + } + return false; + + } + + private boolean dataIsVariable(String payload) { + if (StringUtils.startsWith(payload, "$") || StringUtils.isEmpty(payload)) + return true; + else + return false; + + } + + private JSONObject jsonDataIsVariable(JSONObject paramsJson) { + LOGGER.info("input json is " + paramsJson); + String[] keys = JSONObject.getNames(paramsJson); + for (String k : keys) { + Object a = paramsJson.get(k); + if (a instanceof String) { + if (StringUtils.startsWith(a.toString(), "$") || StringUtils.isEmpty(a.toString())) { + LOGGER.info("removing key " + k); + paramsJson.remove(k); + } + } + } + LOGGER.info("returning json as " + paramsJson); + return paramsJson; } } diff --git a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleResult.java b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleResult.java index b67f3c74b..9ebc63fd6 100644 --- a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleResult.java +++ b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleResult.java @@ -35,6 +35,7 @@ public class AnsibleResult { private String results; private String output; private String serverIp; + private String configData; public AnsibleResult() { this(-1, EMPTY_VALUE, EMPTY_VALUE); @@ -104,6 +105,14 @@ public class AnsibleResult { public String getServerIp() { return this.serverIp; } + + public String getconfigData() { + return this.configData; + } + + public void setconfigData(String configData) { + this.configData = configData; + } } |