Improve setKeyManager & setTrustManager

Change-Id: I14b1bd6872e40496ce233ec4974ad9e5fd92d949
Signed-off-by: Chengkai Yan <martin.c.yan@est.tech>
Issue-ID: DCAEGEN2-867
diff --git a/datafile-app-server/pom.xml b/datafile-app-server/pom.xml
index 4c716bd..3c91f94 100644
--- a/datafile-app-server/pom.xml
+++ b/datafile-app-server/pom.xml
@@ -24,7 +24,7 @@
   <parent>
     <groupId>org.onap.dcaegen2.collectors</groupId>
     <artifactId>datafile</artifactId>
-    <version>1.0.2-SNAPSHOT</version>
+    <version>1.0.3-SNAPSHOT</version>
   </parent>
 
   <groupId>org.onap.dcaegen2.collectors.datafile</groupId>
diff --git a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/configuration/SchedulerConfig.java b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/configuration/SchedulerConfig.java
index c42749c..6420b4a 100644
--- a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/configuration/SchedulerConfig.java
+++ b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/configuration/SchedulerConfig.java
@@ -42,7 +42,7 @@
 @EnableScheduling
 public class SchedulerConfig extends DatafileAppConfig {
 
-    private static final int SCHEDULING_DELAY_FOR_DATAFILE_COLLECTOR_TASKS = 10;
+    private static final int SCHEDULING_DELAY_FOR_DATAFILE_COLLECTOR_TASKS = 15;
     private static final int SCHEDULING_REQUEST_FOR_CONFIGURATION_DELAY = 5;
     private static volatile List<ScheduledFuture> scheduledFutureList = new ArrayList<>();
 
diff --git a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/tasks/RetryTimer.java b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/tasks/RetryTimer.java
index c2b97da..b993f20 100644
--- a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/tasks/RetryTimer.java
+++ b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/tasks/RetryTimer.java
@@ -22,7 +22,7 @@
     public void waitRetryTime() {
         try {
             Thread.sleep(60000);
-        } catch (InterruptedException e) {
+        } catch (Exception e) {
             // Nothing, no one will interrupt.
         }
 
diff --git a/datafile-app-server/src/main/resources/datafile_endpoints.json b/datafile-app-server/src/main/resources/datafile_endpoints.json
index dff77d2..9da01d7 100644
--- a/datafile-app-server/src/main/resources/datafile_endpoints.json
+++ b/datafile-app-server/src/main/resources/datafile_endpoints.json
@@ -2,27 +2,36 @@
     "configs": {
         "dmaap": {
             "dmaapConsumerConfiguration": {
-                "consumerGroup": "notification",
-                "consumerId": "C12",
-                "dmaapContentType": "application/json",
                 "dmaapHostName": "localhost",
                 "dmaapPortNumber": 2222,
-                "dmaapProtocol": "http",
                 "dmaapTopicName": "/events/unauthenticated.VES_NOTIFICATION_OUTPUT",
-                "dmaapUserName": "admin",
-                "dmaapUserPassword": "admin",
-                "messageLimit": 1000,
-                "timeoutMS": 1000
+                "dmaapProtocol": "http",
+                "dmaapUserName": "",
+                "dmaapUserPassword": "",
+                "dmaapContentType": "application/json",
+                "consumerId": "C12",
+                "consumerGroup": "OpenDcae-c12",
+                "timeoutMS": -1,
+                "messageLimit": 1
             },
             "dmaapProducerConfiguration": {
-                "dmaapContentType": "application/octet-stream",
                 "dmaapHostName": "localhost",
                 "dmaapPortNumber": 3907,
-                "dmaapProtocol": "https",
                 "dmaapTopicName": "publish",
+                "dmaapProtocol": "https",
                 "dmaapUserName": "dradmin",
-                "dmaapUserPassword": "dradmin"
+                "dmaapUserPassword": "dradmin",
+                "dmaapContentType": "application/octet-stream"
+            }
+        },
+        "ftp": {
+            "ftpesConfiguration": {
+                "keyCert": "config/ftpKey.jks",
+                "keyPassword": "secret",
+                "trustedCA": "config/cacerts",
+                "trustedCAPassword": "secret"
             }
         }
     }
 }
+
diff --git a/datafile-commons/pom.xml b/datafile-commons/pom.xml
index 9029769..1ae3c2d 100644
--- a/datafile-commons/pom.xml
+++ b/datafile-commons/pom.xml
@@ -24,7 +24,7 @@
   <parent>
     <groupId>org.onap.dcaegen2.collectors</groupId>
     <artifactId>datafile</artifactId>
-    <version>1.0.2-SNAPSHOT</version>
+    <version>1.0.3-SNAPSHOT</version>
   </parent>
 
   <groupId>org.onap.dcaegen2.collectors.datafile</groupId>
diff --git a/datafile-dmaap-client/pom.xml b/datafile-dmaap-client/pom.xml
index 9eb232b..5006b00 100644
--- a/datafile-dmaap-client/pom.xml
+++ b/datafile-dmaap-client/pom.xml
@@ -24,7 +24,7 @@
   <parent>
     <groupId>org.onap.dcaegen2.collectors</groupId>
     <artifactId>datafile</artifactId>
-    <version>1.0.2-SNAPSHOT</version>
+    <version>1.0.3-SNAPSHOT</version>
   </parent>
 
   <groupId>org.onap.dcaegen2.collectors.datafile</groupId>
diff --git a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FTPSClientWrapper.java b/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FTPSClientWrapper.java
index 7704804..2ccf1ba 100644
--- a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FTPSClientWrapper.java
+++ b/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FTPSClientWrapper.java
@@ -89,4 +89,8 @@
         return ftpsClient.retrieveFile(remote, local);
     }
 
