2b8892157f02de0eb18a2d081a00b700c4f93335
[dcaegen2/services.git] /
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.bson.Document;
32
33 import org.json.JSONObject;
34
35 import org.onap.datalake.feeder.domain.Db;
36 import org.onap.datalake.feeder.domain.Topic;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.stereotype.Service;
42
43 import com.mongodb.MongoClient;
44 import com.mongodb.client.MongoCollection;
45 import com.mongodb.client.MongoDatabase;
46
47 /**
48  * Service to use MongoDB
49  * 
50  * @author Guobiao Mo
51  *
52  */
53 @Service
54 public class MongodbService {
55
56         private final Logger log = LoggerFactory.getLogger(this.getClass());
57
58         @Autowired
59         private DbService dbService;
60
61         private MongoDatabase database;
62         private MongoClient mongoClient;
63         private Map<String, MongoCollection<Document>> mongoCollectionMap = new HashMap<>();
64
65         @PostConstruct
66         private void init() {
67                 Db mongodb = dbService.getMongoDB();
68
69                 mongoClient = new MongoClient(mongodb.getHost(), mongodb.getPort());
70                 database = mongoClient.getDatabase(mongodb.getProperty1());
71         }
72
73         @PreDestroy
74         public void cleanUp() {
75                 mongoClient.close();
76         }
77
78         public void saveJsons(Topic topic, List<JSONObject> jsons) {
79                 List<Document> documents = new ArrayList<>(jsons.size());
80                 for (JSONObject json : jsons) {
81                         //convert org.json JSONObject to MongoDB Document
82                         Document doc = Document.parse(json.toString());
83
84                         String id = topic.getMessageId(json); //id can be null
85                         if (id != null) {
86                                 doc.put("_id", id);
87                         }
88                         documents.add(doc);
89                 }
90
91                 String collectionName = topic.getName().replaceAll("[^a-zA-Z0-9]","");//remove - _ .
92                 MongoCollection<Document> collection = mongoCollectionMap.computeIfAbsent(collectionName, k -> database.getCollection(k));
93                 collection.insertMany(documents);
94
95                 log.debug("saved text to topic = {}, topic total count = {} ", topic, collection.countDocuments());
96         }
97
98 }