Added logger
[appc.git] / appc-adapters / appc-ansible-adapter / appc-ansible-adapter-bundle / src / main / java / org / onap / appc / adapter / ansible / impl / AnsibleAdapterImpl.java
index fafe3de..1314480 100644 (file)
@@ -6,6 +6,8 @@
  * ================================================================================
  * Copyright (C) 2017 Amdocs
  * =============================================================================
+ * Modifications Copyright (C) 2019 IBM
+ * =============================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
@@ -186,6 +188,7 @@ public class AnsibleAdapterImpl implements AnsibleAdapter {
 
         } catch (Exception e) {
             defaultTimeout = 600 * 1000;
+            logger.error("Error while reading time out property" , e);
         }
         //continuing for checking timeout
         try {
@@ -194,6 +197,7 @@ public class AnsibleAdapterImpl implements AnsibleAdapter {
 
         } catch (Exception e) {
             defaultSocketTimeout = 60 * 1000;
+            logger.error("Error while reading socket time out property" , e);
         }
         //continuing for checking timeout
         try {
@@ -202,6 +206,7 @@ public class AnsibleAdapterImpl implements AnsibleAdapter {
 
         } catch (Exception e) {
             defaultPollInterval = 60 * 1000;
+            logger.error("Error while reading poll interval property" , e);
         }
         logger.info("Initialized Ansible Adapter");
     }
@@ -314,12 +319,13 @@ public class AnsibleAdapterImpl implements AnsibleAdapter {
             // post the test request
             logger.info("Posting ansible request = " + payload + " to url = " + agentUrl);
             AnsibleResult testResult = postExecRequest(agentUrl, payload, user, password,ctx);
+          if (testResult != null) {
             logger.info("Received response on ansible post request " + testResult.getStatusMessage());
             // Process if HTTP was successful
             if (testResult.getStatusCode() == 200) {
-                testResult = messageProcessor.parsePostResponse(testResult.getStatusMessage());
+              testResult = messageProcessor.parsePostResponse(testResult.getStatusMessage());
             } else {
-                doFailure(ctx, testResult.getStatusCode(),
+              doFailure(ctx, testResult.getStatusCode(),
                         "Error posting request. Reason = " + testResult.getStatusMessage());
             }
             String output = StringUtils.EMPTY;
@@ -329,16 +335,19 @@ public class AnsibleAdapterImpl implements AnsibleAdapter {
             ctx.setAttribute(OUTPUT_ATTRIBUTE_NAME, output);
             String serverIp = testResult.getServerIp();
             if (StringUtils.isBlank(serverIp))
-                ctx.setAttribute("ServerIP", serverIp);
+            ctx.setAttribute("ServerIP", serverIp);
             else
-                ctx.setAttribute("ServerIP", "");
+              ctx.setAttribute("ServerIP", "");
             // Check status of test request returned by Agent
             if (code == AnsibleResultCodes.PENDING.getValue()) {
-                logger.info(String.format("Submission of Test %s successful.", playbookName));
-                // test request accepted. We are in asynchronous case
+              logger.info(String.format("Submission of Test %s successful.", playbookName));
+              // test request accepted. We are in asynchronous case
             } else {
-                doFailure(ctx, code, "Request for execution of playbook rejected. Reason = " + message);
+              doFailure(ctx, code, "Request for execution of playbook rejected. Reason = " + message);
             }
+          } else {
+            doFailure(ctx, code, "Ansible Test result is null");
+          }
         } catch (APPCException e) {
             logger.error(APPC_EXCEPTION_CAUGHT, e);
             doFailure(ctx, AnsibleResultCodes.UNKNOWN_EXCEPTION.getValue(),
@@ -476,12 +485,14 @@ public class AnsibleAdapterImpl implements AnsibleAdapter {
     private AnsibleResult postExecRequest(String agentUrl, String payload, String user, String password,
             SvcLogicContext ctx) {
 
-        AnsibleResult testResult;
+        AnsibleResult testResult = null;
         ConnectionBuilder httpClient = getHttpConn(defaultSocketTimeout, "");
         if (!testMode) {
+          if(httpClient!=null) {
             httpClient.setHttpContext(user, password);
             testResult = httpClient.post(agentUrl, payload);
             httpClient.close();
+          }
         } else {
             testResult = testServer.Post(agentUrl, payload);
         }
@@ -509,9 +520,11 @@ public class AnsibleAdapterImpl implements AnsibleAdapter {
             logger.info("Querying ansible GetResult URL = " + agentUrl);
 
             if (!testMode) {
+              if(httpClient!=null) {
                 httpClient.setHttpContext(user, password);
                 testResult = httpClient.get(agentUrl);
                 httpClient.close();
+              }
             } else {
                 testResult = testServer.Get(agentUrl);
             }