+    @Override
+    public void setTimeout(Integer t) {
+        this.ftpsClient.setDefaultTimeout(t);
+    }
 }
diff --git a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FileCollectResult.java b/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FileCollectResult.java
index 6cd048a..c596217 100644
--- a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FileCollectResult.java
+++ b/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FileCollectResult.java
@@ -36,7 +36,10 @@
     }
 
     public String getErrorData() {
-        return errorData.toString();
+        if (errorData != null) {
+            return errorData.toString();
+        }
+        return "";
     }
 
     @Override
diff --git a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClient.java b/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClient.java
index a88072c..120868c 100644
--- a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClient.java
+++ b/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClient.java
@@ -57,6 +57,8 @@
     private IFile lf;
     private IFileSystemResource fileResource;
     private IOutputStream os;
+    private boolean keyManagerSet = false;
+    private boolean trustManagerSet = false;
 
     @Override
     public FileCollectResult retryCollectFile() {
@@ -84,19 +86,27 @@
 
     private boolean setUpKeyManager(IFTPSClient ftps) {
         boolean result = true;
+        if (keyManagerSet) {
+            return result;
+        }
         try {
             IKeyManagerUtils keyManagerUtils = getKeyManagerUtils();
             keyManagerUtils.setCredentials(keyCertPath, keyCertPassword);
             ftps.setKeyManager(keyManagerUtils.getClientKeyManager());
+            keyManagerSet = true;
         } catch (GeneralSecurityException | IOException e) {
             addError("Unable to use own key store " + keyCertPath, e);
             result = false;
         }
+        logger.trace("complete setUpKeyManager");
         return result;
     }
 
     private boolean setUpTrustedCA(IFTPSClient ftps) {
         boolean result = true;
+        if (trustManagerSet) {
+            return result;
+        }
         try {
             IFileSystemResource fileSystemResource = getFileSystemResource();
             fileSystemResource.setPath(trustedCAPath);
@@ -107,11 +117,13 @@
             ITrustManagerFactory tmf = getTrustManagerFactory();
             tmf.init(ks.getKeyStore());
             ftps.setTrustManager(tmf.getTrustManagers()[0]);
+            trustManagerSet = true;
 
         } catch (Exception e) {
             addError("Unable to trust xNF's CA, " + trustedCAPath, e);
             result = false;
         }
+        logger.trace("complete setUpTrustedCA");
         return result;
     }
 
@@ -119,7 +131,7 @@
         boolean result = true;
         try {
             ftps.connect(fileServerData.serverAddress(), fileServerData.port());
-
+            logger.trace("after ftp connect");
             boolean loginSuccesful = ftps.login(fileServerData.userId(), fileServerData.password());
             if (!loginSuccesful) {
                 ftps.logout();
@@ -135,7 +147,8 @@
                 ftps.execPROT("P");
             } else {
                 ftps.disconnect();
-                addError("Unable to connect to xNF. " + fileServerData + "xNF reply code: " + ftps.getReplyCode(), null);
+                addError("Unable to connect to xNF. " + fileServerData + " xNF reply code: " + ftps.getReplyCode(),
+                        null);
                 result = false;
             }
         } catch (Exception ex) {
@@ -157,8 +170,13 @@
             IOutputStream outputStream = getOutputStream();
             OutputStream output = outputStream.getOutputStream(outfile.getFile());
 
-            ftps.retrieveFile(remoteFile, output);
-
+            result = ftps.retrieveFile(remoteFile, output);
+            if (!result) {
+                output.close();
+                logger.debug("Unable to retrieve file from xNF. Cause unknown!");
+                addError("Unable to retrieve file from xNF. Cause unknown!", null);
+                return result;
+            }
             output.close();
             logger.debug("File {} Download Successfull from xNF", localFile);
         } catch (IOException ex) {
diff --git a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/IFTPSClient.java b/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/IFTPSClient.java
index 2f370b9..b147202 100644
--- a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/IFTPSClient.java
+++ b/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/IFTPSClient.java
@@ -48,4 +48,6 @@
     public void execPROT(String prot) throws IOException;
 
     public boolean retrieveFile(String remote, OutputStream local) throws IOException;
+
+    void setTimeout(Integer t);
 }
diff --git a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClientTest.java b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClientTest.java
index e9e00c3..e5693d5 100644
--- a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClientTest.java
+++ b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClientTest.java
@@ -102,6 +102,7 @@
         when(localFileMock.getFile()).thenReturn(fileMock);
         OutputStream osMock = mock(OutputStream.class);
         when(outputStreamMock.getOutputStream(fileMock)).thenReturn(osMock);
+        when(ftpsClientMock.retrieveFile(REMOTE_FILE_PATH, osMock)).thenReturn(true);
 
         ImmutableFileServerData fileServerData = ImmutableFileServerData.builder().serverAddress(XNF_ADDRESS)
                 .userId(USERNAME).password(PASSWORD).port(PORT).build();
diff --git a/pom.xml b/pom.xml
index b04a798..a3af87a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -30,7 +30,7 @@
 
   <groupId>org.onap.dcaegen2.collectors</groupId>
   <artifactId>datafile</artifactId>
-  <version>1.0.2-SNAPSHOT</version>
+  <version>1.0.3-SNAPSHOT</version>
 
   <name>dcaegen2-collectors.datafile</name>
   <description>datafile collector</description>
diff --git a/version.properties b/version.properties
index 2263272..39e03d9 100644
--- a/version.properties
+++ b/version.properties
@@ -1,6 +1,6 @@
 major=1

 minor=0

-patch=2

+patch=3

 base_version=${major}.${minor}.${patch}

 release_version=${base_version}

 snapshot_version=${base_version}-SNAPSHOT