Fix Mongo connection method
[dcaegen2/services.git] / components / datalake-handler / feeder / src / main / java / org / onap / datalake / feeder / service / MongodbService.java
1 /*
2 * ============LICENSE_START=======================================================
3 * ONAP : DATALAKE
4 * ================================================================================
5 * Copyright 2018 China Mobile
6 *=================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *     http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
19 */
20
21 package org.onap.datalake.feeder.service;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import javax.annotation.PostConstruct;
29 import javax.annotation.PreDestroy;
30
31 import org.apache.commons.lang3.StringUtils;
32 import org.bson.Document;
33
34 import org.json.JSONObject;
35 import org.onap.datalake.feeder.config.ApplicationConfiguration;
36 import org.onap.datalake.feeder.domain.Db;
37 import org.onap.datalake.feeder.domain.Topic;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.stereotype.Service;
43
44 import com.mongodb.MongoClient;
45 import com.mongodb.MongoClientOptions;
46 import com.mongodb.MongoClientOptions.Builder;
47 import com.mongodb.MongoCredential;
48 import com.mongodb.ServerAddress;
49 import com.mongodb.client.MongoCollection;
50 import com.mongodb.client.MongoDatabase;
51 import com.mongodb.DB;
52
53 /**
54  * Service for using MongoDB
55  * 
56  * @author Guobiao Mo
57  *
58  */
59 @Service
60 public class MongodbService {
61
62         private final Logger log = LoggerFactory.getLogger(this.getClass());
63
64         @Autowired
65         private ApplicationConfiguration config;
66         private boolean dbReady = false;
67
68         @Autowired
69         private DbService dbService;
70
71         private MongoDatabase database;
72         private MongoClient mongoClient;
73         private Map<String, MongoCollection<Document>> mongoCollectionMap = new HashMap<>();
74
75         @PostConstruct
76         private void init() {
77                 Db mongodb = dbService.getMongoDB();
78
79                 String host = mongodb.getHost();
80
81                 Integer port = mongodb.getPort();
82                 if (port == null || port == 0) {
83                         port = 27017; //MongoDB default
84                 }
85
86                 String databaseName = mongodb.getDatabase();
87                 String userName = mongodb.getLogin();
88                 String password = mongodb.getPass();
89
90                 MongoCredential credential = null;
91                 if (StringUtils.isNoneBlank(userName) && StringUtils.isNoneBlank(password)) {
92                         credential = MongoCredential.createCredential(userName, databaseName, password.toCharArray());
93                 }
94
95                 Builder builder = MongoClientOptions.builder();
96                 builder.serverSelectionTimeout(30000);//server selection timeout, in milliseconds
97
98                 //http://mongodb.github.io/mongo-java-driver/3.0/driver/reference/connecting/ssl/
99                 if (config.isEnableSSL()) {
100                         builder.sslEnabled(Boolean.TRUE.equals(mongodb.getEncrypt()));// getEncrypt() can be null
101                 }
102                 MongoClientOptions options = builder.build();
103                 List<ServerAddress> addrs = new ArrayList<ServerAddress>();
104
105                 addrs.add(new ServerAddress(host, port)); // FIXME should be a list of address
106
107
108                 try {
109                         if(StringUtils.isNoneBlank(userName) && StringUtils.isNoneBlank(password))
110                         {
111                                 credential = MongoCredential.createCredential(userName, databaseName, password.toCharArray());
112                                 List<MongoCredential> credentialList = new ArrayList<MongoCredential>();
113                                 credentialList.add(credential);
114                                 mongoClient = new MongoClient(addrs, credentialList, options);
115                         }else
116                         {
117                                 mongoClient = new MongoClient(addrs, options);
118                         }
119                 }catch(Exception ex){
120                         dbReady = false;
121                         log.error("Fail to initiate MongoDB" + mongodb.getHost());
122                         return;
123                 }
124                 database = mongoClient.getDatabase(mongodb.getDatabase());
125                 dbReady = true;
126
127         }
128
129         @PreDestroy
130         public void cleanUp() {
131                 mongoClient.close();
132         }
133
134         public void saveJsons(Topic topic, List<JSONObject> jsons) {
135                 if(dbReady == false)
136                         return;
137                 List<Document> documents = new ArrayList<>(jsons.size());
138                 for (JSONObject json : jsons) {
139                         //convert org.json JSONObject to MongoDB Document
140                         Document doc = Document.parse(json.toString());
141
142                         String id = topic.getMessageId(json); //id can be null
143                         if (id != null) {
144                                 doc.put("_id", id);
145                         }
146                         documents.add(doc);
147                 }
148
149                 String collectionName = topic.getName().replaceAll("[^a-zA-Z0-9]", "");//remove - _ .
150                 MongoCollection<Document> collection = mongoCollectionMap.computeIfAbsent(collectionName, k -> database.getCollection(k));
151                 collection.insertMany(documents);
152
153                 log.debug("saved text to topic = {}, topic total count = {} ", topic, collection.countDocuments());
154         }
155
156 }