2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.cacher.common;
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import com.mongodb.DB;
25 import com.mongodb.MongoClient;
26 import com.mongodb.client.MongoDatabase;
27 import de.flapdoodle.embed.mongo.Command;
28 import de.flapdoodle.embed.mongo.MongodExecutable;
29 import de.flapdoodle.embed.mongo.MongodProcess;
30 import de.flapdoodle.embed.mongo.MongodStarter;
31 import de.flapdoodle.embed.mongo.config.Defaults;
32 import de.flapdoodle.embed.mongo.config.MongoCmdOptions;
33 import de.flapdoodle.embed.mongo.config.MongodConfig;
34 import de.flapdoodle.embed.mongo.config.Net;
35 import de.flapdoodle.embed.mongo.distribution.Version;
36 import de.flapdoodle.embed.process.config.io.ProcessOutput;
37 import de.flapdoodle.embed.process.io.Processors;
38 import de.flapdoodle.embed.process.io.Slf4jLevel;
39 import de.flapdoodle.embed.process.runtime.Network;
40 import org.onap.aai.exceptions.AAIException;
41 import org.onap.aai.logging.ErrorLogHelper;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.beans.factory.annotation.Value;
45 import org.springframework.context.annotation.Bean;
46 import org.springframework.context.annotation.Configuration;
48 import java.io.IOException;
52 public class MongoConfig {
54 private static final EELFLogger EELF_LOGGER = EELFManager.getInstance().getLogger(MongoConfig.class);
56 @Value("${spring.data.mongodb.host}")
57 private String MONGO_DB_HOST;
58 @Value("${spring.data.mongodb.database}")
59 private String MONGO_DB_NAME;
60 @Value("${spring.data.mongodb.port}")
61 private int MONGO_DB_PORT;
63 private MongodProcess mongod;
66 public MongoClient mongoClient(MongodProcess mongodProcess) {
67 // To connect to mongodb server
68 try (MongoClient mongoC = new MongoClient(MONGO_DB_HOST, MONGO_DB_PORT)) {
70 // Now connect to your databases
71 EELF_LOGGER.info("Connect to database successfully");
75 } catch (Exception e) {
76 AAIException aaiException = new AAIException("AAI_4000");
77 ErrorLogHelper.logException(aaiException);
84 public DB db(MongoClient mongoClient) {
85 return mongoClient.getDB(MONGO_DB_NAME);
89 public MongoDatabase mongoDatabase(MongoClient mongoClient) {
90 return mongoClient.getDatabase(MONGO_DB_NAME);
94 public MongodProcess mongoEmbedded() throws IOException {
96 Logger logger = LoggerFactory.getLogger("mongo");
97 int port = MONGO_DB_PORT;
99 MongodConfig mongoConfigConfig = MongodConfig.builder()
100 .version(Version.Main.PRODUCTION)
101 .net(new Net(port, Network.localhostIsIPv6()))
102 .cmdOptions(MongoCmdOptions.builder().enableTextSearch(true).useNoPrealloc(false).build())
103 .isConfigServer(false)
106 ProcessOutput processOutput = new ProcessOutput(Processors.logTo(logger, Slf4jLevel.WARN), Processors.logTo(logger,
107 Slf4jLevel.WARN), Processors.logTo(logger, Slf4jLevel.WARN));
109 MongodExecutable mongodExecutable = MongodStarter
110 .getInstance(Defaults.runtimeConfigFor(Command.MongoD)
111 .processOutput(processOutput)
113 .prepare(mongoConfigConfig);
115 mongod = mongodExecutable.start();
117 if (mongod.isProcessRunning()) {
118 System.out.println("Embedded mongo RUNNING");
119 logger.info("Embedded mongo RUNNING");