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"),
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;
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);
}
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";
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);
ansibleResult.setStatusCode(codeStatus);
ansibleResult.setStatusMessage(messageStatus);
+ ansibleResult.setconfigData("UNKNOWN");
LOGGER.info("Received response with code = {}, Message = {}", codeStatus, messageStatus);
if (!postRsp.isNull("Results")) {
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());
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);
}
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;
"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;
}
}
private String results;
private String output;
private String serverIp;
+ private String configData;
public AnsibleResult() {
this(-1, EMPTY_VALUE, EMPTY_VALUE);
public String getServerIp() {
return this.serverIp;
}
+
+ public String getconfigData() {
+ return this.configData;
+ }
+
+ public void setconfigData(String configData) {
+ this.configData = configData;
+ }
}
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;
result = new AnsibleResult();
result.setStatusMessage("Success");
result.setResults("Success");
+ result.setOutput("{}");
Whitebox.setInternalState(adapter, "messageProcessor", messageProcessor);
spyAdapter = Mockito.spy(adapter);
}
* @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
*
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);
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
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 {