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 | |
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>
5 files changed, 135 insertions, 20 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; + } } diff --git a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/test/java/org/onap/appc/adapter/ansible/impl/TestAnsibleAdapterImpl.java b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/test/java/org/onap/appc/adapter/ansible/impl/TestAnsibleAdapterImpl.java index 02048aee3..8e13a66ea 100644 --- a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/test/java/org/onap/appc/adapter/ansible/impl/TestAnsibleAdapterImpl.java +++ b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/test/java/org/onap/appc/adapter/ansible/impl/TestAnsibleAdapterImpl.java @@ -60,8 +60,8 @@ public class TestAnsibleAdapterImpl { private static final String STATUS_CODE = "StatusCode"; private static final String STATUS_MESSAGE = "StatusMessage"; private static final String PENDING = "100"; - private static final String SUCCESS = "200"; - + private static final String SUCCESS = "SUCCESS"; + private static final String MESSAGE_ATTRIBUTE_NAME = "SUCCESS"; private AnsibleAdapterImpl adapter; private boolean testMode = true; private Map<String, String> params; @@ -109,6 +109,7 @@ public class TestAnsibleAdapterImpl { result = new AnsibleResult(); result.setStatusMessage("Success"); result.setResults("Success"); + result.setOutput("{}"); Whitebox.setInternalState(adapter, "messageProcessor", messageProcessor); spyAdapter = Mockito.spy(adapter); } @@ -150,16 +151,15 @@ public class TestAnsibleAdapterImpl { * @throws APPCException * If the request cannot be processed for some reason */ - @Test + @Test(expected = SvcLogicException.class) public void reqExecResult_shouldSetSuccess() throws SvcLogicException, APPCException { params.put("Id", "100"); - result.setStatusCode(Integer.valueOf(SUCCESS)); + result.setStatusMessage(SUCCESS); when(messageProcessor.reqUriResult(params)).thenReturn(agentUrl); when(messageProcessor.parseGetResponse(anyString())).thenReturn(result); spyAdapter.reqExecResult(params, svcContext); - assertEquals("400", svcContext.getAttribute(RESULT_CODE_ATTRIBUTE_NAME)); + assertEquals(SUCCESS, svcContext.getAttribute(MESSAGE_ATTRIBUTE_NAME)); } - /** * This test case is used to test the Failure of the request * @@ -174,6 +174,10 @@ public class TestAnsibleAdapterImpl { params.put("Id", "100"); result.setStatusCode(100); result.setStatusMessage("Failed"); + JSONObject cData = new JSONObject(); + cData.put("GatewayInfo", "Radius"); + result.setconfigData(cData.toString()); + result.setOutput(cData.toString()); when(messageProcessor.reqUriResult(params)).thenReturn(agentUrl); when(messageProcessor.parseGetResponse(anyString())).thenReturn(result); adapter.reqExecResult(params, svcContext); diff --git a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/test/java/org/onap/appc/adapter/ansible/model/TestAnsibleMessageParser.java b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/test/java/org/onap/appc/adapter/ansible/model/TestAnsibleMessageParser.java index 6d2e7bbf1..90bff6451 100644 --- a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/test/java/org/onap/appc/adapter/ansible/model/TestAnsibleMessageParser.java +++ b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/test/java/org/onap/appc/adapter/ansible/model/TestAnsibleMessageParser.java @@ -165,8 +165,10 @@ public class TestAnsibleMessageParser { params.put("Password", "TestPassword"); params.put("Timeout", "3"); params.put("Version", "1"); + params.put("InventoryNames", "VNFC"); JSONObject jObject = msgParser.reqMessage(params); assertEquals("1", jObject.get("Version")); + assertEquals("VNFC",jObject.get("InventoryNames")); } @Test @@ -183,6 +185,34 @@ public class TestAnsibleMessageParser { assertEquals("TestUser",result.get("User")); assertEquals("TestPassword",result.get("Password")); } + + @Test + public void TestParseGetConfigResponseResult() throws Exception { + AnsibleResult ansibleResult; + String input = "{\"StatusCode\":\"200\",\"StatusMessage\":\"TestMessage\",\"Results\":{\"host\":{\"StatusCode\":\"200\",\"StatusMessage\":\"SUCCESS\",\"Output\":{\"info\":{\"configData\":{\"abc\":\"TestOutPutResult\",\"rtr\":\"vfc\"}}}}}}"; + ansibleResult = msgParser.parseGetResponse(input); + String result = "abc"; + assertEquals(true, ansibleResult.getconfigData().contains(result)); + } + + @Test + public void testParseOptionalParamTest2() throws Exception { + + Map<String, String> params = new HashMap<String, String>(); + params.put("AgentUrl", "TestAgentUrl"); + params.put("PlaybookName", "TestPlaybookName"); + params.put("User", "TestUser"); + params.put("Password", "TestPassword"); + //params.put("Timeout", "3"); + params.put("Version", "1"); + params.put("InventoryNames", "VNFC"); + params.put("Timeout", "4"); + params.put("EnvParameters", "{ \"userID\": \"$0002\", \"vnf-type\" : \"\", \"vnf\" : \"abc\" }"); + params.put("NodeList", "${Nodelist}"); + JSONObject jObject = msgParser.reqMessage(params); + assertEquals("1", jObject.get("Version")); + assertEquals("4",jObject.get("Timeout")); + } @Test public void testReqUriResultWithIPs() throws Exception { |