From c06bd49c6a58ef995943f8b4236f84e1cec1f79c Mon Sep 17 00:00:00 2001 From: Enbo Wang Date: Sun, 14 Apr 2019 23:51:33 +0800 Subject: Add missing GetOutput method in ansible-adapter Add missing GetOutput method in ansible-adapter for ansible-server. Change-Id: Ifc544176b86fc033f2760a6b02d18f6e29bfd88a Issue-ID: CCSDK-1225 Signed-off-by: Enbo Wang --- .../ccsdk/sli/adaptors/ansible/AnsibleAdapter.java | 3 ++ .../adaptors/ansible/impl/AnsibleAdapterImpl.java | 34 ++++++++++++++++++++++ .../ansible/model/AnsibleMessageParser.java | 15 ++++++++++ .../ansible/impl/TestAnsibleAdapterImpl.java | 17 +++++++++++ 4 files changed, 69 insertions(+) (limited to 'ansible-adapter') diff --git a/ansible-adapter/ansible-adapter-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/ansible/AnsibleAdapter.java b/ansible-adapter/ansible-adapter-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/ansible/AnsibleAdapter.java index a2d537e5..e43d3e3c 100644 --- a/ansible-adapter/ansible-adapter-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/ansible/AnsibleAdapter.java +++ b/ansible-adapter/ansible-adapter-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/ansible/AnsibleAdapter.java @@ -49,4 +49,7 @@ public interface AnsibleAdapter extends SvcLogicJavaPlugin { /* Method to get log of a playbook execution request */ void reqExecLog(Map params, SvcLogicContext ctx) throws SvcLogicException; + + /* Method to get output of a playbook execution request */ + void reqExecOutput(Map params, SvcLogicContext ctx) throws SvcLogicException; } diff --git a/ansible-adapter/ansible-adapter-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/ansible/impl/AnsibleAdapterImpl.java b/ansible-adapter/ansible-adapter-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/ansible/impl/AnsibleAdapterImpl.java index 79103dbb..2361feeb 100644 --- a/ansible-adapter/ansible-adapter-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/ansible/impl/AnsibleAdapterImpl.java +++ b/ansible-adapter/ansible-adapter-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/ansible/impl/AnsibleAdapterImpl.java @@ -76,6 +76,7 @@ public class AnsibleAdapterImpl implements AnsibleAdapter { private static final String RESULTS_ATTRIBUTE_NAME = "org.onap.appc.adapter.ansible.results"; private static final String ID_ATTRIBUTE_NAME = "org.onap.appc.adapter.ansible.Id"; private static final String LOG_ATTRIBUTE_NAME = "org.onap.appc.adapter.ansible.log"; + private static final String OUTPUT_ATTRIBUTE_NAME = "org.onap.appc.adapter.ansible.output"; private static final String CLIENT_TYPE_PROPERTY_NAME = "org.onap.appc.adapter.ansible.clientType"; private static final String TRUSTSTORE_PROPERTY_NAME = "org.onap.appc.adapter.ansible.trustStore"; @@ -390,6 +391,39 @@ public class AnsibleAdapterImpl implements AnsibleAdapter { } } + /** + * Public method to get output from playbook execution for a specific request + * + * It blocks till the Ansible Server responds or the session times out very similar to + * reqExecResult and output is returned in the DG context variable org.onap.appc.adapter.ansible.output + */ + @Override + public void reqExecOutput(Map params, SvcLogicContext ctx) throws SvcLogicException { + + String reqUri = StringUtils.EMPTY; + try { + reqUri = messageProcessor.reqUriOutput(params); + logger.info("Retrieving results from " + reqUri); + } catch (Exception e) { + logger.error("Exception caught", e); + doFailure(ctx, AnsibleResultCodes.INVALID_PAYLOAD.getValue(), e.getMessage()); + } + + String message = StringUtils.EMPTY; + try { + // Try to retrieve the test results (modify the url for that) + AnsibleResult testResult = queryServer(reqUri, params.get("User"), params.get(PASSD)); + message = testResult.getStatusMessage(); + logger.info("Request output = " + message); + ctx.setAttribute(OUTPUT_ATTRIBUTE_NAME, message); + ctx.setStatus(OUTCOME_SUCCESS); + } catch (Exception e) { + logger.error("Exception caught", e); + doFailure(ctx, AnsibleResultCodes.UNKNOWN_EXCEPTION.getValue(), + "Exception encountered retreiving output : " + e.getMessage()); + } + } + /** * Method that posts the request */ diff --git a/ansible-adapter/ansible-adapter-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/ansible/model/AnsibleMessageParser.java b/ansible-adapter/ansible-adapter-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/ansible/model/AnsibleMessageParser.java index 427149cc..5f6342d9 100644 --- a/ansible-adapter/ansible-adapter-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/ansible/model/AnsibleMessageParser.java +++ b/ansible-adapter/ansible-adapter-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/ansible/model/AnsibleMessageParser.java @@ -132,6 +132,21 @@ public class AnsibleMessageParser { return params.get(AGENT_URL_KEY) + "?Id=" + params.get(ID_KEY) + "&Type=GetLog"; } + /** + * Method that validates that the Map has enough information + * to query Ansible server for an output. If so, it returns + * the appropriate url, else an empty string. + */ + public String reqUriOutput(Map params) throws SvcLogicException { + + final String[] mandatoryTestParams = {AGENT_URL_KEY, ID_KEY, USER_KEY, PASS_KEY}; + + for (String mandatoryParam : mandatoryTestParams) { + throwIfMissingMandatoryParam(params, mandatoryParam); + } + return params.get(AGENT_URL_KEY) + "?Id=" + params.get(ID_KEY) + "&Type=GetOutput"; + } + /** * This method parses response from the Ansible Server when we do a post * and returns an AnsibleResult object. diff --git a/ansible-adapter/ansible-adapter-bundle/src/test/java/org/onap/ccsdk/adapter/ansible/impl/TestAnsibleAdapterImpl.java b/ansible-adapter/ansible-adapter-bundle/src/test/java/org/onap/ccsdk/adapter/ansible/impl/TestAnsibleAdapterImpl.java index 86bed7e4..4636d245 100644 --- a/ansible-adapter/ansible-adapter-bundle/src/test/java/org/onap/ccsdk/adapter/ansible/impl/TestAnsibleAdapterImpl.java +++ b/ansible-adapter/ansible-adapter-bundle/src/test/java/org/onap/ccsdk/adapter/ansible/impl/TestAnsibleAdapterImpl.java @@ -127,4 +127,21 @@ public class TestAnsibleAdapterImpl { fail(e.getMessage() + " Unknown exception encountered "); } } + + @Test + public void reqExecOutput_shouldSetMessage() throws IllegalStateException, IllegalArgumentException { + + params.put("Id", "101"); + + try { + adapter.reqExecOutput(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.ansible.output"); + assertEquals(message, status); + } catch (SvcLogicException e) { + String status = svcContext.getAttribute("org.onap.appc.adapter.ansible.output"); + fail(e.getMessage() + " Code = " + status); + } catch (Exception e) { + fail(e.getMessage() + " Unknown exception encountered "); + } + } } -- cgit 1.2.3-korg