Add missing GetOutput method in ansible-adapter 70/85270/2
authorEnbo Wang <wangenbo@huawei.com>
Sun, 14 Apr 2019 15:51:33 +0000 (23:51 +0800)
committerEnbo Wang <wangenbo@huawei.com>
Mon, 15 Apr 2019 13:47:54 +0000 (21:47 +0800)
Add missing GetOutput method in ansible-adapter for ansible-server.

Change-Id: Ifc544176b86fc033f2760a6b02d18f6e29bfd88a
Issue-ID: CCSDK-1225
Signed-off-by: Enbo Wang <wangenbo@huawei.com>
ansible-adapter/ansible-adapter-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/ansible/AnsibleAdapter.java
ansible-adapter/ansible-adapter-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/ansible/impl/AnsibleAdapterImpl.java
ansible-adapter/ansible-adapter-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/ansible/model/AnsibleMessageParser.java
ansible-adapter/ansible-adapter-bundle/src/test/java/org/onap/ccsdk/adapter/ansible/impl/TestAnsibleAdapterImpl.java

index a2d537e..e43d3e3 100644 (file)
@@ -49,4 +49,7 @@ public interface AnsibleAdapter extends SvcLogicJavaPlugin {
 
     /* Method to get log of a playbook execution request */
     void reqExecLog(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException;
+
+    /* Method to get output of a playbook execution request */
+    void reqExecOutput(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException;
 }
index 79103db..2361fee 100644 (file)
@@ -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<String, String> 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
      */
index 427149c..5f6342d 100644 (file)
@@ -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<String, String> 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.
index 86bed7e..4636d24 100644 (file)
@@ -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 ");
+        }
+    }
 }