Fix Mongo connection method 81/85281/1
authorKate Hsuan <kate.hsuan@qct.io>
Mon, 15 Apr 2019 05:31:54 +0000 (13:31 +0800)
committerKate Hsuan <kate.hsuan@qct.io>
Mon, 15 Apr 2019 05:31:54 +0000 (13:31 +0800)
Address and credential must be a list.

Issue-ID: DCAEGEN2-1411

Change-Id: I5705d4086476ea02d9eb54c0cf4c329c1848f5cf
Signed-off-by: Kate Hsuan <kate.hsuan@qct.io>
components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/MongodbService.java

index 35426af..fb3f806 100644 (file)
@@ -48,6 +48,7 @@ import com.mongodb.MongoCredential;
 import com.mongodb.ServerAddress;
 import com.mongodb.client.MongoCollection;
 import com.mongodb.client.MongoDatabase;
+import com.mongodb.DB;
 
 /**
  * Service for using MongoDB
@@ -62,6 +63,7 @@ public class MongodbService {
 
        @Autowired
        private ApplicationConfiguration config;
+        private boolean dbReady = false;
 
        @Autowired
        private DbService dbService;
@@ -98,13 +100,30 @@ public class MongodbService {
                        builder.sslEnabled(Boolean.TRUE.equals(mongodb.getEncrypt()));// getEncrypt() can be null
                }
                MongoClientOptions options = builder.build();
+               List<ServerAddress> addrs = new ArrayList<ServerAddress>();
 
-               if (credential == null) {
-                       mongoClient = new MongoClient(new ServerAddress(host, port), options);
-               } else {
-                       mongoClient = new MongoClient(new ServerAddress(host, port), credential, options);
+               addrs.add(new ServerAddress(host, port)); // FIXME should be a list of address
+
+
+               try {
+                       if(StringUtils.isNoneBlank(userName) && StringUtils.isNoneBlank(password))
+                       {
+                               credential = MongoCredential.createCredential(userName, databaseName, password.toCharArray());
+                               List<MongoCredential> credentialList = new ArrayList<MongoCredential>();
+                               credentialList.add(credential);
+                               mongoClient = new MongoClient(addrs, credentialList, options);
+                       }else
+                       {
+                               mongoClient = new MongoClient(addrs, options);
+                       }
+               }catch(Exception ex){
+                       dbReady = false;
+                       log.error("Fail to initiate MongoDB" + mongodb.getHost());
+                       return;
                }
-               database = mongoClient.getDatabase(databaseName);
+               database = mongoClient.getDatabase(mongodb.getDatabase());
+               dbReady = true;
+
        }
 
        @PreDestroy
@@ -113,6 +132,8 @@ public class MongodbService {
        }
 
        public void saveJsons(Topic topic, List<JSONObject> jsons) {
+               if(dbReady == false)
+                       return;
                List<Document> documents = new ArrayList<>(jsons.size());
                for (JSONObject json : jsons) {
                        //convert org.json JSONObject to MongoDB Document