aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/cacher/common/MongoConfig.java
blob: 854437db2f0b6b3cf4f3ee9db127b62f6972b2c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.cacher.common;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import de.flapdoodle.embed.mongo.Command;
import de.flapdoodle.embed.mongo.MongodExecutable;
import de.flapdoodle.embed.mongo.MongodProcess;
import de.flapdoodle.embed.mongo.MongodStarter;
import de.flapdoodle.embed.mongo.config.Defaults;
import de.flapdoodle.embed.mongo.config.MongoCmdOptions;
import de.flapdoodle.embed.mongo.config.MongodConfig;
import de.flapdoodle.embed.mongo.config.Net;
import de.flapdoodle.embed.mongo.distribution.Version;
import de.flapdoodle.embed.process.config.io.ProcessOutput;
import de.flapdoodle.embed.process.io.Processors;
import de.flapdoodle.embed.process.io.Slf4jLevel;
import de.flapdoodle.embed.process.runtime.Network;
import org.onap.aai.exceptions.AAIException;
import org.onap.aai.logging.ErrorLogHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;


@Configuration
public class MongoConfig {

    private static final EELFLogger EELF_LOGGER = EELFManager.getInstance().getLogger(MongoConfig.class);

    @Value("${spring.data.mongodb.host}")
    private String MONGO_DB_HOST;
    @Value("${spring.data.mongodb.database}")
    private String MONGO_DB_NAME;
    @Value("${spring.data.mongodb.port}")
    private int MONGO_DB_PORT;

    private MongodProcess mongod;

    @Bean
    public MongoClient mongoClient(MongodProcess mongodProcess) {
        // To connect to mongodb server
        try (MongoClient mongoC = new MongoClient(MONGO_DB_HOST, MONGO_DB_PORT)) {

            // Now connect to your databases
            EELF_LOGGER.info("Connect to database successfully");

            return mongoC;

        } catch (Exception e) {
            AAIException aaiException = new AAIException("AAI_4000");
            ErrorLogHelper.logException(aaiException);
        }

        return null;
    }

    @Bean
    public DB db(MongoClient mongoClient) {
        return mongoClient.getDB(MONGO_DB_NAME);
    }

    @Bean
    public MongoDatabase mongoDatabase(MongoClient mongoClient) {
        return mongoClient.getDatabase(MONGO_DB_NAME);
    }

    @Bean
    public MongodProcess mongoEmbedded() throws IOException {

        Logger logger = LoggerFactory.getLogger("mongo");
        int port = MONGO_DB_PORT;

        MongodConfig mongoConfigConfig = MongodConfig.builder()
                .version(Version.Main.PRODUCTION)
                .net(new Net(port, Network.localhostIsIPv6()))
                .cmdOptions(MongoCmdOptions.builder().enableTextSearch(true).useNoPrealloc(false).build())
                .isConfigServer(false)
                .build();

        ProcessOutput processOutput = new ProcessOutput(Processors.logTo(logger, Slf4jLevel.WARN), Processors.logTo(logger,
                Slf4jLevel.WARN), Processors.logTo(logger, Slf4jLevel.WARN));

        MongodExecutable mongodExecutable = MongodStarter
                .getInstance(Defaults.runtimeConfigFor(Command.MongoD)
                        .processOutput(processOutput)
                        .build())
                .prepare(mongoConfigConfig);

        mongod = mongodExecutable.start();

        if (mongod.isProcessRunning()) {
            System.out.println("Embedded mongo RUNNING");
            logger.info("Embedded mongo RUNNING");
        }
        return mongod;
    }
}